dongjoon-hyun commented on code in PR #60732:
URL: https://github.com/apache/spark/pull/60732#discussion_r4647088481
##########
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:
**[3] Nit: a missing replacement would drop the column without an error.**
This guard is false in two different situations: when there are no
replacements, and when there are replacements but this column has no entry. In
both, the match falls through to the implicit case iii in the comment above,
which drops the column. The second situation cannot happen today, because both
maps are keyed by the same root columns and the nested case fails earlier. But
if a later change breaks that invariant, `|> SET` would silently lose the
column instead of failing. This is a different point from the lookup style in
[this
thread](https://github.com/apache/spark/pull/60732#discussion_r4635506788). How
about stating the invariant, so that a missing entry throws?
```suggestion
case (col, Some(_)) if replacements.isDefined =>
```
##########
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:
**[4] The `|> EVAL` PR adds a file at this same path, and the same copy
check.**
The `|> EVAL` PR that the description mentions also adds
`UnresolvedStarExceptOrReplaceSuite.scala` in this package. So whichever PR
merges second gets an add/add conflict on this file, in addition to the
`expandStar` conflict that the description mentions.
That PR also has its own copy of the duplicate-copy check
(`expandedAttributes.attrs.filter(_.semanticEquals(column.toAttribute))` with
`ambiguousReferenceError`). The two copies would need to change together, for
example for [1]. Could we move the check into one small helper in
`UnresolvedStarExceptOrReplace` that both SET and EVAL call, and agree on which
PR adds this suite?
##########
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:
**[6] Nit on the PR description: SQL does build nodes with several EXCEPT
names.**
About a node whose EXCEPT names matched columns out of column order, the
description says "SQL builds one EXCEPT name per node, so no query reached
that." But `SELECT * EXCEPT (a, b)` and `|> DROP a, b` build one node with
several EXCEPT names. What no query builds is a node with several EXCEPT names
and replacements, since `|> SET` builds one node per assignment. Could we say
"SQL builds one EXCEPT name per node with replacements"? The doc of the new
suite already puts it that way.
##########
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:
**[5] Nit: `expandedCols.map(_.toAttribute)` is rebuilt for every EXCEPT
name.**
Each iteration builds a new `Seq` from `expandedCols` and scans it, even
when no exprId repeats, which is the common case. How about computing the
attributes once before the loop? `resolvedExcepts` at L688-L689 could reuse the
same `Seq`, too. The cost is small for `|> SET`, which builds one EXCEPT name
per node, so this is only a nit.
##########
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:
**[1] Two copies of a column from a UNION branch still get only one
replacement.**
This check counts the copies by exprId. Inside a UNION branch,
`DeduplicateUnionChildOutput` (SPARK-37865) aliases every copy of a column
after the first with a new exprId and tags it with `__is_duplicate`.
`AttributeSeq.getCandidatesForResolution` then prunes the tagged copy, so `x`
resolves to one attribute, `copies.size` is 1, and the second copy passes
through unchanged:
```sql
SELECT * FROM (
SELECT x, x FROM VALUES (0), (1) t(x)
UNION ALL
SELECT x, x FROM VALUES (2), (3) s(x))
|> SET x = 9;
-- struct<x:int,x:int>
-- [9,0]
-- [9,1]
-- [9,2]
-- [9,3]
```
I ran this on Spark 4.3.0, where the two output columns are `x#4` and `x#5`.
I did not run it with this PR, but this check keys on the exprId, so I expect
the same result. The first `x` takes the new value and the second keeps the old
one, without an error.
The PR description says that a name that matches two copies of the same
column now fails with `AMBIGUOUS_REFERENCE`, and `docs/sql-pipe-syntax.md` says
that each SET column must appear in the input table exactly once. This query
matches neither. `|> DROP x` on the same input also keeps the tagged copy, so
"still exclude every copy" in the description does not hold here either.
Could we count the expanded columns that match the name, including the
`__is_duplicate` ones, instead of the copies with the same exprId? A test with
this query in `pipe-operators.sql` would cover it. The result was the same
before this PR, so if you prefer to keep this PR narrow, a separate JIRA would
also work.
##########
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:
**[2] With replacements, the new checks now run before
`EXCEPT_OVERLAPPING_COLUMNS`.**
Before this PR, `filterColumns` raised `EXCEPT_OVERLAPPING_COLUMNS` for
overlapping EXCEPT names, with or without replacements. Now this nested-name
check and the copy check below run first, so a node with replacements reports a
different error:
- `excepts = Seq(Seq("col"), Seq("col", "i1"))` with two replacements used
to fail with `EXCEPT_OVERLAPPING_COLUMNS`. It now fails with this
`INTERNAL_ERROR`.
- `excepts = Seq(Seq("x"), Seq("x"))` with two replacements over `[x#1,
x#1]` used to fail with `EXCEPT_OVERLAPPING_COLUMNS` too. It now fails with
`AMBIGUOUS_REFERENCE`.
SQL cannot build either node today, since `|> SET` builds one EXCEPT name
per node. Still, the approving review says that overlapping excepts still hit
`EXCEPT_OVERLAPPING_COLUMNS` first, and that is not true for these nodes. Could
we run the overlap check before these two checks, or skip the nested check when
the same root column is also excepted as a whole?
--
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]