sbroeder opened a new pull request, #4959:
URL: https://github.com/apache/calcite/pull/4959

   Summary
   RexCall.isAlwaysTrue() and isAlwaysFalse() group CAST together with IS_TRUE 
/ IS_NOT_FALSE (and IS_FALSE / IS_NOT_TRUE) and simply delegate to the operand. 
That is wrong when the cast changes the type: CAST(TRUE AS INTEGER) is an 
INTEGER expression that evaluates to 1, not a boolean, so it must report 
isAlwaysTrue() == false. Today it returns true.
   
   This is an API-contract violation. RexSimplify happens not to mis-fire on 
this expression upstream (its Comparison helper requires a LITERAL-kind 
operand, which CAST is not), but any caller that consults isAlwaysTrue/False on 
an arbitrary RexNode and trusts the result to be boolean-valued can be misled.
   
   Fix
   Split the CAST case out of the fall-through group in both methods and only 
delegate to the operand when the result type is BOOLEAN.
   
     - core/src/main/java/org/apache/calcite/rex/RexCall.java
   
   Tests
   core/src/test/java/org/apache/calcite/rex/RexProgramTest.java:
   
     - testIsAlwaysTrueCastBooleanToInteger — CAST(TRUE AS 
INTEGER).isAlwaysTrue() is false. Fails on unfixed code.
     - testIsAlwaysFalseCastBooleanToInteger — CAST(FALSE AS 
INTEGER).isAlwaysFalse() is false. Fails on unfixed code.
     - testIsAlwaysTrueCastBooleanToBoolean — CAST(TRUE AS 
BOOLEAN).isAlwaysTrue() is still true (no regression for same-type casts).
     - testIsAlwaysFalseCastBooleanToBoolean — CAST(FALSE AS 
BOOLEAN).isAlwaysFalse() is still true.
   
   Repro (against unfixed code)
   
     ./gradlew :core:test \
       --tests 
"org.apache.calcite.rex.RexProgramTest.testIsAlwaysTrueCastBooleanToInteger" \
       --tests 
"org.apache.calcite.rex.RexProgramTest.testIsAlwaysFalseCastBooleanToInteger"
   
     Both fail with Expected: is <false> but: was <true>. With the fix applied, 
all four tests pass.
   
   [CALCITE-7542](https://issues.apache.org/jira/browse/CALCITE-7542)
   
   ## Changes Proposed
   <!--
     Split the CAST case out of the fall-through group in both isAlwaysTrue() 
and isAlwaysFalse() and only delegate to the operand when the result type is 
BOOLEAN:
   
     - core/src/main/java/org/apache/calcite/rex/RexCall.java
   -->
   


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