xiangfu0 commented on code in PR #20070:
URL: https://github.com/apache/pinot/pull/20070#discussion_r4655647225


##########
pinot-query-planner/src/main/java/org/apache/pinot/calcite/rex/PinotRexExecutor.java:
##########
@@ -105,6 +123,50 @@ Function1<DataContext, Object[]> getCast(RexBuilder 
builder, RelDataType source,
     });
   }
 
+  /// Returns whether the expression casts a non-null exact numeric literal 
(integer or DECIMAL) to an approximate type
+  /// (DOUBLE, FLOAT, REAL). Overflow and underflow are folded too: the 
literal holds the resulting infinity or signed
+  /// zero, as the cast would. Approximate sources are left to the fallback: 
reading them as a BigDecimal rounds to 16
+  /// digits, and NaN or infinity cannot be read that way at all.
+  @VisibleForTesting
+  static boolean isNumericToApproximateCast(RexNode expression) {
+    if (!(expression instanceof RexCall)) {
+      return false;
+    }
+    RexCall call = (RexCall) expression;
+    if (call.getOperator() != SqlStdOperatorTable.CAST || 
call.getOperands().size() != 1
+        || !SqlTypeUtil.isApproximateNumeric(call.getType())) {
+      return false;
+    }
+    RexNode operand = call.getOperands().get(0);
+    return operand instanceof RexLiteral && !((RexLiteral) operand).isNull()
+        && SqlTypeUtil.isExactNumeric(operand.getType());
+  }
+
+  /// The value of `CAST(<numeric literal> AS <approximate type>)`: REAL 
rounds to single precision, while FLOAT and
+  /// DOUBLE are both double precision in Calcite.
+  private static double approximateValue(RexCall cast) {
+    BigDecimal value = ((RexLiteral) 
cast.getOperands().get(0)).getValueAs(BigDecimal.class);
+    return cast.getType().getSqlTypeName() == SqlTypeName.REAL ? 
value.floatValue() : value.doubleValue();
+  }
+
+  /// Returns whether the expression is an `ARRAY[...]` constructor whose 
elements are all non-null literals.
+  @VisibleForTesting
+  static boolean isLiteralArray(RexNode expression) {
+    if (!(expression instanceof RexCall)) {
+      return false;
+    }
+    RexCall call = (RexCall) expression;
+    if (call.getOperator() != SqlStdOperatorTable.ARRAY_VALUE_CONSTRUCTOR || 
call.getOperands().isEmpty()) {
+      return false;
+    }
+    for (RexNode operand : call.getOperands()) {
+      if (!(operand instanceof RexLiteral) || ((RexLiteral) operand).isNull()) 
{

Review Comment:
   A non-null RexLiteral can still need coercion to the array's component type. 
With an inferred array of CHAR(1) `'a'` and CHAR(3) `'abc'`, Calcite's executor 
rebuilds the first element as CHAR(3) `'a  '`. This shortcut returns CHAR(1) 
`'a'`. Binary padding differs too. Even homogeneous REAL literals can carry a 
double that still needs rounding. For example, RexBuilder can store 
`0.12345678912345` as REAL, which Calcite reduces to `0.12345679104328156`. 
Please restrict the shortcut to vector literals whose values and types are 
already in Calcite's final representation. Other inputs should preserve 
whole-batch fallback.



##########
pinot-query-planner/src/main/java/org/apache/pinot/calcite/rex/PinotRexExecutor.java:
##########
@@ -105,6 +123,50 @@ Function1<DataContext, Object[]> getCast(RexBuilder 
builder, RelDataType source,
     });
   }
 
+  /// Returns whether the expression casts a non-null exact numeric literal 
(integer or DECIMAL) to an approximate type
+  /// (DOUBLE, FLOAT, REAL). Overflow and underflow are folded too: the 
literal holds the resulting infinity or signed
+  /// zero, as the cast would. Approximate sources are left to the fallback: 
reading them as a BigDecimal rounds to 16
+  /// digits, and NaN or infinity cannot be read that way at all.
+  @VisibleForTesting
+  static boolean isNumericToApproximateCast(RexNode expression) {
+    if (!(expression instanceof RexCall)) {
+      return false;
+    }
+    RexCall call = (RexCall) expression;
+    if (call.getOperator() != SqlStdOperatorTable.CAST || 
call.getOperands().size() != 1
+        || !SqlTypeUtil.isApproximateNumeric(call.getType())) {
+      return false;
+    }
+    RexNode operand = call.getOperands().get(0);
+    return operand instanceof RexLiteral && !((RexLiteral) operand).isNull()
+        && SqlTypeUtil.isExactNumeric(operand.getType());

Review Comment:
   The declared operand type alone does not prove that its stored value can be 
converted directly. For an INTEGER-typed Rex literal carrying `2147483648`, 
Calcite casts the signed 32-bit value to DOUBLE (`-2147483648.0`), while this 
path returns `2147483648.0`. Pinot can produce out-of-range INTEGER-typed Rex 
literals during planning (see #20277). An INTEGER-typed SARG also passes the 
predicate, then throws an AssertionError when read as BigDecimal. Please limit 
direct conversion to ordinary supported numeric values and preserve 
source-width semantics or delegate those cases. Unsigned sources should retain 
Calcite's existing behavior.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to