Skip to content
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

Simplify IsNotNull and IsNull expression #6345

Merged
merged 1 commit into from
May 15, 2023
Merged

Simplify IsNotNull and IsNull expression #6345

merged 1 commit into from
May 15, 2023

Conversation

jonahgao
Copy link
Member

@jonahgao jonahgao commented May 12, 2023

Which issue does this PR close?

None

Rationale for this change

expr_non_null IS NOT NULL is always true
expr_non_null IS NULL is always false

They are a type of predicate that is either unnecessary or impossible.
By simplifying them, we can gain more opportunities for optimization.

For example:

  1. SELECT * FROM t WHERE col_non_null IS NOT NULL --(Simplify)--> SELECT * FROM t WHERE TRUE --(EliminateFilter)--> SELECT * FROM t

  2. SELECT * FROM t WHERE col_non_null IS NULL --(Simplify)--> SELECT * FROM t WHERE FALSE --(EliminateFilter)--> <EmptyRelation>

Are these changes tested?

Yes

Are there any user-facing changes?

No

@github-actions github-actions bot added the optimizer Optimizer rules label May 12, 2023
@@ -1161,6 +1161,12 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for Simplifier<'a, S> {
lit(!negated)
}

// a IS NOT NULL --> true, if a is not nullable
Expr::IsNotNull(expr) if !info.nullable(&expr)? => lit(true),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @byteink. would you mind adding more description? Why the simplification needed and what exactly problem it solves? is it a bug or something?

Copy link
Member

@jackwener jackwener May 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create table test(a int not null);

select a is null

we can convert it into

select false

because a is not null

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@comphead It is not a bug. I've added some descriptions.

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great -- thank you @byteink and @comphead and @jackwener

@alamb alamb merged commit 7563cdb into apache:main May 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
optimizer Optimizer rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants