This is an automated email from the ASF dual-hosted git repository.

the-other-tim-brown pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-xtable.git


The following commit(s) were added to refs/heads/main by this push:
     new b29d3972 fix(core): resolve Delta partition values by storage name 
when column mapping is enabled (#973)
b29d3972 is described below

commit b29d3972d258e55e1a78cafca4c1b48eb2532852
Author: mola <[email protected]>
AuthorDate: Sun Nov 1 09:57:02 2026 +0800

    fix(core): resolve Delta partition values by storage name when column 
mapping is enabled (#973)
    
    * test(core): reproduce #972 - kernel partition extraction misses 
column-mapped (physical-name) partition values
    
    * Fix #972: resolve Delta partition values by storage (physical) name under 
column mapping
    
    * test(core): drop scratch repro class - regression cases now live in the 
extractor test classes
    
    * test(core): cover column-mapping partition path in Delta to Iceberg IT
    
    * test(core): partition column-mapping Delta->Iceberg IT by level (review 
follow-up: validate partition path in integration test)
    
    * test(core): add regression test for #977 column-mapping physicalName 
propagation
    
    * Address vaibhavk1992 review: storageName is authoritative, Kernel-path IT
    
    1. Null partition values no longer fall back to the logical name: in both 
DeltaPartitionExtractor and DeltaKernelPartitionExtractor, 
getSerializedPartitionValue now returns values.get(storageName) whenever the 
map contains the storage key, including an explicit null entry. Previously a 
null under the storage key fell through to the logical name, which under 
late-enabled column mapping can match another column's physical key 
(vaibhavk1992 repro: renames a->tmp, b->a, row (1, X, NULL) r [...]
    2. Kernel-path IT: testLateColumnMappingRenameDeltaToIceberg runs the #977 
scenario (mapping enabled after data exists, double rename level->tmp, 
street->level, plus a null partition row) through both the Kernel and the 
Standalone Delta sources, syncing to Iceberg and asserting the row reads back 
tmp=X, level=NULL. Running both sources also covers #886.
    Generated partition columns under column mapping stay a separate follow-up 
(non-blocking per review).
    
    * Fix spotless format violation in ITConversionController (reflow Javadoc 
comment to 100 columns)
    
    * fix(iceberg): map column-mapped fields by storage name only
    
    Adding both the logical and storage name to the Iceberg name mapping
    fails with a duplicate-key error when a rename makes one field's logical
    name equal another field's storage name (e.g. level -> tmp, street ->
    level). Delta writes Parquet columns under the storage name, so the
    logical alias is not needed for those fields.
    
    * test: run column-mapping ITs against both Delta sources
    
    - Parameterize testColumnMappingEnabledDeltaToIceberg and
      testLateColumnMappingRenameDeltaToIceberg over the Standalone and
      Kernel sources.
    - Replace the partial-column INSERT, which Delta rejects, with
      insertRows; dataset equivalence already compares every column.
    - Shorten comments and simplify setup in the partition and schema
      extractor tests.
---
 .../xtable/delta/DeltaPartitionExtractor.java      |  6 +++
 .../xtable/iceberg/IcebergConversionTarget.java    | 17 ++++---
 .../kernel/DeltaKernelPartitionExtractor.java      |  6 +++
 .../xtable/kernel/DeltaKernelSchemaExtractor.java  |  1 +
 .../org/apache/xtable/ITConversionController.java  | 58 ++++++++++++++++++++--
 .../xtable/delta/TestDeltaPartitionExtractor.java  | 54 ++++++++++++++++++++
 .../kernel/TestDeltaKernelPartitionExtractor.java  | 53 ++++++++++++++++++++
 .../kernel/TestDeltaKernelSchemaExtractor.java     | 30 +++++++++++
 8 files changed, 214 insertions(+), 11 deletions(-)

diff --git 
a/xtable-core/src/main/java/org/apache/xtable/delta/DeltaPartitionExtractor.java
 
b/xtable-core/src/main/java/org/apache/xtable/delta/DeltaPartitionExtractor.java
index 62aaa8cc..252138db 100644
--- 
a/xtable-core/src/main/java/org/apache/xtable/delta/DeltaPartitionExtractor.java
+++ 
b/xtable-core/src/main/java/org/apache/xtable/delta/DeltaPartitionExtractor.java
@@ -320,6 +320,12 @@ public class DeltaPartitionExtractor {
       Map<String, String> values, InternalPartitionField partitionField) {
     if (partitionField.getPartitionFieldNames() == null
         || partitionField.getPartitionFieldNames().isEmpty()) {
+      // With column mapping, partition values are keyed by physical name. 
That key wins even when
+      // its value is null, since the logical name may match another column's 
physical name.
+      String storageName = partitionField.getSourceField().getStorageName();
+      if (storageName != null && values.containsKey(storageName)) {
+        return values.get(storageName);
+      }
       return values.getOrDefault(partitionField.getSourceField().getName(), 
null);
     }
     List<String> partitionFieldNames = partitionField.getPartitionFieldNames();
diff --git 
a/xtable-core/src/main/java/org/apache/xtable/iceberg/IcebergConversionTarget.java
 
b/xtable-core/src/main/java/org/apache/xtable/iceberg/IcebergConversionTarget.java
index c47774e8..c36f50bc 100644
--- 
a/xtable-core/src/main/java/org/apache/xtable/iceberg/IcebergConversionTarget.java
+++ 
b/xtable-core/src/main/java/org/apache/xtable/iceberg/IcebergConversionTarget.java
@@ -21,6 +21,7 @@ package org.apache.xtable.iceberg;
 import java.time.Duration;
 import java.time.Instant;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -171,9 +172,9 @@ public class IcebergConversionTarget implements 
ConversionTarget {
   }
 
   /**
-   * Create a name mapping from the given schema, add storage names in {@link
-   * IcebergSchemaExtractor#getIdToStorageName()}, if any exist, to the name 
mapping, and apply the
-   * updated mapping to the table.
+   * Create a name mapping from the given schema, use storage names in {@link
+   * IcebergSchemaExtractor#getIdToStorageName()}, if any exist, in place of 
field names, and apply
+   * the updated mapping to the table.
    *
    * <p>This method should only be called when the name mapping is not set, or 
when field IDs are
    * provided in the source schema
@@ -199,10 +200,12 @@ public class IcebergConversionTarget implements 
ConversionTarget {
     }
     List<MappedField> fieldResults = new ArrayList<>();
     for (MappedField field : mapping.fields()) {
-      Set<String> fieldNames = new HashSet<>(field.names());
-      if (idToStorageName.containsKey(field.id())) {
-        fieldNames.add(idToStorageName.get(field.id()));
-      }
+      // Files are written with the storage name, so map only that; the 
logical name can collide
+      // with another field's storage name after renames.
+      Set<String> fieldNames =
+          idToStorageName.containsKey(field.id())
+              ? Collections.singleton(idToStorageName.get(field.id()))
+              : new HashSet<>(field.names());
       MappedFields nestedMapping = updateNameMapping(field.nestedMapping(), 
idToStorageName);
       fieldResults.add(MappedField.of(field.id(), fieldNames, nestedMapping));
     }
diff --git 
a/xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelPartitionExtractor.java
 
b/xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelPartitionExtractor.java
index 01532041..2f2778da 100644
--- 
a/xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelPartitionExtractor.java
+++ 
b/xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelPartitionExtractor.java
@@ -313,6 +313,12 @@ public class DeltaKernelPartitionExtractor {
       Map<String, String> values, InternalPartitionField partitionField) {
     if (partitionField.getPartitionFieldNames() == null
         || partitionField.getPartitionFieldNames().isEmpty()) {
+      // With column mapping, partition values are keyed by physical name. 
That key wins even when
+      // its value is null, since the logical name may match another column's 
physical name.
+      String storageName = partitionField.getSourceField().getStorageName();
+      if (storageName != null && values.containsKey(storageName)) {
+        return values.get(storageName);
+      }
       return values.getOrDefault(partitionField.getSourceField().getName(), 
null);
     }
     List<String> partitionFieldNames = partitionField.getPartitionFieldNames();
diff --git 
a/xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelSchemaExtractor.java
 
b/xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelSchemaExtractor.java
index a51fa170..303fda10 100644
--- 
a/xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelSchemaExtractor.java
+++ 
b/xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelSchemaExtractor.java
@@ -176,6 +176,7 @@ public class DeltaKernelSchemaExtractor {
                         .fieldId(fieldId)
                         .parentPath(parentPath)
                         .schema(schema)
+                        .storageName(storageName)
                         .defaultValue(
                             field.isNullable() ? 
InternalField.Constants.NULL_DEFAULT_VALUE : null)
                         .build();
diff --git 
a/xtable-core/src/test/java/org/apache/xtable/ITConversionController.java 
b/xtable-core/src/test/java/org/apache/xtable/ITConversionController.java
index c6b097d8..6e92ffc6 100644
--- a/xtable-core/src/test/java/org/apache/xtable/ITConversionController.java
+++ b/xtable-core/src/test/java/org/apache/xtable/ITConversionController.java
@@ -212,6 +212,15 @@ public class ITConversionController {
     return Stream.of(Arguments.of(SyncMode.INCREMENTAL), 
Arguments.of(SyncMode.FULL));
   }
 
+  private ConversionSourceProvider<?> getDeltaConversionSourceProvider(boolean 
useKernelSource) {
+    if (!useKernelSource) {
+      return getConversionSourceProvider(DELTA);
+    }
+    ConversionSourceProvider<Long> provider = new 
DeltaKernelConversionSourceProvider();
+    provider.init(jsc.hadoopConfiguration());
+    return provider;
+  }
+
   private ConversionSourceProvider<?> getConversionSourceProvider(String 
sourceTableFormat) {
     switch (sourceTableFormat.toUpperCase()) {
       case HUDI:
@@ -1112,12 +1121,15 @@ public class ITConversionController {
     }
   }
 
-  @Test
-  public void testColumnMappingEnabledDeltaToIceberg() {
+  @ParameterizedTest(name = "useKernelSource={0}")
+  @ValueSource(booleans = {false, true})
+  public void testColumnMappingEnabledDeltaToIceberg(boolean useKernelSource) {
     String tableName = getTableName();
-    ConversionSourceProvider<?> conversionSourceProvider = 
getConversionSourceProvider(DELTA);
+    ConversionSourceProvider<?> conversionSourceProvider =
+        getDeltaConversionSourceProvider(useKernelSource);
     try (TestSparkDeltaTable table =
-        TestSparkDeltaTable.forColumnMappingEnabled(tableName, tempDir, 
sparkSession, null)) {
+        // Partitioned so partition values keyed by physical name are 
validated.
+        TestSparkDeltaTable.forColumnMappingEnabled(tableName, tempDir, 
sparkSession, "level")) {
       table.insertRows(20);
       ConversionController conversionController =
           new ConversionController(jsc.hadoopConfiguration());
@@ -1154,6 +1166,44 @@ public class ITConversionController {
     }
   }
 
+  // Column mapping enabled after data exists, then the partition column is 
renamed and another
+  // column takes its old name, so logical and physical names cross.
+  @ParameterizedTest(name = "useKernelSource={0}")
+  @ValueSource(booleans = {false, true})
+  public void testLateColumnMappingRenameDeltaToIceberg(boolean 
useKernelSource) {
+    String tableName = getTableName();
+    ConversionSourceProvider<?> conversionSourceProvider =
+        getDeltaConversionSourceProvider(useKernelSource);
+    try (TestSparkDeltaTable table =
+        TestSparkDeltaTable.forSchemaWithAdditionalColumnsAndPartitioning(
+            tableName, tempDir, sparkSession, "level")) {
+      table.insertRows(20);
+      sparkSession.sql(
+          String.format(
+              "ALTER TABLE delta.`%s` SET TBLPROPERTIES "
+                  + 
"('delta.minReaderVersion'='2','delta.minWriterVersion'='5',"
+                  + "'delta.columnMapping.mode'='name')",
+              table.getBasePath()));
+      // tmp now has physical name "level"; level has physical name "street".
+      table.renameColumn("level", "tmp");
+      table.renameColumn("street", "level");
+      table.insertRows(10);
+      ConversionController conversionController =
+          new ConversionController(jsc.hadoopConfiguration());
+      ConversionConfig conversionConfig =
+          getTableSyncConfig(
+              DELTA,
+              SyncMode.INCREMENTAL,
+              tableName,
+              table,
+              Collections.singletonList(ICEBERG),
+              null,
+              null);
+      conversionController.sync(conversionConfig, conversionSourceProvider);
+      checkDatasetEquivalence(DELTA, table, 
Collections.singletonList(ICEBERG), 30);
+    }
+  }
+
   @Test
   public void testMetadataRetention() throws Exception {
     String tableName = getTableName();
diff --git 
a/xtable-core/src/test/java/org/apache/xtable/delta/TestDeltaPartitionExtractor.java
 
b/xtable-core/src/test/java/org/apache/xtable/delta/TestDeltaPartitionExtractor.java
index 0aaf3d60..58eaa55a 100644
--- 
a/xtable-core/src/test/java/org/apache/xtable/delta/TestDeltaPartitionExtractor.java
+++ 
b/xtable-core/src/test/java/org/apache/xtable/delta/TestDeltaPartitionExtractor.java
@@ -46,6 +46,8 @@ import org.apache.xtable.model.stat.Range;
 
 /** Validates the partition extraction logic from Delta tables. */
 public class TestDeltaPartitionExtractor {
+  private static final String PHYSICAL_NAME = 
"col-1a2b3c4d-5e6f-4a5b-8c9d-0e1f2a3b4c5d";
+
   private static final Map<String, StructField> STRUCT_FIELD_MAP =
       new HashMap<String, StructField>() {
         {
@@ -375,6 +377,44 @@ public class TestDeltaPartitionExtractor {
     assertEquals(expectedPartitionValues, partitionValues);
   }
 
+  @Test
+  public void testPartitionValueExtractionWithColumnMappingPhysicalName() {
+    // With column mapping, partition values are keyed by the physical name, 
not the logical one.
+    InternalPartitionField partitionField = 
columnMappedPartitionField("country", PHYSICAL_NAME);
+    List<PartitionValue> partitionValues =
+        deltaPartitionExtractor.partitionValueExtraction(
+            convertJavaMapToScalaMap(Collections.singletonMap(PHYSICAL_NAME, 
"USA")),
+            Collections.singletonList(partitionField));
+    assertEquals(
+        Collections.singletonList(
+            PartitionValue.builder()
+                .partitionField(partitionField)
+                .range(Range.scalar("USA"))
+                .build()),
+        partitionValues);
+  }
+
+  @Test
+  public void 
testPartitionValueExtractionWithColumnMappingNullValueIsAuthoritative() {
+    // A null under the physical key must not fall back to the logical name, 
which can be another
+    // column's physical name after renames.
+    Map<String, String> partitionValuesMap = new HashMap<>();
+    partitionValuesMap.put(PHYSICAL_NAME, null);
+    partitionValuesMap.put("country", "USA");
+    InternalPartitionField partitionField = 
columnMappedPartitionField("country", PHYSICAL_NAME);
+    List<PartitionValue> partitionValues =
+        deltaPartitionExtractor.partitionValueExtraction(
+            convertJavaMapToScalaMap(partitionValuesMap),
+            Collections.singletonList(partitionField));
+    assertEquals(
+        Collections.singletonList(
+            PartitionValue.builder()
+                .partitionField(partitionField)
+                .range(Range.scalar(null))
+                .build()),
+        partitionValues);
+  }
+
   @Test
   public void testDateFormatGeneratedPartitionValueExtraction() {
     // date_partition_column is generated in the table as 
DATE_FORMAT(some_date_column,
@@ -592,6 +632,20 @@ public class TestDeltaPartitionExtractor {
     assertEquals(expected, actual);
   }
 
+  private static InternalPartitionField columnMappedPartitionField(
+      String name, String physicalName) {
+    return InternalPartitionField.builder()
+        .sourceField(
+            InternalField.builder()
+                .name(name)
+                .storageName(physicalName)
+                .schema(
+                    
InternalSchema.builder().name("string").dataType(InternalType.STRING).build())
+                .build())
+        .transformType(PartitionTransformType.VALUE)
+        .build();
+  }
+
   private scala.collection.mutable.Map<String, String> 
convertJavaMapToScalaMap(
       Map<String, String> javaMap) {
     return JavaConverters.mapAsScalaMapConverter(javaMap).asScala();
diff --git 
a/xtable-core/src/test/java/org/apache/xtable/kernel/TestDeltaKernelPartitionExtractor.java
 
b/xtable-core/src/test/java/org/apache/xtable/kernel/TestDeltaKernelPartitionExtractor.java
index e574f99b..436ba40a 100644
--- 
a/xtable-core/src/test/java/org/apache/xtable/kernel/TestDeltaKernelPartitionExtractor.java
+++ 
b/xtable-core/src/test/java/org/apache/xtable/kernel/TestDeltaKernelPartitionExtractor.java
@@ -50,6 +50,8 @@ import org.apache.xtable.model.stat.PartitionValue;
 import org.apache.xtable.model.stat.Range;
 
 public class TestDeltaKernelPartitionExtractor {
+  private static final String PHYSICAL_NAME = 
"col-1a2b3c4d-5e6f-4a5b-8c9d-0e1f2a3b4c5d";
+
   private static final Map<String, StructField> STRUCT_FIELD_MAP =
       new HashMap<String, StructField>() {
         {
@@ -452,6 +454,43 @@ public class TestDeltaKernelPartitionExtractor {
     assertEquals(expectedPartitionValues, partitionValues);
   }
 
+  @Test
+  public void testPartitionValueExtractionWithColumnMappingPhysicalName() {
+    // With column mapping, partition values are keyed by the physical name, 
not the logical one.
+    InternalPartitionField partitionField = 
columnMappedPartitionField("country", PHYSICAL_NAME);
+    List<PartitionValue> partitionValues =
+        deltaKernelPartitionExtractor.partitionValueExtraction(
+            Collections.singletonMap(PHYSICAL_NAME, "USA"),
+            Collections.singletonList(partitionField));
+    assertEquals(
+        Collections.singletonList(
+            PartitionValue.builder()
+                .partitionField(partitionField)
+                .range(Range.scalar("USA"))
+                .build()),
+        partitionValues);
+  }
+
+  @Test
+  public void 
testPartitionValueExtractionWithColumnMappingNullValueIsAuthoritative() {
+    // A null under the physical key must not fall back to the logical name, 
which can be another
+    // column's physical name after renames.
+    Map<String, String> partitionValuesMap = new HashMap<>();
+    partitionValuesMap.put(PHYSICAL_NAME, null);
+    partitionValuesMap.put("country", "USA");
+    InternalPartitionField partitionField = 
columnMappedPartitionField("country", PHYSICAL_NAME);
+    List<PartitionValue> partitionValues =
+        deltaKernelPartitionExtractor.partitionValueExtraction(
+            partitionValuesMap, Collections.singletonList(partitionField));
+    assertEquals(
+        Collections.singletonList(
+            PartitionValue.builder()
+                .partitionField(partitionField)
+                .range(Range.scalar(null))
+                .build()),
+        partitionValues);
+  }
+
   @Test
   public void testYearMonthDayHourGeneratedPartitionValueExtraction() {
     // year, month and day are generated in the table as based on 
some_date_column which is of
@@ -606,6 +645,20 @@ public class TestDeltaKernelPartitionExtractor {
     assertEquals(expected, actual);
   }
 
+  private static InternalPartitionField columnMappedPartitionField(
+      String name, String physicalName) {
+    return InternalPartitionField.builder()
+        .sourceField(
+            InternalField.builder()
+                .name(name)
+                .storageName(physicalName)
+                .schema(
+                    
InternalSchema.builder().name("string").dataType(InternalType.STRING).build())
+                .build())
+        .transformType(PartitionTransformType.VALUE)
+        .build();
+  }
+
   private scala.collection.mutable.Map<String, String> 
convertJavaMapToScalaMap(
       Map<String, String> javaMap) {
     return JavaConverters.mapAsScalaMapConverter(javaMap).asScala();
diff --git 
a/xtable-core/src/test/java/org/apache/xtable/kernel/TestDeltaKernelSchemaExtractor.java
 
b/xtable-core/src/test/java/org/apache/xtable/kernel/TestDeltaKernelSchemaExtractor.java
index 7051ac7d..3687929a 100644
--- 
a/xtable-core/src/test/java/org/apache/xtable/kernel/TestDeltaKernelSchemaExtractor.java
+++ 
b/xtable-core/src/test/java/org/apache/xtable/kernel/TestDeltaKernelSchemaExtractor.java
@@ -412,6 +412,28 @@ public class TestDeltaKernelSchemaExtractor {
             "_one_field_element"));
   }
 
+  @Test
+  public void testColumnMappingPhysicalNamePropagatedAsStorageName() {
+    // A renamed column keeps its pre-rename physical name, which Iceberg's 
name mapping needs to
+    // read files written before the rename.
+    FieldMetadata renamedMetadata =
+        FieldMetadata.builder()
+            .putLong("delta.columnMapping.id", 3)
+            .putString("delta.columnMapping.physicalName", "name")
+            .build();
+    StructType structRepresentation =
+        new StructType()
+            .add("new_name", StringType.STRING, true, renamedMetadata)
+            .add("id", IntegerType.INTEGER, false);
+
+    InternalSchema internalSchema =
+        
DeltaKernelSchemaExtractor.getInstance().toInternalSchema(structRepresentation);
+
+    Assertions.assertEquals(3, fieldId(internalSchema, "new_name"));
+    Assertions.assertEquals("name", storageName(internalSchema, "new_name"));
+    Assertions.assertNull(storageName(internalSchema, "id"));
+  }
+
   private static FieldMetadata columnMappingMetadata(
       long id, String physicalName, FieldMetadata nestedIds) {
     return FieldMetadata.builder()
@@ -446,6 +468,14 @@ public class TestDeltaKernelSchemaExtractor {
     return found.getFieldId();
   }
 
+  private static String storageName(InternalSchema schema, String fieldName) {
+    return schema.getFields().stream()
+        .filter(field -> fieldName.equals(field.getName()))
+        .findFirst()
+        .orElseThrow(() -> new AssertionError("missing " + fieldName))
+        .getStorageName();
+  }
+
   @Test
   public void testTimestamps() {
     Map<InternalSchema.MetadataKey, Object> metadata =

Reply via email to