1fanwang opened a new pull request, #25521:
URL: https://github.com/apache/datafusion/pull/25521

   ## Which issue does this PR close?
   
   Related to https://github.com/apache/datafusion/issues/21066. This addresses 
generated SQL that PostgreSQL cannot execute; DataFusion's separate restriction 
on reserved internal column names remains unchanged.
   
   ## Rationale for this change
   
   An outer query can reference a computed aggregate by its logical output 
name, while the generated inner query leaves that expression unnamed. 
PostgreSQL then assigns its own column name and rejects the outer reference.
   
   ## What changes are included in this PR?
   
   Named derived projections now give unnamed expressions the output names 
expected by the outer query. Existing explicit aliases, column alias lists, and 
plain column references retain their rendering.
   
   ## What is the testing strategy for this PR?
   
   The regression builds a derived projection with one explicitly named 
aggregate and one implicit aggregate name. I ran its emitted SQL against an 
isolated PostgreSQL 14.19 database containing the integers 1 and 2.
   
   ### Testing Done
   
   The baseline fails with a missing-column error. The fixed SQL returns 3.
   
   From the PR checkout, prepare the baseline with the same regression:
   
   ```bash
   git worktree add -b 1fannnw/repro-derived-expression-names \
     ../datafusion-derived-names-before 925d7f8ffdcb5584e26f2410facbcfc5b4234d20
   git diff 925d7f8ffdcb5584e26f2410facbcfc5b4234d20 HEAD \
     -- datafusion/sql/tests/cases/plan_to_sql.rs |
     git -C ../datafusion-derived-names-before apply -
   
   CARGO_BUILD_JOBS=2 cargo test --locked --profile ci \
     --manifest-path ../datafusion-derived-names-before/Cargo.toml \
     -p datafusion -p datafusion-sql --features 
datafusion-sql/recursive_protection \
     --test sql_integration unparse_preserves_derived_aggregate_output_name \
     -- --nocapture > before-output 2>&1
   
   CARGO_BUILD_JOBS=2 cargo test --locked --profile ci \
     -p datafusion -p datafusion-sql --features 
datafusion-sql/recursive_protection \
     --test sql_integration unparse_preserves_derived_aggregate_output_name \
     -- --nocapture > after-output 2>&1
   
   sed -n 's/^UNPARSED_SQL=//p' before-output > before-query
   sed -n 's/^UNPARSED_SQL=//p' after-output > after-query
   ```
   
   With PostgreSQL installed, run both statements in a disposable local 
database:
   
   ```bash
   initdb -D pg-proof --username=probe --auth-local=trust --auth-host=reject \
     --no-locale --encoding=UTF8
   {
     printf 'CREATE TABLE j1 (j1_id INTEGER);\nINSERT INTO j1 VALUES (1),(2);\n'
     cat before-query
   } | postgres --single -D pg-proof postgres
   postgres --single -D pg-proof postgres < after-query
   ```
   
   <details>
   <summary>Raw result excerpts</summary>
   
   Before:
   
   ```text
   ERROR:  column "sum(j1.j1_id)" does not exist at character 8
   ```
   
   After:
   
   ```text
         1: sum(j1.j1_id) = "3" (typeid = 20, len = 8, typmod = -1, byval = t)
   ```
   
   </details>
   
   - [x] Local code review completed
   
   ## Are there any user-facing changes?
   
   PostgreSQL can resolve computed columns exposed by these generated derived 
projections. The SQL input validation rules and public APIs are unchanged.
   


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