This is an automated email from the ASF dual-hosted git repository.
JingsongLi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git
The following commit(s) were added to refs/heads/master by this push:
new a8ffa234d7 [core] Handle missing snapshots in branch time travel
(#10860)
a8ffa234d7 is described below
commit a8ffa234d715fc229999bb04d59fa3bbe116b947
Author: LsomeYeah <[email protected]>
AuthorDate: Sat Nov 7 00:00:37 2026 +0800
[core] Handle missing snapshots in branch time travel (#10860)
---
docs/generated/core_configuration.html | 6 +
.../main/java/org/apache/paimon/CoreOptions.java | 11 +
.../paimon/table/FallbackReadFileStoreTable.java | 129 +++++++-
.../apache/paimon/table/FileStoreTableFactory.java | 25 +-
.../paimon/table/ChainTableFileStoreTableTest.java | 37 +++
.../table/FallbackReadFileStoreTableTest.java | 359 +++++++++++++++++++++
6 files changed, 552 insertions(+), 15 deletions(-)
diff --git a/docs/generated/core_configuration.html
b/docs/generated/core_configuration.html
index e7cabf15b6..bbeed02c58 100644
--- a/docs/generated/core_configuration.html
+++ b/docs/generated/core_configuration.html
@@ -1572,6 +1572,12 @@ For an internal format table in a REST catalog, it also
makes the catalog own th
<td>String</td>
<td>When a batch job queries from a table, if a partition does not
exist in the current branch, the reader will try to get this partition from
this fallback branch.</td>
</tr>
+ <tr>
+ <td><h5>scan.fallback-branch.allow-earliest-snapshot</h5></td>
+ <td style="word-wrap: break-word;">false</td>
+ <td>Boolean</td>
+ <td>Whether to allow reading the earliest available snapshot of a
fallback or primary branch when no snapshot exists at or before the current
branch's requested snapshot time. This applies to snapshot-ID time travel,
including numeric scan.version. By default, such reads fail unless the other
branch has no snapshots. Enabling this option may read data committed after the
requested time.</td>
+ </tr>
<tr>
<td><h5>scan.fallback-branch.read-fail-fast</h5></td>
<td style="word-wrap: break-word;">false</td>
diff --git a/paimon-api/src/main/java/org/apache/paimon/CoreOptions.java
b/paimon-api/src/main/java/org/apache/paimon/CoreOptions.java
index 4a76cbd236..4de6ea06a4 100644
--- a/paimon-api/src/main/java/org/apache/paimon/CoreOptions.java
+++ b/paimon-api/src/main/java/org/apache/paimon/CoreOptions.java
@@ -2400,6 +2400,17 @@ public class CoreOptions implements Serializable {
+ "falls through to the current branch,
which can mask data issues. "
+ "Set this to true to surface fallback
branch errors to the caller instead.");
+ public static final ConfigOption<Boolean>
SCAN_FALLBACK_BRANCH_ALLOW_EARLIEST_SNAPSHOT =
+ key("scan.fallback-branch.allow-earliest-snapshot")
+ .booleanType()
+ .defaultValue(false)
+ .withDescription(
+ "Whether to allow reading the earliest available
snapshot of a fallback or primary branch "
+ + "when no snapshot exists at or before
the current branch's requested snapshot time. "
+ + "This applies to snapshot-ID time
travel, including numeric scan.version. "
+ + "By default, such reads fail unless the
other branch has no snapshots. "
+ + "Enabling this option may read data
committed after the requested time.");
+
public static final ConfigOption<String> SCAN_PRIMARY_BRANCH =
key("scan.primary-branch")
.stringType()
diff --git
a/paimon-core/src/main/java/org/apache/paimon/table/FallbackReadFileStoreTable.java
b/paimon-core/src/main/java/org/apache/paimon/table/FallbackReadFileStoreTable.java
index 00fec793c5..da21a5a66c 100644
---
a/paimon-core/src/main/java/org/apache/paimon/table/FallbackReadFileStoreTable.java
+++
b/paimon-core/src/main/java/org/apache/paimon/table/FallbackReadFileStoreTable.java
@@ -88,6 +88,8 @@ public class FallbackReadFileStoreTable extends
DelegatedFileStoreTable {
private static final Logger LOG =
LoggerFactory.getLogger(FallbackReadFileStoreTable.class);
+ private static final long EMPTY_BRANCH_SNAPSHOT_ID = -1;
+
private final FileStoreTable other;
private final boolean wrappedFirst;
@@ -121,9 +123,29 @@ public class FallbackReadFileStoreTable extends
DelegatedFileStoreTable {
@Override
public FileStoreTable copy(TableSchema newTableSchema) {
+ Map<String, String> otherOptions = new
HashMap<>(newTableSchema.options());
+ String snapshotKey = CoreOptions.SCAN_SNAPSHOT_ID.key();
+ String versionKey = CoreOptions.SCAN_VERSION.key();
+ if (!coreOptions().isChainTable()
+ && wrapped.coreOptions().scanSnapshotId() != null
+ && other.coreOptions().scanSnapshotId() != null
+ && Objects.equals(otherOptions.get(snapshotKey),
wrapped.options().get(snapshotKey))
+ && Objects.equals(
+ otherOptions.get(versionKey),
wrapped.options().get(versionKey))) {
+ // A schema copy with the same selector keeps the already resolved
branch snapshot.
+ otherOptions.remove(snapshotKey);
+ otherOptions.remove(versionKey);
+ otherOptions = rewriteOtherOptions(otherOptions);
+ otherOptions.put(snapshotKey, other.options().get(snapshotKey));
+ } else {
+ otherOptions = rewriteOtherOptions(otherOptions);
+ }
+ if (!coreOptions().isChainTable()) {
+ otherOptions.put(CoreOptions.BRANCH.key(),
other.coreOptions().branch());
+ }
return new FallbackReadFileStoreTable(
wrapped.copy(newTableSchema),
-
other.copy(newTableSchema.copy(rewriteOtherOptions(newTableSchema.options()))),
+ other.copy(newTableSchema.copy(otherOptions)),
wrappedFirst);
}
@@ -222,13 +244,20 @@ public class FallbackReadFileStoreTable extends
DelegatedFileStoreTable {
long id = Long.parseLong(scanSnapshotId);
long millis = wrapped.snapshotManager().snapshot(id).timeMillis();
Snapshot otherSnapshot =
other.snapshotManager().earlierOrEqualTimeMills(millis);
- long otherId;
+ if (otherSnapshot == null && !coreOptions().isChainTable()) {
+ otherSnapshot = other.snapshotManager().earliestSnapshot();
+ }
if (otherSnapshot == null) {
- otherId = Snapshot.FIRST_SNAPSHOT_ID;
+ // Preserve the original snapshot-1 fallback for chain tables.
+ result.put(
+ scanSnapshotIdOptionKey,
+ String.valueOf(
+ coreOptions().isChainTable()
+ ? Snapshot.FIRST_SNAPSHOT_ID
+ : EMPTY_BRANCH_SNAPSHOT_ID));
} else {
- otherId = otherSnapshot.id();
+ result.put(scanSnapshotIdOptionKey,
String.valueOf(otherSnapshot.id()));
}
- result.put(scanSnapshotIdOptionKey, String.valueOf(otherId));
}
// bucket number of main branch and other branch are very likely
different,
@@ -249,10 +278,96 @@ public class FallbackReadFileStoreTable extends
DelegatedFileStoreTable {
}
public DataTableScan newFallbackScan(Function<FileStoreTable,
DataTableScan> scanCreator) {
- validateSchema();
+ boolean otherIsEmpty =
+ !coreOptions().isChainTable()
+ && Objects.equals(
+ other.coreOptions().scanSnapshotId(),
EMPTY_BRANCH_SNAPSHOT_ID);
+ if (!otherIsEmpty) {
+ validateSchema();
+ }
FileStoreTable first = wrappedFirst ? wrapped : other;
FileStoreTable second = wrappedFirst ? other : wrapped;
- return new FallbackReadScan(first, second, wrapped.schema(),
scanCreator);
+ if (coreOptions().isChainTable()) {
+ return new FallbackReadScan(first, second, wrapped.schema(),
scanCreator);
+ }
+ if (otherIsEmpty) {
+ LOG.warn(
+ "Skipping branch '{}' because it was empty when resolving
this read of branch '{}'.",
+ other.coreOptions().branch(),
+ wrapped.coreOptions().branch());
+ } else {
+ validateSnapshotTime();
+ }
+ return new FallbackReadScan(
+ first,
+ second,
+ wrapped.schema(),
+ table -> {
+ if (table == other && otherIsEmpty) {
+ return new EmptyBranchScan();
+ }
+ return scanCreator.apply(table);
+ });
+ }
+
+ private void validateSnapshotTime() {
+ Long mainId = wrapped.coreOptions().scanSnapshotId();
+ Long otherId = other.coreOptions().scanSnapshotId();
+ if (mainId == null || otherId == null) {
+ return;
+ }
+ Snapshot mainSnapshot = wrapped.snapshotManager().snapshot(mainId);
+ Snapshot otherSnapshot = other.snapshotManager().snapshot(otherId);
+ if (otherSnapshot.timeMillis() <= mainSnapshot.timeMillis()) {
+ return;
+ }
+ Preconditions.checkArgument(
+ coreOptions()
+ .toConfiguration()
+
.get(CoreOptions.SCAN_FALLBACK_BRANCH_ALLOW_EARLIEST_SNAPSHOT),
+ "Snapshot %s (time %s) of branch '%s' is later than the
requested snapshot %s (time %s). "
+ + "Set '%s' to true to allow reading it.",
+ otherId,
+ otherSnapshot.timeMillis(),
+ other.coreOptions().branch(),
+ mainId,
+ mainSnapshot.timeMillis(),
+
CoreOptions.SCAN_FALLBACK_BRANCH_ALLOW_EARLIEST_SNAPSHOT.key());
+ LOG.warn(
+ "Reading snapshot {} (time {}) of branch '{}' after the
requested time {} because '{}' is enabled.",
+ otherId,
+ otherSnapshot.timeMillis(),
+ other.coreOptions().branch(),
+ mainSnapshot.timeMillis(),
+
CoreOptions.SCAN_FALLBACK_BRANCH_ALLOW_EARLIEST_SNAPSHOT.key());
+ }
+
+ private static class EmptyBranchScan implements DataTableScan {
+
+ @Override
+ public DataTableScan withShard(int indexOfThisSubtask, int
numberOfParallelSubtasks) {
+ return this;
+ }
+
+ @Override
+ public InnerTableScan withFilter(Predicate predicate) {
+ return this;
+ }
+
+ @Override
+ public InnerTableScan withInternalPartitionFilter(PartitionPredicate
partitionPredicate) {
+ return this;
+ }
+
+ @Override
+ public Plan plan() {
+ return new DataFilePlan<>(Collections.emptyList());
+ }
+
+ @Override
+ public List<PartitionEntry> listPartitionEntries() {
+ return Collections.emptyList();
+ }
}
protected void validateSchema() {
diff --git
a/paimon-core/src/main/java/org/apache/paimon/table/FileStoreTableFactory.java
b/paimon-core/src/main/java/org/apache/paimon/table/FileStoreTableFactory.java
index e58fb03e41..3b01d44f1d 100644
---
a/paimon-core/src/main/java/org/apache/paimon/table/FileStoreTableFactory.java
+++
b/paimon-core/src/main/java/org/apache/paimon/table/FileStoreTableFactory.java
@@ -98,25 +98,34 @@ public class FileStoreTableFactory {
fileIO, tablePath, tableSchema, dynamicOptions,
catalogEnvironment);
Options options = new Options(table.options());
+ Options branchReadOptions = new Options(dynamicOptions.toMap());
+ // Reuse the resolved selector, including persisted options and
numeric scan.version.
+ if (options.contains(CoreOptions.SCAN_SNAPSHOT_ID)) {
+ branchReadOptions.set(
+ CoreOptions.SCAN_SNAPSHOT_ID,
options.get(CoreOptions.SCAN_SNAPSHOT_ID));
+ branchReadOptions.set(CoreOptions.SCAN_VERSION.key(), null);
+ }
String fallbackBranch = options.get(CoreOptions.SCAN_FALLBACK_BRANCH);
String primaryBranch = options.get(CoreOptions.SCAN_PRIMARY_BRANCH);
if (ChainTableUtils.isChainTable(options.toMap())) {
table = createChainTable(table, fileIO, tablePath, dynamicOptions,
catalogEnvironment);
} else if (!StringUtils.isNullOrWhitespaceOnly(fallbackBranch)) {
FileStoreTable otherTable =
- createOtherBranchTable(
- fileIO, tablePath, fallbackBranch, dynamicOptions,
catalogEnvironment);
+ createOtherBranchTable(fileIO, tablePath, fallbackBranch,
catalogEnvironment);
if (otherTable != null) {
- table = new FallbackReadFileStoreTable(table, otherTable,
true);
+ table =
+ new FallbackReadFileStoreTable(table, otherTable, true)
+ .copy(branchReadOptions.toMap());
} else {
LOG.error("Fallback branch {} not found for table {}",
fallbackBranch, tablePath);
}
} else if (!StringUtils.isNullOrWhitespaceOnly(primaryBranch)) {
FileStoreTable otherTable =
- createOtherBranchTable(
- fileIO, tablePath, primaryBranch, dynamicOptions,
catalogEnvironment);
+ createOtherBranchTable(fileIO, tablePath, primaryBranch,
catalogEnvironment);
if (otherTable != null) {
- table = new FallbackReadFileStoreTable(table, otherTable,
false);
+ table =
+ new FallbackReadFileStoreTable(table, otherTable,
false)
+ .copy(branchReadOptions.toMap());
} else {
LOG.error("Primary branch {} not found for table {}",
primaryBranch, tablePath);
}
@@ -188,9 +197,9 @@ public class FileStoreTableFactory {
FileIO fileIO,
Path tablePath,
String branchName,
- Options dynamicOptions,
CatalogEnvironment catalogEnvironment) {
- Options branchOptions = new Options(dynamicOptions.toMap());
+ // Apply dynamic options after wrapping so snapshot IDs are aligned
across branches.
+ Options branchOptions = new Options();
branchOptions.set(CoreOptions.BRANCH, branchName);
Optional<TableSchema> schema =
new FileSystemSchemaManager(fileIO, tablePath,
branchName).latest();
diff --git
a/paimon-core/src/test/java/org/apache/paimon/table/ChainTableFileStoreTableTest.java
b/paimon-core/src/test/java/org/apache/paimon/table/ChainTableFileStoreTableTest.java
index 28d047aad7..65a9446f91 100644
---
a/paimon-core/src/test/java/org/apache/paimon/table/ChainTableFileStoreTableTest.java
+++
b/paimon-core/src/test/java/org/apache/paimon/table/ChainTableFileStoreTableTest.java
@@ -122,6 +122,43 @@ public class ChainTableFileStoreTableTest {
tableName = "chain_t_" + uuid.replace("-", "");
}
+ @ParameterizedTest
+ @CsvSource({"false,false", "false,true", "true,false", "true,true"})
+ public void testTimeTravelKeepsChainSnapshotHandling(boolean group,
boolean deltaBeforeCopy)
+ throws Exception {
+ createChainTable(options -> {});
+ FileStoreTable table = loadTable();
+ FileStoreTable snapshot = table.switchToBranch(SNAPSHOT_BRANCH);
+ for (int i = 0; i < 2; i++) {
+ writeWithCommit(snapshot, row(1L, 1L, "snapshot", "CN",
"20250810", "20"));
+ writeWithCommit(table, row(3L, 1L, "main", "US", "20250810",
"20"));
+ }
+ table = loadTable();
+ long snapshotTime =
+ Math.max(
+ table.snapshotManager().snapshot(2).timeMillis(),
+ snapshot.snapshotManager().snapshot(2).timeMillis());
+ while (System.currentTimeMillis() <= snapshotTime) {
+ Thread.yield();
+ }
+ FileStoreTable delta = table.switchToBranch(DELTA_BRANCH);
+ if (deltaBeforeCopy) {
+ writeWithCommit(delta, row(2L, 1L, "delta", "CN", "20250810",
"21"));
+ }
+ FileStoreTable target = group ? ((ChainTableFileStoreTable)
table).other() : table;
+ FileStoreTable historical =
+
target.copy(Collections.singletonMap(CoreOptions.SCAN_SNAPSHOT_ID.key(), "2"));
+ if (!deltaBeforeCopy) {
+ writeWithCommit(delta, row(2L, 1L, "delta", "CN", "20250810",
"21"));
+ }
+
+ assertThat(getResult(historical, Collections.singletonMap("region",
"CN")))
+ .containsExactlyInAnyOrder(
+ row(1L, 1L, "snapshot", "CN", "20250810", "20"),
+ row(1L, 1L, "snapshot", "CN", "20250810", "21"),
+ row(2L, 1L, "delta", "CN", "20250810", "21"));
+ }
+
private static Stream<Arguments> filterPushdownParams() {
return Stream.of("parquet", "orc")
.flatMap(
diff --git
a/paimon-core/src/test/java/org/apache/paimon/table/FallbackReadFileStoreTableTest.java
b/paimon-core/src/test/java/org/apache/paimon/table/FallbackReadFileStoreTableTest.java
index 5c161a1a70..4952d0588d 100644
---
a/paimon-core/src/test/java/org/apache/paimon/table/FallbackReadFileStoreTableTest.java
+++
b/paimon-core/src/test/java/org/apache/paimon/table/FallbackReadFileStoreTableTest.java
@@ -70,7 +70,9 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.stream.Collectors;
@@ -366,6 +368,363 @@ public class FallbackReadFileStoreTableTest {
assertThat(readAndCollect((FallbackReadFileStoreTable) versioned, scan
-> {}))
.containsExactlyInAnyOrder(Pair.of(1, 10), Pair.of(2, 20));
+ FallbackReadFileStoreTable fromFactory =
+ createFromFactory(
+ mainTable,
+ "bc",
+ true,
+
Collections.singletonMap(CoreOptions.SCAN_VERSION.key(), "2"));
+
assertThat(fromFactory.other().coreOptions().scanSnapshotId()).isEqualTo(1L);
+ assertThat(readAndCollect(fromFactory, scan -> {}))
+ .containsExactlyInAnyOrder(Pair.of(1, 10), Pair.of(2, 20));
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ public void testFactoryWithPersistedSnapshot(boolean wrappedFirst) throws
Exception {
+ FileStoreTable mainTable = createTable();
+ writeDataIntoTable(mainTable, 0, rowData(1, 10));
+ mainTable.createBranch("bc");
+ FileStoreTable branchTable = createTableFromBranch(mainTable, "bc");
+ long firstSnapshotTime =
mainTable.snapshotManager().snapshot(1).timeMillis();
+ while (System.currentTimeMillis() <= firstSnapshotTime) {
+ Thread.yield();
+ }
+ writeDataIntoTable(branchTable, 0, rowData(3, 30));
+ writeDataIntoTable(mainTable, 1, rowData(2, 20));
+ long snapshotTime =
mainTable.snapshotManager().snapshot(2).timeMillis();
+ while (System.currentTimeMillis() <= snapshotTime) {
+ Thread.yield();
+ }
+ writeDataIntoTable(branchTable, 1, rowData(4, 40));
+ mainTable
+ .schemaManager()
+ .commitChanges(
+ SchemaChange.setOption(
+ (wrappedFirst
+ ?
CoreOptions.SCAN_FALLBACK_BRANCH
+ :
CoreOptions.SCAN_PRIMARY_BRANCH)
+ .key(),
+ "bc"),
+
SchemaChange.setOption(CoreOptions.SCAN_SNAPSHOT_ID.key(), "2"));
+
+ FallbackReadFileStoreTable historical =
+ (FallbackReadFileStoreTable)
FileStoreTableFactory.create(fileIO, tablePath);
+ assertThat(readAndCollect(historical, scan -> {}))
+ .containsExactlyInAnyOrder(Pair.of(1, 10), Pair.of(2, 20),
Pair.of(3, 30));
+
assertThat(historical.other().coreOptions().scanSnapshotId()).isEqualTo(1L);
+ historical =
+ (FallbackReadFileStoreTable)
+ historical.copy(
+ Collections.singletonMap(
+
CoreOptions.SCAN_FALLBACK_BRANCH_ALLOW_EARLIEST_SNAPSHOT
+ .key(),
+ "false"));
+ assertThat(readAndCollect(historical, scan -> {}))
+ .containsExactlyInAnyOrder(Pair.of(1, 10), Pair.of(2, 20),
Pair.of(3, 30));
+
+ Options overrides = new Options();
+ overrides.set(CoreOptions.PATH, tablePath.toString());
+ overrides.set(CoreOptions.SCAN_SNAPSHOT_ID.key(), "1");
+ assertThatThrownBy(() -> FileStoreTableFactory.create(fileIO,
overrides).newScan().plan())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("later than");
+ overrides.remove(CoreOptions.SCAN_SNAPSHOT_ID.key());
+ overrides.set(CoreOptions.SCAN_VERSION, "1");
+ assertThatThrownBy(() -> FileStoreTableFactory.create(fileIO,
overrides).newScan().plan())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("later than");
+ overrides.set(CoreOptions.SCAN_VERSION.key(), null);
+ overrides.set(CoreOptions.SCAN_SNAPSHOT_ID.key(), null);
+ assertThat(
+ readAndCollect(
+ (FallbackReadFileStoreTable)
+ FileStoreTableFactory.create(fileIO,
overrides),
+ scan -> {}))
+ .containsExactlyInAnyOrder(
+ Pair.of(1, 10), Pair.of(2, 20), Pair.of(3, 30),
Pair.of(4, 40));
+ }
+
+ @ParameterizedTest
+ @CsvSource({"false,false,false", "false,true,true", "true,false,true",
"true,true,false"})
+ public void testTimeTravelWithEmptyOtherBranch(
+ boolean wrappedFirst, boolean scanVersion, boolean allowEarliest)
throws Exception {
+ FileStoreTable mainTable = createTable();
+ writeDataIntoTable(mainTable, 0, rowData(1, 10));
+ mainTable.createBranch("empty");
+ FileStoreTable branchTable = createTableFromBranch(mainTable, "empty");
+ assertThat(branchTable.snapshotManager().latestSnapshotId()).isNull();
+ Map<String, String> options = new HashMap<>();
+ options.put(
+ scanVersion ? CoreOptions.SCAN_VERSION.key() :
CoreOptions.SCAN_SNAPSHOT_ID.key(),
+ "1");
+ options.put(
+ CoreOptions.SCAN_FALLBACK_BRANCH_ALLOW_EARLIEST_SNAPSHOT.key(),
+ String.valueOf(allowEarliest));
+
+ FallbackReadFileStoreTable latest =
+ new FallbackReadFileStoreTable(mainTable, branchTable,
wrappedFirst);
+ assertThat(readAndCollect(latest, scan ->
{})).containsExactly(Pair.of(1, 10));
+ FallbackReadFileStoreTable fromFactory =
+ createFromFactory(mainTable, "empty", wrappedFirst, options);
+
assertThat(fromFactory.other().coreOptions().scanSnapshotId()).isEqualTo(-1L);
+ assertThat(readAndCollect(fromFactory, scan ->
{})).containsExactly(Pair.of(1, 10));
+ FallbackReadFileStoreTable table = (FallbackReadFileStoreTable)
latest.copy(options);
+
assertThat(table.other().coreOptions().scanSnapshotId()).isEqualTo(-1L);
+
+ long snapshotTime =
mainTable.snapshotManager().snapshot(1).timeMillis();
+ while (System.currentTimeMillis() <= snapshotTime) {
+ Thread.yield();
+ }
+ writeDataIntoTable(branchTable, 0, rowData(2, 20));
+
+ assertThat(readAndCollect(fromFactory, scan ->
{})).containsExactly(Pair.of(1, 10));
+ assertThat(readAndCollect(table, scan ->
{})).containsExactly(Pair.of(1, 10));
+ assertThat(table.newScan().listPartitionEntries())
+ .map(entry -> entry.partition().getInt(0))
+ .containsExactly(1);
+ for (FileStoreTable copied :
+ Arrays.asList(
+ table.copy(Collections.emptyMap()),
+ table.copyWithoutTimeTravel(Collections.emptyMap()),
+ table.copyWithLatestSchema(),
+ table.copy(table.schema()),
+ InstantiationUtil.deserializeObject(
+ InstantiationUtil.serializeObject(table),
+ getClass().getClassLoader()))) {
+ assertThat(readAndCollect((FallbackReadFileStoreTable) copied,
scan -> {}))
+ .containsExactly(Pair.of(1, 10));
+ }
+ if (allowEarliest) {
+ FallbackReadFileStoreTable resolved = (FallbackReadFileStoreTable)
table.copy(options);
+
assertThat(resolved.other().coreOptions().scanSnapshotId()).isEqualTo(1L);
+ assertThat(readAndCollect(resolved, scan -> {}))
+ .containsExactlyInAnyOrder(Pair.of(1, 10), Pair.of(2, 20));
+ } else {
+ assertThatThrownBy(() -> table.copy(options).newScan().plan())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("later than");
+ }
+
+ options.clear();
+ options.put(CoreOptions.SCAN_SNAPSHOT_ID.key(), null);
+ options.put(CoreOptions.SCAN_VERSION.key(), null);
+ options.put(CoreOptions.SCAN_MODE.key(),
CoreOptions.StartupMode.LATEST_FULL.toString());
+ assertThat(readAndCollect((FallbackReadFileStoreTable)
table.copy(options), scan -> {}))
+ .containsExactlyInAnyOrder(Pair.of(1, 10), Pair.of(2, 20));
+ writeDataIntoTable(mainTable, 1, rowData(3, 30));
+ Map<String, String> newSelector =
+ Collections.singletonMap(CoreOptions.SCAN_SNAPSHOT_ID.key(),
"2");
+ for (FileStoreTable copied :
+ Arrays.asList(
+ table.copy(newSelector),
+ table.copy(
+ table.schema()
+ .copy(
+ new Options(table.options(),
newSelector)
+ .toMap())))) {
+ assertThat(readAndCollect((FallbackReadFileStoreTable) copied,
scan -> {}))
+ .containsExactlyInAnyOrder(Pair.of(1, 10), Pair.of(2, 20),
Pair.of(3, 30));
+ }
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ public void testSchemaValidationWithEmptyOtherBranch(boolean wrappedFirst)
throws Exception {
+ FileStoreTable mainTable = createTable();
+ writeDataIntoTable(mainTable, 0, rowData(1, 10));
+ mainTable.createBranch("bc");
+ FileStoreTable branchTable = createTableFromBranch(mainTable, "bc");
+ branchTable
+ .schemaManager()
+ .commitChanges(SchemaChange.addColumn("extra",
DataTypes.STRING()));
+ branchTable = branchTable.copyWithLatestSchema();
+ FallbackReadFileStoreTable historical =
+ createFromFactory(
+ mainTable,
+ "bc",
+ wrappedFirst,
+
Collections.singletonMap(CoreOptions.SCAN_SNAPSHOT_ID.key(), "1"));
+ assertThat(readAndCollect(historical, scan ->
{})).containsExactly(Pair.of(1, 10));
+ assertThat(historical.newScan().listPartitions()).hasSize(1);
+
+ writeDataIntoTable(branchTable, 0, rowData(2, 20, null));
+ writeDataIntoTable(mainTable, 1, rowData(3, 30));
+ assertThat(readAndCollect(historical, scan ->
{})).containsExactly(Pair.of(1, 10));
+ for (String snapshotId : Arrays.asList(null, "2")) {
+ FallbackReadFileStoreTable nonempty =
+ createFromFactory(
+ mainTable,
+ "bc",
+ wrappedFirst,
+ Collections.singletonMap(
+ CoreOptions.SCAN_SNAPSHOT_ID.key(),
snapshotId));
+ assertThatThrownBy(nonempty::newScan)
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("does not have the same row type");
+ }
+ }
+
+ @ParameterizedTest
+ @CsvSource({"false,false", "false,true", "true,false", "true,true"})
+ public void testTimeTravelBeforeOtherSnapshots(boolean wrappedFirst,
boolean expired)
+ throws Exception {
+ String policyKey =
CoreOptions.SCAN_FALLBACK_BRANCH_ALLOW_EARLIEST_SNAPSHOT.key();
+ FileStoreTable mainTable = createTable();
+ writeDataIntoTable(mainTable, 0, rowData(1, 10));
+ long snapshotTime =
mainTable.snapshotManager().snapshot(1).timeMillis();
+ mainTable.createBranch("bc");
+ FileStoreTable branchTable = createTableFromBranch(mainTable, "bc");
+ while (System.currentTimeMillis() <= snapshotTime) {
+ Thread.yield();
+ }
+ writeDataIntoTable(branchTable, 0, rowData(1, 100), rowData(2, 20));
+ if (expired) {
+ writeDataIntoTable(branchTable, 1, rowData(3, 30));
+ branchTable.snapshotManager().deleteSnapshot(1);
+ branchTable.snapshotManager().commitEarliestHint(2);
+ }
+ // The latest snapshot must differ from earliest; its extra row must
not be read.
+ writeDataIntoTable(branchTable, 2, rowData(4, 40));
+ if (expired) {
+ branchTable
+ .schemaManager()
+ .commitChanges(SchemaChange.addColumn("extra",
DataTypes.STRING()));
+ branchTable = branchTable.copyWithLatestSchema();
+ }
+ FallbackReadFileStoreTable table =
+ new FallbackReadFileStoreTable(mainTable, branchTable,
wrappedFirst);
+ Map<String, String> options = new HashMap<>();
+ options.put(CoreOptions.SCAN_SNAPSHOT_ID.key(), "1");
+ assertThatThrownBy(() -> table.copy(options).newScan().plan())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("later than");
+ assertThatThrownBy(
+ () ->
+ createFromFactory(mainTable, "bc",
wrappedFirst, options)
+ .newScan()
+ .plan())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("later than");
+
+ options.put(policyKey, "true");
+ FallbackReadFileStoreTable historical = (FallbackReadFileStoreTable)
table.copy(options);
+ long earliestId = expired ? 2L : 1L;
+
assertThat(historical.other().coreOptions().scanSnapshotId()).isEqualTo(earliestId);
+
assertThat(historical.other().rowType()).isEqualTo(mainTable.rowType());
+ List<Pair<Integer, Integer>> expected =
+ new ArrayList<>(Arrays.asList(Pair.of(1, wrappedFirst ? 10 :
100), Pair.of(2, 20)));
+ if (expired) {
+ expected.add(Pair.of(3, 30));
+ }
+ assertThat(readAndCollect(historical, scan -> {}))
+ .containsExactlyInAnyOrderElementsOf(expected);
+ FallbackReadFileStoreTable fromFactory =
+ createFromFactory(mainTable, "bc", wrappedFirst, options);
+
assertThat(fromFactory.other().coreOptions().scanSnapshotId()).isEqualTo(earliestId);
+
assertThat(fromFactory.other().rowType()).isEqualTo(mainTable.rowType());
+ assertThat(readAndCollect(fromFactory, scan -> {}))
+ .containsExactlyInAnyOrderElementsOf(expected);
+
+ options.remove(policyKey);
+ assertThat(
+ ((FallbackReadFileStoreTable) historical.copy(options))
+ .other()
+ .coreOptions()
+ .scanSnapshotId())
+ .isEqualTo(earliestId);
+ Map<String, String> schemaOptions = new
HashMap<>(historical.options());
+ schemaOptions.remove(policyKey);
+ List<FileStoreTable> strictCopies = new ArrayList<>();
+
strictCopies.add(historical.copy(historical.schema().copy(schemaOptions)));
+ for (String override : Arrays.asList("false", null)) {
+ options.put(policyKey, override);
+ strictCopies.add(historical.copy(options));
+ Map<String, String> policyOptions = new HashMap<>();
+ policyOptions.put(policyKey, override);
+ strictCopies.add(historical.copy(policyOptions));
+ strictCopies.add(historical.copyWithoutTimeTravel(policyOptions));
+ policyOptions.put(CoreOptions.SCAN_VERSION.key(), null);
+ strictCopies.add(historical.copy(policyOptions));
+ }
+ for (FileStoreTable strict : strictCopies) {
+ assertThatThrownBy(() -> strict.newScan().plan())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("later than");
+ }
+ }
+
+ @ParameterizedTest
+ @CsvSource({"false,1", "false,-1", "true,1", "true,-1"})
+ public void testExplicitMissingSnapshotIsNotReplaced(boolean expired,
String snapshotId)
+ throws Exception {
+ FileStoreTable table = createTable();
+ if (expired) {
+ writeDataIntoTable(table, 0, rowData(1, 10));
+ writeDataIntoTable(table, 1, rowData(2, 20));
+ table.snapshotManager().deleteSnapshot(1);
+ table.snapshotManager().commitEarliestHint(2);
+ }
+ Map<String, String> options = new HashMap<>();
+ options.put(CoreOptions.SCAN_SNAPSHOT_ID.key(), snapshotId);
+
options.put(CoreOptions.SCAN_FALLBACK_BRANCH_ALLOW_EARLIEST_SNAPSHOT.key(),
"true");
+
+ assertThatThrownBy(() -> table.copy(options).newScan().plan())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining(
+ expired
+ ? "out of available snapshotId range"
+ : "There is currently no snapshot.");
+ }
+
+ @ParameterizedTest
+ @CsvSource({"scan.tag-name,base", "scan.version,1"})
+ public void testTagTimeTravelWithoutActiveOtherSnapshots(String
scanOption, String tagName)
+ throws Exception {
+ FileStoreTable mainTable = createTable();
+ writeDataIntoTable(mainTable, 0, rowData(1, 10));
+ mainTable.createTag(tagName);
+ mainTable.createBranch("bc");
+ FileStoreTable branchTable = createTableFromBranch(mainTable, "bc");
+ writeDataIntoTable(branchTable, 0, rowData(2, 20));
+ branchTable.createTag(tagName);
+ branchTable.snapshotManager().deleteSnapshot(1);
+ branchTable.snapshotManager().deleteLatestHint();
+ assertThat(branchTable.snapshotManager().latestSnapshotId()).isNull();
+ FallbackReadFileStoreTable table =
+ (FallbackReadFileStoreTable)
+ new FallbackReadFileStoreTable(mainTable, branchTable,
true)
+ .copy(Collections.singletonMap(scanOption,
tagName));
+
+ assertThat(readAndCollect(table, scan -> {}))
+ .containsExactlyInAnyOrder(Pair.of(1, 10), Pair.of(2, 20));
+ assertThat(
+ readAndCollect(
+ createFromFactory(
+ mainTable,
+ "bc",
+ true,
+ Collections.singletonMap(scanOption,
tagName)),
+ scan -> {}))
+ .containsExactlyInAnyOrder(Pair.of(1, 10), Pair.of(2, 20));
+ }
+
+ private FallbackReadFileStoreTable createFromFactory(
+ FileStoreTable table,
+ String branch,
+ boolean wrappedFirst,
+ Map<String, String> options) {
+ Options dynamicOptions = new Options(options);
+ dynamicOptions.set(
+ wrappedFirst ? CoreOptions.SCAN_FALLBACK_BRANCH :
CoreOptions.SCAN_PRIMARY_BRANCH,
+ branch);
+ return (FallbackReadFileStoreTable)
+ FileStoreTableFactory.create(
+ fileIO,
+ tablePath,
+ table.schema(),
+ dynamicOptions,
+ CatalogEnvironment.empty());
}
private DataTableScan queryAuthScan(DataTableScan delegate,
TableQueryAuthResult authResult) {