Skip to content

Commit 2d07216

Browse files
maltevelinzedtang
andauthored
[Spark] Add support for sorting within partitions when Z-ordering (#4006)
<!-- Thanks for sending a pull request! Here are some tips for you: 1. If this is your first time, please read our contributor guidelines: https://github.com/delta-io/delta/blob/master/CONTRIBUTING.md 2. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP] Your PR title ...'. 3. Be sure to keep the PR description updated to reflect all changes. 4. Please write your PR title to summarize what this PR proposes. 5. If possible, provide a concise example to reproduce the issue for a faster review. 6. If applicable, include the corresponding issue number in the PR title and link it in the body. --> #### Which Delta project/connector is this regarding? <!-- Please add the component selected below to the beginning of the pull request title For example: [Spark] Title of my pull request --> - [x] Spark - [ ] Standalone - [ ] Flink - [ ] Kernel - [ ] Other (fill in here) ## Description <!-- - Describe what this PR changes. - Describe why we need the change. If this PR resolves an issue be sure to include "Resolves #XXX" to correctly link and close the issue upon merge. --> Resolves #4000 by introducing a new configuration property `spark.databricks.io.skipping.mdc.sortWithinPartitions` that clusters records in row groups, within Parquet files, based on Z-order or Hilbert curve values. This improves data skipping on the Parquet level. Benchmarks included in the issue demonstrate speedups of approximately 8× and 11× on two different datasets. Please refer to the issue for more details. ## How was this patch tested? <!-- If tests were added, say they were added here. Please make sure to test the changes thoroughly including negative and positive cases if possible. If the changes were tested in any way other than unit tests, please clarify how you tested step by step (ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future). If the changes were not tested, please explain why. --> Added test cases in `MultiDimClusteringSuite.scala` for Hilbert and Z-order curves. ## Does this PR introduce _any_ user-facing changes? <!-- If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible. If possible, please also clarify if this is a user-facing change compared to the released Delta Lake versions or within the unreleased branches such as master. If no, write 'No'. --> Yes. This PR introduces a new configuration property `spark.databricks.io.skipping.mdc.sortWithinPartitions`. The property defaults to `false`, ensuring that existing users remain unaffected unless they opt-in by setting it to `true`. **Previous Behavior** Z-ordering did not sort data within partitions. **New Behavior** When the property is enabled, `sortWithinPartitions` is applied after `repartitionByRange` in `MultiDimClustering.scala`. --------- Signed-off-by: Malte Velin <[email protected]> Co-authored-by: Jiaheng Tang <[email protected]>
1 parent 00a6829 commit 2d07216

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

spark/src/main/scala/org/apache/spark/sql/delta/skipping/MultiDimClustering.scala

+5
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ trait SpaceFillingCurveClustering extends MultiDimClustering {
7474
val conf = df.sparkSession.sessionState.conf
7575
val numRanges = conf.getConf(DeltaSQLConf.MDC_NUM_RANGE_IDS)
7676
val addNoise = conf.getConf(DeltaSQLConf.MDC_ADD_NOISE)
77+
val sortWithinFiles = conf.getConf(DeltaSQLConf.MDC_SORT_WITHIN_FILES)
7778

7879
val cols = colNames.map(df(_))
7980
val mdcCol = getClusteringExpression(cols, numRanges)
@@ -90,6 +91,10 @@ trait SpaceFillingCurveClustering extends MultiDimClustering {
9091
.repartitionByRange(approxNumPartitions, col(repartitionKeyColName))
9192
}
9293

94+
if (sortWithinFiles) {
95+
repartitionedDf = repartitionedDf.sortWithinPartitions(repartitionKeyColName)
96+
}
97+
9398
repartitionedDf.drop(repartitionKeyColName)
9499
}
95100
}

spark/src/main/scala/org/apache/spark/sql/delta/sources/DeltaSQLConf.scala

+9
Original file line numberDiff line numberDiff line change
@@ -1517,6 +1517,15 @@ trait DeltaSQLConfBase {
15171517
.booleanConf
15181518
.createWithDefault(true)
15191519

1520+
val MDC_SORT_WITHIN_FILES =
1521+
SQLConf.buildConf("spark.databricks.io.skipping.mdc.sortWithinFiles")
1522+
.internal()
1523+
.doc("If enabled, sort within files by the specified MDC curve. " +
1524+
"This might improve row-group skipping and data compression, at " +
1525+
"the cost of additional overhead for sorting.")
1526+
.booleanConf
1527+
.createWithDefault(false)
1528+
15201529
val DELTA_OPTIMIZE_ZORDER_COL_STAT_CHECK =
15211530
buildConf("optimize.zorder.checkStatsCollection.enabled")
15221531
.internal()

spark/src/test/scala/org/apache/spark/sql/delta/skipping/MultiDimClusteringSuite.scala

+84
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,90 @@ class MultiDimClusteringSuite extends QueryTest
149149
}
150150
}
151151

