From 20c72d0d37c4663966c5ec2e8c521ba1ee6f9e0a Mon Sep 17 00:00:00 2001 From: Honah J Date: Fri, 10 Jan 2025 16:11:30 -0800 Subject: [PATCH] First try: Add Metadata Builder for tests --- .../org/apache/iceberg/MetadataTestUtils.java | 239 +++++++ .../org/apache/iceberg/TestTableMetadata.java | 610 +++++++++--------- 2 files changed, 549 insertions(+), 300 deletions(-) create mode 100644 core/src/test/java/org/apache/iceberg/MetadataTestUtils.java diff --git a/core/src/test/java/org/apache/iceberg/MetadataTestUtils.java b/core/src/test/java/org/apache/iceberg/MetadataTestUtils.java new file mode 100644 index 000000000000..cd59d56fdbb9 --- /dev/null +++ b/core/src/test/java/org/apache/iceberg/MetadataTestUtils.java @@ -0,0 +1,239 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.iceberg; + +import static org.apache.iceberg.TableMetadata.INITIAL_SEQUENCE_NUMBER; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.apache.iceberg.util.SerializableSupplier; + +public class MetadataTestUtils { + + public static TableMetadataBuilder buildTestTableMetadataFromEmpty(int formatVersion) { + return new TableMetadataBuilder(formatVersion); + } + + public static class TableMetadataBuilder { + private String metadataLocation; + private int formatVersion; + private String uuid; + private String location; + private long lastSequenceNumber; + private Long lastUpdatedMillis; + private int lastColumnId; + private int currentSchemaId; + private List schemas; + private int defaultSpecId; + private List specs; + private int lastAssignedPartitionId; + private int defaultSortOrderId; + private List sortOrders; + private Map properties; + private long currentSnapshotId; + private List snapshotLog; + private List previousFiles; + private List statisticsFiles; + private List partitionStatisticsFiles; + private List changes; + private SerializableSupplier> snapshotsSupplier; + private List snapshots; + private Map refs; + + private TableMetadataBuilder(int formatVersion) { + this.formatVersion = formatVersion; + this.uuid = UUID.randomUUID().toString(); + this.lastSequenceNumber = INITIAL_SEQUENCE_NUMBER; + this.lastUpdatedMillis = System.currentTimeMillis(); + this.lastColumnId = -1; + this.currentSchemaId = -1; + this.schemas = Lists.newArrayList(); + this.defaultSpecId = -1; + this.specs = Lists.newArrayList(); + this.lastAssignedPartitionId = 999; + this.defaultSortOrderId = -1; + this.sortOrders = Lists.newArrayList(); + this.properties = Maps.newHashMap(); + this.snapshots = Lists.newArrayList(); + this.currentSnapshotId = -1; + this.changes = Lists.newArrayList(); + this.snapshotLog = Lists.newArrayList(); + this.previousFiles = Lists.newArrayList(); + this.refs = Maps.newHashMap(); + this.statisticsFiles = Lists.newArrayList(); + this.partitionStatisticsFiles = Lists.newArrayList(); + } + + public TableMetadataBuilder withMetadataFileLocation(String metadataFileLocation) { + this.metadataLocation = metadataFileLocation; + return this; + } + + public TableMetadataBuilder withFormatVersion(int formatVersion) { + this.formatVersion = formatVersion; + return this; + } + + public TableMetadataBuilder withUUID(String uuid) { + this.uuid = uuid; + return this; + } + + public TableMetadataBuilder withLocation(String location) { + this.location = location; + return this; + } + + public TableMetadataBuilder withLastSequenceNumber(long lastSequenceNumber) { + this.lastSequenceNumber = lastSequenceNumber; + return this; + } + + public TableMetadataBuilder withLastUpdatedMillis(long lastUpdatedMillis) { + this.lastUpdatedMillis = lastUpdatedMillis; + return this; + } + + public TableMetadataBuilder withLastColumnId(int lastColumnId) { + this.lastColumnId = lastColumnId; + return this; + } + + public TableMetadataBuilder withCurrentSchemaId(int currentSchemaId) { + this.currentSchemaId = currentSchemaId; + return this; + } + + public TableMetadataBuilder withSchemas(List schemas) { + this.schemas = schemas; + return this; + } + + public TableMetadataBuilder withDefaultSpecId(int defaultSpecId) { + this.defaultSpecId = defaultSpecId; + return this; + } + + public TableMetadataBuilder withSpecs(List specs) { + this.specs = specs; + return this; + } + + public TableMetadataBuilder withLastAssignedPartitionId(int lastAssignedPartitionId) { + this.lastAssignedPartitionId = lastAssignedPartitionId; + return this; + } + + public TableMetadataBuilder withDefaultSortOrderId(int defaultSortOrderId) { + this.defaultSortOrderId = defaultSortOrderId; + return this; + } + + public TableMetadataBuilder withSortOrders(List sortOrders) { + this.sortOrders = sortOrders; + return this; + } + + public TableMetadataBuilder withProperties(Map properties) { + this.properties = properties; + return this; + } + + public TableMetadataBuilder withCurrentSnapshotId(long snapshotId) { + this.currentSnapshotId = snapshotId; + return this; + } + + public TableMetadataBuilder withSnapshotsSupplier( + SerializableSupplier> snapshotsSupplier) { + this.snapshotsSupplier = snapshotsSupplier; + return this; + } + + public TableMetadataBuilder withSnapshots(List snapshots) { + this.snapshots = snapshots; + return this; + } + + public TableMetadataBuilder withSnapshotLog(List snapshotLog) { + this.snapshotLog = snapshotLog; + return this; + } + + public TableMetadataBuilder withMetadataHistory( + List previousFiles) { + this.previousFiles = previousFiles; + return this; + } + + public TableMetadataBuilder withRefs(Map refs) { + this.refs = refs; + return this; + } + + public TableMetadataBuilder withChanges(List changes) { + this.changes = changes; + return this; + } + + public TableMetadataBuilder withStatisticsFiles(List statisticsFiles) { + this.statisticsFiles = statisticsFiles; + return this; + } + + public TableMetadataBuilder withPartitionStatisticsFiles( + List partitionStatisticsFiles) { + this.partitionStatisticsFiles = partitionStatisticsFiles; + return this; + } + + public TableMetadata build() { + return new TableMetadata( + metadataLocation, + formatVersion, + uuid, + location, + lastSequenceNumber, + lastUpdatedMillis, + lastColumnId, + currentSchemaId, + ImmutableList.copyOf(schemas), + defaultSpecId, + ImmutableList.copyOf(specs), + lastAssignedPartitionId, + defaultSortOrderId, + ImmutableList.copyOf(sortOrders), + ImmutableMap.copyOf(properties), + currentSnapshotId, + ImmutableList.copyOf(snapshots), + snapshotsSupplier, + ImmutableList.copyOf(snapshotLog), + ImmutableList.copyOf(previousFiles), + ImmutableMap.copyOf(refs), + ImmutableList.copyOf(statisticsFiles), + ImmutableList.copyOf(partitionStatisticsFiles), + ImmutableList.copyOf(changes)); + } + } +} diff --git a/core/src/test/java/org/apache/iceberg/TestTableMetadata.java b/core/src/test/java/org/apache/iceberg/TestTableMetadata.java index a593f3537a01..87482d787979 100644 --- a/core/src/test/java/org/apache/iceberg/TestTableMetadata.java +++ b/core/src/test/java/org/apache/iceberg/TestTableMetadata.java @@ -168,31 +168,32 @@ public void testJsonConversion(int formatVersion) throws Exception { .build()); TableMetadata expected = - new TableMetadata( - null, - formatVersion, - UUID.randomUUID().toString(), - TEST_LOCATION, - SEQ_NO, - System.currentTimeMillis(), - 3, - 7, - ImmutableList.of(TEST_SCHEMA, schema), - 5, - ImmutableList.of(SPEC_5), - SPEC_5.lastAssignedFieldId(), - 3, - ImmutableList.of(SORT_ORDER_3), - ImmutableMap.of("property", "value"), - currentSnapshotId, - Arrays.asList(previousSnapshot, currentSnapshot), - null, - snapshotLog, - ImmutableList.of(), - refs, - statisticsFiles, - partitionStatisticsFiles, - ImmutableList.of()); + MetadataTestUtils.buildTestTableMetadataFromEmpty(formatVersion) + .withMetadataFileLocation(null) + .withFormatVersion(formatVersion) + .withUUID(UUID.randomUUID().toString()) + .withLocation(TEST_LOCATION) + .withLastSequenceNumber(SEQ_NO) + .withLastUpdatedMillis(System.currentTimeMillis()) + .withLastColumnId(3) + .withCurrentSchemaId(7) + .withSchemas(ImmutableList.of(TEST_SCHEMA, schema)) + .withDefaultSpecId(5) + .withSpecs(ImmutableList.of(SPEC_5)) + .withLastAssignedPartitionId(SPEC_5.lastAssignedFieldId()) + .withDefaultSortOrderId(3) + .withSortOrders(ImmutableList.of(SORT_ORDER_3)) + .withProperties(ImmutableMap.of("property", "value")) + .withCurrentSnapshotId(currentSnapshotId) + .withSnapshots(Arrays.asList(previousSnapshot, currentSnapshot)) + .withSnapshotsSupplier(null) + .withSnapshotLog(snapshotLog) + .withMetadataHistory(ImmutableList.of()) + .withRefs(refs) + .withStatisticsFiles(statisticsFiles) + .withPartitionStatisticsFiles(partitionStatisticsFiles) + .withChanges(ImmutableList.of()) + .build(); String asJson = TableMetadataParser.toJson(expected); TableMetadata metadata = TableMetadataParser.fromJson(asJson); @@ -257,31 +258,32 @@ public void testBackwardCompat() throws Exception { manifestList); TableMetadata expected = - new TableMetadata( - null, - 1, - null, - TEST_LOCATION, - 0, - System.currentTimeMillis(), - 3, - TableMetadata.INITIAL_SCHEMA_ID, - ImmutableList.of(schema), - 6, - ImmutableList.of(spec), - spec.lastAssignedFieldId(), - TableMetadata.INITIAL_SORT_ORDER_ID, - ImmutableList.of(sortOrder), - ImmutableMap.of("property", "value"), - currentSnapshotId, - Arrays.asList(previousSnapshot, currentSnapshot), - null, - ImmutableList.of(), - ImmutableList.of(), - ImmutableMap.of(), - ImmutableList.of(), - ImmutableList.of(), - ImmutableList.of()); + MetadataTestUtils.buildTestTableMetadataFromEmpty(1) + .withMetadataFileLocation(null) + .withFormatVersion(1) + .withUUID(null) + .withLocation(TEST_LOCATION) + .withLastSequenceNumber(0) + .withLastUpdatedMillis(System.currentTimeMillis()) + .withLastColumnId(3) + .withCurrentSchemaId(TableMetadata.INITIAL_SCHEMA_ID) + .withSchemas(ImmutableList.of(schema)) + .withDefaultSpecId(6) + .withSpecs(ImmutableList.of(spec)) + .withLastAssignedPartitionId(spec.lastAssignedFieldId()) + .withDefaultSortOrderId(TableMetadata.INITIAL_SORT_ORDER_ID) + .withSortOrders(ImmutableList.of(sortOrder)) + .withProperties(ImmutableMap.of("property", "value")) + .withCurrentSnapshotId(currentSnapshotId) + .withSnapshots(Arrays.asList(previousSnapshot, currentSnapshot)) + .withSnapshotsSupplier(null) + .withSnapshotLog(ImmutableList.of()) + .withMetadataHistory(ImmutableList.of()) + .withRefs(ImmutableMap.of()) + .withStatisticsFiles(ImmutableList.of()) + .withPartitionStatisticsFiles(ImmutableList.of()) + .withChanges(ImmutableList.of()) + .build(); String asJson = toJsonWithoutSpecAndSchemaList(expected); TableMetadata metadata = TableMetadataParser.fromJson(asJson); @@ -368,31 +370,32 @@ public void testInvalidMainBranch(int formatVersion) throws IOException { assertThatThrownBy( () -> - new TableMetadata( - null, - formatVersion, - UUID.randomUUID().toString(), - TEST_LOCATION, - SEQ_NO, - System.currentTimeMillis(), - 3, - 7, - ImmutableList.of(TEST_SCHEMA, schema), - 5, - ImmutableList.of(SPEC_5), - SPEC_5.lastAssignedFieldId(), - 3, - ImmutableList.of(SORT_ORDER_3), - ImmutableMap.of("property", "value"), - currentSnapshotId, - Arrays.asList(previousSnapshot, currentSnapshot), - null, - snapshotLog, - ImmutableList.of(), - refs, - ImmutableList.of(), - ImmutableList.of(), - ImmutableList.of())) + MetadataTestUtils.buildTestTableMetadataFromEmpty(formatVersion) + .withMetadataFileLocation(null) + .withFormatVersion(formatVersion) + .withUUID(UUID.randomUUID().toString()) + .withLocation(TEST_LOCATION) + .withLastSequenceNumber(SEQ_NO) + .withLastUpdatedMillis(System.currentTimeMillis()) + .withLastColumnId(3) + .withCurrentSchemaId(7) + .withSchemas(ImmutableList.of(TEST_SCHEMA, schema)) + .withDefaultSpecId(5) + .withSpecs(ImmutableList.of(SPEC_5)) + .withLastAssignedPartitionId(SPEC_5.lastAssignedFieldId()) + .withDefaultSortOrderId(3) + .withSortOrders(ImmutableList.of(SORT_ORDER_3)) + .withProperties(ImmutableMap.of("property", "value")) + .withCurrentSnapshotId(currentSnapshotId) + .withSnapshots(Arrays.asList(previousSnapshot, currentSnapshot)) + .withSnapshotsSupplier(null) + .withSnapshotLog(snapshotLog) + .withMetadataHistory(ImmutableList.of()) + .withRefs(refs) + .withStatisticsFiles(ImmutableList.of()) + .withPartitionStatisticsFiles(ImmutableList.of()) + .withChanges(ImmutableList.of()) + .build()) .isInstanceOf(IllegalArgumentException.class) .hasMessageStartingWith("Current snapshot ID does not match main branch"); } @@ -416,31 +419,31 @@ public void testMainWithoutCurrent(int formatVersion) throws IOException { assertThatThrownBy( () -> - new TableMetadata( - null, - formatVersion, - UUID.randomUUID().toString(), - TEST_LOCATION, - SEQ_NO, - System.currentTimeMillis(), - 3, - 7, - ImmutableList.of(TEST_SCHEMA, schema), - 5, - ImmutableList.of(SPEC_5), - SPEC_5.lastAssignedFieldId(), - 3, - ImmutableList.of(SORT_ORDER_3), - ImmutableMap.of("property", "value"), - -1, - ImmutableList.of(snapshot), - null, - ImmutableList.of(), - ImmutableList.of(), - refs, - ImmutableList.of(), - ImmutableList.of(), - ImmutableList.of())) + MetadataTestUtils.buildTestTableMetadataFromEmpty(formatVersion) + .withMetadataFileLocation(null) + .withFormatVersion(formatVersion) + .withUUID(UUID.randomUUID().toString()) + .withLocation(TEST_LOCATION) + .withLastSequenceNumber(SEQ_NO) + .withLastUpdatedMillis(System.currentTimeMillis()) + .withLastColumnId(3) + .withCurrentSchemaId(7) + .withSchemas(ImmutableList.of(TEST_SCHEMA, schema)) + .withDefaultSpecId(5) + .withSpecs(ImmutableList.of(SPEC_5)) + .withLastAssignedPartitionId(SPEC_5.lastAssignedFieldId()) + .withDefaultSortOrderId(3) + .withSortOrders(ImmutableList.of(SORT_ORDER_3)) + .withProperties(ImmutableMap.of("property", "value")) + .withCurrentSnapshotId(-1) + .withSnapshots(ImmutableList.of(snapshot)) + .withSnapshotsSupplier(null) + .withMetadataHistory(ImmutableList.of()) + .withRefs(refs) + .withStatisticsFiles(ImmutableList.of()) + .withPartitionStatisticsFiles(ImmutableList.of()) + .withChanges(ImmutableList.of()) + .build()) .isInstanceOf(IllegalArgumentException.class) .hasMessageStartingWith("Current snapshot is not set, but main branch exists"); } @@ -459,31 +462,31 @@ public void testBranchSnapshotMissing(int formatVersion) { assertThatThrownBy( () -> - new TableMetadata( - null, - formatVersion, - UUID.randomUUID().toString(), - TEST_LOCATION, - SEQ_NO, - System.currentTimeMillis(), - 3, - 7, - ImmutableList.of(TEST_SCHEMA, schema), - 5, - ImmutableList.of(SPEC_5), - SPEC_5.lastAssignedFieldId(), - 3, - ImmutableList.of(SORT_ORDER_3), - ImmutableMap.of("property", "value"), - -1, - ImmutableList.of(), - null, - ImmutableList.of(), - ImmutableList.of(), - refs, - ImmutableList.of(), - ImmutableList.of(), - ImmutableList.of())) + MetadataTestUtils.buildTestTableMetadataFromEmpty(formatVersion) + .withMetadataFileLocation(null) + .withFormatVersion(formatVersion) + .withUUID(UUID.randomUUID().toString()) + .withLocation(TEST_LOCATION) + .withLastSequenceNumber(SEQ_NO) + .withLastUpdatedMillis(System.currentTimeMillis()) + .withLastColumnId(3) + .withCurrentSchemaId(7) + .withSchemas(ImmutableList.of(TEST_SCHEMA, schema)) + .withDefaultSpecId(5) + .withSpecs(ImmutableList.of(SPEC_5)) + .withLastAssignedPartitionId(SPEC_5.lastAssignedFieldId()) + .withDefaultSortOrderId(3) + .withSortOrders(ImmutableList.of(SORT_ORDER_3)) + .withProperties(ImmutableMap.of("property", "value")) + .withCurrentSnapshotId(-1) + .withSnapshots(ImmutableList.of()) + .withSnapshotsSupplier(null) + .withMetadataHistory(ImmutableList.of()) + .withRefs(refs) + .withStatisticsFiles(ImmutableList.of()) + .withPartitionStatisticsFiles(ImmutableList.of()) + .withChanges(ImmutableList.of()) + .build()) .isInstanceOf(IllegalArgumentException.class) .hasMessageEndingWith("does not exist in the existing snapshots list"); } @@ -564,31 +567,32 @@ public void testJsonWithPreviousMetadataLog(int formatVersion) throws Exception currentTimestamp, "/tmp/000001-" + UUID.randomUUID() + ".metadata.json")); TableMetadata base = - new TableMetadata( - null, - formatVersion, - UUID.randomUUID().toString(), - TEST_LOCATION, - 0, - System.currentTimeMillis(), - 3, - 7, - ImmutableList.of(TEST_SCHEMA), - 5, - ImmutableList.of(SPEC_5), - SPEC_5.lastAssignedFieldId(), - 3, - ImmutableList.of(SORT_ORDER_3), - ImmutableMap.of("property", "value"), - currentSnapshotId, - Arrays.asList(previousSnapshot, currentSnapshot), - null, - reversedSnapshotLog, - ImmutableList.copyOf(previousMetadataLog), - ImmutableMap.of(), - ImmutableList.of(), - ImmutableList.of(), - ImmutableList.of()); + MetadataTestUtils.buildTestTableMetadataFromEmpty(formatVersion) + .withMetadataFileLocation(null) + .withFormatVersion(formatVersion) + .withUUID(UUID.randomUUID().toString()) + .withLocation(TEST_LOCATION) + .withLastSequenceNumber(0) + .withLastUpdatedMillis(System.currentTimeMillis()) + .withLastColumnId(3) + .withCurrentSchemaId(7) + .withSchemas(ImmutableList.of(TEST_SCHEMA)) + .withDefaultSpecId(5) + .withSpecs(ImmutableList.of(SPEC_5)) + .withLastAssignedPartitionId(SPEC_5.lastAssignedFieldId()) + .withDefaultSortOrderId(3) + .withSortOrders(ImmutableList.of(SORT_ORDER_3)) + .withProperties(ImmutableMap.of("property", "value")) + .withCurrentSnapshotId(currentSnapshotId) + .withSnapshots(Arrays.asList(previousSnapshot, currentSnapshot)) + .withSnapshotsSupplier(null) + .withSnapshotLog(reversedSnapshotLog) + .withMetadataHistory(ImmutableList.copyOf(previousMetadataLog)) + .withRefs(ImmutableMap.of()) + .withStatisticsFiles(ImmutableList.of()) + .withPartitionStatisticsFiles(ImmutableList.of()) + .withChanges(ImmutableList.of()) + .build(); String asJson = TableMetadataParser.toJson(base); TableMetadata metadataFromJson = TableMetadataParser.fromJson(asJson); @@ -641,31 +645,32 @@ public void testAddPreviousMetadataRemoveNone(int formatVersion) throws IOExcept currentTimestamp - 80, "/tmp/000003-" + UUID.randomUUID() + ".metadata.json"); TableMetadata base = - new TableMetadata( - latestPreviousMetadata.file(), - formatVersion, - UUID.randomUUID().toString(), - TEST_LOCATION, - 0, - currentTimestamp - 80, - 3, - 7, - ImmutableList.of(TEST_SCHEMA), - 5, - ImmutableList.of(SPEC_5), - SPEC_5.lastAssignedFieldId(), - 3, - ImmutableList.of(SORT_ORDER_3), - ImmutableMap.of("property", "value"), - currentSnapshotId, - Arrays.asList(previousSnapshot, currentSnapshot), - null, - reversedSnapshotLog, - ImmutableList.copyOf(previousMetadataLog), - ImmutableMap.of(), - ImmutableList.of(), - ImmutableList.of(), - ImmutableList.of()); + MetadataTestUtils.buildTestTableMetadataFromEmpty(formatVersion) + .withMetadataFileLocation(latestPreviousMetadata.file()) + .withFormatVersion(formatVersion) + .withUUID(UUID.randomUUID().toString()) + .withLocation(TEST_LOCATION) + .withLastSequenceNumber(0) + .withLastUpdatedMillis(currentTimestamp - 80) + .withLastColumnId(3) + .withCurrentSchemaId(7) + .withSchemas(ImmutableList.of(TEST_SCHEMA)) + .withDefaultSpecId(5) + .withSpecs(ImmutableList.of(SPEC_5)) + .withLastAssignedPartitionId(SPEC_5.lastAssignedFieldId()) + .withDefaultSortOrderId(3) + .withSortOrders(ImmutableList.of(SORT_ORDER_3)) + .withProperties(ImmutableMap.of("property", "value")) + .withCurrentSnapshotId(currentSnapshotId) + .withSnapshots(Arrays.asList(previousSnapshot, currentSnapshot)) + .withSnapshotsSupplier(null) + .withSnapshotLog(reversedSnapshotLog) + .withMetadataHistory(ImmutableList.copyOf(previousMetadataLog)) + .withRefs(ImmutableMap.of()) + .withStatisticsFiles(ImmutableList.of()) + .withPartitionStatisticsFiles(ImmutableList.of()) + .withChanges(ImmutableList.of()) + .build(); previousMetadataLog.add(latestPreviousMetadata); @@ -733,31 +738,32 @@ public void testAddPreviousMetadataRemoveOne(int formatVersion) throws IOExcepti currentTimestamp - 50, "/tmp/000006-" + UUID.randomUUID() + ".metadata.json"); TableMetadata base = - new TableMetadata( - latestPreviousMetadata.file(), - formatVersion, - UUID.randomUUID().toString(), - TEST_LOCATION, - 0, - currentTimestamp - 50, - 3, - 7, - ImmutableList.of(TEST_SCHEMA), - 5, - ImmutableList.of(SPEC_5), - SPEC_5.lastAssignedFieldId(), - 3, - ImmutableList.of(SORT_ORDER_3), - ImmutableMap.of("property", "value"), - currentSnapshotId, - Arrays.asList(previousSnapshot, currentSnapshot), - null, - reversedSnapshotLog, - ImmutableList.copyOf(previousMetadataLog), - ImmutableMap.of(), - ImmutableList.of(), - ImmutableList.of(), - ImmutableList.of()); + MetadataTestUtils.buildTestTableMetadataFromEmpty(formatVersion) + .withMetadataFileLocation(latestPreviousMetadata.file()) + .withFormatVersion(formatVersion) + .withUUID(UUID.randomUUID().toString()) + .withLocation(TEST_LOCATION) + .withLastSequenceNumber(0) + .withLastUpdatedMillis(currentTimestamp - 50) + .withLastColumnId(3) + .withCurrentSchemaId(7) + .withSchemas(ImmutableList.of(TEST_SCHEMA)) + .withDefaultSpecId(5) + .withSpecs(ImmutableList.of(SPEC_5)) + .withLastAssignedPartitionId(SPEC_5.lastAssignedFieldId()) + .withDefaultSortOrderId(3) + .withSortOrders(ImmutableList.of(SORT_ORDER_3)) + .withProperties(ImmutableMap.of("property", "value")) + .withCurrentSnapshotId(currentSnapshotId) + .withSnapshots(Arrays.asList(previousSnapshot, currentSnapshot)) + .withSnapshotsSupplier(null) + .withSnapshotLog(reversedSnapshotLog) + .withMetadataHistory(ImmutableList.copyOf(previousMetadataLog)) + .withRefs(ImmutableMap.of()) + .withStatisticsFiles(ImmutableList.of()) + .withPartitionStatisticsFiles(ImmutableList.of()) + .withChanges(ImmutableList.of()) + .build(); previousMetadataLog.add(latestPreviousMetadata); @@ -829,31 +835,32 @@ public void testAddPreviousMetadataRemoveMultiple(int formatVersion) throws IOEx currentTimestamp - 50, "/tmp/000006-" + UUID.randomUUID() + ".metadata.json"); TableMetadata base = - new TableMetadata( - latestPreviousMetadata.file(), - formatVersion, - UUID.randomUUID().toString(), - TEST_LOCATION, - 0, - currentTimestamp - 50, - 3, - 7, - ImmutableList.of(TEST_SCHEMA), - SPEC_5.specId(), - ImmutableList.of(SPEC_5), - SPEC_5.lastAssignedFieldId(), - SortOrder.unsorted().orderId(), - ImmutableList.of(SortOrder.unsorted()), - ImmutableMap.of("property", "value"), - currentSnapshotId, - Arrays.asList(previousSnapshot, currentSnapshot), - null, - reversedSnapshotLog, - ImmutableList.copyOf(previousMetadataLog), - ImmutableMap.of(), - ImmutableList.of(), - ImmutableList.of(), - ImmutableList.of()); + MetadataTestUtils.buildTestTableMetadataFromEmpty(formatVersion) + .withMetadataFileLocation(latestPreviousMetadata.file()) + .withFormatVersion(formatVersion) + .withUUID(UUID.randomUUID().toString()) + .withLocation(TEST_LOCATION) + .withLastSequenceNumber(0) + .withLastUpdatedMillis(currentTimestamp - 50) + .withLastColumnId(3) + .withCurrentSchemaId(7) + .withSchemas(ImmutableList.of(TEST_SCHEMA)) + .withDefaultSpecId(SPEC_5.specId()) + .withSpecs(ImmutableList.of(SPEC_5)) + .withLastAssignedPartitionId(SPEC_5.lastAssignedFieldId()) + .withDefaultSortOrderId(SortOrder.unsorted().orderId()) + .withSortOrders(ImmutableList.of(SortOrder.unsorted())) + .withProperties(ImmutableMap.of("property", "value")) + .withCurrentSnapshotId(currentSnapshotId) + .withSnapshots(Arrays.asList(previousSnapshot, currentSnapshot)) + .withSnapshotsSupplier(null) + .withSnapshotLog(reversedSnapshotLog) + .withMetadataHistory(ImmutableList.copyOf(previousMetadataLog)) + .withRefs(ImmutableMap.of()) + .withStatisticsFiles(ImmutableList.of()) + .withPartitionStatisticsFiles(ImmutableList.of()) + .withChanges(ImmutableList.of()) + .build(); previousMetadataLog.add(latestPreviousMetadata); @@ -878,31 +885,32 @@ public void testV2UUIDValidation(int formatVersion) { assertThatThrownBy( () -> - new TableMetadata( - null, - formatVersion, - null, - TEST_LOCATION, - SEQ_NO, - System.currentTimeMillis(), - LAST_ASSIGNED_COLUMN_ID, - 7, - ImmutableList.of(TEST_SCHEMA), - SPEC_5.specId(), - ImmutableList.of(SPEC_5), - SPEC_5.lastAssignedFieldId(), - 3, - ImmutableList.of(SORT_ORDER_3), - ImmutableMap.of(), - -1L, - ImmutableList.of(), - null, - ImmutableList.of(), - ImmutableList.of(), - ImmutableMap.of(), - ImmutableList.of(), - ImmutableList.of(), - ImmutableList.of())) + MetadataTestUtils.buildTestTableMetadataFromEmpty(formatVersion) + .withMetadataFileLocation(null) + .withFormatVersion(formatVersion) + .withUUID(null) + .withLocation(TEST_LOCATION) + .withLastSequenceNumber(SEQ_NO) + .withLastUpdatedMillis(System.currentTimeMillis()) + .withLastColumnId(LAST_ASSIGNED_COLUMN_ID) + .withCurrentSchemaId(7) + .withSchemas(ImmutableList.of(TEST_SCHEMA)) + .withDefaultSpecId(SPEC_5.specId()) + .withSpecs(ImmutableList.of(SPEC_5)) + .withLastAssignedPartitionId(SPEC_5.lastAssignedFieldId()) + .withDefaultSortOrderId(3) + .withSortOrders(ImmutableList.of(SORT_ORDER_3)) + .withProperties(ImmutableMap.of()) + .withCurrentSnapshotId(-1L) + .withSnapshots(ImmutableList.of()) + .withSnapshotsSupplier(null) + .withSnapshotLog(ImmutableList.of()) + .withMetadataHistory(ImmutableList.of()) + .withRefs(ImmutableMap.of()) + .withStatisticsFiles(ImmutableList.of()) + .withPartitionStatisticsFiles(ImmutableList.of()) + .withChanges(ImmutableList.of()) + .build()) .isInstanceOf(IllegalArgumentException.class) .hasMessage(String.format("UUID is required in format v%s", formatVersion)); } @@ -913,31 +921,32 @@ public void testVersionValidation() { int unsupportedVersion = supportedVersion + 1; assertThatThrownBy( () -> - new TableMetadata( - null, - unsupportedVersion, - null, - TEST_LOCATION, - SEQ_NO, - System.currentTimeMillis(), - LAST_ASSIGNED_COLUMN_ID, - 7, - ImmutableList.of(TEST_SCHEMA), - SPEC_5.specId(), - ImmutableList.of(SPEC_5), - SPEC_5.lastAssignedFieldId(), - 3, - ImmutableList.of(SORT_ORDER_3), - ImmutableMap.of(), - -1L, - ImmutableList.of(), - null, - ImmutableList.of(), - ImmutableList.of(), - ImmutableMap.of(), - ImmutableList.of(), - ImmutableList.of(), - ImmutableList.of())) + MetadataTestUtils.buildTestTableMetadataFromEmpty(unsupportedVersion) + .withMetadataFileLocation(null) + .withFormatVersion(unsupportedVersion) + .withUUID(null) + .withLocation(TEST_LOCATION) + .withLastSequenceNumber(SEQ_NO) + .withLastUpdatedMillis(System.currentTimeMillis()) + .withLastColumnId(LAST_ASSIGNED_COLUMN_ID) + .withCurrentSchemaId(7) + .withSchemas(ImmutableList.of(TEST_SCHEMA)) + .withDefaultSpecId(SPEC_5.specId()) + .withSpecs(ImmutableList.of(SPEC_5)) + .withLastAssignedPartitionId(SPEC_5.lastAssignedFieldId()) + .withDefaultSortOrderId(3) + .withSortOrders(ImmutableList.of(SORT_ORDER_3)) + .withProperties(ImmutableMap.of()) + .withCurrentSnapshotId(-1L) + .withSnapshots(ImmutableList.of()) + .withSnapshotsSupplier(null) + .withSnapshotLog(ImmutableList.of()) + .withMetadataHistory(ImmutableList.of()) + .withRefs(ImmutableMap.of()) + .withStatisticsFiles(ImmutableList.of()) + .withPartitionStatisticsFiles(ImmutableList.of()) + .withChanges(ImmutableList.of()) + .build()) .isInstanceOf(IllegalArgumentException.class) .hasMessage( "Unsupported format version: v%s (supported: v%s)", @@ -959,31 +968,32 @@ public void testVersionValidation() { // should be allowed in the supported version assertThat( - new TableMetadata( - null, - supportedVersion, - UUID.randomUUID().toString(), - TEST_LOCATION, - SEQ_NO, - System.currentTimeMillis(), - LAST_ASSIGNED_COLUMN_ID, - 7, - ImmutableList.of(TEST_SCHEMA), - SPEC_5.specId(), - ImmutableList.of(SPEC_5), - SPEC_5.lastAssignedFieldId(), - 3, - ImmutableList.of(SORT_ORDER_3), - ImmutableMap.of(), - -1L, - ImmutableList.of(), - null, - ImmutableList.of(), - ImmutableList.of(), - ImmutableMap.of(), - ImmutableList.of(), - ImmutableList.of(), - ImmutableList.of())) + MetadataTestUtils.buildTestTableMetadataFromEmpty(supportedVersion) + .withMetadataFileLocation(null) + .withFormatVersion(supportedVersion) + .withUUID(UUID.randomUUID().toString()) + .withLocation(TEST_LOCATION) + .withLastSequenceNumber(SEQ_NO) + .withLastUpdatedMillis(System.currentTimeMillis()) + .withLastColumnId(LAST_ASSIGNED_COLUMN_ID) + .withCurrentSchemaId(7) + .withSchemas(ImmutableList.of(TEST_SCHEMA)) + .withDefaultSpecId(SPEC_5.specId()) + .withSpecs(ImmutableList.of(SPEC_5)) + .withLastAssignedPartitionId(SPEC_5.lastAssignedFieldId()) + .withDefaultSortOrderId(3) + .withSortOrders(ImmutableList.of(SORT_ORDER_3)) + .withProperties(ImmutableMap.of()) + .withCurrentSnapshotId(-1L) + .withSnapshots(ImmutableList.of()) + .withSnapshotsSupplier(null) + .withSnapshotLog(ImmutableList.of()) + .withMetadataHistory(ImmutableList.of()) + .withRefs(ImmutableMap.of()) + .withStatisticsFiles(ImmutableList.of()) + .withPartitionStatisticsFiles(ImmutableList.of()) + .withChanges(ImmutableList.of()) + .build()) .isNotNull(); assertThat(