|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.gobblin.data.management.copy.iceberg; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.HashMap; |
| 22 | + |
| 23 | +import org.apache.iceberg.PartitionSpec; |
| 24 | +import org.apache.iceberg.TableMetadata; |
| 25 | +import org.apache.iceberg.avro.AvroSchemaUtil; |
| 26 | +import org.apache.iceberg.Schema; |
| 27 | +import org.apache.iceberg.shaded.org.apache.avro.SchemaBuilder; |
| 28 | +import org.testng.Assert; |
| 29 | +import org.testng.annotations.Test; |
| 30 | + |
| 31 | +public class IcebergTableMetadataValidatorUtilsTest { |
| 32 | + private static final PartitionSpec unpartitionedPartitionSpec = PartitionSpec.unpartitioned(); |
| 33 | + private static final Schema schema1 = AvroSchemaUtil.toIceberg(SchemaBuilder.record("schema1") |
| 34 | + .fields() |
| 35 | + .requiredString("field1") |
| 36 | + .requiredString("field2") |
| 37 | + .endRecord()); |
| 38 | + private static final Schema schema2IsNotSchema1Compat = AvroSchemaUtil.toIceberg(SchemaBuilder.record("schema2") |
| 39 | + .fields() |
| 40 | + .requiredString("field2") |
| 41 | + .requiredString("field1") |
| 42 | + .endRecord()); |
| 43 | + private static final Schema schema3 = AvroSchemaUtil.toIceberg(SchemaBuilder.record("schema3") |
| 44 | + .fields() |
| 45 | + .requiredString("field1") |
| 46 | + .requiredString("field2") |
| 47 | + .requiredInt("field3") |
| 48 | + .endRecord()); |
| 49 | + private static final Schema schema4IsNotSchema3Compat = AvroSchemaUtil.toIceberg(SchemaBuilder.record("schema4") |
| 50 | + .fields() |
| 51 | + .requiredInt("field1") |
| 52 | + .requiredString("field2") |
| 53 | + .requiredInt("field3") |
| 54 | + .endRecord()); |
| 55 | + private static final PartitionSpec partitionSpec1 = PartitionSpec.builderFor(schema1) |
| 56 | + .identity("field1") |
| 57 | + .build(); |
| 58 | + private static final TableMetadata tableMetadataWithSchema1AndUnpartitionedSpec = TableMetadata.newTableMetadata( |
| 59 | + schema1, unpartitionedPartitionSpec, "tableLocationForSchema1WithUnpartitionedSpec", new HashMap<>()); |
| 60 | + private static final TableMetadata tableMetadataWithSchema1AndPartitionSpec1 = TableMetadata.newTableMetadata( |
| 61 | + schema1, partitionSpec1, "tableLocationForSchema1WithPartitionSpec1", new HashMap<>()); |
| 62 | + private static final TableMetadata tableMetadataWithSchema3AndUnpartitionedSpec = TableMetadata.newTableMetadata( |
| 63 | + schema3, unpartitionedPartitionSpec, "tableLocationForSchema3WithUnpartitionedSpec", new HashMap<>()); |
| 64 | + private static final String SCHEMA_MISMATCH_EXCEPTION = "Schema Mismatch between Metadata"; |
| 65 | + private static final String PARTITION_SPEC_MISMATCH_EXCEPTION = "Partition Spec Mismatch between Metadata"; |
| 66 | + private static final boolean VALIDATE_STRICT_PARTITION_EQUALITY_TRUE = true; |
| 67 | + private static final boolean VALIDATE_STRICT_PARTITION_EQUALITY_FALSE = false; |
| 68 | + @Test |
| 69 | + public void testValidateSameSchema() throws IOException { |
| 70 | + IcebergTableMetadataValidatorUtils.failUnlessCompatibleStructure( |
| 71 | + tableMetadataWithSchema1AndUnpartitionedSpec, tableMetadataWithSchema1AndUnpartitionedSpec, |
| 72 | + VALIDATE_STRICT_PARTITION_EQUALITY_TRUE |
| 73 | + ); |
| 74 | + Assert.assertTrue(true); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testValidateDifferentSchemaFails() { |
| 79 | + // Schema 1 and Schema 2 have different field order |
| 80 | + |
| 81 | + TableMetadata tableMetadataWithSchema2AndUnpartitionedSpec = TableMetadata.newTableMetadata(schema2IsNotSchema1Compat, |
| 82 | + unpartitionedPartitionSpec, "tableLocationForSchema2WithUnpartitionedSpec", new HashMap<>()); |
| 83 | + |
| 84 | + verifyStrictFailUnlessCompatibleStructureThrows(tableMetadataWithSchema1AndUnpartitionedSpec, |
| 85 | + tableMetadataWithSchema2AndUnpartitionedSpec, SCHEMA_MISMATCH_EXCEPTION); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testValidateSchemaWithDifferentTypesFails() { |
| 90 | + // schema 3 and schema 4 have different field types for field1 |
| 91 | + |
| 92 | + TableMetadata tableMetadataWithSchema4AndUnpartitionedSpec = TableMetadata.newTableMetadata(schema4IsNotSchema3Compat, |
| 93 | + unpartitionedPartitionSpec, "tableLocationForSchema4WithUnpartitionedSpec", new HashMap<>()); |
| 94 | + |
| 95 | + verifyStrictFailUnlessCompatibleStructureThrows(tableMetadataWithSchema3AndUnpartitionedSpec, |
| 96 | + tableMetadataWithSchema4AndUnpartitionedSpec, SCHEMA_MISMATCH_EXCEPTION); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testValidateSchemaWithEvolvedSchemaIFails() { |
| 101 | + // Schema 3 has one more extra field as compared to Schema 1 |
| 102 | + verifyStrictFailUnlessCompatibleStructureThrows(tableMetadataWithSchema1AndUnpartitionedSpec, |
| 103 | + tableMetadataWithSchema3AndUnpartitionedSpec, SCHEMA_MISMATCH_EXCEPTION); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testValidateSchemaWithEvolvedSchemaIIFails() { |
| 108 | + // TODO: This test should pass in the future when we support schema evolution |
| 109 | + // Schema 3 has one more extra field as compared to Schema 1 |
| 110 | + verifyStrictFailUnlessCompatibleStructureThrows(tableMetadataWithSchema3AndUnpartitionedSpec, |
| 111 | + tableMetadataWithSchema1AndUnpartitionedSpec, SCHEMA_MISMATCH_EXCEPTION); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testValidateOneSchemaEvolvedFromIntToLongTypeFails() { |
| 116 | + // Adding this test as to verify that partition copy doesn't proceed further for this case |
| 117 | + // as while doing poc and testing had seen final commit gets fail if there is mismatch in field type |
| 118 | + // specially from int to long |
| 119 | + Schema schema5EvolvedFromSchema4 = AvroSchemaUtil.toIceberg(SchemaBuilder.record("schema5") |
| 120 | + .fields() |
| 121 | + .requiredLong("field1") |
| 122 | + .requiredString("field2") |
| 123 | + .requiredInt("field3") |
| 124 | + .endRecord()); |
| 125 | + PartitionSpec partitionSpec = PartitionSpec.builderFor(schema5EvolvedFromSchema4) |
| 126 | + .identity("field1") |
| 127 | + .build(); |
| 128 | + TableMetadata tableMetadataWithSchema5AndPartitionSpec = TableMetadata.newTableMetadata(schema5EvolvedFromSchema4, |
| 129 | + partitionSpec, "tableLocationForSchema5WithPartitionSpec", new HashMap<>()); |
| 130 | + |
| 131 | + verifyStrictFailUnlessCompatibleStructureThrows(tableMetadataWithSchema1AndUnpartitionedSpec, |
| 132 | + tableMetadataWithSchema5AndPartitionSpec, SCHEMA_MISMATCH_EXCEPTION); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testValidateSamePartitionSpec() throws IOException { |
| 137 | + IcebergTableMetadataValidatorUtils.failUnlessCompatibleStructure( |
| 138 | + tableMetadataWithSchema1AndPartitionSpec1, tableMetadataWithSchema1AndPartitionSpec1, |
| 139 | + VALIDATE_STRICT_PARTITION_EQUALITY_TRUE |
| 140 | + ); |
| 141 | + Assert.assertTrue(true); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void testValidatePartitionSpecWithDiffNameFails() { |
| 146 | + PartitionSpec partitionSpec12 = PartitionSpec.builderFor(schema1) |
| 147 | + .identity("field2") |
| 148 | + .build(); |
| 149 | + TableMetadata tableMetadataWithSchema1AndPartitionSpec12 = TableMetadata.newTableMetadata(schema1, partitionSpec12, |
| 150 | + "tableLocationForSchema1WithPartitionSpec12", new HashMap<>()); |
| 151 | + verifyStrictFailUnlessCompatibleStructureThrows(tableMetadataWithSchema1AndPartitionSpec1, |
| 152 | + tableMetadataWithSchema1AndPartitionSpec12, PARTITION_SPEC_MISMATCH_EXCEPTION); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + public void testValidatePartitionSpecWithUnpartitionedFails() { |
| 157 | + verifyStrictFailUnlessCompatibleStructureThrows(tableMetadataWithSchema1AndUnpartitionedSpec, |
| 158 | + tableMetadataWithSchema1AndPartitionSpec1, PARTITION_SPEC_MISMATCH_EXCEPTION); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void testPartitionSpecWithDifferentTransformFails() { |
| 163 | + PartitionSpec partitionSpec12 = PartitionSpec.builderFor(schema1) |
| 164 | + .truncate("field1", 4) |
| 165 | + .build(); |
| 166 | + TableMetadata tableMetadataWithSchema1AndPartitionSpec12 = TableMetadata.newTableMetadata(schema1, partitionSpec12, |
| 167 | + "tableLocationForSchema1WithPartitionSpec12", new HashMap<>()); |
| 168 | + verifyStrictFailUnlessCompatibleStructureThrows(tableMetadataWithSchema1AndPartitionSpec1, |
| 169 | + tableMetadataWithSchema1AndPartitionSpec12, PARTITION_SPEC_MISMATCH_EXCEPTION); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + public void testStrictPartitionSpecEqualityOffVsOn() throws IOException { |
| 174 | + PartitionSpec partitionSpecWithTwoCols = PartitionSpec.builderFor(schema1) |
| 175 | + .identity("field1") |
| 176 | + .identity("field2") |
| 177 | + .build(); |
| 178 | + |
| 179 | + TableMetadata tableMetadataWithSchema1AndPartitionSpecWithTwoCols = TableMetadata.newTableMetadata(schema1, |
| 180 | + partitionSpecWithTwoCols, "tableLocationForSchema1WithPartitionSpecWithTwoCols", new HashMap<>()); |
| 181 | + TableMetadata updatedMetadataForTableMetadataWithSchema1AndPartitionSpec1 = tableMetadataWithSchema1AndPartitionSpec1 |
| 182 | + .updatePartitionSpec(tableMetadataWithSchema1AndPartitionSpecWithTwoCols.spec()); |
| 183 | + |
| 184 | + IcebergTableMetadataValidatorUtils.failUnlessCompatibleStructure( |
| 185 | + tableMetadataWithSchema1AndPartitionSpecWithTwoCols, |
| 186 | + updatedMetadataForTableMetadataWithSchema1AndPartitionSpec1, |
| 187 | + VALIDATE_STRICT_PARTITION_EQUALITY_FALSE); |
| 188 | + Assert.assertTrue(true); // passes w/ non-strict equality... |
| 189 | + // but fails when strict equality |
| 190 | + verifyStrictFailUnlessCompatibleStructureThrows(tableMetadataWithSchema1AndPartitionSpecWithTwoCols, |
| 191 | + updatedMetadataForTableMetadataWithSchema1AndPartitionSpec1, PARTITION_SPEC_MISMATCH_EXCEPTION); |
| 192 | + } |
| 193 | + |
| 194 | + private void verifyStrictFailUnlessCompatibleStructureThrows(TableMetadata tableAMetadata, |
| 195 | + TableMetadata tableBMetadata, String expectedMessage) { |
| 196 | + IOException exception = Assert.expectThrows(IOException.class, () -> { |
| 197 | + IcebergTableMetadataValidatorUtils.failUnlessCompatibleStructure(tableAMetadata, tableBMetadata, |
| 198 | + VALIDATE_STRICT_PARTITION_EQUALITY_TRUE); |
| 199 | + }); |
| 200 | + Assert.assertTrue(exception.getMessage().startsWith(expectedMessage)); |
| 201 | + } |
| 202 | +} |
0 commit comments