steveyzzz opened a new pull request, #60732:
URL: https://github.com/apache/spark/pull/60732

   ### What changes were proposed in this pull request?
   
   A pipe `|> SET` assignment to a name that matches two copies of the same 
column now fails with `AMBIGUOUS_REFERENCE`. Before, it failed with an internal 
error:
   
   ```sql
   SELECT x, x FROM t |> SET x = 1;
   -- Before: java.lang.IndexOutOfBoundsException: 1
   -- After:  [AMBIGUOUS_REFERENCE] Reference `x` is ambiguous, could be: 
[`spark_catalog`.`default`.`t`.`x`, `spark_catalog`.`default`.`t`.`x`]. 
SQLSTATE: 42704
   ```
   
   Both copies are the same attribute, so resolving the name finds one column 
and doesn't raise `AMBIGUOUS_REFERENCE`, as it does for two different columns 
(`SELECT 1 AS a, 2 AS a |> SET a = 3`). The star expansion then replaced both 
copies, and the second replacement read past the end of the one-element 
replacement list.
   
   `UnresolvedStarExceptOrReplace` now maps each replacement to the column that 
its EXCEPT name resolves to, instead of handing replacements out in column 
order, and rejects a name that matches more than one copy of its column. 
`SELECT * EXCEPT` and `|> DROP`, which have no replacements, still exclude 
every copy.
   
   #60629, which adds pipe `|> EVAL`, rejects the same case for `EVAL`. Both 
PRs change `UnresolvedStarExceptOrReplace.expandStar`, so whichever merges 
second keeps one check when it rebases.
   
   ### Why are the changes needed?
   
   An internal error is a bug. An assignment can't say which copy it replaces, 
so it is ambiguous, as it is for two different columns with the name. Mapping 
replacements by EXCEPT name also fixes a latent mismatch: a node whose EXCEPT 
names matched columns out of column order put each replacement in the other's 
column. SQL builds one EXCEPT name per node, so no query reached that.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes: the query above fails with `AMBIGUOUS_REFERENCE` instead of an internal 
error.
   
   ### How was this patch tested?
   
   - New case in `pipe-operators.sql`: `SELECT x, x FROM t |> SET x = 1`.
   - New `UnresolvedStarExceptOrReplaceSuite`: replacements for EXCEPT names 
that match columns out of column order; a replacement for a name that two 
copies of one column share; and an EXCEPT name without a replacement, which 
still excludes every copy.
   - Ran `SQLQueryTestSuite` (`pipe-operators.sql`, `selectExcept.sql`), 
`UnresolvedStarExceptOrReplaceSuite`, `StarTreePatternSuite`, `AnalysisSuite`, 
and scalastyle.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code, Opus 5.5
   
   This pull request and its description were written by Isaac.
   


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