-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Core: Fix querying metadata tables with multiple specs #2936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,9 +19,16 @@ | |
|
|
||
| package org.apache.iceberg; | ||
|
|
||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
| import org.apache.iceberg.exceptions.ValidationException; | ||
| import org.apache.iceberg.expressions.Expressions; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Maps; | ||
| import org.apache.iceberg.transforms.PartitionSpecVisitor; | ||
| import org.apache.iceberg.types.Types.NestedField; | ||
| import org.apache.iceberg.types.Types.StructType; | ||
|
|
||
| public class Partitioning { | ||
| private Partitioning() { | ||
|
|
@@ -177,4 +184,42 @@ public Void alwaysNull(int fieldId, String sourceName, int sourceId) { | |
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Builds a common partition type for all specs in a table. | ||
| * <p> | ||
| * Whenever a table has multiple specs, the partition type is a struct containing | ||
| * all columns that have ever been a part of any spec in the table. | ||
| * | ||
| * @param table a table with one or many specs | ||
| * @return the constructed common partition type | ||
| */ | ||
| public static StructType partitionType(Table table) { | ||
| if (table.specs().size() == 1) { | ||
| return table.spec().partitionType(); | ||
| } | ||
|
|
||
| Map<Integer, NestedField> commonFields = Maps.newHashMap(); | ||
|
|
||
| for (PartitionSpec spec : table.specs().values()) { | ||
| List<NestedField> fields = spec.partitionType().fields(); | ||
|
|
||
| for (NestedField field : fields) { | ||
| int fieldId = field.fieldId(); | ||
| NestedField existingField = commonFields.get(fieldId); | ||
|
|
||
| // partition fields may conflict in v1 tables | ||
| ValidationException.check(existingField == null || existingField.equals(field), | ||
|
aokolnychyi marked this conversation as resolved.
Outdated
|
||
| "Conflicting partition fields: ['%s', '%s']", | ||
| field, existingField); | ||
|
|
||
| commonFields.put(fieldId, field); | ||
| } | ||
| } | ||
|
|
||
| List<NestedField> sortedCommonFields = commonFields.values().stream() | ||
| .sorted(Comparator.comparingInt(NestedField::fieldId)) | ||
| .collect(Collectors.toList()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 for sorting by |
||
| return StructType.of(sortedCommonFields); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /* | ||
| * 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 java.io.File; | ||
| import java.io.IOException; | ||
| import org.apache.iceberg.exceptions.ValidationException; | ||
| import org.apache.iceberg.expressions.Expressions; | ||
| import org.apache.iceberg.types.Types; | ||
| import org.apache.iceberg.types.Types.NestedField; | ||
| import org.apache.iceberg.types.Types.StructType; | ||
| import org.junit.After; | ||
| import org.junit.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.rules.TemporaryFolder; | ||
|
|
||
| import static org.apache.iceberg.types.Types.NestedField.required; | ||
|
|
||
| public class TestPartitioning { | ||
|
|
||
| private static final int V1_FORMAT_VERSION = 1; | ||
| private static final int V2_FORMAT_VERSION = 2; | ||
| private static final Schema SCHEMA = new Schema( | ||
| required(1, "id", Types.IntegerType.get()), | ||
| required(2, "data", Types.StringType.get()), | ||
| required(3, "category", Types.StringType.get()) | ||
| ); | ||
|
|
||
| @Rule | ||
| public TemporaryFolder temp = new TemporaryFolder(); | ||
| private File tableDir = null; | ||
|
|
||
| @Before | ||
| public void setupTableDir() throws IOException { | ||
| this.tableDir = temp.newFolder(); | ||
| } | ||
|
|
||
| @After | ||
| public void cleanupTables() { | ||
| TestTables.clearTables(); | ||
| } | ||
|
|
||
| @Test | ||
|
aokolnychyi marked this conversation as resolved.
|
||
| public void testPartitionTypeWithSpecEvolutionInV1Tables() { | ||
| PartitionSpec initialSpec = PartitionSpec.builderFor(SCHEMA) | ||
| .identity("data") | ||
| .build(); | ||
| TestTables.TestTable table = TestTables.create(tableDir, "test", SCHEMA, initialSpec, V1_FORMAT_VERSION); | ||
|
|
||
| table.updateSpec() | ||
| .addField(Expressions.bucket("category", 8)) | ||
| .commit(); | ||
|
|
||
| Assert.assertEquals("Should have 2 specs", 2, table.specs().size()); | ||
|
|
||
| StructType expectedType = StructType.of( | ||
| NestedField.optional(1000, "data", Types.StringType.get()), | ||
| NestedField.optional(1001, "category_bucket_8", Types.IntegerType.get()) | ||
| ); | ||
| StructType actualType = Partitioning.partitionType(table); | ||
| Assert.assertEquals("Types must match", expectedType, actualType); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPartitionTypeWithSpecEvolutionInV2Tables() { | ||
| PartitionSpec initialSpec = PartitionSpec.builderFor(SCHEMA) | ||
| .identity("data") | ||
| .build(); | ||
| TestTables.TestTable table = TestTables.create(tableDir, "test", SCHEMA, initialSpec, V2_FORMAT_VERSION); | ||
|
|
||
| table.updateSpec() | ||
| .removeField("data") | ||
| .addField("category") | ||
| .commit(); | ||
|
|
||
| Assert.assertEquals("Should have 2 specs", 2, table.specs().size()); | ||
|
|
||
| StructType expectedType = StructType.of( | ||
| NestedField.optional(1000, "data", Types.StringType.get()), | ||
| NestedField.optional(1001, "category", Types.StringType.get()) | ||
| ); | ||
| StructType actualType = Partitioning.partitionType(table); | ||
| Assert.assertEquals("Types must match", expectedType, actualType); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPartitionTypeWithIncompatibleSpecEvolution() { | ||
| PartitionSpec initialSpec = PartitionSpec.builderFor(SCHEMA) | ||
| .identity("data") | ||
| .build(); | ||
| TestTables.TestTable table = TestTables.create(tableDir, "test", SCHEMA, initialSpec, V1_FORMAT_VERSION); | ||
|
|
||
| PartitionSpec newSpec = PartitionSpec.builderFor(table.schema()) | ||
| .identity("category") | ||
| .build(); | ||
|
|
||
| TableOperations ops = ((HasTableOperations) table).operations(); | ||
| TableMetadata current = ops.current(); | ||
| ops.commit(current, current.updatePartitionSpec(newSpec)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make change for the method
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This API is hidden from users. The user-facing API is There could be some tables where people evolved partitioning before the public API appeared. It is an edge case but this test ensures we get a reasonable exception for such tables.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, this part, https://iceberg.apache.org/spec/#partition-evolution. |
||
|
|
||
| Assert.assertEquals("Should have 2 specs", 2, table.specs().size()); | ||
|
|
||
| AssertHelpers.assertThrows("Should complain about incompatible specs", | ||
| ValidationException.class, "Conflicting partition fields", | ||
| () -> Partitioning.partitionType(table)); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.