Skip to content
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
2 changes: 1 addition & 1 deletion core/src/main/java/org/apache/iceberg/SnapshotScan.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected Map<Integer, PartitionSpec> specs() {
ImmutableMap.Builder<Integer, PartitionSpec> newSpecs =
ImmutableMap.builderWithExpectedSize(specs.size());
for (Map.Entry<Integer, PartitionSpec> entry : specs.entrySet()) {
newSpecs.put(entry.getKey(), entry.getValue().toUnbound().bind(snapshotSchema));
newSpecs.put(entry.getKey(), entry.getValue().toUnbound().bind(snapshotSchema, true));
}

return newSpecs.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,45 @@ public void testPartitionSourceRename() throws IOException {
assertThat(tasks).hasSize(1);
}

@TestTemplate
public void testPartitionSourceAdd() throws IOException {
Table table = TestTables.create(temp, "test", SCHEMA, SPEC, formatVersion);

DataFile fileOne = createDataFile("one");
DataFile fileTwo = createDataFile("two");

table.newAppend().appendFile(fileOne).appendFile(fileTwo).commit();
long firstSnapshotId = table.currentSnapshot().snapshotId();

List<FileScanTask> tasks =
Lists.newArrayList(table.newScan().filter(Expressions.equal("part", "one")).planFiles());

assertThat(tasks).hasSize(1);

// add a new partition column
table.updateSchema().addColumn("hour", Types.IntegerType.get()).commit();
table.updateSpec().addField("hour").commit();

// plan the scan using the new column in a filter
tasks = Lists.newArrayList(table.newScan().filter(Expressions.isNull("hour")).planFiles());

assertThat(tasks).hasSize(2);

// create a new commit
table.newAppend().appendFile(createDataFile("three")).commit();

// plan the scan using the existing column in a filter
tasks =
Lists.newArrayList(
table
.newScan()
.useSnapshot(firstSnapshotId)
.filter(Expressions.equal("part", "one"))
.planFiles());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The planFiles() fails with the below exception without this PR's change:

Cannot find source column for partition field: 1001: hour: identity(4)
org.apache.iceberg.exceptions.ValidationException: Cannot find source column for partition field: 1001: hour: identity(4)
	at org.apache.iceberg.exceptions.ValidationException.check(ValidationException.java:49)
	at org.apache.iceberg.PartitionSpec.checkCompatibility(PartitionSpec.java:656)
	at org.apache.iceberg.PartitionSpec$Builder.build(PartitionSpec.java:628)
	at org.apache.iceberg.PartitionSpec$Builder.build(PartitionSpec.java:623)
	at org.apache.iceberg.UnboundPartitionSpec.bind(UnboundPartitionSpec.java:46)
	at org.apache.iceberg.SnapshotScan.specs(SnapshotScan.java:98)
	at org.apache.iceberg.DataTableScan.doPlanFiles(DataTableScan.java:77)
	at org.apache.iceberg.SnapshotScan.planFiles(SnapshotScan.java:161)


assertThat(tasks).hasSize(1);
}

@TestTemplate
public void testPartitionSourceDrop() throws IOException {
Table table = TestTables.create(temp, "test", SCHEMA, SPEC, formatVersion);
Expand Down Expand Up @@ -179,6 +218,37 @@ public void testPartitionSourceDrop() throws IOException {
.collect(Collectors.toList()));
}

@TestTemplate
public void testColumnAdd() throws IOException {
Table table = TestTables.create(temp, "test", SCHEMA, SPEC, formatVersion);

DataFile fileOne = createDataFile("one");
DataFile fileTwo = createDataFile("two");

table.newAppend().appendFile(fileOne).appendFile(fileTwo).commit();
long firstSnapshotId = table.currentSnapshot().snapshotId();

table.updateSchema().addColumn("hour", Types.IntegerType.get()).commit();

DataFile fileThree = createDataFile("three", table.schema(), table.spec());
table.newAppend().appendFile(fileThree).commit();

// plan the scan using the new column in a filter
List<FileScanTask> tasks =
Lists.newArrayList(table.newScan().filter(Expressions.isNull("hour")).planFiles());
assertThat(tasks).hasSize(3);

// plan the scan using the existing column in a filter
tasks =
Lists.newArrayList(
table
.newScan()
.useSnapshot(firstSnapshotId)
.filter(Expressions.equal("data", "xyz"))
.planFiles());
assertThat(tasks).hasSize(2);
}

@TestTemplate
public void testColumnRename() throws IOException {
Table table = TestTables.create(temp, "test", SCHEMA, SPEC, formatVersion);
Expand Down