Skip to content

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
}

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