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


##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -16099,6 +16099,28 @@ void testTimestampDiff(boolean coercionEnabled) {
         "BOOLEAN", true);
   }
 
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-6950";>[CALCITE-6950]
+   * Use ANY operator to check if an element exists in an array throws 
exception</a>. */
+  @Test void testQuantifyCollectionOperators2() {
+    final SqlOperatorFixture f = fixture();
+    QUANTIFY_OPERATORS.forEach(operator -> f.setFor(operator, 
SqlOperatorFixture.VmName.EXPAND));
+
+    f.checkNull("1.0 = some (ARRAY[2,3,null])");

Review Comment:
   why is this null?



##########
core/src/main/java/org/apache/calcite/sql/validate/implicit/TypeCoercionImpl.java:
##########
@@ -563,6 +564,76 @@ protected boolean booleanEquality(SqlCallBinding binding,
     return false;
   }
 
+  /**
+   * {@inheritDoc}
+   *
+   * <p>STRATEGIES
+   *
+   * <p>To determine the common type:
+   *
+   * <ul>
+   *
+   * <li>When the LHS has a Simple type and RHS has a Collection type 
determined by {@link SqlTypeUtil#isCollection},
+   * to find the common type of LHS's type and RHS's component type.
+   * <li>If the common type differs from the LHS type, then coerced LHS type,
+   * and nullable remains unchanged.
+   * <li>Create a new Collection type that matches the common type,
+   * and nullable keep same as RHS's component type and RHS's collection type.
+   * <li>If this new Collection type differs from the RHS type, adjust the RHS 
type as needed.
+   *
+   *<pre>
+   * field1       ARRAY(field2, field3, field4)
+   *    |                |       |        |

Review Comment:
   lines are not aligned with pluses



##########
core/src/main/java/org/apache/calcite/sql/validate/implicit/TypeCoercionImpl.java:
##########
@@ -563,6 +564,76 @@ protected boolean booleanEquality(SqlCallBinding binding,
     return false;
   }
 
+  /**
+   * {@inheritDoc}
+   *
+   * <p>STRATEGIES
+   *
+   * <p>To determine the common type:
+   *
+   * <ul>
+   *
+   * <li>When the LHS has a Simple type and RHS has a Collection type 
determined by {@link SqlTypeUtil#isCollection},
+   * to find the common type of LHS's type and RHS's component type.
+   * <li>If the common type differs from the LHS type, then coerced LHS type,
+   * and nullable remains unchanged.
+   * <li>Create a new Collection type that matches the common type,
+   * and nullable keep same as RHS's component type and RHS's collection type.
+   * <li>If this new Collection type differs from the RHS type, adjust the RHS 
type as needed.
+   *
+   *<pre>
+   * field1       ARRAY(field2, field3, field4)
+   *    |                |       |        |
+   *    |               +-------+---------+
+   *    |                       |
+   *    |                 component type
+   *    |                      |
+   *    +-----common type------+
+   *</pre>
+   *
+   * <li>Notice: If either LHS or RHS has a {@link SqlTypeName#NULL} type, it 
will directly return without any adjusting.
+   * </ul>
+   */
+  @Override public boolean quantifyOperationCoercion(SqlCallBinding binding) {
+    final RelDataType type1 = binding.getOperandType(0);
+    final RelDataType collectionType = binding.getOperandType(1);
+    final RelDataType type2 = collectionType.getComponentType();
+    assert type2 != null;
+    if (type1.getSqlTypeName() == SqlTypeName.NULL || type2.getSqlTypeName() 
== SqlTypeName.NULL) {
+      return false;
+    }
+    final SqlNode node1 = binding.operand(0);
+    final SqlNode node2 = binding.operand(1);
+    RelDataType widenType = commonTypeForBinaryComparison(type1, type2);
+    if (widenType == null) {
+      widenType = getTightestCommonType(type1, type2);
+    }
+    if (widenType == null) {
+      return false;
+    }
+    final RelDataType leftWidenType =
+        binding.getTypeFactory().enforceTypeWithNullability(widenType, 
type1.isNullable());
+    boolean coercedLeft =
+        coerceOperandType(getScope(binding), binding.getCall(), 0, 
leftWidenType);
+    if (coercedLeft) {
+      updateInferredType(node1, leftWidenType);
+    }
+    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(getScope(binding), binding.getCall(), 1, 
collectionWidenType);

Review Comment:
   I hope these calls adjust the types in the typeMap and the callbinding too.
   



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