Skip to content

Commit

Permalink
remove created_from_snapshot field from BranchesTable
Browse files Browse the repository at this point in the history
  • Loading branch information
herefree committed Sep 10, 2024
1 parent 28e02a9 commit 16b5297
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.apache.paimon.table.source.SingletonSplit;
import org.apache.paimon.table.source.Split;
import org.apache.paimon.table.source.TableRead;
import org.apache.paimon.types.BigIntType;
import org.apache.paimon.types.DataField;
import org.apache.paimon.types.RowType;
import org.apache.paimon.types.TimestampType;
Expand Down Expand Up @@ -70,7 +69,6 @@
import static org.apache.paimon.utils.BranchManager.BRANCH_PREFIX;
import static org.apache.paimon.utils.BranchManager.branchPath;
import static org.apache.paimon.utils.FileUtils.listVersionedDirectories;
import static org.apache.paimon.utils.Preconditions.checkArgument;

/** A {@link Table} for showing branches of table. */
public class BranchesTable implements ReadonlyTable {
Expand All @@ -86,8 +84,7 @@ public class BranchesTable implements ReadonlyTable {
0, "branch_name", SerializationUtils.newStringType(false)),
new DataField(
1, "created_from_tag", SerializationUtils.newStringType(true)),
new DataField(2, "created_from_snapshot", new BigIntType(true)),
new DataField(3, "create_time", new TimestampType(false, 3))));
new DataField(2, "create_time", new TimestampType(false, 3))));

private final FileIO fileIO;
private final Path location;
Expand Down Expand Up @@ -235,7 +232,6 @@ private List<InternalRow> branches(FileStoreTable table) throws IOException {
for (Pair<Path, Long> path : paths) {
String branchName = path.getLeft().getName().substring(BRANCH_PREFIX.length());
String basedTag = null;
Long basedSnapshotId = null;
long creationTime = path.getRight();

Optional<TableSchema> tableSchema =
Expand All @@ -247,20 +243,14 @@ private List<InternalRow> branches(FileStoreTable table) throws IOException {
SortedMap<Snapshot, List<String>> snapshotTags =
branchTable.tagManager().tags();
Long earliestSnapshotId = branchTable.snapshotManager().earliestSnapshotId();
if (snapshotTags.isEmpty()) {
// create based on snapshotId
basedSnapshotId = earliestSnapshotId;
} else {
if (!snapshotTags.isEmpty()) {
Snapshot snapshot = snapshotTags.firstKey();
if (Objects.equals(earliestSnapshotId, snapshot.id())) {
// create based on tag
List<String> tags = snapshotTags.get(snapshot);
checkArgument(tags.size() == 1);
if (earliestSnapshotId >= snapshot.id()) {
List<String> tags =
branchTable
.tagManager()
.sortTagsOfOneSnapshot(snapshotTags.get(snapshot));
basedTag = tags.get(0);
basedSnapshotId = snapshot.id();
} else {
// create based on snapshotId
basedSnapshotId = earliestSnapshotId;
}
}
}
Expand All @@ -269,7 +259,6 @@ private List<InternalRow> branches(FileStoreTable table) throws IOException {
GenericRow.of(
BinaryString.fromString(branchName),
BinaryString.fromString(basedTag),
basedSnapshotId,
Timestamp.fromLocalDateTime(
DateTimeUtils.toLocalDateTime(creationTime))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,34 @@ public void testTagsTable() throws Exception {
assertThat(result).containsExactly(Row.of("tag1", 1L, 0L, 1L));
}

@Test
public void testBranchesTable() throws Exception {
sql("CREATE TABLE T (a INT, b INT)");
sql("INSERT INTO T VALUES (1, 2)");
sql("INSERT INTO T VALUES (3, 4)");

paimonTable("T").createTag("tag1", 1);

paimonTable("T").createBranch("branch1", "tag1");

paimonTable("T").createBranch("branch2", "tag1");
paimonTable("T$branch_branch2").createTag("tag_branch2", 1);

paimonTable("T").createBranch("branch3");
sql("INSERT INTO T$branch_branch3 VALUES (3, 4)");
sql("INSERT INTO T$branch_branch3 VALUES (2, 4)");
paimonTable("T$branch_branch3").createTag("tag_branch1", 2);

List<Row> result =
sql("SELECT branch_name, created_from_tag FROM T$branches ORDER BY branch_name");

assertThat(result)
.containsExactly(
Row.of("branch1", "tag1"),
Row.of("branch2", "tag1"),
Row.of("branch3", null));
}

@Test
public void testConsumersTable() throws Exception {
batchSql("CREATE TABLE T (a INT, b INT)");
Expand Down

0 comments on commit 16b5297

Please sign in to comment.