Skip to content

Commit

Permalink
[Fix](planner) fix create view star except and modify cast to sql (#3…
Browse files Browse the repository at this point in the history
…5324)

cherry-pick #25951 to branch-2.0
cherry-pick #33726 to branch-2.0

---------

Co-authored-by: morrySnow <101034200+morrySnow@users.noreply.github.com>
Co-authored-by: feiniaofeiafei <moailing@selectdb.com>
  • Loading branch information
3 people authored May 27, 2024
1 parent 3239ef3 commit b1a15f9
Show file tree
Hide file tree
Showing 22 changed files with 203 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public boolean getResultIsNullable() {
@Override
public String toSql(int depth) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("agg_state(");
stringBuilder.append("AGG_STATE(");
for (int i = 0; i < subTypes.size(); i++) {
if (i > 0) {
stringBuilder.append(", ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ public static ArrayType create(Type type, boolean containsNull) {
@Override
public String toSql(int depth) {
if (!containsNull) {
return "array<not_null(" + itemType.toSql(depth + 1) + ")>";
return "ARRAY<" + itemType.toSql(depth + 1) + " NOT NULL>";
} else {
return "array<" + itemType.toSql(depth + 1) + ">";
return "ARRAY<" + itemType.toSql(depth + 1) + ">";
}
}

Expand Down Expand Up @@ -211,7 +211,7 @@ public boolean supportSubType(Type subType) {

@Override
public String toString() {
return String.format("ARRAY<%s>", itemType.toString()).toUpperCase();
return String.format("ARRAY<%s>", itemType.toString());
}

@Override
Expand Down
48 changes: 24 additions & 24 deletions fe/fe-common/src/main/java/org/apache/doris/catalog/ScalarType.java
Original file line number Diff line number Diff line change
Expand Up @@ -614,37 +614,37 @@ public String toSql(int depth) {
switch (type) {
case CHAR:
if (isWildcardChar()) {
stringBuilder.append("character");
stringBuilder.append("CHARACTER");
} else if (Strings.isNullOrEmpty(lenStr)) {
stringBuilder.append("char").append("(").append(len).append(")");
stringBuilder.append("CHAR").append("(").append(len).append(")");
} else {
stringBuilder.append("char").append("(`").append(lenStr).append("`)");
stringBuilder.append("CHAR").append("(`").append(lenStr).append("`)");
}
break;
case VARCHAR:
if (isWildcardVarchar()) {
stringBuilder.append("varchar(*)");
return "VARCHAR(" + MAX_VARCHAR_LENGTH + ")";
} else if (Strings.isNullOrEmpty(lenStr)) {
stringBuilder.append("varchar").append("(").append(len).append(")");
stringBuilder.append("VARCHAR").append("(").append(len).append(")");
} else {
stringBuilder.append("varchar").append("(`").append(lenStr).append("`)");
stringBuilder.append("VARCHAR").append("(`").append(lenStr).append("`)");
}
break;
case DECIMALV2:
if (Strings.isNullOrEmpty(precisionStr)) {
stringBuilder.append("decimal").append("(").append(precision)
stringBuilder.append("DECIMAL").append("(").append(precision)
.append(", ").append(scale).append(")");
} else if (!Strings.isNullOrEmpty(precisionStr) && !Strings.isNullOrEmpty(scaleStr)) {
stringBuilder.append("decimal").append("(`").append(precisionStr)
stringBuilder.append("DECIMAL").append("(`").append(precisionStr)
.append("`, `").append(scaleStr).append("`)");
} else {
stringBuilder.append("decimal").append("(`").append(precisionStr).append("`)");
stringBuilder.append("DECIMAL").append("(`").append(precisionStr).append("`)");
}
break;
case DECIMAL32:
case DECIMAL64:
case DECIMAL128:
String typeName = "decimalv3";
String typeName = "DECIMALV3";
if (Strings.isNullOrEmpty(precisionStr)) {
stringBuilder.append(typeName).append("(").append(precision)
.append(", ").append(scale).append(")");
Expand All @@ -656,26 +656,26 @@ public String toSql(int depth) {
}
break;
case DATETIMEV2:
stringBuilder.append("datetimev2").append("(").append(scale).append(")");
stringBuilder.append("DATETIMEV2").append("(").append(scale).append(")");
break;
case TIME:
stringBuilder.append("time");
stringBuilder.append("TIME");
break;
case TIMEV2:
stringBuilder.append("time").append("(").append(scale).append(")");
stringBuilder.append("TIME").append("(").append(scale).append(")");
break;
case BOOLEAN:
return "boolean";
return "BOOLEAN";
case TINYINT:
return "tinyint(4)";
return "TINYINT";
case SMALLINT:
return "smallint(6)";
return "SMALLINT";
case INT:
return "int(11)";
return "INT";
case BIGINT:
return "bigint(20)";
return "BIGINT";
case LARGEINT:
return "largeint(40)";
return "LARGEINT";
case FLOAT:
case DOUBLE:
case DATE:
Expand All @@ -688,19 +688,19 @@ public String toSql(int depth) {
case LAMBDA_FUNCTION:
case ARRAY:
case NULL_TYPE:
stringBuilder.append(type.toString().toLowerCase());
stringBuilder.append(type);
break;
case STRING:
stringBuilder.append("text");
stringBuilder.append("TEXT");
break;
case JSONB:
stringBuilder.append("json");
stringBuilder.append("JSON");
break;
case AGG_STATE:
stringBuilder.append("agg_state(unknown)");
stringBuilder.append("AGG_STATE(UNKNOWN)");
break;
default:
stringBuilder.append("unknown type: " + type.toString());
stringBuilder.append("unknown type: ").append(type);
break;
}
return stringBuilder.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public String toSqlImpl() {
return getChild(0).toSql();
}
if (isAnalyzed) {
return "CAST(" + getChild(0).toSql() + " AS " + type.toString() + ")";
return "CAST(" + getChild(0).toSql() + " AS " + type.toSql() + ")";
} else {
return "CAST(" + getChild(0).toSql() + " AS "
+ (isImplicit ? type.toString() : targetTypeDef.toSql()) + ")";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,9 @@ public void analyze(Analyzer analyzer) throws UserException {
}
// populate selectListExprs, aliasSMap, groupingSmap and colNames
if (selectList.isExcept()) {
if (needToSql) {
originalExpr = new ArrayList<>();
}
List<SelectListItem> items = selectList.getItems();
TableName tblName = items.get(0).getTblName();
if (tblName == null) {
Expand All @@ -550,10 +553,6 @@ public void analyze(Analyzer analyzer) throws UserException {
// remove excepted columns
resultExprs.removeIf(expr -> exceptCols.contains(expr.toColumnLabel()));
colLabels.removeIf(exceptCols::contains);
if (needToSql) {
originalExpr = Expr.cloneList(resultExprs);
}

} else {
if (needToSql) {
originalExpr = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@ public void testNormal() throws AnalysisException {
columns.add(definition);
AddColumnsClause clause = new AddColumnsClause(columns, null, null);
clause.analyze(analyzer);
Assert.assertEquals("ADD COLUMN (`col1` int(11) NOT NULL DEFAULT \"0\" COMMENT \"\", "
+ "`col2` int(11) NOT NULL DEFAULT \"0\" COMMENT \"\")", clause.toString());
Assert.assertEquals("ADD COLUMN (`col1` INT NOT NULL DEFAULT \"0\" COMMENT \"\", "
+ "`col2` INT NOT NULL DEFAULT \"0\" COMMENT \"\")", clause.toString());

clause = new AddColumnsClause(columns, "", null);
clause.analyze(analyzer);
Assert.assertEquals("ADD COLUMN (`col1` int(11) NOT NULL DEFAULT \"0\" COMMENT \"\", "
+ "`col2` int(11) NOT NULL DEFAULT \"0\" COMMENT \"\")",
Assert.assertEquals("ADD COLUMN (`col1` INT NOT NULL DEFAULT \"0\" COMMENT \"\", "
+ "`col2` INT NOT NULL DEFAULT \"0\" COMMENT \"\")",
clause.toString());
Assert.assertNull(clause.getRollupName());

clause = new AddColumnsClause(columns, "testTable", null);
clause.analyze(analyzer);

Assert.assertEquals("ADD COLUMN (`col1` int(11) NOT NULL DEFAULT \"0\" COMMENT \"\", "
+ "`col2` int(11) NOT NULL DEFAULT \"0\" COMMENT \"\") IN `testTable`",
Assert.assertEquals("ADD COLUMN (`col1` INT NOT NULL DEFAULT \"0\" COMMENT \"\", "
+ "`col2` INT NOT NULL DEFAULT \"0\" COMMENT \"\") IN `testTable`",
clause.toString());
Assert.assertNull(clause.getProperties());
Assert.assertEquals("testTable", clause.getRollupName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void testNormal() throws AnalysisException {
ColumnDef column = new ColumnDef("col", intCol);
column.analyze(true);

Assert.assertEquals("`col` int(11) NOT NULL COMMENT \"\"", column.toString());
Assert.assertEquals("`col` INT NOT NULL COMMENT \"\"", column.toString());
Assert.assertEquals("col", column.getName());
Assert.assertEquals(PrimitiveType.INT, column.getType().getPrimitiveType());
Assert.assertNull(column.getAggregateType());
Expand All @@ -72,14 +72,14 @@ public void testNormal() throws AnalysisException {
column.analyze(true);
Assert.assertNull(column.getAggregateType());
Assert.assertEquals("10", column.getDefaultValue());
Assert.assertEquals("`col` int(11) NOT NULL DEFAULT \"10\" COMMENT \"\"", column.toSql());
Assert.assertEquals("`col` INT NOT NULL DEFAULT \"10\" COMMENT \"\"", column.toSql());

// agg
column = new ColumnDef("col", floatCol, false, AggregateType.SUM, false, new DefaultValue(true, "10"), "");
column.analyze(true);
Assert.assertEquals("10", column.getDefaultValue());
Assert.assertEquals(AggregateType.SUM, column.getAggregateType());
Assert.assertEquals("`col` float SUM NOT NULL DEFAULT \"10\" COMMENT \"\"", column.toSql());
Assert.assertEquals("`col` FLOAT SUM NOT NULL DEFAULT \"10\" COMMENT \"\"", column.toSql());
}

@Test
Expand All @@ -89,14 +89,14 @@ 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(11) 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
ColumnDef column = new ColumnDef("col", intCol, false, AggregateType.REPLACE_IF_NOT_NULL, false, new DefaultValue(true, "10"), "");
column.analyze(true);
Assert.assertEquals(AggregateType.REPLACE_IF_NOT_NULL, column.getAggregateType());
Assert.assertEquals("`col` int(11) REPLACE_IF_NOT_NULL NULL DEFAULT \"10\" COMMENT \"\"", column.toSql());
Assert.assertEquals("`col` INT REPLACE_IF_NOT_NULL NULL DEFAULT \"10\" COMMENT \"\"", column.toSql());
} // CHECKSTYLE IGNORE THIS LINE
}

Expand Down
Loading

0 comments on commit b1a15f9

Please sign in to comment.