[CALCITE-7722] RexSimplify IS [NOT] NULL on a safe operator with Strong policy ANY and unsafe operands can be further simplified - #5184
Conversation
| case ANY: | ||
| // "f" is a strong operator, so "f(operand0, operand1) IS NOT NULL" | ||
| // simplifies to "operand0 IS NOT NULL AND operand1 IS NOT NULL" | ||
| // simplifies to "operand0 IS NOT NULL AND operand1 IS NOT NULL". |
There was a problem hiding this comment.
this comment could be shorter
There was a problem hiding this comment.
comment simplified
| if (!SafeRexVisitor.INSTANCE.isShallowSafe(a)) { | ||
| return simplifiedResult; | ||
| } | ||
| // The peel only preserves runtime semantics when each operand can be |
There was a problem hiding this comment.
comment simplified
| case ANY: | ||
| // "f" is a strong operator, so "f(operand0, operand1) IS NULL" simplifies | ||
| // to "operand0 IS NULL OR operand1 IS NULL" | ||
| // to "operand0 IS NULL OR operand1 IS NULL". This branch PRESERVES the |
There was a problem hiding this comment.
And I think this one too
There was a problem hiding this comment.
comment simplified
| checkSimplifyUnchanged(div(cast(vVarchar(), tInt(false)), nullInt)); | ||
| } | ||
|
|
||
| /** Test cases for peeling {@code IS [NOT] NULL} across a strong outer |
There was a problem hiding this comment.
Maybe you can follow the established pattern for test case JavaDoc, "Test case for [CALCITE-7722]" etc?
| // IS NULL(CAST(10/0 AS BIGINT)) stays as IS NULL(10/0) | ||
| // after the lossless-CAST strip; the DIVIDE is not | ||
| // shallow-safe, so no further distribution occurs. | ||
| checkSimplify(isNull(cast(div(vIntNotNull(), literal(0)), tBigInt())), |
There was a problem hiding this comment.
can we have some tests using checked arithmetic or arithmetic on intervals?
There was a problem hiding this comment.
good point; added both cases
|
@julianhyde the patch is actually using Strong. I have update the Jira to make it more accurate with the actual fix proposal |
thomasrebele
left a comment
There was a problem hiding this comment.
I find the idea of "shallow-safety" interesting. It would be nice if we could support simplifying ((1/0)+1) IS NOT NULL.
Side note: This PR makes me think that there are some limitations in the current code for representing the properties of the operators (e.g., CALCITE-7264). I think there was a discussion somewhere whether the possibility that a RexNode may throw an exception could be included in the type system. The safety and shallow-safety of an operator would be attached to the operator itself instead of a visitor (see Julian's comment).
The concept of "safety" and "shallow-safety" could be collapsed, i.e., an operator is safe iff its evaluation does not throw an exception. A RexNode expression is safe iff all its operators are safe. Might be a bit easier to understand than "shallow-safety".
|
|
||
| // The outer PLUS is shallow-safe, but the div(1, 0) is not, so the peel is therefore suppressed | ||
| checkSimplifyUnchanged(isNotNull(plus(div(literal(1), literal(0)), vIntNotNull()))); | ||
| checkSimplifyUnchanged(isNull(plus(div(literal(1), literal(0)), vIntNotNull()))); |
There was a problem hiding this comment.
The previous version of the PR stated the reason for not simplifying this: otherwise it would be simplified to true because RexCall#isAlwaysTrue() returns true for IS NULL(1/0). This sounds like a bug in RexCall.isAlwaysTrue() to me. Could we make RexCall#isAlwaysTrue check for the safety of the IS [NOT] NULL operand? Potentially in a follow-up ticket if this turns out to be too complex.
There was a problem hiding this comment.
Correct, the problem is that RexCall#isAlwaysTrue/isAlwaysFalse of an IS [NOT] NULL on an expression that is of a non-nullable type will always return a short-circuited true/false, without considering if the expression is safe, so we can hide a rutime issue; e.g. IS NOT NULL of a division by zero of type non-nullable would be collapsed to true. IMO this is a different improvement, I think this should be handled separately, as a follow-up ticket.
UPDATE: on a second thought, maybe if we include it in here we can simplify the current patch, checking....
UPDATE II: actually, fixing the RexCall isAlwaysTrue / isAlwaysFalse simplification for IS [NOT] NULL so that it considers isSafe makes the patch simpler, and enables more simplifications; so I've just applied it on the last commit. Thanks for pointing this out @thomasrebele !
|
+1 (This overrides my earlier '-1'. Thank you to @rubenada for explaining in Jira the purpose of this change.) |
|
@thomasrebele Yes, we could use better terminology. "Safe", "Strict" and "Strong" are parallel concepts, dealing with exceptions (non-termination), whether arguments get evaluated, and null values. It's necessary to distinguish an operator's propagation characteristics (e.g. whether it returns null if and only if both its arguments are null) from the characteristics of an expression (e.g. whether it may return null, or may throw). Even if you are able to find a good terminology that the community agrees on, implementing it is a challenge - you would have to modify the source code, potentially renaming classes, and deal with the fact that there are closed issues and commits that cannot be retrospectively changed. |
… operations if they do not affect the nullability
|
|
||
| // Operators with checked arithmetic: they cannot be peeled because they are not "safe" | ||
| // (they will throw at runtime in case of overflow) | ||
| checkSimplifyUnchanged( |
There was a problem hiding this comment.
@thomasrebele @mihaibudiu I have rebased with the latest main branch (which includes the patch "CALCITE-7725 Review safety of checked arithmetic operators").
As expected, I had to adjust the tests here, since the checked arithmetic expressions are no longer simplified (because they are not considered safe, i.e. it is now working as expected).
When you have a bit of time, could you please take another look at the PR?
|
| } | ||
| switch (Strong.policy(a)) { | ||
| case NOT_NULL: | ||
| // Drops the subtree; require full-tree safety so we don't hide runtime errors |
There was a problem hiding this comment.
should the comment be inside the braces?
| * <p>Non-{@link RexCall} nodes are always shallow-safe (they cannot | ||
| * throw at their own level). | ||
| */ | ||
| boolean isShallowSafe(RexNode node) { |
There was a problem hiding this comment.
Can this be called isOperatorSafe? After all, it's a property of the operator.
| */ | ||
| @Test void testSimplifyIsNotNullDistributesAcrossStrongOpWithLossyCast() { | ||
| // "(CAST(?0.varchar0):INTEGER + 1) IS NOT NULL" ==> "IS NOT NULL(CAST(?0.varchar0):INTEGER)" | ||
| // The outer PLUS is strong AND shallow-safe; distribution keeps the |
There was a problem hiding this comment.
what is "distribution"?
| isNull(plus(cast(vVarchar(), tInt(true)), literal(1))), | ||
| "IS NULL(CAST(?0.varchar0):INTEGER)"); | ||
|
|
||
| // Confirm this is consistent with same expression without CAST |
There was a problem hiding this comment.
I don't know which cast this is talking about
| isNotNull(plus(cast(vVarchar(), tDate(true)), interval(10, TimeUnit.DAY))), | ||
| "IS NOT NULL(CAST(?0.varchar0):DATE)"); | ||
| checkSimplify( | ||
| isNull(plus(cast(vVarchar(), tDate(true)), interval(1, TimeUnit.MONTH))), |
There was a problem hiding this comment.
Isn't this wrong? Arithmetic on dates should be checked.
Or maybe this kind of code can never be generated?



Jira Link
CALCITE-7722
Changes Proposed
RexSimplify.simplifyIsNotNull / simplifyIsNull currently bail out of the whole simplification when the input RexCall is not fully safe (i.e. isSafeExpression(a) == false). This is stricter than necessary for operators with Strong.Policy.ANY, where IS [NOT] NULL(f(x, y, ...)) is semantically equivalent to IS [NOT] NULL(x) OR/AND IS [NOT] NULL(y) OR/AND ... — the operator itself does not need to be evaluated to compute the result.
Example (regression for downstream projects such as Hive):
Before (≤ 1.34):
IS NOT NULL(CAST(key AS DOUBLE) + 1.0) → IS NOT NULL(CAST(key AS DOUBLE))
After (≥ 1.35):
IS NOT NULL(CAST(key AS DOUBLE) + 1.0) → (unchanged)
The rewrite is dropped because CAST(key AS DOUBLE) + 1.0 is a non-lossless cast wrapped in a +, so isSafeExpression returns false — even though + is Strong.ANY and the distribution is a valid rewrite regardless of the outer call's safety.
Proposed fix:
In the Strong.Policy.ANY branch, replace the full-tree safety requirement with a shallow safety check on the outer call. Because the branch rewraps the input as IS [NOT] NULL(operand_i) and joins the results with OR/AND, add a per-operand guard to prevent RexCall.isAlwaysTrue()/isAlwaysFalse() from silently collapsing a rewrapped IS [NOT] NULL(op) whose operand is typed non-nullable but not fully safe (which would otherwise erase a throwing subexpression such as 1 / 0).
Strong.Policy.NOT_NULL and CUSTOM continue to require full-tree safety, since those branches drop the subtree entirely.