liuyongvs commented on code in PR #3189:
URL: https://github.com/apache/calcite/pull/3189#discussion_r1197508002


##########
core/src/main/java/org/apache/calcite/sql/fun/SqlCastFunction.java:
##########
@@ -122,8 +128,50 @@ static SqlReturnTypeInference returnTypeInference(boolean 
safe) {
   /** Derives the type of "CAST(expression AS targetType)". */
   public static RelDataType deriveType(RelDataTypeFactory typeFactory,
       RelDataType expressionType, RelDataType targetType, boolean safe) {
-    return typeFactory.createTypeWithNullability(targetType,
-        expressionType.isNullable() || safe);
+    return createTypeWithNullabilityFromExpr(typeFactory, expressionType, 
targetType, safe);
+  }
+
+  public static RelDataType 
createTypeWithNullabilityFromExpr(RelDataTypeFactory typeFactory,
+      RelDataType expressionType, RelDataType targetType, boolean safe) {
+    boolean isn = expressionType.isNullable() || safe;
+
+    if (isCollection(expressionType)) {
+      RelDataType expressionElementType = expressionType.getComponentType();
+      RelDataType targetElementType = targetType.getComponentType();
+      RelDataType newElementType =
+          createTypeWithNullabilityFromExpr(typeFactory, 
expressionElementType, targetElementType, false);

Review Comment:
   after dig the code of spark, you are right, thanks. 
   I intuitively thought it was the latter.
   
   ```
     test("element type nullability") {
       val array = Literal.create(Seq("123", "true"),
         ArrayType(StringType, containsNull = false))
       // array element can be null after try_cast which violates the target 
type.
       val c1 = cast(array, ArrayType(BooleanType, containsNull = false))
       assert(!c1.resolved)
   
       val map = Literal.create(Map("a" -> "123", "b" -> "true"),
         MapType(StringType, StringType, valueContainsNull = false))
       // key can be null after try_cast which violates the map key requirement.
       val c2 = cast(map, MapType(IntegerType, StringType, valueContainsNull = 
true))
       assert(!c2.resolved)
       // map value can be null after try_cast which violates the target type.
       val c3 = cast(map, MapType(StringType, IntegerType, valueContainsNull = 
false))
       assert(!c3.resolved)
   
       val struct = Literal.create(
         InternalRow(
           UTF8String.fromString("123"),
           UTF8String.fromString("true")),
         new StructType()
           .add("a", StringType, nullable = true)
           .add("b", StringType, nullable = true))
       // struct field `b` can be null after try_cast which violates the target 
type.
       val c4 = cast(struct, new StructType()
         .add("a", BooleanType, nullable = true)
         .add("b", BooleanType, nullable = false))
       assert(!c4.resolved)
     }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to