-
Notifications
You must be signed in to change notification settings - Fork 271
Align GpuUnionExec with Spark 4.1's partitioner-aware union behavior [databricks] #14164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3cfaa98
ed51acb
d5b590e
9283666
1239861
cb302e3
aea6a7d
0d12e44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,7 +37,7 @@ import org.apache.spark.internal.Logging | |
| import org.apache.spark.rdd.RDD | ||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.plans.physical.{Partitioning, RangePartitioning, SinglePartition, UnknownPartitioning} | ||
| import org.apache.spark.sql.catalyst.plans.physical.{Partitioning, PartitioningCollection, RangePartitioning, SinglePartition, UnknownPartitioning} | ||
| import org.apache.spark.sql.catalyst.util.{ArrayData, MapData} | ||
| import org.apache.spark.sql.execution.{FilterExec, ProjectExec, SampleExec, SparkPlan} | ||
| import org.apache.spark.sql.rapids.{GpuCreateArray, GpuCreateMap, GpuCreateNamedStruct, GpuPartitionwiseSampledRDD, GpuPoissonSampler} | ||
|
|
@@ -231,7 +231,53 @@ trait GpuProjectExecLike extends ShimUnaryExecNode with GpuExec { | |
|
|
||
| override def outputOrdering: Seq[SortOrder] = child.outputOrdering | ||
|
|
||
| override def outputPartitioning: Partitioning = child.outputPartitioning | ||
| /** | ||
| * Compute output partitioning, handling PartitioningCollection from joins. | ||
| * | ||
| * This matches Spark's PartitioningPreservingUnaryExecNode behavior: | ||
| * 1. Flatten any PartitioningCollection into individual partitionings | ||
| * 2. Filter to only keep partitionings whose attributes are in the output | ||
| * 3. Remap attributes through aliases | ||
| * | ||
| * This is critical for Spark 4.1+ where UnionExec uses outputPartitioning | ||
| * to decide between partitioner-aware union vs concatenation. | ||
| */ | ||
| override def outputPartitioning: Partitioning = { | ||
| val attributeMap = child.output.zip(output).toMap | ||
| val outputSet = AttributeSet(output) | ||
|
|
||
| // Flatten a PartitioningCollection into individual partitionings | ||
| def flattenPartitioning(p: Partitioning): Seq[Partitioning] = p match { | ||
| case PartitioningCollection(childPartitionings) => | ||
| childPartitionings.flatMap(flattenPartitioning) | ||
| case other => Seq(other) | ||
| } | ||
|
|
||
| def remapPartitioning(p: Partitioning): Partitioning = p match { | ||
| case e: Expression => | ||
| e.transform { | ||
| case a: Attribute if attributeMap.contains(a) => attributeMap(a) | ||
| }.asInstanceOf[Partitioning] | ||
| case other => other | ||
| } | ||
|
|
||
| val partitionings = flattenPartitioning(child.outputPartitioning).flatMap { | ||
| case e: Expression => | ||
| // Only keep partitionings whose attributes are all in the output | ||
| val remapped = remapPartitioning(e.asInstanceOf[Partitioning]) | ||
| remapped match { | ||
| case re: Expression if re.references.subsetOf(outputSet) => Some(remapped) | ||
| case _ => None | ||
| } | ||
| case other => Some(other) | ||
| } | ||
|
|
||
| partitionings match { | ||
| case Seq() => UnknownPartitioning(child.outputPartitioning.numPartitions) | ||
| case Seq(single) => single | ||
| case multiple => PartitioningCollection(multiple) | ||
| } | ||
| } | ||
|
|
||
| override def doExecute(): RDD[InternalRow] = | ||
| throw new IllegalStateException(s"Row-based execution should not occur for $this") | ||
|
|
@@ -1609,6 +1655,9 @@ case class GpuUnionExec(children: Seq[SparkPlan]) extends ShimSparkPlan with Gpu | |
| } | ||
| } | ||
|
|
||
| override def outputPartitioning: Partitioning = | ||
| GpuUnionExecShim.getOutputPartitioning(children, output, conf) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, can we take the
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @jihoonson for the review. Your review comments on the refactor makes sense. I will do it as a follow-on PR for 26.04 if that's okay. Filed an issue for these - #14229 |
||
|
|
||
| // The smallest of our children | ||
| override def outputBatching: CoalesceGoal = | ||
| children.map(GpuExec.outputBatching).reduce(CoalesceGoal.minProvided) | ||
|
|
@@ -1620,11 +1669,12 @@ case class GpuUnionExec(children: Seq[SparkPlan]) extends ShimSparkPlan with Gpu | |
| val numOutputRows = gpuLongMetric(NUM_OUTPUT_ROWS) | ||
| val numOutputBatches = gpuLongMetric(NUM_OUTPUT_BATCHES) | ||
|
|
||
| sparkContext.union(children.map(_.executeColumnar())).map { batch => | ||
| numOutputBatches += 1 | ||
| numOutputRows += batch.numRows | ||
| batch | ||
| } | ||
| GpuUnionExecShim.unionColumnarRdds( | ||
| sparkContext, | ||
| children.map(_.executeColumnar()), | ||
| outputPartitioning, | ||
| numOutputRows, | ||
| numOutputBatches) | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to also update what we say out output partitioning is? |
||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* | ||
| * Copyright (c) 2026, NVIDIA CORPORATION. | ||
| * | ||
| * Licensed 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. | ||
| */ | ||
|
|
||
| /*** spark-rapids-shim-json-lines | ||
| {"spark": "320"} | ||
| {"spark": "321"} | ||
| {"spark": "321cdh"} | ||
| {"spark": "322"} | ||
| {"spark": "323"} | ||
| {"spark": "324"} | ||
| {"spark": "330"} | ||
| {"spark": "330cdh"} | ||
| {"spark": "330db"} | ||
| {"spark": "331"} | ||
| {"spark": "332"} | ||
| {"spark": "332cdh"} | ||
| {"spark": "332db"} | ||
| {"spark": "333"} | ||
| {"spark": "334"} | ||
| {"spark": "340"} | ||
| {"spark": "341"} | ||
| {"spark": "341db"} | ||
| {"spark": "342"} | ||
| {"spark": "343"} | ||
| {"spark": "344"} | ||
| {"spark": "350"} | ||
| {"spark": "350db143"} | ||
| {"spark": "351"} | ||
| {"spark": "352"} | ||
| {"spark": "353"} | ||
| {"spark": "354"} | ||
| {"spark": "355"} | ||
| {"spark": "356"} | ||
| {"spark": "357"} | ||
| {"spark": "400"} | ||
| {"spark": "401"} | ||
| spark-rapids-shim-json-lines ***/ | ||
| package com.nvidia.spark.rapids.shims | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing {"spark": "400db173"} ?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added [databricks] marker in the PR title. I will add "400db173" in the PR to support for Databricks-17.3. Adding now itself might be confusing for some. |
||
|
|
||
| import com.nvidia.spark.rapids.GpuMetric | ||
|
|
||
| import org.apache.spark.SparkContext | ||
| import org.apache.spark.rdd.RDD | ||
| import org.apache.spark.sql.catalyst.expressions.Attribute | ||
| import org.apache.spark.sql.catalyst.plans.physical.{Partitioning, UnknownPartitioning} | ||
| import org.apache.spark.sql.execution.SparkPlan | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.vectorized.ColumnarBatch | ||
|
|
||
| /** | ||
| * Pre-Spark 4.1 implementation: uses sparkContext.union() which concatenates partitions. | ||
| */ | ||
| object GpuUnionExecShim { | ||
|
|
||
| def unionColumnarRdds( | ||
| sc: SparkContext, | ||
| rdds: Seq[RDD[ColumnarBatch]], | ||
| outputPartitioning: Partitioning, | ||
| numOutputRows: GpuMetric, | ||
| numOutputBatches: GpuMetric): RDD[ColumnarBatch] = { | ||
| sc.union(rdds).map { batch => | ||
| numOutputBatches += 1 | ||
| numOutputRows += batch.numRows | ||
| batch | ||
| } | ||
| } | ||
|
|
||
| def getOutputPartitioning( | ||
| children: Seq[SparkPlan], | ||
| unionOutput: Seq[Attribute], | ||
| conf: SQLConf): Partitioning = { | ||
| UnknownPartitioning(0) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we not need to handle the case when the project has aliases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also wonder if it's a good idea to have
GpuProjectExecLikeextendPartitioningPreservingUnaryExecNodeinstead of copying this code.