adriangb commented on code in PR #25412:
URL: https://github.com/apache/datafusion/pull/25412#discussion_r4038668131


##########
datafusion/optimizer/src/extract_leaf_expressions.rs:
##########
@@ -702,27 +720,36 @@ fn build_extraction_projection_impl(
         // than target_schema (the projection's output) because columns 
produced
         // by alias expressions (e.g., CSE's __common_expr_N) exist in the 
output but
         // not the input, and cannot be added as pass-through Column 
references.
+        //
+        // Both sides of that check are read in the input's own spelling. A 
column can
+        // arrive here unqualified while the input names it `t.c`, and the 
other way round:
+        // a projection of bare names over a qualified input is what 
eliminating one side
+        // of a union leaves behind. Resolving both sides lets a pass-through 
the
+        // projection already carries match the one about to be added, and 
pushes the one
+        // that is genuinely new under the name the input gives it. Left 
unresolved, the
+        // merged projection would hold `t.c` and a bare `c` together, which
+        // `Projection::try_new` rejects as ambiguous.
+        let input_schema = existing.input.schema();
         let existing_cols: IndexSet<Column> = existing
             .expr
             .iter()
             .filter_map(|e| {
                 if let Expr::Column(c) = e {
-                    Some(c.clone())
+                    resolve_against(input_schema, c)

Review Comment:
   Fixed in 5e7ec7a266: the existing side now collects `passthrough_column(e)`, 
so `t.c AS c` counts the same as a bare `t.c`. Added 
`test_merge_into_projection_with_same_name_alias`, which fails without the 
change with `AmbiguousReference { test.a }`. A same-name alias case in SQL does 
not reach this path (the planner drops `env AS env` first), so the regression 
test is a unit test.



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