Skip to content

Commit

Permalink
[Hudi] setCommitFileUpdates bug fix (#3309)
Browse files Browse the repository at this point in the history
<!--
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
-->

- [ ] Spark
- [ ] Standalone
- [ ] Flink
- [ ] Kernel
- [x] Other (Hudi read support)

## 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.
-->
This PR fixes a bug in the Delta->Hudi metadata conversion logic. When
the number of actions to convert is greater than the action batch size
(can be changed by user), the previous code incorrectly only converted
the last batch instead of converting all batches.
## How was this patch tested?
Unit test
<!--
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.
-->

## 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
anniewang-db authored Jun 27, 2024
1 parent c395a63 commit 47871d8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ class HudiConversionTransaction(
private var metaClient = providedMetaClient
private val instantTime = convertInstantToCommit(
Instant.ofEpochMilli(postCommitSnapshot.timestamp))
private var writeStatuses: util.List[WriteStatus] = Collections.emptyList[WriteStatus]
private var writeStatuses: util.List[WriteStatus] =
new util.ArrayList[WriteStatus]()
private var partitionToReplacedFileIds: util.Map[String, util.List[String]] =
Collections.emptyMap[String, util.List[String]]
new util.HashMap[String, util.List[String]]()

private val version = postCommitSnapshot.version
/** Tracks if this transaction has already committed. You can only commit once. */
Expand All @@ -101,7 +102,7 @@ class HudiConversionTransaction(
def setCommitFileUpdates(actions: scala.collection.Seq[Action]): Unit = {
// for all removed files, group by partition path and then map to
// the file group ID (name in this case)
partitionToReplacedFileIds = actions
val newPartitionToReplacedFileIds = actions
.map(_.wrap)
.filter(action => action.remove != null)
.map(_.remove)
Expand All @@ -111,15 +112,17 @@ class HudiConversionTransaction(
(partitionPath, path.getName)})
.groupBy(_._1).map(v => (v._1, v._2.map(_._2).asJava))
.asJava
partitionToReplacedFileIds.putAll(newPartitionToReplacedFileIds)
// Convert the AddFiles to write statuses for the commit
writeStatuses = actions
val newWriteStatuses = actions
.map(_.wrap)
.filter(action => action.add != null)
.map(_.add)
.map(add => {
convertAddFile(add, tablePath, instantTime)
})
.asJava
writeStatuses.addAll(newWriteStatuses)
}

def commit(): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import org.apache.spark.sql.avro.SchemaConverters
import org.apache.spark.sql.delta.DeltaOperations.Truncate
import org.apache.spark.sql.delta.{DeltaConfigs, DeltaLog, DeltaUnsupportedOperationException, OptimisticTransaction}
import org.apache.spark.sql.delta.actions.{Action, AddFile, Metadata, RemoveFile}
import org.apache.spark.sql.delta.sources.DeltaSQLConf
import org.apache.spark.sql.types.StructType
import org.apache.spark.util.{ManualClock, Utils}
import org.scalatest.concurrent.Eventually
Expand Down Expand Up @@ -215,6 +216,25 @@ class ConvertToHudiSuite extends QueryTest with Eventually {
verifyFilesAndSchemaMatch()
}

test("all batches of actions are converted") {
withSQLConf(
DeltaSQLConf.HUDI_MAX_COMMITS_TO_CONVERT.key -> "3"
) {
_sparkSession.sql(
s"""CREATE TABLE `$testTableName` (col1 INT)
| USING DELTA
|LOCATION '$testTablePath'""".stripMargin)
for (i <- 1 to 10) {
_sparkSession.sql(s"INSERT INTO `$testTableName` VALUES ($i)")
}
_sparkSession.sql(
s"""ALTER TABLE `$testTableName` SET TBLPROPERTIES (
| 'delta.universalFormat.enabledFormats' = 'hudi'
|)""".stripMargin)
verifyFilesAndSchemaMatch()
}
}

def buildHudiMetaClient(): HoodieTableMetaClient = {
val hadoopConf: Configuration = _sparkSession.sparkContext.hadoopConfiguration
val storageConf : StorageConfiguration[_] = new HadoopStorageConfiguration(hadoopConf)
Expand Down

0 comments on commit 47871d8

Please sign in to comment.