Skip to content

Commit

Permalink
[BugFix] Fix error create table sql (backport #49769) (#49819)
Browse files Browse the repository at this point in the history
Co-authored-by: zhangqiang <[email protected]>
  • Loading branch information
mergify[bot] and sevev authored Aug 15, 2024
1 parent a22701e commit 363b693
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
14 changes: 8 additions & 6 deletions fe/fe-core/src/main/java/com/starrocks/catalog/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -703,19 +703,21 @@ public String toSqlWithoutAggregateTypeName(Map<ColumnId, Column> idToColumn) {
} else {
sb.append("NOT NULL ");
}
if (defaultExpr == null && isAutoIncrement) {
sb.append("AUTO_INCREMENT ");
} else if (defaultExpr != null) {
if (defaultExpr == null) {
if (isAutoIncrement) {
sb.append("AUTO_INCREMENT ");
}
if (defaultValue != null && !type.isOnlyMetricType()) {
sb.append("DEFAULT \"").append(StringEscapeUtils.escapeJava(defaultValue)).append("\" ");
}
} else {
if ("now()".equalsIgnoreCase(defaultExpr.getExpr())) {
// compatible with mysql
sb.append("DEFAULT ").append("CURRENT_TIMESTAMP").append(" ");
} else {
sb.append("DEFAULT ").append("(").append(defaultExpr.getExpr()).append(") ");
}
}
if (defaultValue != null && !type.isOnlyMetricType()) {
sb.append("DEFAULT \"").append(StringEscapeUtils.escapeJava(defaultValue)).append("\" ");
}
if (isGeneratedColumn()) {
String generatedColumnSql;
if (idToColumn != null) {
Expand Down
36 changes: 29 additions & 7 deletions test/sql/test_fast_schema_evolution/R/test_fast_schema_evolution
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ insert into tab1 values (600, "k2_600", 600, 600, 600, "v4_600", "v5_600");
-- !result
alter table tab1 add column c1 bigint;
-- result:
[]
-- !result
function: wait_alter_table_finish()
-- result:
Expand Down Expand Up @@ -498,14 +499,35 @@ function: wait_alter_table_finish()
-- result:
None
-- !result
select * from tab1;
alter table tab1 add column date1 datetime default current_timestamp;
-- result:
100 k2_100 100 100 100 v4_100 v5_100 None
200 k2_200 200 200 200 v4_200 v5_200 None
300 k2_300 300 300 300 v4_300 v5_300 None
400 k2_400 400 400 400 v4_400 v5_400 None
500 k2_500 500 500 500 v4_500 v5_500 None
600 k2_600 600 600 600 v4_600 v5_600 None
-- !result
function: wait_alter_table_finish()
-- result:
None
-- !result
show create table tab1;
-- result:
tab1 CREATE TABLE `tab1` (
`k1` int(11) NOT NULL COMMENT "",
`k2` varchar(50) NOT NULL COMMENT "",
`v1` int(11) NULL COMMENT "",
`v2` int(11) NULL COMMENT "",
`v3` int(11) NULL COMMENT "",
`v4` varchar(50) NULL COMMENT "",
`v5` varchar(50) NULL COMMENT "",
`c1` largeint(40) NULL COMMENT "",
`date1` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT ""
) ENGINE=OLAP
PRIMARY KEY(`k1`, `k2`)
DISTRIBUTED BY HASH(`k1`) BUCKETS 1
PROPERTIES (
"compression" = "LZ4",
"enable_persistent_index" = "true",
"fast_schema_evolution" = "true",
"replicated_storage" = "true",
"replication_num" = "1"
);
-- !result
drop table tab1;
-- result:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ select * from tab1;
alter table tab1 modify column c1 largeint;
function: wait_alter_table_finish()

select * from tab1;
alter table tab1 add column date1 datetime default current_timestamp;
function: wait_alter_table_finish()
show create table tab1;

drop table tab1;
drop database test_fast_schema_evolution_and_alter;
Expand Down

0 comments on commit 363b693

Please sign in to comment.