steveyzzz commented on code in PR #60732:
URL: https://github.com/apache/spark/pull/60732#discussion_r4649876551
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/unresolved.scala:
##########
@@ -723,6 +724,28 @@ case class UnresolvedStarExceptOrReplace(
case except: NamedExpression => except -> Seq.empty
}
+ // Each replacement takes the place of the top-level 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: AttributeMap[NamedExpression] = replacements
match {
+ case Some(replacementList) =>
+ assert(exceptPairs.size == replacementList.size,
+ "The number of replacements must be the same as the number of
excepts")
+ if (exceptPairs.exists { case (_, nestedColumn) =>
nestedColumn.nonEmpty }) {
+ throw SparkException.internalError(
+ "An EXCEPT name with a replacement must resolve to a top-level
column")
+ }
+ excepts.zip(exceptPairs).foreach { case (exceptParts, (column, _)) =>
+ val copies =
expandedCols.map(_.toAttribute).filter(_.semanticEquals(column.toAttribute))
Review Comment:
Thanks, @dongjoon-hyun. I ran your query on master: `|> SET x = 9` and `|>
EVAL x = 9` both returned `[9,0]` through `[9,3]`. Fixed in 74e067bb48 for
both. A name with a replacement now also counts the tagged columns that it
matches, so both queries fail with `AMBIGUOUS_REFERENCE`. `pipe-operators.sql`
has the query for `SET` and for `EVAL`.
While testing it I found a related `EVAL` case. If a UNION branch outputs
`x` three times and `|> DROP x` removes the untagged copy, name resolution
finds neither remaining copy, so `|> EVAL x = 1` appended a third `x`. It now
fails with `AMBIGUOUS_REFERENCE` too.
I left `|> DROP x` and `SELECT * EXCEPT (x)` as they are. They keep the
tagged copy, as they did before this PR, and changing that would change results
of existing queries. I fixed the description to say so.
@dtenedor done: I merged master in 2934036a6f, and `SET` and `EVAL` now
share one check.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/unresolved.scala:
##########
@@ -723,6 +724,28 @@ case class UnresolvedStarExceptOrReplace(
case except: NamedExpression => except -> Seq.empty
}
+ // Each replacement takes the place of the top-level 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: AttributeMap[NamedExpression] = replacements
match {
+ case Some(replacementList) =>
+ assert(exceptPairs.size == replacementList.size,
+ "The number of replacements must be the same as the number of
excepts")
+ if (exceptPairs.exists { case (_, nestedColumn) =>
nestedColumn.nonEmpty }) {
Review Comment:
Done in 74e067bb48. With replacements, overlapping EXCEPT names now fail
with `EXCEPT_OVERLAPPING_COLUMNS` before the nested-name check and the copy
check. A new suite test runs both of your nodes three ways: without
replacements, with them, and with `appendMissingReplacements`. All three fail
with `EXCEPT_OVERLAPPING_COLUMNS`.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/unresolved.scala:
##########
@@ -791,10 +806,9 @@ case class UnresolvedStarExceptOrReplace(
errorClass = "EXCEPT_OVERLAPPING_COLUMNS",
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)
+ // found a match with a replacement - replace the column
+ case (col, Some(_)) if replacementsByColumn.contains(col.toAttribute)
=>
Review Comment:
Done in 74e067bb48, with one change from the suggestion. The case now
matches whenever there are replacements, and a column without an entry fails
with an internal error, not with the map's `NoSuchElementException`.
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/UnresolvedStarExceptOrReplaceSuite.scala:
##########
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.catalyst.analysis
+
+import org.apache.spark.{SparkException, SparkFunSuite}
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.catalyst.dsl.expressions._
+import org.apache.spark.sql.catalyst.expressions.{Alias, AttributeReference,
Literal}
+import org.apache.spark.sql.catalyst.plans.logical.LocalRelation
+import org.apache.spark.sql.types.{IntegerType, StructField, StructType}
+
+/**
+ * Tests [[UnresolvedStarExceptOrReplace]] replacements, including a node that
holds several EXCEPT
+ * names. Pipe `|> SET` builds one node per assignment, so SQL tests only
reach the single-name
+ * case.
+ */
+class UnresolvedStarExceptOrReplaceSuite extends SparkFunSuite {
Review Comment:
#60629 merged first, so this PR now adds its tests to the suite that #60629
added (2934036a6f). `SET` and `EVAL` share one copy check, `columnCopies`, so
the fix for [1] covers both.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/unresolved.scala:
##########
@@ -723,6 +724,28 @@ case class UnresolvedStarExceptOrReplace(
case except: NamedExpression => except -> Seq.empty
}
+ // Each replacement takes the place of the top-level 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: AttributeMap[NamedExpression] = replacements
match {
+ case Some(replacementList) =>
+ assert(exceptPairs.size == replacementList.size,
+ "The number of replacements must be the same as the number of
excepts")
+ if (exceptPairs.exists { case (_, nestedColumn) =>
nestedColumn.nonEmpty }) {
+ throw SparkException.internalError(
+ "An EXCEPT name with a replacement must resolve to a top-level
column")
+ }
+ excepts.zip(exceptPairs).foreach { case (exceptParts, (column, _)) =>
Review Comment:
Done in 74e067bb48. `expandStar` builds the `AttributeSeq` once and uses it
to resolve the EXCEPT names and to find copies.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/unresolved.scala:
##########
@@ -723,6 +724,28 @@ case class UnresolvedStarExceptOrReplace(
case except: NamedExpression => except -> Seq.empty
}
+ // Each replacement takes the place of the top-level 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: AttributeMap[NamedExpression] = replacements
match {
+ case Some(replacementList) =>
+ assert(exceptPairs.size == replacementList.size,
+ "The number of replacements must be the same as the number of
excepts")
+ if (exceptPairs.exists { case (_, nestedColumn) =>
nestedColumn.nonEmpty }) {
+ throw SparkException.internalError(
+ "An EXCEPT name with a replacement must resolve to a top-level
column")
+ }
+ excepts.zip(exceptPairs).foreach { case (exceptParts, (column, _)) =>
+ val copies =
expandedCols.map(_.toAttribute).filter(_.semanticEquals(column.toAttribute))
+ if (copies.size > 1) {
+ throw QueryCompilationErrors.ambiguousReferenceError(
+ UnresolvedAttribute(exceptParts).name, copies)
+ }
+ }
+ AttributeMap(exceptPairs.map(_._1.toAttribute).zip(replacementList))
Review Comment:
Fixed, thanks. The description now says that SQL builds one EXCEPT name per
node with replacements.
--
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]