dtenedor commented on code in PR #60732:
URL: https://github.com/apache/spark/pull/60732#discussion_r4635506788


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/unresolved.scala:
##########
@@ -792,9 +800,8 @@ case class UnresolvedStarExceptOrReplace(
             messageParameters = Map(
               "columns" -> this.excepts.map(_.mkString(".")).mkString(", ")))
         // found a match and the 'replacements' list is populated - replace 
the column
-        case (_, Some(_)) if replacements.nonEmpty =>
-          replacementIndex += 1
-          replacements.get(replacementIndex - 1)
+        case (col, Some(_)) if replacementsByColumn.nonEmpty =>
+          replacementsByColumn.get(col.toAttribute)

Review Comment:
   Two small things on this line:
   
   1. With a nested EXCEPT name and replacements, `filterColumns` recurses into 
the struct, and the extracted fields there have fresh exprIds that aren't keys 
in `replacementsByColumn`, so this throws `NoSuchElementException`. The old 
code replaced the nested field instead. SQL can't reach this today 
(`visitOperatorPipeSetAssignmentSeq` rejects multi-part keys), but the class 
doc still allows it. Could we assert that replacements only come with 
single-part excepts, or note that restriction in the scaladoc?
   
   2. `replacementsByColumn.get(col.toAttribute)` reads like `Map.get` 
returning an Option, but it's `Option.get` followed by `AttributeMap.apply`. 
Building a plain `AttributeMap[NamedExpression]` (empty when `replacements` is 
`None`) and matching `replacementsByColumn.get(col.toAttribute)` as `Some(r)` 
would read more clearly and would also make the nested case explicit.



##########
sql/core/src/test/resources/sql-tests/inputs/pipe-operators.sql:
##########
@@ -368,6 +368,10 @@ values (0), (1) lhs(a)
 table t
 |> set z = 1;
 
+-- SET with a column name that matches two copies of the same column.
+select x, x from t
+|> set x = 1;

Review Comment:
   Optional: could we also add `select x, * from t |> set x = 1;`? It hits the 
same path and is probably the more common way users end up with two copies of a 
column.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/unresolved.scala:
##########
@@ -723,6 +723,22 @@ case class UnresolvedStarExceptOrReplace(
       case except: NamedExpression => except -> Seq.empty
     }
 
+    // Each replacement takes the place of the column that its except names. 
Two copies of the
+    // same column, as in `SELECT x, x`, resolve to one attribute, so a 
replacement for that name
+    // is ambiguous, as it would be for two different columns with the name.
+    val replacementsByColumn = replacements.map { replacementList =>
+      assert(exceptPairs.size == replacementList.size,
+        "The number of replacements must be the same as the number of excepts")
+      excepts.zip(exceptPairs).foreach { case (exceptParts, (column, _)) =>
+        val copies = 
expandedCols.map(_.toAttribute).filter(_.semanticEquals(column.toAttribute))
+        if (copies.size > 1) {
+          throw QueryCompilationErrors.ambiguousReferenceError(

Review Comment:
   Nit, non-blocking: the message comes out as ``could be: [`t`.`x`, 
`t`.`x`]``, i.e. two identical names, which may confuse users. It's consistent 
with AMBIGUOUS_REFERENCE, so fine to keep. Just flagging it in case a clearer 
message for duplicate copies of one column is worth a follow-up.



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