Repository navigation
[BugFix] Support cast of boolean to double and float in Calcite (#5987) - #5993
Conversation
…search-project#5987) Calcite rewrites BOOLEAN to exact numeric casts into CASE expressions, but not BOOLEAN to approximate numeric casts, so the generated code passed a boolean to Primitive.numberValueRoundDown(Number) and failed to compile, returning a 500. Rewrite these casts in ExtendedRexBuilder so true becomes 1.0, false becomes 0.0 and null stays null, matching the integer casts and the v2 engine. Signed-off-by: Shubham <87247136+shuseng8@users.noreply.github.com>
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Explore these optional code suggestions:
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5993 +/- ##
============================================
+ Coverage 89.46% 89.47% +0.01%
- Complexity 12251 12255 +4
============================================
Files 980 980
Lines 41967 41979 +12
Branches 4829 4831 +2
============================================
+ Hits 37545 37561 +16
+ Misses 2945 2942 -3
+ Partials 1477 1476 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Hi @Swiddis, thanks for the approval! The remaining red checks look unrelated to this change (analytics-engine security IT / async PPL lifecycle IT failures, and codecov/project). Could you re-run the failed jobs when you get a chance? Happy to look at anything that still fails. |
Description
cast(<boolean> as double)andcast(<boolean> as float)failed with a 500 (No applicable constructor/method found for actual parameters "boolean"; candidates are: "public java.lang.Object org.apache.calcite.linq4j.tree.Primitive.numberValueRoundDown(java.lang.Number)"), whilecast(<boolean> as int)andcast(<boolean> as long)worked.Root cause: Calcite's
RexBuilder.makeCastrewrites a BOOLEAN → exact numeric cast (INTEGER/BIGINT) intoCASE(b, 1, 0), which is why the int/long casts work. There is no such rewrite for approximate numeric targets, so a BOOLEAN → DOUBLE/REAL cast reachesRexToLixTranslator, which emitsPrimitive.of(double.class).numberValueRoundDown(<boolean>)and the generated code does not compile.Fix:
ExtendedRexBuilder.makeCastnow rewrites BOOLEAN → approximate numeric (DOUBLE, FLOAT/REAL) casts the same way Calcite does for exact numerics:CASE(b, 1.0, 0.0), wrapped inCASE(IS NOT NULL(b), ..., null)when the input is nullable. Sotrue→1.0,false→0.0, andnullstaysnull. This matches the int/long casts, the v2 engine (TypeCastOperatorsmaps boolean to1D/0Dand1f/0f), and the documentedv?1:0rule indocs/user/ppl/functions/conversion.md, so no doc change is needed.Related Issues
Resolves #5987
Check List
ExtendedRexBuilderTestfor the rewritten expression (nullable and non-null boolean, DOUBLE and REAL).CalcitePPLCastFunctionTestthat executecast(... as int/double/float)on true, false and null booleans and on boolean literals. Both fail onmainwith the codegen error from the issue.CalcitePPLCastFunctionIT.testCastBOOLEAN, which were commented out with a TODO for this codegen error.issues/5987.ymlwith true, false and missing values.--signoffor-s.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.