[pick](cast)Feature cast complexttype2 json (#38632)
## Proposed changes backport: https://github.com/apache/doris/pull/36548 Issue Number: close #xxx <!--Describe your changes.-->
This commit is contained in:
@ -152,6 +152,10 @@ public class CastExpr extends Expr {
|
||||
Type from = getActualArgTypes(collectChildReturnTypes())[0];
|
||||
Type to = getActualType(type);
|
||||
NullableMode nullableMode = TYPE_NULLABLE_MODE.get(Pair.of(from, to));
|
||||
// for complex type cast to jsonb we make ret is always nullable
|
||||
if (from.isComplexType() && type.isJsonbType()) {
|
||||
nullableMode = Function.NullableMode.ALWAYS_NULLABLE;
|
||||
}
|
||||
Preconditions.checkState(nullableMode != null,
|
||||
"cannot find nullable node for cast from " + from + " to " + to);
|
||||
fn = new Function(new FunctionName(getFnName(type)), Lists.newArrayList(e.type), type,
|
||||
|
||||
@ -92,6 +92,9 @@ public class CheckCast implements ExpressionPatternRuleFactory {
|
||||
}
|
||||
return true;
|
||||
} else if (originalType instanceof JsonType || targetType instanceof JsonType) {
|
||||
if (originalType.isComplexType() && !checkMapKeyIsStringLikeForJson(originalType)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return checkPrimitiveType(originalType, targetType);
|
||||
@ -127,4 +130,23 @@ public class CheckCast implements ExpressionPatternRuleFactory {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* check if complexType type which contains map, make sure key is string like for json
|
||||
*
|
||||
* @param complexType need to check
|
||||
* @return true if complexType can cast to json
|
||||
*/
|
||||
public static boolean checkMapKeyIsStringLikeForJson(DataType complexType) {
|
||||
if (complexType.isMapType()) {
|
||||
return ((MapType) complexType).getKeyType().isStringLikeType();
|
||||
} else if (complexType.isArrayType()) {
|
||||
return checkMapKeyIsStringLikeForJson(((ArrayType) complexType).getItemType());
|
||||
} else if (complexType.isStructType()) {
|
||||
for (StructField f : ((StructType) complexType).getFields()) {
|
||||
return checkMapKeyIsStringLikeForJson(f.getDataType());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -79,7 +79,7 @@ public class Cast extends Expression implements UnaryExpression {
|
||||
return true;
|
||||
} else if (!childDataType.isTimeLikeType() && targetType.isTimeLikeType()) {
|
||||
return true;
|
||||
} else if (childDataType.isJsonType()) {
|
||||
} else if (childDataType.isJsonType() || targetType.isJsonType()) {
|
||||
return true;
|
||||
} else {
|
||||
return child().nullable();
|
||||
|
||||
Reference in New Issue
Block a user