mihaibudiu commented on code in PR #4877:
URL: https://github.com/apache/calcite/pull/4877#discussion_r3068748720


##########
core/src/main/java/org/apache/calcite/sql/validate/implicit/TypeCoercionImpl.java:
##########
@@ -652,7 +652,23 @@ private boolean coalesceCoercion(SqlCallBinding 
callBinding) {
   @Override public boolean quantifyOperationCoercion(SqlCallBinding binding) {
     final RelDataType type1 = binding.getOperandType(0);
     final RelDataType collectionType = binding.getOperandType(1);
-    final RelDataType type2 = collectionType.getComponentType();
+    RelDataType type2 = collectionType.getComponentType();
+
+    // Determine whether the right-hand side is a subquery or a collection.
+    final boolean isSubQuery = binding.operand(1) instanceof SqlSelect;
+
+    if (type2 == null) {
+      if (!isSubQuery) {
+        return false;
+      }
+      // Subquery path: derive type2 from the first output column.

Review Comment:
   why only the first column and not the entire row?



##########
core/src/main/java/org/apache/calcite/sql/validate/implicit/TypeCoercionImpl.java:
##########
@@ -674,16 +690,23 @@ private boolean coalesceCoercion(SqlCallBinding 
callBinding) {
     }
     final RelDataType rightWidenType =
         binding.getTypeFactory().enforceTypeWithNullability(widenType, 
type2.isNullable());
-    RelDataType collectionWidenType =
-        binding.getTypeFactory().createArrayType(rightWidenType, -1);
-    collectionWidenType =
-        binding
-            .getTypeFactory()
-            .enforceTypeWithNullability(collectionWidenType, 
collectionType.isNullable());
-    boolean coercedRight =
-        coerceOperandType(scope, sqlCall, 1, collectionWidenType);
-    if (coercedRight) {
-      updateInferredType(node2, collectionWidenType);
+    boolean coercedRight;
+    if (isSubQuery) {
+      // For subquery, cast the output column directly.
+      // There is no array wrapper to reconstruct.
+      coercedRight = false;
+    } else {
+      // For collection (ARRAY[...]), reconstruct the array type with the

Review Comment:
   why always ARRAY?
   Why not a MULTISET, for example?
   Or a MAP?
   



##########
core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml:
##########
@@ -1530,7 +1530,7 @@ LogicalProject(MGR=[$3])
     <Resource name="planMid">
       <![CDATA[
 LogicalProject(DEPTNO=[$0], EXPR$1=[OR(AND(IS NOT NULL($5), <>($2, 0)), 
AND(<($3, $2), null, <>($2, 0), IS NULL($5)))])
-  LogicalJoin(condition=[=($1, $4)], joinType=[left])
+  LogicalJoin(condition=[=(CAST($1):INTEGER NOT NULL, $4)], joinType=[left])

Review Comment:
   This looks dangerous: how do we know that $1 is not null?
   A cast to a NOT NULL type may fail at runtime for a NULL value.



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