-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[HUDI-3099] Purge drop partition for spark sql #4436
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -18,12 +18,11 @@ | |
| package org.apache.spark.sql.hudi | ||
|
|
||
| import org.apache.hudi.DataSourceWriteOptions._ | ||
| import org.apache.hudi.common.util.PartitionPathEncodeUtils | ||
| import org.apache.hudi.config.HoodieWriteConfig | ||
| import org.apache.hudi.keygen.{ComplexKeyGenerator, SimpleKeyGenerator} | ||
| import org.apache.spark.sql.SaveMode | ||
|
|
||
| import scala.util.control.NonFatal | ||
|
|
||
| class TestAlterTableDropPartition extends TestHoodieSqlBase { | ||
|
|
||
| test("Drop non-partitioned table") { | ||
|
|
@@ -88,7 +87,62 @@ class TestAlterTableDropPartition extends TestHoodieSqlBase { | |
| // drop 2021-10-01 partition | ||
| spark.sql(s"alter table $tableName drop partition (dt='2021/10/01')") | ||
|
|
||
| checkAnswer(s"select dt from $tableName") (Seq(s"2021/10/02")) | ||
| val partitionPath = if (urlencode) { | ||
| PartitionPathEncodeUtils.escapePathName("2021/10/01") | ||
| } else { | ||
| "2021/10/01" | ||
| } | ||
| checkAnswer(s"select dt from $tableName")(Seq(s"2021/10/02")) | ||
| assertResult(true)(existsPath(s"${tmp.getCanonicalPath}/$tableName/$partitionPath")) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Seq(false, true).foreach { urlencode => | ||
| test(s"Purge drop single-partition table' partitions, urlencode: $urlencode") { | ||
| withTempDir { tmp => | ||
| val tableName = generateTableName | ||
| val tablePath = s"${tmp.getCanonicalPath}/$tableName" | ||
|
|
||
| import spark.implicits._ | ||
| val df = Seq((1, "z3", "v1", "2021/10/01"), (2, "l4", "v1", "2021/10/02")) | ||
| .toDF("id", "name", "ts", "dt") | ||
|
|
||
| df.write.format("hudi") | ||
| .option(HoodieWriteConfig.TBL_NAME.key, tableName) | ||
| .option(TABLE_TYPE.key, COW_TABLE_TYPE_OPT_VAL) | ||
| .option(RECORDKEY_FIELD.key, "id") | ||
| .option(PRECOMBINE_FIELD.key, "ts") | ||
| .option(PARTITIONPATH_FIELD.key, "dt") | ||
| .option(URL_ENCODE_PARTITIONING.key(), urlencode) | ||
| .option(KEYGENERATOR_CLASS_NAME.key, classOf[SimpleKeyGenerator].getName) | ||
| .option(HoodieWriteConfig.INSERT_PARALLELISM_VALUE.key, "1") | ||
| .option(HoodieWriteConfig.UPSERT_PARALLELISM_VALUE.key, "1") | ||
| .mode(SaveMode.Overwrite) | ||
| .save(tablePath) | ||
|
|
||
| // register meta to spark catalog by creating table | ||
| spark.sql( | ||
| s""" | ||
| |create table $tableName using hudi | ||
| |tblproperties ( | ||
| | primaryKey = 'id', | ||
| | preCombineField = 'ts' | ||
| |) | ||
| |partitioned by (dt) | ||
| |location '$tablePath' | ||
| |""".stripMargin) | ||
|
|
||
| // drop 2021-10-01 partition | ||
| spark.sql(s"alter table $tableName drop partition (dt='2021/10/01') purge") | ||
|
|
||
| val partitionPath = if (urlencode) { | ||
| PartitionPathEncodeUtils.escapePathName("2021/10/01") | ||
| } else { | ||
| "2021/10/01" | ||
| } | ||
| checkAnswer(s"select dt from $tableName")(Seq(s"2021/10/02")) | ||
| assertResult(false)(existsPath(s"${tmp.getCanonicalPath}/$tableName/$partitionPath")) | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -172,4 +226,51 @@ class TestAlterTableDropPartition extends TestHoodieSqlBase { | |
| } | ||
| } | ||
| } | ||
|
|
||
| Seq(false, true).foreach { hiveStyle => | ||
| test(s"Purge drop multi-level partitioned table's partitions, isHiveStylePartitioning: $hiveStyle") { | ||
| withTempDir { tmp => | ||
| val tableName = generateTableName | ||
| val tablePath = s"${tmp.getCanonicalPath}/$tableName" | ||
|
|
||
| import spark.implicits._ | ||
| val df = Seq((1, "z3", "v1", "2021", "10", "01"), (2, "l4", "v1", "2021", "10","02")) | ||
| .toDF("id", "name", "ts", "year", "month", "day") | ||
|
|
||
| df.write.format("hudi") | ||
| .option(HoodieWriteConfig.TBL_NAME.key, tableName) | ||
| .option(TABLE_TYPE.key, COW_TABLE_TYPE_OPT_VAL) | ||
| .option(RECORDKEY_FIELD.key, "id") | ||
| .option(PRECOMBINE_FIELD.key, "ts") | ||
| .option(PARTITIONPATH_FIELD.key, "year,month,day") | ||
| .option(HIVE_STYLE_PARTITIONING.key, hiveStyle) | ||
| .option(KEYGENERATOR_CLASS_NAME.key, classOf[ComplexKeyGenerator].getName) | ||
| .option(HoodieWriteConfig.INSERT_PARALLELISM_VALUE.key, "1") | ||
| .option(HoodieWriteConfig.UPSERT_PARALLELISM_VALUE.key, "1") | ||
| .mode(SaveMode.Overwrite) | ||
| .save(tablePath) | ||
|
|
||
| // register meta to spark catalog by creating table | ||
| spark.sql( | ||
| s""" | ||
| |create table $tableName using hudi | ||
| |tblproperties ( | ||
| | primaryKey = 'id', | ||
| | preCombineField = 'ts' | ||
| |) | ||
| |partitioned by (year, month, day) | ||
| |location '$tablePath' | ||
| |""".stripMargin) | ||
|
|
||
| // drop 2021-10-01 partition | ||
| spark.sql(s"alter table $tableName drop partition (year='2021', month='10', day='01') purge") | ||
|
|
||
| checkAnswer(s"select id, name, ts, year, month, day from $tableName")( | ||
| Seq(2, "l4", "v1", "2021", "10", "02") | ||
| ) | ||
| assertResult(false)(existsPath( | ||
| s"${tmp.getCanonicalPath}/$tableName/year='2021'/month='10'/day='01'")) | ||
|
||
| } | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would you please also add a test about dropping partition purge for non-partitioned table?