152+
test("ensure records in each partition are sorted according to Z-order values") {
153+
withSQLConf(
154+
MDC_SORT_WITHIN_FILES.key -> "true",
155+
MDC_ADD_NOISE.key -> "false") {
156+
val data = Seq(
157+
// "c1" -> "c2", // (rangeId_c1, rangeId_c2) -> ZOrder (decimal Z-Order)
158+
"a" -> 20, "a" -> 20, // (0, 1) -> 0x01 (1)
159+
"b" -> 20, // (1, 1) -> 0x03 (3)
160+
"c" -> 30, // (2, 2) -> 0x0C (12)
161+
"d" -> 70, // (3, 3) -> 0x0F (15)
162+
"e" -> 90, "e" -> 90, "e" -> 90, // (4, 4) -> 0x30 (48)
163+
"f" -> 200, // (5, 5) -> 0x33 (51)
164+
"g" -> 10, // (6, 0) -> 0x28 (40)
165+
"h" -> 20) // (7, 1) -> 0x2B (43)
166+
167+
// Randomize the data. Use seed for deterministic input.
168+
val inputDf = new Random(seed = 101).shuffle(data)
169+
.toDF("c1", "c2")
170+
171+
// Cluster the data, range partition into one partition, and sort.
172+
val outputDf = MultiDimClustering.cluster(
173+
inputDf,
174+
approxNumPartitions = 1,
175+
colNames = Seq("c1", "c2"),
176+
curve = "zorder")
177+
178+
// Check that dataframe is sorted.
179+
checkAnswer(
180+
outputDf,
181+
Seq(
182+
"a" -> 20, "a" -> 20,
183+
"b" -> 20,
184+
"c" -> 30,
185+
"d" -> 70,
186+
"g" -> 10,
187+
"h" -> 20,
188+
"e" -> 90, "e" -> 90, "e" -> 90,
189+
"f" -> 200
190+
).toDF("c1", "c2").collect())
191+
}
192+
}
193+
194+
test("ensure records in each partition are sorted according to Hilbert curve values") {
195+
withSQLConf(
196+
MDC_SORT_WITHIN_FILES.key -> "true",
197+
MDC_ADD_NOISE.key -> "false") {
198+
val data = Seq(
199+
// "c1" -> "c2", // (rangeId_c1, rangeId_c2) -> Decimal Hilbert index
200+
"a" -> 20, "a" -> 20, // (0, 1) -> 3
201+
"b" -> 20, // (1, 1) -> 2
202+
"c" -> 30, // (2, 2) -> 8
203+
"d" -> 70, // (3, 3) -> 10
204+
"e" -> 90, "e" -> 90, "e" -> 90, // (4, 4) -> 32
205+
"f" -> 200, // (5, 5) -> 34
206+
"g" -> 10, // (6, 0) -> 20
207+
"h" -> 20) // (7, 1) -> 22
208+
209+
// Randomize the data. Use seed for deterministic input.
210+
val inputDf = new Random(seed = 101).shuffle(data)
211+
.toDF("c1", "c2")
212+
213+
// Cluster the data, range partition into one partition, and sort.
214+
val outputDf = MultiDimClustering.cluster(
215+
inputDf,
216+
approxNumPartitions = 1,
217+
colNames = Seq("c1", "c2"),
218+
curve = "hilbert")
219+
220+
// Check that dataframe is sorted.
221+
checkAnswer(
222+
outputDf,
223+
Seq(
224+
"b" -> 20,
225+
"a" -> 20, "a" -> 20,
226+
"c" -> 30,
227+
"d" -> 70,
228+
"g" -> 10,
229+
"h" -> 20,
230+
"e" -> 90, "e" -> 90, "e" -> 90,
231+
"f" -> 200
232+
).toDF("c1", "c2").collect())
233+
}
234+
}
235+
152236
test("noise is helpful in skew handling") {
153237
Seq("zorder", "hilbert").foreach { curve =>
154238
Seq("true", "false").foreach { addNoise =>

0 commit comments

Comments
 (0)