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


##########
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:
   Done in 73e3cb1358. With replacements, `expandStar` now fails with an 
internal error when an EXCEPT name resolves to a nested field, and the 
`replacements` scaladoc says so. I checked for nested fields rather than 
multi-part names, so a qualified top-level name like `t.x` still gets its 
replacement. `UnresolvedStarExceptOrReplaceSuite` covers both. 
`replacementsByColumn` is now a plain `AttributeMap`, empty without 
replacements, and the last case looks the column up with `contains` and `apply`.
   



##########
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:
   Keeping it as is for now. A clearer message for duplicate copies of one 
column would apply to `|> EVAL` in #60629 too, so we may do it later as one 
cleanup for both.
   



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