Skip to content
Closed
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: 12 additions & 0 deletions api/src/main/java/org/apache/iceberg/TableScan.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ public interface TableScan extends Scan<TableScan, FileScanTask, CombinedScanTas
*/
TableScan asOfTime(long timestampMillis);

/**
* Create a new {@link TableScan} from this scan's configuration that will use the snapshot
* referenced by a tag.
*
* @param tag a snapshot ref of type tag.
* @return a new scan based on this with the given snapshot ref tag
* @throws IllegalArgumentException if the tag cannot be found
*/
default TableScan useTag(String tag) {
throw new UnsupportedOperationException("Scanning from a tag is not supported");
}

/**
* Create a new {@link TableScan} from this that will read the given data columns. This produces
* an expected schema that includes all fields that are either selected or used by this scan's
Expand Down
8 changes: 8 additions & 0 deletions core/src/main/java/org/apache/iceberg/DataTableScan.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ public TableScan useSnapshot(long scanSnapshotId) {
tableOps(), table(), snapshotSchema, context().useSnapshotId(scanSnapshotId));
}

@Override
public TableScan useTag(String tag) {
Preconditions.checkArgument(tag != null, "Tag does not exist: %s", tag);
Preconditions.checkArgument(
table().refs().get(tag).isTag(), "Ref %s is a branch, not a tag", tag);
return useSnapshot(table().snapshot(tag).snapshotId());
Copy link
Contributor

@hililiwei hililiwei Aug 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    SnapshotRef ref = tableOps().current().ref(tag);
    Preconditions.checkArgument(ref != null, "Tag does not exist: %s", tag);
    Preconditions.checkArgument(ref.isTag(), "Ref %s is a tag not a branch", tag);

The error message is modified to be consistent with that in UpdateSnapshotReferencesOperations.

public UpdateSnapshotReferencesOperation removeTag(String name) {
Preconditions.checkNotNull(name, "Tag name cannot be null");
SnapshotRef ref = updatedRefs.remove(name);
Preconditions.checkArgument(ref != null, "Tag does not exist: %s", name);
Preconditions.checkArgument(ref.isTag(), "Ref %s is a branch not a tag", name);
return this;
}
public UpdateSnapshotReferencesOperation renameBranch(String name, String newName) {
Preconditions.checkNotNull(name, "Branch to rename cannot be null");
Preconditions.checkNotNull(newName, "New branch name cannot be null");
Preconditions.checkArgument(!name.equals(SnapshotRef.MAIN_BRANCH), "Cannot rename main branch");
SnapshotRef ref = updatedRefs.get(name);
Preconditions.checkArgument(ref != null, "Branch does not exist: %s", name);
Preconditions.checkArgument(ref.isBranch(), "Ref %s is a tag not a branch", name);
SnapshotRef existing = updatedRefs.put(newName, ref);
Preconditions.checkArgument(existing == null, "Ref %s already exists", newName);
updatedRefs.remove(name, ref);
return this;

}

@Override
protected TableScan newRefinedScan(
TableOperations ops, Table table, Schema schema, TableScanContext context) {
Expand Down