Skip to content

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

Merged
alamb merged 3 commits intoapache:mainfrom
simonvandel:optimize-lit-and-not-lit
Jan 13, 2026
Merged

Simplify expr = L1 AND expr != L2 to expr = L1 when L1 != L2#19731
alamb merged 3 commits intoapache:mainfrom
simonvandel:optimize-lit-and-not-lit

Conversation

@simonvandel
Copy link
Copy Markdown
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
Comment thread datafusion/optimizer/src/simplify_expressions/utils.rs
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
de-bgunter pushed a commit to de-bgunter/datafusion that referenced this pull request Mar 24, 2026
…pache#19731)

## Which issue does this PR close?

<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax. For example
`Closes apache#123` indicates that this PR will close issue apache#123.
-->

- Closes #.

## Rationale for this change

<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->

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?

<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->

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

## Are these changes tested?

<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code

If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->

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.

<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->

<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->
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