-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[CALCITE-7722] RexSimplify IS [NOT] NULL on a safe operator with Strong policy ANY and unsafe operands can be further simplified #5184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rubenada
wants to merge
4
commits into
apache:main
Choose a base branch
from
rubenada:CALCITE-7722
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4a3058c
[CALCITE-7722] Simplify IS NULL / IS NOT NULL expressions by removing…
rubenada 2953c94
Improve tests, simplify comments
rubenada 52811f0
Consider isSafe on RexCall isAlwaysTrue / isAlwaysFalse simplificatio…
rubenada 73e650f
Add clarification comment on test
rubenada File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| package org.apache.calcite.rex; | ||
|
|
||
| import org.apache.calcite.avatica.util.ByteString; | ||
| import org.apache.calcite.avatica.util.TimeUnit; | ||
| import org.apache.calcite.plan.RelOptPredicateList; | ||
| import org.apache.calcite.plan.RelOptUtil; | ||
| import org.apache.calcite.plan.Strong; | ||
|
|
@@ -3024,6 +3025,128 @@ trueLiteral, literal(1), | |
| checkSimplifyUnchanged(div(cast(vVarchar(), tInt(false)), nullInt)); | ||
| } | ||
|
|
||
| /** | ||
| * Test cases for <a href="https://issues.apache.org/jira/browse/CALCITE-7722">[CALCITE-7722] | ||
| * RexSimplify IS [NOT] NULL on a safe operator with Strong policy ANY and unsafe operands | ||
| * can be further simplified</a>. | ||
| */ | ||
| @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 | ||
| // non-lossless CAST inside the rewrapped IS NOT NULL | ||
| checkSimplify( | ||
| isNotNull(plus(cast(vVarchar(), tInt(true)), literal(1))), | ||
| "IS NOT NULL(CAST(?0.varchar0):INTEGER)"); | ||
|
|
||
| // Symmetric IS NULL peel: | ||
| // "(CAST(?0.varchar0):INTEGER + 1) IS NULL" ==> "IS NULL(CAST(?0.varchar0):INTEGER)" | ||
| checkSimplify( | ||
| isNull(plus(cast(vVarchar(), tInt(true)), literal(1))), | ||
| "IS NULL(CAST(?0.varchar0):INTEGER)"); | ||
|
|
||
| // Confirm this is consistent with same expression without CAST | ||
| checkSimplify(isNotNull(plus(vInt(), literal(1))), "IS NOT NULL(?0.int0)"); | ||
| checkSimplify(isNull(plus(vInt(), literal(1))), "IS NULL(?0.int0)"); | ||
|
|
||
| // MULTIPLY is also strong + shallow-safe | ||
| checkSimplify( | ||
| isNotNull(mul(cast(vVarchar(), tInt(true)), literal(2))), | ||
| "IS NOT NULL(CAST(?0.varchar0):INTEGER)"); | ||
| checkSimplify( | ||
| isNull(mul(cast(vVarchar(), tInt(true)), literal(2))), | ||
| "IS NULL(CAST(?0.varchar0):INTEGER)"); | ||
| checkSimplify(isNotNull(mul(vInt(), literal(2))), "IS NOT NULL(?0.int0)"); | ||
| checkSimplify(isNull(mul(vInt(), literal(2))), "IS NULL(?0.int0)"); | ||
|
|
||
| // PLUS of two non-lossless CAST | ||
| checkSimplify( | ||
| isNotNull( | ||
| plus( | ||
| cast(vVarchar(0), tInt(true)), | ||
| cast(vVarchar(1), tInt(true)))), | ||
| "AND(IS NOT NULL(CAST(?0.varchar0):INTEGER), IS NOT NULL(CAST(?0.varchar1):INTEGER))"); | ||
| checkSimplify( | ||
| isNull( | ||
| plus( | ||
| cast(vVarchar(0), tInt(true)), | ||
| cast(vVarchar(1), tInt(true)))), | ||
| "OR(IS NULL(CAST(?0.varchar0):INTEGER), IS NULL(CAST(?0.varchar1):INTEGER))"); | ||
|
|
||
| // Nested PLUS: | ||
| // "((CAST(?0.varchar0):INTEGER + 1) + 2) IS NOT NULL" | ||
| // ==> "IS NOT NULL(CAST(?0.varchar0):INTEGER)" | ||
| checkSimplify( | ||
| isNotNull( | ||
| plus(plus(cast(vVarchar(), tInt(true)), literal(1)), literal(2))), | ||
| "IS NOT NULL(CAST(?0.varchar0):INTEGER)"); | ||
|
|
||
| // Operators with checked arithmetic | ||
| // WARNING: these simplifications are a bit "controversial" since checked operators | ||
| // can throw at runtime (in case of overflow); however, at the moment they are considered | ||
| // "safe" by RexSimplify#SafeRexVisitor, so these simplifications are applied; if this | ||
| // gets reviewed in the future (see CALCITE-7725) these tests might need to get adjusted | ||
| checkSimplify( | ||
| isNotNull(checkedPlus(cast(vVarchar(), tInt(true)), literal(1))), | ||
| "IS NOT NULL(CAST(?0.varchar0):INTEGER)"); | ||
| checkSimplify( | ||
| isNull(checkedPlus(cast(vVarchar(), tInt(true)), literal(1))), | ||
| "IS NULL(CAST(?0.varchar0):INTEGER)"); | ||
| checkSimplify( | ||
| isNotNull(checkedMul(cast(vVarchar(), tInt(true)), literal(2))), | ||
| "IS NOT NULL(CAST(?0.varchar0):INTEGER)"); | ||
| checkSimplify( | ||
| isNull(checkedMul(cast(vVarchar(), tInt(true)), literal(2))), | ||
| "IS NULL(CAST(?0.varchar0):INTEGER)"); | ||
|
|
||
| // Arithmetic on INTERVAL | ||
| checkSimplify( | ||
| 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))), | ||
| "IS NULL(CAST(?0.varchar0):DATE)"); | ||
|
|
||
| // The outer PLUS is shallow-safe, but the div(1, 0) is not, so no further simplification occurs | ||
| checkSimplify( | ||
| isNotNull(plus(div(literal(1), literal(0)), vIntNotNull())), | ||
| "IS NOT NULL(/(1, 0))"); | ||
| checkSimplify( | ||
| isNull(plus(div(literal(1), literal(0)), vIntNotNull())), | ||
| "IS NULL(/(1, 0))"); | ||
|
|
||
| // The outer PLUS / MULT is shallow-safe, but the CAST is not (non-lossless), | ||
| // so no further simplification occurs | ||
| checkSimplify(isNull(plus(cast(vVarchar(), tInt(false)), literal(2))), | ||
| "IS NULL(CAST(?0.varchar0):INTEGER NOT NULL)"); | ||
| checkSimplify(isNull(mul(cast(vVarchar(), tInt(false)), literal(2))), | ||
| "IS NULL(CAST(?0.varchar0):INTEGER NOT NULL)"); | ||
| checkSimplify(isNotNull(plus(cast(vVarchar(), tInt(false)), literal(2))), | ||
| "IS NOT NULL(CAST(?0.varchar0):INTEGER NOT NULL)"); | ||
| checkSimplify(isNotNull(mul(cast(vVarchar(), tInt(false)), literal(2))), | ||
| "IS NOT NULL(CAST(?0.varchar0):INTEGER NOT NULL)"); | ||
|
|
||
| // The outer PLUS / MULT is shallow-safe, and the CAST is safe too (lossless CAST), | ||
| // so fully simplified | ||
| checkSimplify(isNull(plus(cast(vSmallInt(), tInt(false)), literal(2))), | ||
| "false"); | ||
| checkSimplify(isNull(mul(cast(vSmallInt(), tInt(false)), literal(2))), | ||
| "false"); | ||
| checkSimplify(isNotNull(plus(cast(vSmallInt(), tInt(false)), literal(2))), | ||
| "true"); | ||
| checkSimplify(isNotNull(mul(cast(vSmallInt(), tInt(false)), literal(2))), | ||
| "true"); | ||
|
|
||
| // IS NOT NULL(x/0) itself is not peeled, because DIVIDE with a literal-zero divisor is not safe | ||
| checkSimplifyUnchanged(isNotNull(div(vIntNotNull(), literal(0)))); | ||
| checkSimplifyUnchanged(isNull(div(vIntNotNull(), literal(0)))); | ||
| checkSimplifyUnchanged(isNull(div(cast(vIntNotNull(), tBigInt()), literal(0)))); | ||
|
|
||
| // IS NULL(CAST(10/0 AS BIGINT)) stays as IS NULL(10/0) after the lossless-CAST strip; | ||
| // the DIVIDE is not safe, so no further distribution occurs | ||
| checkSimplify(isNull(cast(div(vIntNotNull(), literal(0)), tBigInt())), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we have some tests using checked arithmetic or arithmetic on intervals?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point; added both cases |
||
| "IS NULL(/(?0.notNullInt0, 0))"); | ||
| } | ||
|
|
||
| @Test void testPushNotIntoCase() { | ||
| checkSimplify( | ||
| not( | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.