[FIX](map/struct)fix map/struct literal from fe (#28026)

This commit is contained in:
amory
2023-12-06 13:56:56 +08:00
committed by GitHub
parent 0a22d969e1
commit 393c491820
8 changed files with 368 additions and 15 deletions

View File

@ -18,7 +18,6 @@
package org.apache.doris.analysis;
import org.apache.doris.catalog.ArrayType;
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.catalog.Type;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.thrift.TExprNode;
@ -52,6 +51,9 @@ public class ArrayLiteral extends LiteralExpr {
Type itemType = Type.NULL;
boolean containsNull = true;
for (LiteralExpr expr : exprs) {
if (!ArrayType.ARRAY.supportSubType(expr.getType())) {
throw new AnalysisException("Invalid item type in Array, not support " + expr.getType());
}
if (itemType == Type.NULL) {
itemType = expr.getType();
} else {
@ -135,13 +137,7 @@ public class ArrayLiteral extends LiteralExpr {
List<String> list = new ArrayList<>(children.size());
children.forEach(v -> {
// we should use type to decide we output array is suitable for json format
if (!(v instanceof NullLiteral) && v.getType().isScalarType()
&& (Type.getNumericTypes().contains((ScalarType) v.getActualScalarType(v.getType()))
|| v.getType() == Type.BOOLEAN)) {
list.add(v.getStringValueInFe());
} else {
list.add(v.getStringValueForArray());
}
list.add(getStringLiteralForComplexType(v));
});
return "[" + StringUtils.join(list, ", ") + "]";
}

View File

@ -109,6 +109,21 @@ public abstract class LiteralExpr extends Expr implements Comparable<LiteralExpr
return literalExpr;
}
public static String getStringLiteralForComplexType(Expr v) {
if (!(v instanceof NullLiteral) && v.getType().isScalarType()
&& (Type.getNumericTypes().contains((ScalarType) v.getActualScalarType(v.getType()))
|| v.getType() == Type.BOOLEAN)) {
return v.getStringValueInFe();
} else if (v.getType().isComplexType()) {
// these type should also call getStringValueInFe which should handle special case for itself
return v.getStringValueInFe();
} else {
return v.getStringValueForArray();
}
}
/**
* Init LiteralExpr's Type information
* only use in rewrite alias function

View File

@ -64,9 +64,12 @@ public class MapLiteral extends LiteralExpr {
// 1. limit key type with map-key support
// 2. check type can be assigment for cast
for (int idx = 0; idx < exprs.length && idx + 1 < exprs.length; idx += 2) {
if (!MapType.MAP.supportSubType(exprs[idx].getType())) {
if (exprs[idx].getType().isComplexType() || !MapType.MAP.supportSubType(exprs[idx].getType())) {
throw new AnalysisException("Invalid key type in Map, not support " + exprs[idx].getType());
}
if (!MapType.MAP.supportSubType(exprs[idx + 1].getType())) {
throw new AnalysisException("Invalid value type in Map, not support " + exprs[idx + 1].getType());
}
boolean enableDecimal256 = SessionVariable.getEnableDecimal256();
keyType = Type.getAssignmentCompatibleType(keyType, exprs[idx].getType(), true, enableDecimal256);
valueType = Type.getAssignmentCompatibleType(valueType, exprs[idx + 1].getType(), true, enableDecimal256);
@ -159,7 +162,26 @@ public class MapLiteral extends LiteralExpr {
@Override
public String getStringValueForArray() {
return null;
List<String> list = new ArrayList<>(children.size());
for (int i = 0; i < children.size() && i + 1 < children.size(); i += 2) {
list.add(children.get(i).getStringValueForArray() + ":" + children.get(i + 1).getStringValueForArray());
}
return "{" + StringUtils.join(list, ", ") + "}";
}
@Override
public String getStringValueInFe() {
List<String> list = new ArrayList<>(children.size());
for (int i = 0; i < children.size() && i + 1 < children.size(); i += 2) {
// we should use type to decide we output array is suitable for json format
if (children.get(i).getType().isComplexType()) {
// map key type do not support complex type
throw new UnsupportedOperationException("Unsupport key type for MAP: " + children.get(i).getType());
}
list.add(children.get(i).getStringValueForArray()
+ ":" + getStringLiteralForComplexType(children.get(i + 1)));
}
return "{" + StringUtils.join(list, ", ") + "}";
}
@Override

View File

@ -45,8 +45,8 @@ public class StructLiteral extends LiteralExpr {
type = new StructType();
children = new ArrayList<>();
for (LiteralExpr expr : exprs) {
if (!expr.getType().isNull() && !type.supportSubType(expr.getType())) {
throw new AnalysisException("Invalid element type in STRUCT.");
if (!StructType.STRUCT.supportSubType(expr.getType())) {
throw new AnalysisException("Invalid element type in STRUCT: " + expr.getType());
}
((StructType) type).addField(new StructField(expr.getType()));
children.add(expr);
@ -80,7 +80,16 @@ public class StructLiteral extends LiteralExpr {
@Override
public String getStringValueForArray() {
return null;
List<String> list = new ArrayList<>(children.size());
children.forEach(v -> list.add(v.getStringValueForArray()));
return "{" + StringUtils.join(list, ", ") + "}";
}
@Override
public String getStringValueInFe() {
List<String> list = new ArrayList<>(children.size());
children.forEach(v -> list.add(getStringLiteralForComplexType(v)));
return "{" + StringUtils.join(list, ", ") + "}";
}
@Override