-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-47094][SQL] SPJ : Dynamically rebalance number of buckets when they are not equal #45267
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
b2f7cc0
cda8b26
eee9198
7bf1610
18db83a
3fdb0d7
23c580f
0c6f494
d2de9c3
d3e196c
1fa24a4
8053e58
ad61b0c
0356f9e
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 |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * 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.connector.catalog.functions; | ||
|
|
||
| import org.apache.spark.annotation.Evolving; | ||
|
|
||
| /** | ||
| * A 'reducer' for output of user-defined functions. | ||
| * | ||
| * @see ReducibleFunction | ||
| * | ||
| * A user defined function f_source(x) is 'reducible' on another user_defined function | ||
| * f_target(x) if | ||
| * <ul> | ||
| * <li> There exists a reducer function r(x) such that r(f_source(x)) = f_target(x) for | ||
| * all input x, or </li> | ||
| * <li> More generally, there exists reducer functions r1(x) and r2(x) such that | ||
| * r1(f_source(x)) = r2(f_target(x)) for all input x. </li> | ||
| * </ul> | ||
| * | ||
| * @param <I> reducer input type | ||
szehon-ho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * @param <O> reducer output type | ||
| * @since 4.0.0 | ||
| */ | ||
| @Evolving | ||
| public interface Reducer<I, O> { | ||
| O reduce(I arg); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| * 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.connector.catalog.functions; | ||
|
|
||
| import org.apache.spark.annotation.Evolving; | ||
|
|
||
| /** | ||
| * Base class for user-defined functions that can be 'reduced' on another function. | ||
| * | ||
| * A function f_source(x) is 'reducible' on another function f_target(x) if | ||
| * <ul> | ||
| * <li> There exists a reducer function r(x) such that r(f_source(x)) = f_target(x) | ||
| * for all input x, or </li> | ||
| * <li> More generally, there exists reducer functions r1(x) and r2(x) such that | ||
| * r1(f_source(x)) = r2(f_target(x)) for all input x. </li> | ||
| * </ul> | ||
| * <p> | ||
| * Examples: | ||
| * <ul> | ||
| * <li>Bucket functions where one side has reducer | ||
| * <ul> | ||
| * <li>f_source(x) = bucket(4, x)</li> | ||
| * <li>f_target(x) = bucket(2, x)</li> | ||
| * <li>r(x) = x % 2</li> | ||
| * </ul> | ||
| * | ||
| * <li>Bucket functions where both sides have reducer | ||
| * <ul> | ||
| * <li>f_source(x) = bucket(16, x)</li> | ||
| * <li>f_target(x) = bucket(12, x)</li> | ||
| * <li>r1(x) = x % 4</li> | ||
| * <li>r2(x) = x % 4</li> | ||
| * </ul> | ||
| * | ||
| * <li>Date functions | ||
| * <ul> | ||
| * <li>f_source(x) = days(x)</li> | ||
| * <li>f_target(x) = hours(x)</li> | ||
| * <li>r(x) = x / 24</li> | ||
| * </ul> | ||
| * </ul> | ||
| * @param <I> reducer function input type | ||
| * @param <O> reducer function output type | ||
| * @since 4.0.0 | ||
| */ | ||
| @Evolving | ||
| public interface ReducibleFunction<I, O> { | ||
|
|
||
| /** | ||
| * This method is for the bucket function. | ||
| * | ||
| * If this bucket function is 'reducible' on another bucket function, | ||
| * return the {@link Reducer} function. | ||
| * <p> | ||
| * For example, to return reducer for reducing f_source = bucket(4, x) on f_target = bucket(2, x) | ||
| * <ul> | ||
| * <li>thisBucketFunction = bucket</li> | ||
| * <li>thisNumBuckets = 4</li> | ||
| * <li>otherBucketFunction = bucket</li> | ||
| * <li>otherNumBuckets = 2</li> | ||
| * </ul> | ||
| * | ||
| * @param thisNumBuckets parameter for this function | ||
| * @param otherBucketFunction the other parameterized function | ||
| * @param otherNumBuckets parameter for the other function | ||
| * @return a reduction function if it is reducible, null if not | ||
| */ | ||
| default Reducer<I, O> reducer( | ||
| int thisNumBuckets, | ||
| ReducibleFunction<?, ?> otherBucketFunction, | ||
| int otherNumBuckets) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| /** | ||
| * This method is for all other functions. | ||
| * | ||
| * If this function is 'reducible' on another function, return the {@link Reducer} function. | ||
| * <p> | ||
| * Example of reducing f_source = days(x) on f_target = hours(x) | ||
| * <ul> | ||
| * <li>thisFunction = days</li> | ||
| * <li>otherFunction = hours</li> | ||
| * </ul> | ||
| * | ||
| * @param otherFunction the other function | ||
| * @return a reduction function if it is reducible, null if not. | ||
| */ | ||
| default Reducer<I, O> reducer(ReducibleFunction<?, ?> otherFunction) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,9 +24,10 @@ 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.QueryPlan | ||
| import org.apache.spark.sql.catalyst.plans.physical.{KeyGroupedPartitioning, Partitioning, SinglePartition} | ||
| import org.apache.spark.sql.catalyst.plans.physical.{KeyGroupedPartitioning, KeyGroupedShuffleSpec, Partitioning, SinglePartition} | ||
| import org.apache.spark.sql.catalyst.util.{truncatedString, InternalRowComparableWrapper} | ||
| import org.apache.spark.sql.connector.catalog.Table | ||
| import org.apache.spark.sql.connector.catalog.functions.Reducer | ||
| import org.apache.spark.sql.connector.read._ | ||
| import org.apache.spark.util.ArrayImplicits._ | ||
|
|
||
|
|
@@ -164,6 +165,18 @@ case class BatchScanExec( | |
| (groupedParts, expressions) | ||
| } | ||
|
|
||
| // Also re-group the partitions if we are reducing compatible partition expressions | ||
| val finalGroupedPartitions = spjParams.reducers match { | ||
|
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. I believe this could produce incorrect results when joining presorted bucketed tables with compatible bucket counts. Specifically, if we have two tables:
When performing a bucketed join in Spark, it's expected that the sort order should be preserved. However, the merging or grouping process involved in the join might break these sorting guarantees, leading to incorrect results. |
||
| case Some(reducers) => | ||
| val result = groupedPartitions.groupBy { case (row, _) => | ||
| KeyGroupedShuffleSpec.reducePartitionValue(row, partExpressions, reducers) | ||
| }.map { case (wrapper, splits) => (wrapper.row, splits.flatMap(_._2)) }.toSeq | ||
| val rowOrdering = RowOrdering.createNaturalAscendingOrdering( | ||
| partExpressions.map(_.dataType)) | ||
| result.sorted(rowOrdering.on((t: (InternalRow, _)) => t._1)) | ||
| case _ => groupedPartitions | ||
| } | ||
|
|
||
| // When partially clustered, the input partitions are not grouped by partition | ||
| // values. Here we'll need to check `commonPartitionValues` and decide how to group | ||
| // and replicate splits within a partition. | ||
|
|
@@ -174,7 +187,7 @@ case class BatchScanExec( | |
| .get | ||
| .map(t => (InternalRowComparableWrapper(t._1, partExpressions), t._2)) | ||
| .toMap | ||
| val nestGroupedPartitions = groupedPartitions.map { case (partValue, splits) => | ||
| val nestGroupedPartitions = finalGroupedPartitions.map { case (partValue, splits) => | ||
| // `commonPartValuesMap` should contain the part value since it's the super set. | ||
| val numSplits = commonPartValuesMap | ||
| .get(InternalRowComparableWrapper(partValue, partExpressions)) | ||
|
|
@@ -207,7 +220,7 @@ case class BatchScanExec( | |
| } else { | ||
| // either `commonPartitionValues` is not defined, or it is defined but | ||
| // `applyPartialClustering` is false. | ||
| val partitionMapping = groupedPartitions.map { case (partValue, splits) => | ||
| val partitionMapping = finalGroupedPartitions.map { case (partValue, splits) => | ||
| InternalRowComparableWrapper(partValue, partExpressions) -> splits | ||
| }.toMap | ||
|
|
||
|
|
@@ -259,6 +272,7 @@ case class StoragePartitionJoinParams( | |
| keyGroupedPartitioning: Option[Seq[Expression]] = None, | ||
| joinKeyPositions: Option[Seq[Int]] = None, | ||
| commonPartitionValues: Option[Seq[(InternalRow, Int)]] = None, | ||
| reducers: Option[Seq[Option[Reducer[_, _]]]] = None, | ||
| applyPartialClustering: Boolean = false, | ||
| replicatePartitions: Boolean = false) { | ||
| override def equals(other: Any): Boolean = other match { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.