timothyw553 commented on code in PR #19149:
URL: https://github.com/apache/iceberg/pull/19149#discussion_r4704354496
##########
core/src/main/java/org/apache/iceberg/util/PartitionUtil.java:
##########
@@ -92,9 +101,18 @@ private PartitionUtil() {}
for (int pos = 0; pos < fields.size(); pos += 1) {
PartitionField field = fields.get(pos);
if (field.transform().isIdentity()) {
- Object converted =
- convertConstant.apply(
- partitionFields.get(pos).type(), partitionData.get(pos,
Object.class));
+ Type type = partitionFields.get(pos).type();
+
+ // A historical read may include a partition source field
+ // that is no longer present in the current table schema.
+ if (type.typeId() == Type.TypeID.UNKNOWN && readSchema != null) {
Review Comment:
changing `type` here leaves the decoded value in `partitionData` unwidened,
so if a column was promoted (e.g. int to bigint) before being dropped, could
`partitionData.get` still return an `Integer` paired with `LONG` and cause a
`ClassCastException`? to my understanding this only happens because
`SnapshotScan.specs()` checks
`snapshotId().equals(table().currentSnapshot().snapshotId())` instead of
`tableSchema().schemaId() == table().schema().schemaId()`, since dropping a
column doesn't create a new snapshot. should we fix the spec binding in
`SnapshotScan.specs()` (and add numeric promotion + tagged current snapshot
tests) rather than patching the type here?
##########
spark/v3.5/spark/src/test/java/org/apache/iceberg/spark/sql/TestSelect.java:
##########
@@ -343,6 +343,56 @@ public void testTagReference() {
assertEquals("Snapshot at specific tag reference name", expected, fromDF);
}
+ @TestTemplate
+ public void testTimeTravelWithDroppedPartitionColumn() {
+ checkTimeTravelWithDroppedPartitionColumn(false);
+ }
+
+ @TestTemplate
+ public void testTimeTravelWithDroppedPartitionColumnAndDataGrouping() {
Review Comment:
this test enables data grouping and checks the returned rows, but it doesn't
verify whether `KeyGroupedPartitioning` or the right grouping key type is
actually reported in the scan plan. in `SparkPartitioningAwareScan.specs()`, it
reloads specs from `table().specs()` by id, so wouldn't the grouping field
still have `UNKNOWN` (`NullType`) here? could we add a scan-planning assertion
checking `KeyGroupedPartitioning` and its field types (both with and without
the dropped column projected), and preserve `task.spec()` if we fix core spec
binding? (same across the other spark versions)
##########
core/src/test/java/org/apache/iceberg/util/TestPartitionUtil.java:
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.util;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.List;
+import java.util.Map;
+import org.apache.iceberg.DataFile;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.types.Types;
+import org.junit.jupiter.api.Test;
+
+public class TestPartitionUtil {
+ private static final int SOURCE_ID = 1;
+
+ private static final Schema HISTORICAL_SCHEMA =
+ new Schema(Types.NestedField.optional(SOURCE_ID, "p",
Types.StringType.get()));
+
+ private static final PartitionSpec SPEC =
+ PartitionSpec.builderFor(HISTORICAL_SCHEMA).identity("p").build();
+
+ @Test
+ public void testHistoricalIdentityPartitionType() {
+ FileScanTask task = taskWithUnknownPartitionType("a");
+
+ Map<Integer, ?> constants =
+ PartitionUtil.constantsMap(task, null, HISTORICAL_SCHEMA, (type,
value) -> type.typeId());
+
+
assertThat(constants.get(SOURCE_ID)).isEqualTo(Types.StringType.get().typeId());
+ }
+
+ @Test
+ public void testExistingOverloadPreservesUnknownType() {
+ FileScanTask task = taskWithUnknownPartitionType("a");
+
+ Map<Integer, ?> constants =
+ PartitionUtil.constantsMap(task, null, (type, value) -> type.typeId());
+
+
assertThat(constants.get(SOURCE_ID)).isEqualTo(Types.UnknownType.get().typeId());
+ }
+
+ @Test
+ public void testMissingHistoricalFieldPreservesUnknownType() {
+ FileScanTask task = taskWithUnknownPartitionType("a");
+
+ Map<Integer, ?> constants =
+ PartitionUtil.constantsMap(task, null, new Schema(), (type, value) ->
type.typeId());
+
+
assertThat(constants.get(SOURCE_ID)).isEqualTo(Types.UnknownType.get().typeId());
+ }
+
+ @Test
+ public void testKnownPartitionTypeIsPreserved() {
Review Comment:
both `SPEC` and `HISTORICAL_SCHEMA` use `StringType` for `p`, so wouldn't
this test still pass if `constantsMap` ignored the spec type and always used
`readSchema`? could we test with distinct types (e.g. an int spec and a long
read schema) to verify precedence? also, in `taskWithUnknownPartitionType`
below, could we use `SPEC.toUnbound().bind(new Schema(), true)` instead of
mocking `PartitionSpec`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]