Skip to content

Commit

Permalink
[Spark] Move some ICT test helper utils to InCommitTimestampTestUtils…
Browse files Browse the repository at this point in the history
….scala (#3843)

<!--
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
For code hygiene, we move some ICT test helper utils from the `ICTSuite`
to the object `InCommitTimestampTestUtils`, which is dedicated to
contain the ICT test utils.

<!--
- 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.
-->

## How was this patch tested?
Existing UTs.
<!--
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?
No.
<!--
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'.
-->
  • Loading branch information
longvu-db authored Nov 5, 2024
1 parent 6ae4b62 commit 796f518
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,13 @@ class InCommitTimestampSuite
with DeltaTestUtilsBase
with CoordinatedCommitsTestUtils
with StreamTest {
import InCommitTimestampTestUtils._

override def beforeAll(): Unit = {
super.beforeAll()
spark.conf.set(DeltaConfigs.IN_COMMIT_TIMESTAMPS_ENABLED.defaultTablePropertyKey, "true")
}

private def getInCommitTimestamp(deltaLog: DeltaLog, version: Long): Long = {
val deltaFile = DeltaCommitFileProvider(deltaLog.unsafeVolatileSnapshot).deltaFile(version)
val commitInfo = DeltaHistoryManager.getCommitInfoOpt(
deltaLog.store,
deltaFile,
deltaLog.newDeltaHadoopConf())
commitInfo.get.inCommitTimestamp.get
}

private def getFileModificationTimesMap(
deltaLog: DeltaLog, start: Long, end: Long): Map[Long, Long] = {
deltaLog.store.listFrom(
FileNames.listingPrefix(deltaLog.logPath, start), deltaLog.newDeltaHadoopConf())
.collect { case FileNames.DeltaFile(fs, v) => v -> fs.getModificationTime }
.takeWhile(_._1 <= end)
.toMap
}

test("Enable ICT on commit 0") {
withTempDir { tempDir =>
spark.range(10).write.format("delta").save(tempDir.getAbsolutePath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package org.apache.spark.sql.delta

import org.apache.spark.sql.delta.actions.{Action, CommitInfo}
import org.apache.spark.sql.delta.util.{FileNames, JsonUtils}
import org.apache.spark.sql.delta.util.{DeltaCommitFileProvider, FileNames, JsonUtils}
import org.apache.hadoop.fs.Path

object InCommitTimestampTestUtils {
Expand Down Expand Up @@ -55,4 +55,32 @@ object InCommitTimestampTestUtils {
overwrite = true,
deltaLog.newDeltaHadoopConf())
}

/**
* Retrieves the in-commit timestamp for a specific version of the Delta Log.
*/
def getInCommitTimestamp(deltaLog: DeltaLog, version: Long): Long = {
val deltaFile = DeltaCommitFileProvider(deltaLog.unsafeVolatileSnapshot).deltaFile(version)
val commitInfo = DeltaHistoryManager.getCommitInfoOpt(
deltaLog.store,
deltaFile,
deltaLog.newDeltaHadoopConf())
assert(commitInfo.isDefined, s"CommitInfo should exist for version $version")
assert(commitInfo.get.inCommitTimestamp.isDefined,
s"InCommitTimestamp should exist for CommitInfo's version $version")
commitInfo.get.inCommitTimestamp.get
}

/**
* Retrieves a map of file modification times for Delta Log versions within a specified version
* range.
*/
def getFileModificationTimesMap(
deltaLog: DeltaLog, start: Long, end: Long): Map[Long, Long] = {
deltaLog.store.listFrom(
FileNames.listingPrefix(deltaLog.logPath, start), deltaLog.newDeltaHadoopConf())
.collect { case FileNames.DeltaFile(fs, v) => v -> fs.getModificationTime }
.takeWhile(_._1 <= end)
.toMap
}
}

0 comments on commit 796f518

Please sign in to comment.