Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2025, NVIDIA CORPORATION.
* Copyright (c) 2020-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.
Expand Down Expand Up @@ -28,8 +28,8 @@ 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.{Attribute, Expression}
import org.apache.spark.sql.catalyst.plans.{ExistenceJoin, FullOuter, Inner, InnerLike, JoinType, LeftAnti, LeftOuter, LeftSemi, RightOuter}
import org.apache.spark.sql.catalyst.plans.physical.Distribution
import org.apache.spark.sql.catalyst.plans.{ExistenceJoin, FullOuter, Inner, InnerLike, JoinType, LeftAnti, LeftExistence, LeftOuter, LeftSemi, RightOuter}
import org.apache.spark.sql.catalyst.plans.physical.{Distribution, Partitioning, PartitioningCollection, UnknownPartitioning}
import org.apache.spark.sql.execution.SparkPlan
import org.apache.spark.sql.execution.joins.ShuffledHashJoinExec
import org.apache.spark.sql.rapids.GpuOr
Expand Down Expand Up @@ -168,6 +168,18 @@ case class GpuShuffledHashJoinExec(

override def otherCopyArgs: Seq[AnyRef] = cpuLeftKeys :: cpuRightKeys :: Nil

override def outputPartitioning: Partitioning = joinType match {
case _: InnerLike =>
PartitioningCollection(Seq(left.outputPartitioning, right.outputPartitioning))
case LeftOuter => left.outputPartitioning
case RightOuter => right.outputPartitioning
case FullOuter => UnknownPartitioning(left.outputPartitioning.numPartitions)
case LeftExistence(_) => left.outputPartitioning
case x =>
throw new IllegalArgumentException(
s"GpuShuffledHashJoinExec should not take $x as the JoinType")
}

import GpuMetric._

override val outputRowsLevel: MetricsLevel = ESSENTIAL_LEVEL
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024-2025, NVIDIA CORPORATION.
* Copyright (c) 2024-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.
Expand Down Expand Up @@ -34,7 +34,7 @@ import org.apache.spark.rdd.RDD
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.{Attribute, Expression}
import org.apache.spark.sql.catalyst.plans._
import org.apache.spark.sql.catalyst.plans.physical.Distribution
import org.apache.spark.sql.catalyst.plans.physical.{Distribution, Partitioning, PartitioningCollection, UnknownPartitioning}
import org.apache.spark.sql.execution.SparkPlan
import org.apache.spark.sql.execution.adaptive.ShuffleQueryStageExec
import org.apache.spark.sql.execution.exchange.ReusedExchangeExec
Expand Down Expand Up @@ -819,6 +819,16 @@ case class GpuShuffledSymmetricHashJoinExec(
startWithLeftSide: Boolean): JoinSizer[SpillableColumnarBatch] = {
new SpillableColumnarBatchSymmetricJoinSizer(startWithLeftSide)
}

override def outputPartitioning: Partitioning = joinType match {
case _: InnerLike =>
PartitioningCollection(Seq(left.outputPartitioning, right.outputPartitioning))
case FullOuter =>
UnknownPartitioning(left.outputPartitioning.numPartitions)
case x =>
throw new IllegalArgumentException(
s"GpuShuffledSymmetricHashJoinExec should not take $x as the JoinType")
}
}

object GpuShuffledAsymmetricHashJoinExec {
Expand Down Expand Up @@ -1141,6 +1151,14 @@ case class GpuShuffledAsymmetricHashJoinExec(
startWithLeftSide: Boolean): JoinSizer[SpillableColumnarBatch] = {
new SpillableColumnarBatchAsymmetricJoinSizer(magnificationThreshold)
}

override def outputPartitioning: Partitioning = joinType match {
case LeftOuter => left.outputPartitioning
case RightOuter => right.outputPartitioning
case x =>
throw new IllegalArgumentException(
s"GpuShuffledAsymmetricHashJoinExec should not take $x as the JoinType")
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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 = {
Copy link
Collaborator

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?

Copy link
Collaborator

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 GpuProjectExecLike extend PartitioningPreservingUnaryExecNode instead of copying this code.

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")
Expand Down Expand Up @@ -1609,6 +1655,9 @@ case class GpuUnionExec(children: Seq[SparkPlan]) extends ShimSparkPlan with Gpu
}
}

override def outputPartitioning: Partitioning =
GpuUnionExecShim.getOutputPartitioning(children, output, conf)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, can we take the outputPartitioning of the cpu exec as a parameter of GpuUnionExec instead of duplicating the Spark code?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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)
Expand All @@ -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)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2025, NVIDIA CORPORATION.
* Copyright (c) 2020-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.
Expand All @@ -24,7 +24,7 @@ import org.apache.spark.rdd.RDD
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.Expression
import org.apache.spark.sql.catalyst.plans.{JoinType, LeftAnti}
import org.apache.spark.sql.catalyst.plans.physical.{BroadcastDistribution, Distribution, UnspecifiedDistribution}
import org.apache.spark.sql.catalyst.plans.physical.{BroadcastDistribution, Distribution, Partitioning, UnspecifiedDistribution}
import org.apache.spark.sql.execution.SparkPlan
import org.apache.spark.sql.execution.adaptive.BroadcastQueryStageExec
import org.apache.spark.sql.execution.exchange.ReusedExchangeExec
Expand Down Expand Up @@ -131,6 +131,8 @@ abstract class GpuBroadcastHashJoinExecBase(
}
}

