Skip to content

fix: preserve null semantics in expression simplification - #24258

Open
fzlzjerry wants to merge 3 commits into
apache:mainfrom
fzlzjerry:fix/24246-null-aware-simplification
Open

fix: preserve null semantics in expression simplification#24258
fzlzjerry wants to merge 3 commits into
apache:mainfrom
fzlzjerry:fix/24246-null-aware-simplification

Conversation

@fzlzjerry

Copy link
Copy Markdown

Which issue does this PR close?

Rationale for this change

Several expression simplifications erase SQL NULL semantics. For nullable inputs, identities such as power(a, 0), log(a, 1), and i XOR i can return constants instead of NULL. Set algebra over IN expressions and the array_has to IN rewrite can similarly change NULL or false results.

What changes are included in this PR?

  • Apply the affected logarithm, power, and XOR identities only when the operand being eliminated is non-nullable.
  • Keep IN/NOT IN set operations unchanged when list items may be NULL, and preserve the nullable branch when an otherwise safe operation becomes a constant.
  • Avoid rewriting array_has to IN when a nullable array element would change the result, while retaining the safe same-expression case.
  • Add unit and SQL logic regression coverage for the reported expressions and their query plans.

Are these changes tested?

Yes. The following checks pass:

  • ./dev/rust_lint.sh
  • cargo test --profile=ci -p datafusion-functions -p datafusion-functions-nested -p datafusion-optimizer --lib
  • cargo test --profile=ci --test sqllogictests
  • RUST_BACKTRACE=1 prlimit --nofile=65536:65536 -- cargo test --profile ci --exclude datafusion-examples --exclude datafusion-benchmarks --exclude datafusion-cli --workspace --lib --tests --bins --features avro,json,backtrace,extended_tests,recursive_protection,parquet_encryption

Are there any user-facing changes?

Yes. Expressions involving nullable inputs now retain their SQL three-valued-logic results instead of being simplified to incorrect constants. There are no public API changes.

Copilot AI lite review requested due to automatic review settings August 11, 2026 09:59

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions github-actions Bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels Aug 11, 2026
@AdamGS

AdamGS commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

I'll try and review it later today

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.19512% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.01%. Comparing base (a05388e) to head (ea308e2).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
...imizer/src/simplify_expressions/expr_simplifier.rs 92.22% 3 Missing and 4 partials ⚠️
datafusion/functions-nested/src/array_has.rs 94.52% 3 Missing and 1 partial ⚠️
datafusion/functions/src/math/log.rs 85.00% 1 Missing and 2 partials ⚠️
datafusion/functions/src/math/power.rs 90.90% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main   #24258    +/-   ##
========================================
  Coverage   81.01%   81.01%            
========================================
  Files        1106     1106            
  Lines      383974   384228   +254     
  Branches   383974   384228   +254     
========================================
+ Hits       311085   311297   +212     
- Misses      54554    54577    +23     
- Partials    18335    18354    +19     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@neilconway

Copy link
Copy Markdown
Contributor

FYI there is also #24247 and #24248

@AdamGS

AdamGS commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

@neilconway thanks for the heads up, I'll try and do a pass on all three tonight/tomorrow morning (UK time).

@fzlzjerry

Copy link
Copy Markdown
Author

Thanks for flagging these. I compared the scopes:

I'll keep the current comparison available for the review pass, then promptly rebase and drop whichever overlapping implementation the maintainers decide not to use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expression simplifications may produce incorrect results with NULLs

6 participants