-
Notifications
You must be signed in to change notification settings - Fork 3.6k
branch-4.0: [draft](nereids) optimize case when expression #58186
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
Draft
yujun777
wants to merge
8
commits into
apache:branch-4.0
Choose a base branch
from
yujun777:branch-4.0-opt-case-when
base: branch-4.0
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.
Conversation
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
…ache#56424) for a case when condition, the condition evaluate result is null or false have the same effect: not hit the condition. in most case, nullable cann't fold in logistic expression, for example `null and a = 1` and `null or a = 1` cann't fold. but false can fold in logistic expression, `false and a=1` can fold to false, `false or a = 1` can fold to `a = 1`. so if we replace the null to false in case when condition, then the expression may be fold more simple. in fact, for case/if condition, null can replace with FALSE when it is the expression root or all its ancestors are AND/OR/CASE IF CONDITION, and this rewrite will not change the hit or not of the branch. for example: for sql: 'case when null and a > 1 then ...': 1. after use this rule rewrite to 'case when false and a > 1 then ... ', 2. then constant fold rule will rewrite it to 'case when false then ...', 3. then case when can remove this branch since its condition is false.
…to true/false (apache#56469) for nested case when, replace the inner case duplicate condition to true/false when this condition also exists in outer case condition: 1. if it exists in outer case's current branch condition, replace it with TRUE: case when A then (case when A and B then 1 else 2 end) ... end then inner case condition A will replace with TRUE: case when A then (case when TRUE and B then 1 else 2 end) ... end 2. if it exists in outer case's previous branch condition, replace it with FALSE: case when A then C when B then (case when A and D then 1 else 2 end) ... end then inner case condition A will replace with FALSE: case when A then C when B then (case when FALSE and D then 1 else 2 end) ... end this PR also opt fold case when and fold if statement. for case when / if expression, if all their branches values equals, then rewrite them to the same value.
for a boolean data type case when expression, if all its when clauses' result are true / false literal, then can rewrite this case when to AND / OR expression. for example: case when a = 1 then true when b = 1 then false else c = 1 end rewrite to: (a = 1) <=> true or (not((b = 1) <=> true) and c = 1) if (a = 1, true, b = 1) rewrite to: (a=1) <=> true or b = 1
1. Add fold constant for nullif function; 2. Opt fold nvl: `nvl(a, a)` => `a`, `nvl(a, null)` => `a`; 3. Make AggregateFunction and TableGeneratingFunction to non-foldable.
…when (apache#57025) For a condition expression, replace null to false, null safe equal to equal. The condition include filter condition, join condition, if condition, case when condition. And for the condition expression, only replace those sub expression which its ancestors to the condition root are all AND / OR / CASE WHEN / IF. So, for a expression in a filter, the first null can be replaced, but the second null cann't be replaced because its parent is NOT, not in AND / OR / CASE WHEN / IF. For a expression in condition, if one of them is not-nullable, then it can rewrite to . Note that if a expression is not a condition, can rewrite to require that both x and y are not-nullable.
…che#56899) simplify expression equals with true / false literal.
…6941) push upper function into case when branch. for expression f with n arguments a1, a2, ..., an, if one of its argument is case when/if/nvl/nullif, and the others are literals, then we can push f into each branch of case when/if/nvl/nullif.
Contributor
|
Thank you for your contribution to Apache Doris. Please clearly describe your PR:
|
Contributor
Author
|
run buildall |
Contributor
Author
|
run feut |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What problem does this PR solve?
cherry-pick: #56424, #56469, #56756, #56932, #57025, #56899, #56941, #57537
Release note
None
Check List (For Author)
Test
Behavior changed:
Does this need documentation?
Check List (For Reviewer who merge this PR)