Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix](compatibility) type toSql should return lowercase string #38012

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,17 @@ public boolean getResultIsNullable() {
@Override
public String toSql(int depth) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("AGG_STATE<arguments=").append(functionName).append("(");
stringBuilder.append("agg_state<").append(functionName).append("(");
for (int i = 0; i < subTypes.size(); i++) {
if (i > 0) {
stringBuilder.append(", ");
}
stringBuilder.append(subTypes.get(i).toSql());
if (subTypeNullables.get(i)) {
stringBuilder.append(" NULL");
stringBuilder.append(" null");
}
}
stringBuilder.append("), function_name=");
stringBuilder.append(functionName);
stringBuilder.append(", result_is_nullable=");
stringBuilder.append(resultIsNullable);
stringBuilder.append(")");
stringBuilder.append(">");
return stringBuilder.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,13 @@ public static ArrayType create(Type type, boolean containsNull) {

@Override
public String toSql(int depth) {
StringBuilder typeStr = new StringBuilder();
typeStr.append("array<").append(itemType.toSql(depth + 1));
if (!containsNull) {
return "ARRAY<" + itemType.toSql(depth + 1) + " NOT NULL>";
} else {
return "ARRAY<" + itemType.toSql(depth + 1) + ">";
typeStr.append(" not null");
}
typeStr.append(">");
return typeStr.toString();
}

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

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ public boolean equals(Object other) {
@Override
public String toSql(int depth) {
if (depth >= MAX_NESTING_DEPTH) {
return "MAP<...>";
return "map<...>";
}
return String.format("MAP<%s,%s>",
return String.format("map<%s,%s>",
keyType.toSql(depth + 1), valueType.toSql(depth + 1));
}

Expand Down Expand Up @@ -176,7 +176,7 @@ public Type specializeTemplateType(Type specificType, Map<String, Type> speciali

@Override
public String toString() {
return String.format("MAP<%s,%s>",
return String.format("map<%s,%s>",
keyType.toString(), valueType.toString());
}

Expand Down
85 changes: 41 additions & 44 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 @@ -579,36 +579,34 @@ public static ScalarType createHllType() {
public String toString() {
if (type == PrimitiveType.CHAR) {
if (isWildcardChar()) {
return "CHARACTER(" + MAX_CHAR_LENGTH + ")";
return "character(" + MAX_CHAR_LENGTH + ")";
}
return "CHAR(" + len + ")";
return "char(" + len + ")";
} else if (type == PrimitiveType.DECIMALV2) {
if (isWildcardDecimal()) {
return "DECIMAL(*, *)";
return "decimal(*,*)";
}
return "DECIMAL(" + precision + ", " + scale + ")";
return "decimal(" + precision + "," + scale + ")";
} else if (type.isDecimalV3Type()) {
if (isWildcardDecimal()) {
return "DECIMALV3(*, *)";
return "decimalv3(*,*)";
}
return "DECIMALV3(" + precision + ", " + scale + ")";
return "decimalv3(" + precision + "," + scale + ")";
} else if (type == PrimitiveType.DATETIMEV2) {
return "DATETIMEV2(" + scale + ")";
return "datetimev2(" + scale + ")";
} else if (type == PrimitiveType.TIMEV2) {
return "TIMEV2(" + scale + ")";
return "timev2(" + scale + ")";
} else if (type == PrimitiveType.VARCHAR) {
if (isWildcardVarchar()) {
return "VARCHAR(" + MAX_VARCHAR_LENGTH + ")";
return "varchar(" + MAX_VARCHAR_LENGTH + ")";
}
return "VARCHAR(" + len + ")";
return "varchar(" + len + ")";
} else if (type == PrimitiveType.STRING) {
return "TEXT";
return "text";
} else if (type == PrimitiveType.JSONB) {
return "JSON";
} else if (type == PrimitiveType.VARIANT) {
return "VARIANT";
return "json";
}
return type.toString();
return type.toString().toLowerCase();
}

@Override
Expand All @@ -617,73 +615,73 @@ public String toSql(int depth) {
switch (type) {
case CHAR:
if (isWildcardChar()) {
stringBuilder.append("CHARACTER").append("(").append(MAX_CHAR_LENGTH).append(")");
stringBuilder.append("character").append("(").append(MAX_CHAR_LENGTH).append(")");
} 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()) {
return "VARCHAR(" + MAX_VARCHAR_LENGTH + ")";
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("DECIMALV2").append("(").append(precision)
.append(", ").append(scale).append(")");
stringBuilder.append("decimalv2").append("(").append(precision)
.append(",").append(scale).append(")");
} else if (!Strings.isNullOrEmpty(precisionStr) && !Strings.isNullOrEmpty(scaleStr)) {
stringBuilder.append("DECIMALV2").append("(`").append(precisionStr)
.append("`, `").append(scaleStr).append("`)");
stringBuilder.append("decimalv2").append("(`").append(precisionStr)
.append("`,`").append(scaleStr).append("`)");
} else {
stringBuilder.append("DECIMALV2").append("(`").append(precisionStr).append("`)");
stringBuilder.append("decimalv2").append("(`").append(precisionStr).append("`)");
}
break;
case DECIMAL32:
case DECIMAL64:
case DECIMAL128:
case DECIMAL256:
String typeName = "DECIMALV3";
String typeName = "decimalv3";
if (Strings.isNullOrEmpty(precisionStr)) {
stringBuilder.append(typeName).append("(").append(precision)
.append(", ").append(scale).append(")");
.append(",").append(scale).append(")");
} else if (!Strings.isNullOrEmpty(precisionStr) && !Strings.isNullOrEmpty(scaleStr)) {
stringBuilder.append(typeName).append("(`").append(precisionStr)
.append("`, `").append(scaleStr).append("`)");
.append("`,`").append(scaleStr).append("`)");
} else {
stringBuilder.append(typeName).append("(`").append(precisionStr).append("`)");
}
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";
return "tinyint";
case SMALLINT:
return "SMALLINT";
return "smallint";
case INT:
return "INT";
return "int";
case BIGINT:
return "BIGINT";
return "bigint";
case LARGEINT:
return "LARGEINT";
return "largeint";
case IPV4:
return "IPV4";
return "ipv4";
case IPV6:
return "IPV6";
return "ipv6";
case FLOAT:
case DOUBLE:
case DATE:
Expand All @@ -694,15 +692,14 @@ public String toSql(int depth) {
case VARIANT:
case QUANTILE_STATE:
case LAMBDA_FUNCTION:
case ARRAY:
case NULL_TYPE:
stringBuilder.append(type);
stringBuilder.append(type.toString().toLowerCase());
break;
case STRING:
stringBuilder.append("TEXT");
stringBuilder.append("text");
break;
case JSONB:
stringBuilder.append("JSON");
stringBuilder.append("json");
break;
default:
stringBuilder.append("unknown type: ").append(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public boolean getContainsNull() {
public String toSql(int depth) {
String typeSql;
if (depth < Type.MAX_NESTING_DEPTH) {
typeSql = !containsNull ? "not_null(" + type.toSql(depth) + ")" : type.toSql(depth);
typeSql = type.toSql(depth + 1) + (!containsNull ? " not null" : "");
} else {
typeSql = "...";
}
Expand All @@ -97,7 +97,7 @@ public String toSql(int depth) {
sb.append(":").append(typeSql);
}
if (!Strings.isNullOrEmpty(comment)) {
sb.append(String.format(" COMMENT '%s'", comment));
sb.append(String.format(" comment '%s'", comment));
}
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ public StructType() {
@Override
public String toSql(int depth) {
if (depth >= MAX_NESTING_DEPTH) {
return "STRUCT<...>";
return "struct<...>";
}
ArrayList<String> fieldsSql = Lists.newArrayList();
for (StructField f : fields) {
fieldsSql.add(f.toSql(depth + 1));
}
return String.format("STRUCT<%s>", Joiner.on(",").join(fieldsSql));
return String.format("struct<%s>", Joiner.on(",").join(fieldsSql));
}

@Override
Expand Down Expand Up @@ -331,7 +331,7 @@ public String toString() {
for (StructField f : fields) {
fieldsSql.add(f.toString());
}
return String.format("STRUCT<%s>", Joiner.on(",").join(fieldsSql));
return String.format("struct<%s>", Joiner.on(",").join(fieldsSql));
}

@Override
Expand Down
28 changes: 15 additions & 13 deletions fe/fe-common/src/main/java/org/apache/doris/catalog/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -452,40 +452,42 @@ public boolean typeContainsPrecision() {
}

public String hideVersionForVersionColumn(Boolean isToSql) {
if (isDatetimeV2()) {
StringBuilder typeStr = new StringBuilder("DATETIME");
if (isDatetime() || isDatetimeV2()) {
StringBuilder typeStr = new StringBuilder("datetime");
if (((ScalarType) this).getScalarScale() > 0) {
typeStr.append("(").append(((ScalarType) this).getScalarScale()).append(")");
}
return typeStr.toString();
} else if (isDateV2()) {
return "DATE";
} else if (isDecimalV3()) {
StringBuilder typeStr = new StringBuilder("DECIMAL");
} else if (isDate() || isDateV2()) {
return "date";
} else if (isDecimalV2() || isDecimalV3()) {
StringBuilder typeStr = new StringBuilder("decimal");
ScalarType sType = (ScalarType) this;
int scale = sType.getScalarScale();
int precision = sType.getScalarPrecision();
// not default
if (!sType.isDefaultDecimal()) {
typeStr.append("(").append(precision).append(", ").append(scale)
.append(")");
typeStr.append("(").append(precision).append(",").append(scale).append(")");
return typeStr.toString();
} else if (isTime() || isTimeV2()) {
StringBuilder typeStr = new StringBuilder("time");
if (((ScalarType) this).getScalarScale() > 0) {
typeStr.append("(").append(((ScalarType) this).getScalarScale()).append(")");
}
return typeStr.toString();
} else if (isArrayType()) {
String nestedDesc = ((ArrayType) this).getItemType().hideVersionForVersionColumn(isToSql);
return "ARRAY<" + nestedDesc + ">";
return "array<" + nestedDesc + ">";
} else if (isMapType()) {
String keyDesc = ((MapType) this).getKeyType().hideVersionForVersionColumn(isToSql);
String valueDesc = ((MapType) this).getValueType().hideVersionForVersionColumn(isToSql);
return "MAP<" + keyDesc + "," + valueDesc + ">";
return "map<" + keyDesc + "," + valueDesc + ">";
} else if (isStructType()) {
List<String> fieldDesc = new ArrayList<>();
StructType structType = (StructType) this;
for (int i = 0; i < structType.getFields().size(); i++) {
StructField field = structType.getFields().get(i);
fieldDesc.add(field.getName() + ":" + field.getType().hideVersionForVersionColumn(isToSql));
}
return "STRUCT<" + StringUtils.join(fieldDesc, ",") + ">";
return "struct<" + StringUtils.join(fieldDesc, ",") + ">";
} else if (isToSql) {
return this.toSql();
}
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 NOT NULL DEFAULT \"0\" COMMENT \"\", "
+ "`col2` INT 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 NOT NULL DEFAULT \"0\" COMMENT \"\", "
+ "`col2` INT 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 NOT NULL DEFAULT \"0\" COMMENT \"\", "
+ "`col2` INT 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
Loading
Loading