Skip to content

Commit

Permalink
[Spark] OPTIMIZE on clustered table with no clustering columns should…
Browse files Browse the repository at this point in the history
… run compaction (delta-io#2777)

<!--
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.
-->
Currently, when running OPTIMIZE on a clustered table without any
clustering columns(after ALTER TABLE CLUSTER BY NONE), it would fail
with a long stack trace:
```
[info]   org.apache.spark.SparkException: Exception thrown in awaitResult:
[info]   at org.apache.spark.util.SparkThreadUtils$.awaitResult(SparkThreadUtils.scala:56)
[info]   at org.apache.spark.util.ThreadUtils$.awaitResult(ThreadUtils.scala:310)
[info]   at org.apache.spark.util.ThreadUtils$.parmap(ThreadUtils.scala:383)
[info]   at org.apache.spark.sql.delta.commands.OptimizeExecutor.$anonfun$optimize$1(OptimizeTableCommand.scala:276)
...
[info]   Cause: java.util.concurrent.ExecutionException: Boxed Error
[info]   at scala.concurrent.impl.Promise$.resolver(Promise.scala:87)
...
[info]   Cause: java.lang.AssertionError: assertion failed: Cannot cluster by zero columns!
[info]   at scala.Predef$.assert(Predef.scala:223)
[info]   at org.apache.spark.sql.delta.skipping.MultiDimClustering$.cluster(MultiDimClustering.scala:51)
[info]   at org.apache.spark.sql.delta.commands.OptimizeExecutor.runOptimizeBinJob(OptimizeTableCommand.scala:427)
[info]   at org.apache.spark.sql.delta.commands.OptimizeExecutor.$anonfun$optimize$6(OptimizeTableCommand.scala:277)
...
```

This change makes OPTIMIZE on a clustered table without any clustering
columns run regular compaction, which is the desired behavior.
## 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.
-->
This change adds a new test to verify the correct behavior. The test
would fail without the fix.
## 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'.
-->
No.
  • Loading branch information
zedtang authored Mar 21, 2024
1 parent 8618388 commit 050e018
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ class OptimizeExecutor(

private val isClusteredTable = ClusteredTableUtils.isSupported(txn.snapshot.protocol)

private val isMultiDimClustering = isClusteredTable || zOrderByColumns.nonEmpty
private val isMultiDimClustering =
optimizeStrategy.isInstanceOf[ClusteringStrategy] ||
optimizeStrategy.isInstanceOf[ZOrderStrategy]

private val clusteringColumns: Seq[String] = {
if (zOrderByColumns.nonEmpty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ object OptimizeTableStrategy {

private def getMode(snapshot: Snapshot, zOrderBy: Seq[String]): OptimizeTableMode.Value = {
val isClusteredTable = ClusteredTableUtils.isSupported(snapshot.protocol)
val hasClusteringColumns = ClusteringColumnInfo.extractLogicalNames(snapshot).nonEmpty
val isZOrderBy = zOrderBy.nonEmpty
if (isClusteredTable) {
if (isClusteredTable && hasClusteringColumns) {
assert(!isZOrderBy)
OptimizeTableMode.CLUSTERING
} else if (isZOrderBy) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,8 @@ trait ClusteredTableDDLSuiteBase
extends ClusteredTableCreateOrReplaceDDLSuite
with DeltaSQLCommandTest {

import testImplicits._

test("cluster by with more than 4 columns - alter table") {
val testTable = "test_table"
withClusteredTable(testTable, "a INT, b INT, c INT, d INT, e INT", "a") {
Expand Down Expand Up @@ -564,6 +566,49 @@ trait ClusteredTableDDLSuiteBase
}
}

test("optimize clustered table and trigger regular compaction") {
withClusteredTable(testTable, "a INT, b STRING", "a, b") {
val tableIdentifier = TableIdentifier(testTable)
verifyClusteringColumns(tableIdentifier, "a, b")

(1 to 1000).map(i => (i, i.toString)).toDF("a", "b")
.write.mode("append").format("delta").saveAsTable(testTable)

val (deltaLog, snapshot) = DeltaLog.forTableWithSnapshot(spark, TableIdentifier(testTable))
val targetFileSize = (snapshot.sizeInBytes / 10).toString
withSQLConf(
DeltaSQLConf.DELTA_OPTIMIZE_MAX_FILE_SIZE.key -> targetFileSize,
DeltaSQLConf.DELTA_OPTIMIZE_MIN_FILE_SIZE.key -> targetFileSize) {
runOptimize(testTable) { metrics =>
assert(metrics.numFilesAdded > 0)
assert(metrics.numFilesRemoved > 0)
assert(metrics.clusteringStats.nonEmpty)
assert(metrics.clusteringStats.get.numOutputZCubes == 1)
}
}

// ALTER TABLE CLUSTER BY NONE and then OPTIMIZE to trigger regular compaction.
sql(s"ALTER TABLE $testTable CLUSTER BY NONE")
verifyClusteringColumns(tableIdentifier, "")

(1001 to 2000).map(i => (i, i.toString)).toDF("a", "b")
.repartition(10).write.mode("append").format("delta").saveAsTable(testTable)
val newSnapshot = deltaLog.update()
val newTargetFileSize = (newSnapshot.sizeInBytes / 10).toString
withSQLConf(
DeltaSQLConf.DELTA_OPTIMIZE_MAX_FILE_SIZE.key -> newTargetFileSize,
DeltaSQLConf.DELTA_OPTIMIZE_MIN_FILE_SIZE.key -> newTargetFileSize) {
runOptimize(testTable) { metrics =>
assert(metrics.numFilesAdded > 0)
assert(metrics.numFilesRemoved > 0)
// No clustering or zorder stats indicates regular compaction.
assert(metrics.clusteringStats.isEmpty)
assert(metrics.zOrderStats.isEmpty)
}
}
}
}

test("optimize clustered table - error scenarios") {
withClusteredTable(testTable, "a INT, b STRING", "a") {
// Specify partition predicate.
Expand Down

0 comments on commit 050e018

Please sign in to comment.