Skip to content

Commit dd36ea8

Browse files
committed
fix
1 parent ff93cbb commit dd36ea8

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/basicLogicalOperators.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,13 +614,20 @@ case class Union(
614614
Some(sum.toLong)
615615
}
616616

617+
def duplicateResolvedPerChild: Boolean =
618+
children.forall(child => child.outputSet.size == child.output.size)
619+
617620
def duplicateResolved: Boolean = {
618621
children.map(_.outputSet.size).sum ==
619622
AttributeSet.fromAttributeSets(children.map(_.outputSet)).size
620623
}
621624

622625
override lazy val resolved: Boolean = {
623-
children.length > 1 && !(byName || allowMissingCol) && childrenResolved && allChildrenCompatible
626+
children.length > 1 &&
627+
!(byName || allowMissingCol) &&
628+
childrenResolved &&
629+
allChildrenCompatible &&
630+
duplicateResolvedPerChild
624631
}
625632

626633
override protected def withNewChildrenInternal(newChildren: IndexedSeq[LogicalPlan]): Union =

sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4963,6 +4963,23 @@ class SQLQuerySuite extends QueryTest with SharedSparkSession with AdaptiveSpark
49634963
)
49644964
}
49654965

4966+
test("SPARK-52686: Union should be resolved only if there are no duplicates") {
4967+
withTable("t1") {
4968+
withView("v1") {
4969+
sql("CREATE TABLE t1(col1 STRING)")
4970+
sql("CREATE VIEW v1 AS SELECT * FROM t1")
4971+
val analyzedPlan = sql(
4972+
"SELECT * FROM (SELECT col1, col1 FROM v1 UNION SELECT col1, col1 FROM v1)"
4973+
).queryExecution.analyzed
4974+
4975+
// Resolving * should wait for Union output to be deduplicated, otherwise we are
4976+
// left with duplicate ExprIds in the result.
4977+
val exprIds = analyzedPlan.asInstanceOf[Project].projectList.map(_.exprId)
4978+
assert(exprIds.size == exprIds.distinct.size)
4979+
}
4980+
}
4981+
}
4982+
49664983
Seq(true, false).foreach { codegenEnabled =>
49674984
test(s"SPARK-52060: one row relation with codegen enabled - $codegenEnabled") {
49684985
withSQLConf(SQLConf.WHOLESTAGE_CODEGEN_ENABLED.key -> codegenEnabled.toString) {

0 commit comments

Comments
 (0)