Mohammed-Bakir opened a new pull request, #27468:
URL: https://github.com/apache/datafusion/pull/27468

   ## Which issue does this PR close?
   
   - Closes #27426.
   
   ## Rationale for this change
   
   1. When unparsing an explicit `Projection` with zero expressions (such as a 
filtered `count(1)`), `reconstruct_select_statement` emitted an empty `SELECT` 
list. While PostgreSQL permits `SELECT FROM ...`, dialects like DuckDB, 
BigQuery, and ClickHouse reject empty `SELECT` lists as syntax errors.
   2. When a `Projection` sat above a `Limit`/`Sort` chain ending in `Filter` 
node(s) over a `TableScan` (without a pushed `fetch`), the unparser wrapped the 
`Limit` in a derived subquery while leaving the outer table qualifier in the 
`SELECT` list out of scope.
   
   ## What changes are included in this PR?
   
   - Use `self.empty_projection_fallback()` in `reconstruct_select_statement` 
when `p.expr.is_empty()` so PostgreSQL retains its empty `SELECT` list while 
other dialects emit a fallback literal (`1`).
   - Extend `LogicalPlan::Projection` absorption in 
`datafusion/sql/src/unparser/plan.rs` to fold a complete `Limit`/`Sort` chain 
ending in `Filter+ -> TableScan` (when `fetch` is `None`) into a single 
`SELECT` statement without crossing `Projection`, `Join`, `Distinct`, `Window`, 
or `SubqueryAlias` boundaries.
   - Add regression unit tests in `datafusion/sql/tests/cases/plan_to_sql.rs` 
covering PostgreSQL, DuckDB, and BigQuery dialects.
   
   ## Are these changes tested?
   
   Yes, added unit tests `test_empty_projection_filtered_count_dialects` and 
`test_projected_filtered_limit_keeps_table_in_scope` in 
`datafusion/sql/tests/cases/plan_to_sql.rs`, and verified all existing 
`datafusion-sql` tests pass.


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

Reply via email to