Skip to content

Conversation

@simonvandel
Copy link
Contributor

Which issue does this PR close?

  • Closes #.

Rationale for this change

Add a simplifier rule to remove a comparison.
It's probably unlikely that a human would write such an expression, but a generated query, or other optimizations, may lead to such an expression.

What changes are included in this PR?

Simplify expr = L1 AND expr != L2 to expr = L1 when L1 != L2

Are these changes tested?

Added unit tests and SLT.
The first commit shows the plan before the optimization.

Are there any user-facing changes?

Fewer runtime-ops if the plan contains the pattern.

@github-actions github-actions bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) labels Jan 10, 2026
@simonvandel simonvandel force-pushed the optimize-lit-and-not-lit branch from 7fd7cde to 8375534 Compare January 10, 2026 17:12
@simonvandel simonvandel force-pushed the optimize-lit-and-not-lit branch from 8375534 to fdb54b8 Compare January 10, 2026 17:17
Copy link
Member

@xudong963 xudong963 left a comment

Choose a reason for hiding this comment

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

A clever simplify! It could reduce the CPU cost for FilterExec and row filter in parquet

WHERE ((dept_name != 'Engineering' AND e.name = 'Alice') OR (name != 'Alice' AND e.name = 'Carol'));
----
logical_plan
01)Filter: d.dept_name != Utf8View("Engineering") AND e.name = Utf8View("Alice") OR e.name != Utf8View("Alice") AND e.name = Utf8View("Carol")
Copy link
Contributor

Choose a reason for hiding this comment

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

is this right? It looks like it has rewritten

((dept_name != 'Engineering' AND e.name = 'Alice') OR (name != 'Alice' AND e.name = 'Carol'));

to

((dept_name != 'Engineering' AND e.name = 'Alice') OR (e.name = 'Carol'));

But name and e.name are different 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

Update -- looking at this, it seems like actually the predicate actually did resolve both name references to the same thing (name@1) in the physical plan

01)FilterExec: dept_name@2 != Engineering AND name@1 = Alice OR name@1 != Alice AND name@1 = Carol

Maybe we can update this test (in a follow on PR) so it uses the fully qualified column references to make it clearer the intent

((d.dept_name != 'Engineering' AND e.name = 'Alice') OR (e.name != 'Alice' AND e.name = 'Carol'));

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.

WHERE ((dept_name != 'Engineering' AND e.name = 'Alice') OR (name != 'Alice' AND e.name = 'Carol'));
----
logical_plan
01)Filter: d.dept_name != Utf8View("Engineering") AND e.name = Utf8View("Alice") OR e.name != Utf8View("Alice") AND e.name = Utf8View("Carol")
Copy link
Contributor

Choose a reason for hiding this comment

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

Update -- looking at this, it seems like actually the predicate actually did resolve both name references to the same thing (name@1) in the physical plan

01)FilterExec: dept_name@2 != Engineering AND name@1 = Alice OR name@1 != Alice AND name@1 = Carol

Maybe we can update this test (in a follow on PR) so it uses the fully qualified column references to make it clearer the intent

((d.dept_name != 'Engineering' AND e.name = 'Alice') OR (e.name != 'Alice' AND e.name = 'Carol'));

@alamb alamb added this pull request to the merge queue Jan 13, 2026
Merged via the queue into apache:main with commit e076e59 Jan 13, 2026
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants