NobiGo commented on code in PR #4362:
URL: https://github.com/apache/calcite/pull/4362#discussion_r2082854242
##########
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:
The RHS type has been updated by:
```
if (coercedRight) {
updateInferredType(node2, collectionWidenType);
}
```
I'm not sure if this is the same issue, but it would be helpful to add a
description of the usage issues that may arise when having inaccurate types in
CALCITE-6723.
I will also review another PR mentioned in the comments on CALCITE-6723, as
there may be some duplicate code that can achieve the same capability.
Currently, I am unsure.
--
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]