Skip to content

Commit

Permalink
Core: Include summary without 'operation' when comparing view versions
Browse files Browse the repository at this point in the history
  • Loading branch information
nastra committed Sep 29, 2023
1 parent 8062aef commit 8ae63c2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
10 changes: 8 additions & 2 deletions core/src/main/java/org/apache/iceberg/view/ViewMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,20 @@ private int reuseOrCreateNewViewVersionId(ViewVersion viewVersion) {

/**
* Checks whether the given view versions would behave the same while ignoring the view version
* id, the creation timestamp, and the summary.
* id, the creation timestamp, and the operation.
*
* @param one the view version to compare
* @param two the view version to compare
* @return true if the given view versions would behave the same
*/
private boolean sameViewVersion(ViewVersion one, ViewVersion two) {
return Objects.equals(one.representations(), two.representations())
// ignore the "operation" contained in the summary for the comparison
Map<String, String> summaryOne = Maps.newHashMap(one.summary());
Map<String, String> summaryTwo = Maps.newHashMap(two.summary());
summaryOne.remove("operation");
summaryTwo.remove("operation");
return Objects.equals(summaryOne, summaryTwo)
&& Objects.equals(one.representations(), two.representations())
&& Objects.equals(one.defaultCatalog(), two.defaultCatalog())
&& Objects.equals(one.defaultNamespace(), two.defaultNamespace())
&& one.schemaId() == two.schemaId();
Expand Down
40 changes: 40 additions & 0 deletions core/src/test/java/org/apache/iceberg/view/TestViewMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,46 @@ public void viewVersionDeduplication() {
ImmutableViewVersion.builder().from(viewVersionThree).versionId(3).build());
}

@Test
public void viewVersionDeduplicationWithCustomSummary() {
// all view versions have the same ID
// additionally, there are duplicate view versions that only differ in the summary
ViewVersion viewVersionOne = newViewVersion(1, "select * from ns.tbl");
ViewVersion viewVersionTwo = newViewVersion(1, "select count(*) from ns.tbl");
ViewVersion viewVersionOneUpdated =
ImmutableViewVersion.builder()
.from(viewVersionOne)
.summary(ImmutableMap.of("operation", "replace"))
.build();
ViewVersion viewVersionTwoUpdated =
ImmutableViewVersion.builder()
.from(viewVersionTwo)
.summary(ImmutableMap.of("operation", "replace", "key", "val"))
.build();

ViewMetadata viewMetadata =
ViewMetadata.builder()
.setLocation("custom-location")
.addSchema(new Schema(Types.NestedField.required(1, "x", Types.LongType.get())))
.addVersion(viewVersionOne)
.addVersion(viewVersionTwo)
.addVersion(viewVersionOneUpdated)
.addVersion(viewVersionTwoUpdated)
.setCurrentVersionId(3)
.build();

assertThat(viewMetadata.currentVersion())
.isEqualTo(ImmutableViewVersion.builder().from(viewVersionTwoUpdated).versionId(3).build());

// IDs of the view versions should be re-assigned and view versions should be de-duplicated
assertThat(viewMetadata.versions())
.hasSize(3)
.containsExactly(
viewVersionOne,
ImmutableViewVersion.builder().from(viewVersionTwo).versionId(2).build(),
ImmutableViewVersion.builder().from(viewVersionTwoUpdated).versionId(3).build());
}

@Test
public void schemaIDReassignment() {
Schema schemaOne = new Schema(5, Types.NestedField.required(1, "x", Types.LongType.get()));
Expand Down

0 comments on commit 8ae63c2

Please sign in to comment.