Shoryamishra61 opened a new pull request, #25475:
URL: https://github.com/apache/datafusion/pull/25475
## Which issue does this PR close?
- Closes #25462
## Rationale for this change
When converting DataFusion logical expressions back to SQL AST in
`datafusion-sql`, both `Expr::WindowFunction` and `Expr::AggregateFunction`
previously dropped the `null_treatment` field stored in `WindowFunctionParams`
and `AggregateFunctionParams` and hardcoded `null_treatment: None` on the
generated `sqlparser::ast::Function`.
As a result, expressions using `IGNORE NULLS` or `RESPECT NULLS` (such as
`FIRST_VALUE(v IGNORE NULLS) OVER (...)` or aggregate `FIRST_VALUE(v IGNORE
NULLS)`) lost their explicit NULL treatment clause during unparsing, altering
query semantics upon re-execution.
## What changes are included in this PR?
- Extracted `null_treatment` from `WindowFunctionParams` in
`Expr::WindowFunction` and forwarded it to `ast::Function.null_treatment`.
- Extracted `null_treatment` from `AggregateFunctionParams` in
`Expr::AggregateFunction` and forwarded it to `ast::Function.null_treatment`.
- Added helper function `null_treatment_to_sql` mapping
`datafusion_expr::expr::NullTreatment` to `sqlparser::ast::NullTreatment`.
- Added unit tests covering `IGNORE NULLS`, `RESPECT NULLS`, and `None`
(control) for both window and aggregate functions.
## What is the testing strategy for this PR?
- Added `test_unparse_null_treatment_window_and_aggregate` in
`datafusion/sql/src/unparser/expr.rs` covering:
- Window functions with `IGNORE NULLS`
- Window functions with `RESPECT NULLS`
- Window functions without explicit NULL treatment (control case ensuring
no clause is emitted)
- Aggregate functions with `IGNORE NULLS`
- Aggregate functions with `RESPECT NULLS`
- Aggregate functions without explicit NULL treatment (control case)
- Verified test fails on pre-fix code and passes with the fix.
- Ran all unparser unit tests: `cargo test -p datafusion-sql --lib unparser`
(38 passed, 0 failed).
- Verified lints and formatting: `cargo fmt --all -- --check` and `cargo
clippy -p datafusion-sql --all-targets --all-features -- -D warnings`.
## Are there any user-facing changes?
Bug fix: SQL unparsing now preserves explicit `IGNORE NULLS` and `RESPECT
NULLS` clauses on window and aggregate functions rather than omitting them.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]