Skip to content

Commit

Permalink
[fix](compatibility) type toSql should return lowercase string (#38012)
Browse files Browse the repository at this point in the history
pick from master #38012

revert #25951
  • Loading branch information
morrySnow committed Aug 8, 2024
1 parent 30e2c3f commit 8cd3e69
Show file tree
Hide file tree
Showing 130 changed files with 2,496 additions and 2,504 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,18 @@ public boolean getResultIsNullable() {
@Override
public String toSql(int depth) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("AGG_STATE<").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(")>");
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("DECIMAL").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("DECIMAL").append("(`").append(precisionStr)
.append("`, `").append(scaleStr).append("`)");
stringBuilder.append("decimalv2").append("(`").append(precisionStr)
.append("`,`").append(scaleStr).append("`)");
} else {
stringBuilder.append("DECIMAL").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 @@ -451,40 +451,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 @@ -207,7 +207,7 @@ private void registerFEFunction(ImmutableMultimap.Builder<String, FEFunctionInvo
Method method, FEFunction annotation) {
if (annotation != null) {
String name = annotation.name();
Type returnType = Type.fromPrimitiveType(PrimitiveType.valueOf(annotation.returnType()));
Type returnType = Type.fromPrimitiveType(PrimitiveType.valueOf(annotation.returnType().toUpperCase()));
List<Type> argTypes = new ArrayList<>();
for (String type : annotation.argTypes()) {
argTypes.add(ScalarType.createType(type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public static void write(DataOutput out, Type type) throws IOException {
}

public static Type read(DataInput in) throws IOException {
PrimitiveType primitiveType = PrimitiveType.valueOf(Text.readString(in));
PrimitiveType primitiveType = PrimitiveType.valueOf(Text.readString(in).toUpperCase());
if (primitiveType == PrimitiveType.ARRAY) {
Type itermType = read(in);
boolean containsNull = in.readBoolean();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,11 @@ public void write(DataOutput out) throws IOException {
public void readFields(DataInput in) throws IOException {
int count = in.readInt();
for (int i = 0; i < count; i++) {
PrimitiveType type = PrimitiveType.valueOf(Text.readString(in));
PrimitiveType type = PrimitiveType.valueOf(Text.readString(in).toUpperCase());
boolean isMax = in.readBoolean();
if (type == PrimitiveType.NULL_TYPE) {
String realType = StringLiteral.read(in).getStringValue();
type = PrimitiveType.valueOf(realType);
type = PrimitiveType.valueOf(realType.toUpperCase());
types.add(type);
keys.add(NullLiteral.create(Type.fromPrimitiveType(type)));
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static Histogram deserializeFromJson(String json) {

JsonObject histogramJson = JsonParser.parseString(json).getAsJsonObject();
String typeStr = histogramJson.get("data_type").getAsString();
Type dataType = Type.fromPrimitiveType(PrimitiveType.valueOf(typeStr));
Type dataType = Type.fromPrimitiveType(PrimitiveType.valueOf(typeStr.toUpperCase()));
histogramBuilder.setDataType(dataType);

float sampleRate = histogramJson.get("sample_rate").getAsFloat();
Expand Down
Loading

0 comments on commit 8cd3e69

Please sign in to comment.