[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:
amory
2024-08-01 09:18:15 +08:00
committed by GitHub
parent fa45f3b95a
commit 6bd93b119f
6 changed files with 216 additions and 1 deletions

View File

@ -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,

View File

@ -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;
}
}

View File

@ -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();