spark-test commented on code in PR #60236:
URL: https://github.com/apache/spark/pull/60236#discussion_r4514860620
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/benchmark/UnionBenchmark.scala:
##########
@@ -60,9 +60,9 @@ object UnionBenchmark extends SqlBasedBenchmark {
dfs.reduce((a, b) => a.union(b)).filter(col("id") > 0)
}
- // Type widening: alternate int and long children so UnionExec needs to
- // insert `Cast` nodes in `perChildProjections`, adding per-row work in
- // each helper method.
+ // Type widening: alternate int and long children, so
`WidenSetOperationTypes` puts a
+ // `Project(Cast)` above each child and the union projects widened columns,
adding
Review Comment:
**Nit (P3):** This now names the right analyzer rule but overstates where it
applies. `buildWideningQuery` casts only the even-index children to int, so the
widest type is long, and `WidenSetOperationTypes.widenTypes` adds a
`Project(Cast)` only to a child whose column type differs (the `if (changed)`
branch in `TypeCoercionBase`). The long children are left as they are, so only
every other helper method does the per-row widening cast. The union itself only
forwards child columns by position. As written, readers of the widening results
would attribute cast work to every child. Suggest saying that the analyzer puts
a `Project(Cast)` above each int child, and that the long children and the
union add no conversion.
**Verification:**
- **Inspection:** The benchmark's type-widening comment matches the analyzed
plan for alternating int and long children: a widening cast projection above
each int child only, and no conversion added by the union.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/basicPhysicalOperators.scala:
##########
@@ -1596,12 +1580,17 @@ case class UnionExec(children: Seq[SparkPlan]) extends
SparkPlan with CodegenSup
ctx: CodegenContext, input: Seq[ExprCode], row: ExprCode): String = {
val i = currentEmittingChild.get
require(i >= 0, "UnionExec.doConsume invoked outside doProduce emission
window")
- // Route BoundReference reads through `currentVars` (the incoming row is
- // delivered as variables under WSCG, not via ctx.INPUT_ROW).
- val bound = BindReferences.bindReferences(perChildProjections(i),
children(i).output)
+ // Read the incoming row (delivered as `currentVars` under WSCG) by
position. Binding by
+ // `ExprId` would resolve a child output that repeats an `ExprId` (e.g.
`SELECT a, a`) to
+ // the first occurrence, so the second union output column would take the
first column's
+ // value and its declaration would be emitted twice. `doExecute` is
positional too, since
+ // it concatenates child rows as they are.
ctx.currentVars = input
ctx.INPUT_ROW = null
- val projectedExprCodes = bound.map(_.genCode(ctx))
+ val childOutput = children(i).output
Review Comment:
**Nit (P3):** With `perChildProjections` gone, the emitting-child index is
used only here, apart from the range check above, to pick the child whose
output `doConsume` reads by position. The per-emission state comment at line
1488, which is outside this diff, still says `currentEmittingChild` "tells
`doConsume` which child's projection to bind". No projection or bind step is
left. That sentence now contradicts the `doConsume` comment above and points
readers back to the `ExprId` binding this change removes. Suggest rewording it
to say that `currentEmittingChild` tells `doConsume` which child's output it
reads by position.
**Verification:**
- **Inspection:** The `UnionExec` comments that describe
`currentEmittingChild` and `doConsume` mention no per-child projection or
ExprId binding step, and both describe the emitting child's output being read
by position.
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/UnionCodegenSuite.scala:
##########
@@ -358,6 +358,56 @@ class UnionCodegenSuite extends SharedSparkSession with
AdaptiveSparkPlanHelper
s"Expected the union of both array rows, got $collectedArrays")
}
+ test("SPARK-61232: UnionExec whole-stage codegen fails when a child output
repeats an ExprId") {
+ // `SELECT p, p, w` leaves two output positions with one ExprId, and `r`
is a union, so for
+ // `p2`'s rows its second position holds `r` while its first holds `q`. A
union child reading
+ // its output by ExprId would take the first position for both: the second
union output
+ // column would hold the first column's value, and the generated row would
declare that
+ // value's variable twice, which Janino rejects.
+ //
+ // The analyzer normally replaces a repeated output column with a fresh
ExprId
+ // (`DeduplicateUnionChildOutput`); these confs keep it so the physical
union sees it.
+ withSQLConf(
+ SQLConf.UNION_IS_RESOLVED_WHEN_DUPLICATES_PER_CHILD_RESOLVED.key ->
"false",
+ SQLConf.ENFORCE_TYPE_COERCION_BEFORE_UNION_DEDUPLICATION.key -> "false",
+ SQLConf.ANALYZER_DUAL_RUN_LEGACY_AND_SINGLE_PASS_RESOLVER.key -> "false",
+ SQLConf.SHUFFLE_PARTITIONS.key -> "5",
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
+ SQLConf.WHOLESTAGE_CODEGEN_ENABLED.key -> "true") {
+ def build(): DataFrame = {
+ val p1 = spark.range(1000)
+ .selectExpr("id p", "id w").select(col("p"), col("p"), col("w"))
+ val p2 = spark.range(1000, 11000).selectExpr("id q", "id % 1000 r",
"id w2")
+ // `u` does not line up with `r`'s partitioning, so the union is plain
and fuses
+ // through its query stages.
+ val x = spark.range(1000)
+ .selectExpr("id x", "id c", "id u").repartition(col("u"))
+ val r = p1.union(p2).repartition(p1.col("p"))
+ x.union(r)
+ }
+ val fused = build()
+ val answer = fused.collect().toSeq
+ // The bug needs a union child whose output repeats an ExprId. The
analyzer can drop
+ // that shape, so assert it survived rather than letting the test go
vacuous.
+ assert(
+ collect(fused.queryExecution.executedPlan) { case u: UnionExec => u }
Review Comment:
**Nit (P3):** This collects every `UnionExec` in the plan, including the
inner `p1.union(p2)` inside `r`'s shuffle stage, because
`AdaptiveSparkPlanHelper.collect` descends into query stages. That union's
child `p1` (`Project [id AS p, id AS p, id AS w]`) repeats `p` on its own, with
the same value in both positions. So this assertion passes whatever the fused
union's own children look like, and the fusion check below accepts any
`InputAdapter` child. Today `r`'s stage output inherits the repeated `p`, so
the test does exercise the fix. But if a plan or test change gave the fused
union's stage child unique `ExprId`s, every assertion would stay green while
the fix went unexercised. That is the vacuity this guard is meant to catch.
Suggest asserting the shape on the fused union itself: some union in
`fusedUnions(fused)` has an `InputAdapter` child whose output repeats an
`ExprId`.
**Verification:**
- **Regression:** When the fused union's stage child has unique ExprIds
while the inner union's projection still repeats `p`, the tightened guard fails
instead of letting the test pass without exercising the fix.
- **Behavior:** On the current plan the tightened guard passes, the test
still returns 12000 rows with fused results equal to the
whole-stage-codegen-off results, and it still fails with the codegen compile
error if the union reads child columns by ExprId again.
--
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]