override def outputPartitioning: Partitioning = streamedPlan.outputPartitioning

def broadcastExchange: GpuBroadcastExchangeExec = buildPlan match {
case bqse: BroadcastQueryStageExec if bqse.plan.isInstanceOf[GpuBroadcastExchangeExec] =>
bqse.plan.asInstanceOf[GpuBroadcastExchangeExec]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2025, NVIDIA CORPORATION.
* Copyright (c) 2020-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.
Expand Down Expand Up @@ -31,7 +31,7 @@ import org.apache.spark.rdd.RDD
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.{Attribute, Expression, NamedExpression}
import org.apache.spark.sql.catalyst.plans.{ExistenceJoin, FullOuter, InnerLike, JoinType, LeftAnti, LeftExistence, LeftOuter, LeftSemi, RightOuter}
import org.apache.spark.sql.catalyst.plans.physical.{BroadcastDistribution, Distribution, IdentityBroadcastMode, UnspecifiedDistribution}
import org.apache.spark.sql.catalyst.plans.physical.{BroadcastDistribution, Distribution, IdentityBroadcastMode, Partitioning, UnspecifiedDistribution}
import org.apache.spark.sql.execution.SparkPlan
import org.apache.spark.sql.execution.adaptive.BroadcastQueryStageExec
import org.apache.spark.sql.execution.exchange.ReusedExchangeExec
Expand Down Expand Up @@ -610,6 +610,14 @@ abstract class GpuBroadcastNestedLoopJoinExecBase(
UnspecifiedDistribution :: BroadcastDistribution(IdentityBroadcastMode) :: Nil
}

// Match Spark's BroadcastNestedLoopJoinExec.outputPartitioning
override def outputPartitioning: Partitioning = (joinType, gpuBuildSide) match {
case (_: InnerLike, _) | (LeftOuter, GpuBuildRight) | (RightOuter, GpuBuildLeft) |
(LeftSemi, GpuBuildRight) | (LeftAnti, GpuBuildRight) =>
streamed.outputPartitioning
case _ => super.outputPartitioning
}

override def output: Seq[Attribute] = {
GpuBroadcastNestedLoopJoinExecBase.output(joinType, left.output, right.output)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2025, NVIDIA CORPORATION. All rights reserved.
* Copyright (c) 2020-2026, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,8 +21,10 @@ import com.nvidia.spark.rapids.shims.ShimUnaryExecNode
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.{Attribute, Expression}
import org.apache.spark.sql.catalyst.plans.physical.{Partitioning, UnknownPartitioning}
import org.apache.spark.sql.execution.{CoalescedPartitionSpec, PartialMapperPartitionSpec, PartialReducerPartitionSpec, ShufflePartitionSpec, SparkPlan}
import org.apache.spark.sql.catalyst.plans.physical.{HashPartitioning, Partitioning,
RangePartitioning, RoundRobinPartitioning, SinglePartition, UnknownPartitioning}
import org.apache.spark.sql.execution.{CoalescedPartitionSpec, PartialMapperPartitionSpec,
PartialReducerPartitionSpec, ShufflePartitionSpec, SparkPlan}
import org.apache.spark.sql.execution.adaptive.ShuffleQueryStageExec
import org.apache.spark.sql.execution.exchange.{ReusedExchangeExec, ShuffleExchangeLike}
import org.apache.spark.sql.vectorized.ColumnarBatch
Expand Down Expand Up @@ -77,6 +79,27 @@ case class GpuCustomShuffleReaderExec(
case _ =>
throw new IllegalStateException("operating on canonicalization plan")
}
} else if (isCoalescedRead) {
// For coalesced shuffle read, the data distribution is not changed, only the number of
// partitions is changed.
child.outputPartitioning match {
case h: HashPartitioning =>
h.copy(numPartitions = partitionSpecs.length)
case r: RangePartitioning =>
r.copy(numPartitions = partitionSpecs.length)
// This can only happen for `REBALANCE_PARTITIONS_BY_NONE`, which uses
// `RoundRobinPartitioning` but we don't need to retain the number of partitions.
case r: RoundRobinPartitioning =>
r.copy(numPartitions = partitionSpecs.length)
case other @ SinglePartition =>
throw new IllegalStateException(
"Unexpected partitioning for coalesced shuffle read: " + other)
case _ =>
// Spark plugins may have custom partitioning and may replace this operator
// during the postStageOptimization phase, so return UnknownPartitioning here
// rather than throw an exception
UnknownPartitioning(partitionSpecs.length)
}
} else {
UnknownPartitioning(partitionSpecs.length)
}
Expand Down Expand Up @@ -109,6 +132,16 @@ case class GpuCustomShuffleReaderExec(
def isLocalReader: Boolean =
partitionSpecs.exists(_.isInstanceOf[PartialMapperPartitionSpec])

def isCoalescedRead: Boolean = {
partitionSpecs.sliding(2).forall {
// A single partition spec which is `CoalescedPartitionSpec` also means coalesced read.
case Seq(_: CoalescedPartitionSpec) => true
case Seq(l: CoalescedPartitionSpec, r: CoalescedPartitionSpec) =>
l.endReducerIndex <= r.startReducerIndex
case _ => false
}
}

private var cachedShuffleRDD: RDD[ColumnarBatch] = null

override protected def doExecute(): RDD[InternalRow] = {
Expand Down
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing {"spark": "400db173"} ?
It's better to test Databricks by adding [databricks] marker in the PR title.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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)
}
}
Loading