forked from delta-io/delta
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functions to `DeltaTable` to perform optimization. API documentation: ``` /** * Optimize the data layout of the table. This returns * a [[DeltaOptimizeBuilder]] object that can be used to specify * the partition filter to limit the scope of optimize and * also execute different optimization techniques such as file * compaction or order data using Z-Order curves. * * See the [[DeltaOptimizeBuilder]] for a full description * of this operation. * * Scala example to run file compaction on a subset of * partitions in the table: * {{{ * deltaTable * .optimize() * .where("date='2021-11-18'") * .executeCompaction(); * }}} * * @SInCE 1.3.0 */ def optimize(): DeltaOptimizeBuilder ``` ``` /** * Builder class for constructing OPTIMIZE command and executing. * * @param sparkSession SparkSession to use for execution * @param tableIdentifier Id of the table on which to * execute the optimize * @SInCE 1.3.0 */ class DeltaOptimizeBuilder( sparkSession: SparkSession, tableIdentifier: String) extends AnalysisHelper { /** * Apply partition filter on this optimize command builder to limit * the operation on selected partitions. * @param partitionFilter The partition filter to apply * @return [[DeltaOptimizeBuilder]] with partition filter applied */ def where(partitionFilter: String): DeltaOptimizeBuilder /** * Compact the small files in selected partitions. * @return DataFrame containing the OPTIMIZE execution metrics */ def executeCompaction(): DataFrame } ``` Closes delta-io#961 Fixes delta-io#960 Signed-off-by: Venki Korukanti <[email protected]> GitOrigin-RevId: 615e215b96fb9e9b9223d3d2b429dc18dff102f4
- Loading branch information
1 parent
acfdde1
commit 324bcb8
Showing
3 changed files
with
166 additions
and
28 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
core/src/main/scala/io/delta/tables/DeltaOptimizeBuilder.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright (2021) The Delta Lake Project Authors. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package io.delta.tables | ||
|
||
import org.apache.spark.sql.delta.commands.OptimizeTableCommand | ||
import org.apache.spark.sql.delta.util.AnalysisHelper | ||
|
||
import org.apache.spark.annotation.Unstable | ||
import org.apache.spark.sql.{AnalysisException, Column, DataFrame, SparkSession} | ||
import org.apache.spark.sql.catalyst.TableIdentifier | ||
import org.apache.spark.sql.catalyst.parser.ParseException | ||
|
||
/** | ||
* Builder class for constructing OPTIMIZE command and executing. | ||
* | ||
* @param sparkSession SparkSession to use for execution | ||
* @param tableIdentifier Id of the table on which to | ||
* execute the optimize | ||
* @since 1.3.0 | ||
*/ | ||
class DeltaOptimizeBuilder private( | ||
sparkSession: SparkSession, | ||
tableIdentifier: String) extends AnalysisHelper { | ||
@volatile private var partitionFilter: Option[String] = None | ||
|
||
/** | ||
* Apply partition filter on this optimize command builder to limit | ||
* the operation on selected partitions. | ||
* @param partitionFilter The partition filter to apply | ||
* @return [[DeltaOptimizeBuilder]] with partition filter applied | ||
*/ | ||
def where(partitionFilter: String): DeltaOptimizeBuilder = { | ||
this.partitionFilter = Some(partitionFilter) | ||
this | ||
} | ||
|
||
/** | ||
* Compact the small files in selected partitions. | ||
* @return DataFrame containing the OPTIMIZE execution metrics | ||
*/ | ||
def executeCompaction(): DataFrame = { | ||
val tableId: TableIdentifier = sparkSession | ||
.sessionState | ||
.sqlParser | ||
.parseTableIdentifier(tableIdentifier) | ||
val optimize = OptimizeTableCommand(None, Some(tableId), partitionFilter) | ||
toDataset(sparkSession, optimize) | ||
} | ||
} | ||
|
||
private[delta] object DeltaOptimizeBuilder { | ||
/** | ||
* :: Unstable :: | ||
* | ||
* Private method for internal usage only. Do not call this directly. | ||
*/ | ||
@Unstable | ||
private[delta] def apply( | ||
sparkSession: SparkSession, | ||
tableIdentifier: String): DeltaOptimizeBuilder = { | ||
new DeltaOptimizeBuilder(sparkSession, tableIdentifier) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters