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


##########
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:
   Fixed in a37536e8b5. The shortcut now accepts only non-nullable arrays whose 
approximate numeric elements match the component type and are already at its 
precision. Calcite handles mixed widths, REAL values that still need rounding, 
and NaNs. The expanded mixed-batch test failed against the previous head and 
now passes. An independent 134-case comparison also matched Calcite on the Rex 
trees, element types and floating-point bits.
   



##########
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:
   Fixed in a37536e8b5. Direct numeric conversion requires a BigDecimal value 
and a DECIMAL or in-range signed integer source. Out-of-range integers, 
unsigned types, typed NULLs and numeric-typed SARGs keep whole-batch fallback. 
The test checks the original list is delegated for INTEGER 2147483648 and typed 
NULL. All 399 executor tests passed. I also checked signed bounds, SARGs, 
unsigned sources, floating-point midpoints and overflow/underflow against 
Calcite with Pinot's type system.
   



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