jayzhan211 commented on code in PR #27244:
URL: https://github.com/apache/datafusion/pull/27244#discussion_r4573443036


##########
datafusion/optimizer/src/decorrelate.rs:
##########
@@ -1225,19 +1226,47 @@ pub(crate) fn find_join_exprs(exprs: Vec<&Expr>) -> 
(Vec<Expr>, Vec<Expr>) {
     (joins, others)
 }
 
+/// Returns an error if `col` is not the first field of `schema` with its name.
+///
+/// Wrapping `schema` in a `SubqueryAlias` keeps the first field with a given
+/// name and renames later ones (`k:1`, ...), so qualifying `col` as
+/// `<alias>.<name>` refers to `col` only when it is that first field.
+/// See <https://github.com/apache/datafusion/issues/26234>.
+pub(crate) fn check_alias_keeps_column_name(
+    schema: &DFSchema,
+    col: &Column,
+) -> Result<()> {
+    let Some(index) = schema.maybe_index_of_column(col) else {
+        return Ok(());
+    };
+    let first = schema.fields().iter().position(|f| f.name() == &col.name);
+    if first != Some(index) {

Review Comment:
   @Awesome06 thanks for the update. One shape still slips past this check: a 
column literally named `k:1` that comes after two `k` columns. It is the first 
field with its name, so the check passes, but the alias renames it to `k:1:1` 
(the second `k` already took `k:1`), and `sq."k:1"` then reads `two.k`. This 
returns `1` where the expected result is `1, 2` (the `NOT EXISTS` form returns 
`2`; expected no rows):
   
   ```sql
   CREATE TABLE c26234("k:1" INT) AS VALUES (1), (2);
   SELECT t_outer.k FROM t26234 t_outer WHERE EXISTS (
       SELECT * FROM t26234 one, t26234 two, c26234 c WHERE c."k:1" = t_outer.k 
AND two.k = 1
   );
   ```
   
   Asking `unique_field_aliases`, the function `SubqueryAlias` uses to rename, 
avoids copying its rule here:
   
   ```suggestion
       if unique_field_aliases(schema.fields())[index].is_some() {
   ```
   
   plus the import at the top of the file:
   
   ```diff
   +use datafusion_expr::logical_plan::builder::unique_field_aliases;
    use datafusion_expr::logical_plan::{Join, JoinType};
   ```
   
   With both, the two queries fail with the planning error, and `subquery.slt`, 
`joins.slt`, `lateral.slt`, the optimizer unit tests and clippy pass. Could you 
add the two tests below and update the doc comment ("the first field of 
`schema` with its name" is no longer the rule)?
   
   <details>
   <summary>slt tests to add before <code>DROP TABLE t26234</code></summary>
   
   ```
   # A column literally named `k:1` after two `k` columns: the alias renames the
   # second `k` to `k:1`, so it renames this column too.
   statement ok
   CREATE TABLE c26234("k:1" INT) AS VALUES (1), (2);
   
   statement error Unsupported correlated subquery: correlated column c.k:1 has 
the same name as an earlier column of the subquery output
   SELECT t_outer.k FROM t26234 t_outer WHERE EXISTS (
       SELECT * FROM t26234 one, t26234 two, c26234 c WHERE c."k:1" = t_outer.k 
AND two.k = 1
   );
   
   statement error Unsupported correlated subquery: correlated column c.k:1 has 
the same name as an earlier column of the subquery output
   SELECT t_outer.k FROM t26234 t_outer WHERE NOT EXISTS (
       SELECT * FROM t26234 one, t26234 two, c26234 c WHERE c."k:1" = t_outer.k 
AND two.k = 1
   );
   
   statement ok
   DROP TABLE c26234;
   ```
   
   </details>



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