Skip to content

Commit

Permalink
[fix](analysis) Fix ColumnDef to sql result apache#41205 (apache#41325)
Browse files Browse the repository at this point in the history
cherry pick from apache#41205
  • Loading branch information
w41ter authored Sep 26, 2024
1 parent 4deda2f commit 43c1066
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,17 @@ public String getValue() {
}
return value;
}

public String toSql() {
StringBuilder sb = new StringBuilder();
sb.append("DEFAULT ");
if (value != null) {
sb.append('"').append(value).append('"');
} else {
sb.append("NULL");
}
return sb.toString();
}
}

// parameter initialized in constructor
Expand Down Expand Up @@ -582,7 +593,7 @@ public String toSql() {
}

if (defaultValue.isSet) {
sb.append("DEFAULT \"").append(defaultValue.value).append("\" ");
sb.append(defaultValue.toSql()).append(" ");
}
sb.append("COMMENT \"").append(comment).append("\"");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void testReplaceIfNotNull() throws AnalysisException {
ColumnDef column = new ColumnDef("col", intCol, false, AggregateType.REPLACE_IF_NOT_NULL, false, DefaultValue.NOT_SET, "");
column.analyze(true);
Assert.assertEquals(AggregateType.REPLACE_IF_NOT_NULL, column.getAggregateType());
Assert.assertEquals("`col` int REPLACE_IF_NOT_NULL NULL DEFAULT \"null\" COMMENT \"\"", column.toSql());
Assert.assertEquals("`col` int REPLACE_IF_NOT_NULL NULL DEFAULT NULL COMMENT \"\"", column.toSql());
} // CHECKSTYLE IGNORE THIS LINE
{ // CHECKSTYLE IGNORE THIS LINE
// not allow null
Expand All @@ -98,6 +98,12 @@ public void testReplaceIfNotNull() throws AnalysisException {
Assert.assertEquals(AggregateType.REPLACE_IF_NOT_NULL, column.getAggregateType());
Assert.assertEquals("`col` int REPLACE_IF_NOT_NULL NULL DEFAULT \"10\" COMMENT \"\"", column.toSql());
} // CHECKSTYLE IGNORE THIS LINE
{ // CHECKSTYLE IGNORE THIS LINE
ColumnDef column = new ColumnDef("col", intCol, false, AggregateType.REPLACE_IF_NOT_NULL, true, DefaultValue.NULL_DEFAULT_VALUE, "");
column.analyze(true);
Assert.assertEquals(AggregateType.REPLACE_IF_NOT_NULL, column.getAggregateType());
Assert.assertEquals("`col` int REPLACE_IF_NOT_NULL NULL DEFAULT NULL COMMENT \"\"", column.toSql());
} // CHECKSTYLE IGNORE THIS LINE
}

@Test(expected = AnalysisException.class)
Expand Down

0 comments on commit 43c1066

Please sign in to comment.