Skip to content
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

[core]delete created_from_snapshot and created_from_snapshot from branchTable #4159

Merged
merged 5 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions docs/content/maintenance/system-tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,12 @@ You can query the branches of the table.
SELECT * FROM my_table$branches;

/*
+----------------------+---------------------------+--------------------------+-------------------------+
| branch_name | created_from_tag | created_from_snapshot | create_time |
+----------------------+---------------------------+--------------------------+-------------------------+
| branch1 | tag1 | 2 | 2024-07-18 20:31:39.084 |
| branch2 | tag2 | 5 | 2024-07-18 21:11:14.373 |
+----------------------+---------------------------+--------------------------+-------------------------+
+----------------------+-------------------------+
| branch_name | create_time |
+----------------------+-------------------------+
| branch1 | 2024-07-18 20:31:39.084 |
| branch2 | 2024-07-18 21:11:14.373 |
+----------------------+-------------------------+
2 rows in set
*/
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package org.apache.paimon.table.system;

import org.apache.paimon.Snapshot;
import org.apache.paimon.data.BinaryString;
import org.apache.paimon.data.GenericRow;
import org.apache.paimon.data.InternalRow;
Expand All @@ -28,8 +27,6 @@
import org.apache.paimon.fs.Path;
import org.apache.paimon.predicate.Predicate;
import org.apache.paimon.reader.RecordReader;
import org.apache.paimon.schema.SchemaManager;
import org.apache.paimon.schema.TableSchema;
import org.apache.paimon.table.FileStoreTable;
import org.apache.paimon.table.FileStoreTableFactory;
import org.apache.paimon.table.ReadonlyTable;
Expand All @@ -40,7 +37,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 All @@ -62,15 +58,11 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.SortedMap;
import java.util.stream.Collectors;

import static org.apache.paimon.catalog.Catalog.SYSTEM_TABLE_SPLITTER;
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 @@ -84,10 +76,7 @@ public class BranchesTable implements ReadonlyTable {
Arrays.asList(
new DataField(
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(1, "create_time", new TimestampType(false, 3))));

private final FileIO fileIO;
private final Path location;
Expand All @@ -113,7 +102,7 @@ public RowType rowType() {

@Override
public List<String> primaryKeys() {
return Arrays.asList("branch_name", "tag_name");
return Collections.singletonList("branch_name");
}

@Override
Expand Down Expand Up @@ -227,7 +216,6 @@ public RecordReader<InternalRow> createReader(Split split) {

private List<InternalRow> branches(FileStoreTable table) throws IOException {
BranchManager branchManager = table.branchManager();
SchemaManager schemaManager = new SchemaManager(fileIO, table.location());

List<Pair<Path, Long>> paths =
listVersionedDirectories(fileIO, branchManager.branchDirectory(), BRANCH_PREFIX)
Expand All @@ -237,42 +225,10 @@ 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 =
schemaManager.copyWithBranch(branchName).latest();
if (tableSchema.isPresent()) {
FileStoreTable branchTable =
FileStoreTableFactory.create(
fileIO, new Path(branchPath(table.location(), branchName)));
SortedMap<Snapshot, List<String>> snapshotTags =
branchTable.tagManager().tags();
Long earliestSnapshotId = branchTable.snapshotManager().earliestSnapshotId();
if (snapshotTags.isEmpty()) {
// create based on snapshotId
basedSnapshotId = earliestSnapshotId;
} else {
Snapshot snapshot = snapshotTags.firstKey();
if (Objects.equals(earliestSnapshotId, snapshot.id())) {
// create based on tag
List<String> tags = snapshotTags.get(snapshot);
checkArgument(tags.size() == 1);
basedTag = tags.get(0);
basedSnapshotId = snapshot.id();
} else {
// create based on snapshotId
basedSnapshotId = earliestSnapshotId;
}
}
}

result.add(
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 @@ -320,13 +320,13 @@ public void testCreateDeleteAndForwardBranch() throws Exception {
sql("CALL sys.create_branch('default.T', 'test', 'tag1')");
sql("CALL sys.create_branch('default.T', 'test2', 'tag2')");

assertThat(collectToString("SELECT branch_name, created_from_snapshot FROM `T$branches`"))
.containsExactlyInAnyOrder("+I[test, 1]", "+I[test2, 2]");
assertThat(collectToString("SELECT branch_name FROM `T$branches`"))
.containsExactlyInAnyOrder("+I[test]", "+I[test2]");

sql("CALL sys.delete_branch('default.T', 'test')");

assertThat(collectToString("SELECT branch_name, created_from_snapshot FROM `T$branches`"))
.containsExactlyInAnyOrder("+I[test2, 2]");
assertThat(collectToString("SELECT branch_name FROM `T$branches`"))
.containsExactlyInAnyOrder("+I[test2]");

sql("CALL sys.fast_forward('default.T', 'test2')");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,44 +194,13 @@ public void testDeleteBranchTable() throws Exception {
sql("CALL sys.create_branch('default.T', 'test', 'tag1')");
sql("CALL sys.create_branch('default.T', 'test2', 'tag2')");

assertThat(collectResult("SELECT branch_name, created_from_snapshot FROM `T$branches`"))
.containsExactlyInAnyOrder("+I[test, 1]", "+I[test2, 2]");
assertThat(collectResult("SELECT branch_name FROM `T$branches`"))
.containsExactlyInAnyOrder("+I[test]", "+I[test2]");

sql("CALL sys.delete_branch('default.T', 'test')");

assertThat(collectResult("SELECT branch_name, created_from_snapshot FROM `T$branches`"))
.containsExactlyInAnyOrder("+I[test2, 2]");
}

@Test
public void testBranchManagerGetBranchSnapshotsList() throws Exception {
sql(
"CREATE TABLE T ("
+ " pt INT"
+ ", k INT"
+ ", v STRING"
+ ", PRIMARY KEY (pt, k) NOT ENFORCED"
+ " ) PARTITIONED BY (pt) WITH ("
+ " 'bucket' = '2'"
+ " )");

sql("INSERT INTO T VALUES (1, 10, 'hxh')");
sql("INSERT INTO T VALUES (1, 20, 'hxh')");
sql("INSERT INTO T VALUES (1, 30, 'hxh')");

FileStoreTable table = paimonTable("T");
checkSnapshots(table.snapshotManager(), 1, 3);

sql("CALL sys.create_tag('default.T', 'tag1', 1)");
sql("CALL sys.create_tag('default.T', 'tag2', 2)");
sql("CALL sys.create_tag('default.T', 'tag3', 3)");

sql("CALL sys.create_branch('default.T', 'test1', 'tag1')");
sql("CALL sys.create_branch('default.T', 'test2', 'tag2')");
sql("CALL sys.create_branch('default.T', 'test3', 'tag3')");

assertThat(collectResult("SELECT created_from_snapshot FROM `T$branches`"))
.containsExactlyInAnyOrder("+I[1]", "+I[2]", "+I[3]");
assertThat(collectResult("SELECT branch_name FROM `T$branches`"))
.containsExactlyInAnyOrder("+I[test2]");
}

@Test
Expand Down
Loading