paoValle opened a new pull request, #26477:
URL: https://github.com/apache/datafusion/pull/26477

   
   ## Which issue does this PR close?
   
   - Closes #26462.
   
   ## Rationale for this change
   
   `SELECT lower(e.email) AS email FROM employees e ORDER BY e.email` failed to
   plan:
   
       Schema error: Schema contains qualified field name e.email and 
unqualified
       field name email which would be ambiguous
   
   The sort key is not in the select list, so the planner carries `e.email` into
   the projection to sort on it. That projection then holds the qualified field
   `e.email` and the unqualified field `email` in one schema, which
   `DFSchema::check_names` rejects, even though the carried column is internal 
and
   is projected away above the sort. PostgreSQL plans the same query and sorts 
on
   the input column.
   
   ## What changes are included in this PR?
   
   `LogicalPlanBuilder::sort` (`datafusion/expr/src/logical_plan/builder.rs`) 
now
   carries a missing sort column under an alias when the projection it is added 
to
   already outputs an unqualified field with the same name. The alias is the
   column's flat name (`e.email`), so the carried column cannot collide with the
   projection's other fields, and the sorts are rebound to it. The projection
   above the sort still strips the extra column, so the output schema of the 
query
   is unchanged.
   
   ## What is the testing strategy for this PR?
   
   `datafusion/sqllogictest/test_files/order.slt` gets the query from the issue 
and
   its `ORDER BY email` counterpart, which pins the two resolutions apart:
   `ORDER BY e.email` sorts on the input column (`b@x`, `a@y`), and
   `ORDER BY email` sorts on the output column (`a@y`, `b@x`), as in PostgreSQL.
   A unit test in `datafusion/expr/src/logical_plan/builder.rs` covers the same
   path through `LogicalPlanBuilder` and checks the resulting plan.
   
   Validation, on 61c34724f:
   
   - `cargo test -p datafusion-expr` passed: 285 unit tests, 63 documentation 
tests.
   - `cargo test -p datafusion-sql` passed: 95, 604 and 12 tests.
   - `cargo test -p datafusion-optimizer` passed: 941, 29 and 5 tests.
   - `cargo test -p datafusion --lib` passed: 468 tests.
   - `cargo test --profile ci -p datafusion-sqllogictest --test sqllogictests`
     passed: 534 of 534 files.
   - `cargo clippy -p datafusion-expr --all-targets --all-features -- -D 
warnings`
     and `cargo fmt --all -- --check` passed.
   
   The `order.slt` case was also run against the unpatched code, where it fails
   with the schema error quoted above.
   
   ## Are there any user-facing changes?
   
   None beyond the query now planning. The result and the schema of the query 
are
   the ones PostgreSQL returns; plans that already planned 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