From 159020f21c293e3a2df17dd131a0a82603012199 Mon Sep 17 00:00:00 2001 From: Uniqueyou <134280716+wyxxxcat@users.noreply.github.com> Date: Wed, 11 Dec 2024 22:09:06 +0800 Subject: [PATCH] [Fix] Add column with default value string throw exception (#45000) ``` ALTER TABLE t ADD COLUMN create_time VARCHAR(20) DEFAULT "CURRENT_TIMESTAMP"; ``` ``` errCode = 2, detailMessage = Unexpected exception: Cannot invoke "org.apache.doris.analysis.DefaultValueExprDef.getExprName()" because "this.defaultValueExprDef" is null ``` --- .../org/apache/doris/analysis/ColumnDef.java | 3 +- ...est_alter_add_column_default_value_str.out | 14 ++++ ..._alter_add_column_default_value_str.groovy | 72 +++++++++++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 regression-test/data/alter_p0/test_alter_add_column_default_value_str.out create mode 100644 regression-test/suites/alter_p0/test_alter_add_column_default_value_str.groovy diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/ColumnDef.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/ColumnDef.java index 160dbd4f868cd1..3759c770388f0a 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/ColumnDef.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/ColumnDef.java @@ -126,7 +126,8 @@ public static DefaultValue currentTimeStampDefaultValueWithPrecision(Long precis } public boolean isCurrentTimeStamp() { - return "CURRENT_TIMESTAMP".equals(value) && NOW.equals(defaultValueExprDef.getExprName()); + return "CURRENT_TIMESTAMP".equals(value) && defaultValueExprDef != null + && NOW.equals(defaultValueExprDef.getExprName()); } public boolean isCurrentTimeStampWithPrecision() { diff --git a/regression-test/data/alter_p0/test_alter_add_column_default_value_str.out b/regression-test/data/alter_p0/test_alter_add_column_default_value_str.out new file mode 100644 index 00000000000000..b180902fd16246 --- /dev/null +++ b/regression-test/data/alter_p0/test_alter_add_column_default_value_str.out @@ -0,0 +1,14 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !select1 -- +1 1 1 +2 2 2 +3 3 3 + +-- !select4 -- +1 1 1 CURRENT_TIMESTAMP BITMAP_EMPTY +2 2 2 CURRENT_TIMESTAMP BITMAP_EMPTY +3 3 3 CURRENT_TIMESTAMP BITMAP_EMPTY +4 4 4 4 4 +5 5 5 5 5 +6 6 6 6 6 + diff --git a/regression-test/suites/alter_p0/test_alter_add_column_default_value_str.groovy b/regression-test/suites/alter_p0/test_alter_add_column_default_value_str.groovy new file mode 100644 index 00000000000000..f3d0386498193b --- /dev/null +++ b/regression-test/suites/alter_p0/test_alter_add_column_default_value_str.groovy @@ -0,0 +1,72 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +suite('test_alter_add_column_default_value_str') { + def tbl = 'test_alter_add_column_default_value_str_tbl' + sql "DROP TABLE IF EXISTS ${tbl} FORCE" + sql """ + CREATE TABLE ${tbl} ( + `k1` BIGINT NOT NULL, + `v1` BIGINT NULL, + `v2` INT NULL, + ) ENGINE=OLAP + UNIQUE KEY(`k1`) + DISTRIBUTED BY HASH(`k1`) BUCKETS 1 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1" + ); + """ + + sql """ + INSERT INTO ${tbl} VALUES (1,1,1),(2,2,2),(3,3,3) + """ + + sql """ SYNC """ + + // Check data before ALTER TABLE + qt_select1 """ SELECT * FROM ${tbl} ORDER BY k1 """ + def tb1 = sql """ show create table ${tbl}""" + logger.info("tb1:{}", tb1[0][1]) + assertFalse(tb1[0][1].contains("varchar(20) NOT NULL DEFAULT \"CURRENT_TIMESTAMP\"")) + assertFalse(tb1[0][1].contains("varchar(20) NOT NULL DEFAULT \"BITMAP_EMPTY\"")) + + sql """ + ALTER TABLE ${tbl} add column timestamp_default varchar(20) NOT NULL default "CURRENT_TIMESTAMP"; + """ + + sql """ + ALTER TABLE ${tbl} add column bitmap_default varchar(20) NOT NULL default "BITMAP_EMPTY"; + """ + + waitForSchemaChangeDone { + sql """ SHOW ALTER TABLE COLUMN WHERE TableName='${tbl}' ORDER BY createtime DESC LIMIT 1 """ + time 600 + } + + // Check table structure after ALTER TABLE + def tb2 = sql """ show create table ${tbl}""" + logger.info("tb2:{}", tb2[0][1]) + assertTrue(tb2[0][1].contains("varchar(20) NOT NULL DEFAULT \"CURRENT_TIMESTAMP\"")) + assertTrue(tb2[0][1].contains("varchar(20) NOT NULL DEFAULT \"BITMAP_EMPTY\"")) + def resultCreate = sql """ SHOW CREATE TABLE ${tbl} """ + sql """insert into ${tbl} values (4,4,4,"4","4")""" + sql """insert into ${tbl} (k1,v1,v2,timestamp_default,bitmap_default) values (5,5,5,5,"5")""" + sql """insert into ${tbl} (k1,v1,v2,timestamp_default,bitmap_default) values (6,6,6,6,"6")""" + qt_select4 """ SELECT k1,v1,v2,timestamp_default,bitmap_default FROM ${tbl} ORDER BY k1 """ + + sql "DROP TABLE IF EXISTS ${tbl} FORCE" +}