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 e5463d96 [1022] Read nested column stats in the Delta Kernel source
(#1024)
e5463d96 is described below
commit e5463d961ca4097887428935421ecd9297757261
Author: Neeraj <[email protected]>
AuthorDate: Wed Nov 25 03:51:30 2026 +0530
[1022] Read nested column stats in the Delta Kernel source (#1024)
* [1022] Read nested column stats in the Delta Kernel source
The Delta Kernel source passed only the top-level fields of the read
schema to the stats parser, in both DeltaKernelDataFileExtractor (snapshot
sync) and DeltaKernelConversionSource#getTableChangeForCommit (incremental
sync). Fields inside a struct got no min, max or null count, so Iceberg and
Hudi targets had no stats for them. The Delta Standalone source passes all
fields since #769. Pass getAllFields() in the Kernel source too.
Fixes #1022
* [1022] Share helpers between the Kernel stats tests
testNestedColumnStats now compares whole column stat lists keyed by record
count, as getCurrentSnapshotColumnMappingStatsTest does, through shared
getConversionSource, columnStatsByRecordCount and columnStat helpers. The
getStat and assertStat helpers are removed. The expected stats leave totalSize
unset, since the Kernel source no longer sets it after #1012.
---
.../xtable/kernel/DeltaKernelConversionSource.java | 2 +-
.../kernel/DeltaKernelDataFileExtractor.java | 2 +-
.../kernel/ITDeltaKernelConversionSource.java | 116 ++++++++++++++++-----
3 files changed, 94 insertions(+), 26 deletions(-)
diff --git
a/xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelConversionSource.java
b/xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelConversionSource.java
index ca0f4b94..3a391cd0 100644
---
a/xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelConversionSource.java
+++
b/xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelConversionSource.java
@@ -135,7 +135,7 @@ public class DeltaKernelConversionSource implements
ConversionSource<Long> {
tableBasePath, // Use cached base path
fileFormat,
tableAtVersion.getPartitioningFields(),
- tableAtVersion.getReadSchema().getFields(),
+ tableAtVersion.getReadSchema().getAllFields(),
true,
DeltaKernelPartitionExtractor.getInstance(),
DeltaKernelStatsExtractor.getInstance(),
diff --git
a/xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelDataFileExtractor.java
b/xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelDataFileExtractor.java
index 0ca90dff..abc64d32 100644
---
a/xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelDataFileExtractor.java
+++
b/xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelDataFileExtractor.java
@@ -93,7 +93,7 @@ public class DeltaKernelDataFileExtractor {
this.includeColumnStats = includeColumnStats;
this.table = table;
this.tableBasePath = table.getPath(engine); // Cache base path once
- this.fields = schema.getFields();
+ this.fields = schema.getAllFields();
String provider = ((SnapshotImpl)
snapshot).getMetadata().getFormat().getProvider();
this.fileFormat = actionsConverter.convertToFileFormat(provider);
diff --git
a/xtable-core/src/test/java/org/apache/xtable/kernel/ITDeltaKernelConversionSource.java
b/xtable-core/src/test/java/org/apache/xtable/kernel/ITDeltaKernelConversionSource.java
index 211c0c23..7cd659f9 100644
---
a/xtable-core/src/test/java/org/apache/xtable/kernel/ITDeltaKernelConversionSource.java
+++
b/xtable-core/src/test/java/org/apache/xtable/kernel/ITDeltaKernelConversionSource.java
@@ -35,6 +35,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -376,14 +377,7 @@ public class ITDeltaKernelConversionSource {
sparkSession.sql("ALTER TABLE `" + tableName + "` RENAME COLUMN a TO
a_old");
sparkSession.sql("ALTER TABLE `" + tableName + "` ADD COLUMNS (a INT)");
sparkSession.sql("INSERT INTO TABLE `" + tableName + "` VALUES (3, 30,
300)");
- SourceTable tableConfig =
- SourceTable.builder()
- .name(tableName)
- .basePath(basePath.toString())
- .formatName(TableFormat.DELTA)
- .build();
- DeltaKernelConversionSource conversionSource =
- conversionSourceProvider.getConversionSourceInstance(tableConfig);
+ DeltaKernelConversionSource conversionSource =
getConversionSource(tableName, basePath);
InternalSnapshot snapshot = conversionSource.getCurrentSnapshot();
@@ -396,25 +390,17 @@ public class ITDeltaKernelConversionSource {
InternalField a = fields.get(2);
assertEquals("a", aOld.getStorageName());
assertTrue(a.getStorageName().startsWith("col-"));
- List<InternalDataFile> dataFiles =
- snapshot.getPartitionedDataFiles().stream()
- .flatMap(group -> group.getDataFiles().stream())
- .collect(Collectors.toList());
- assertEquals(2, dataFiles.size());
- Map<Long, List<ColumnStat>> columnStatsByRecordCount =
- dataFiles.stream()
- .collect(
- Collectors.toMap(
- InternalDataFile::getRecordCount,
InternalDataFile::getColumnStats));
+ Map<Long, List<ColumnStat>> columnStatsByRecordCount =
columnStatsByRecordCount(snapshot);
+ assertEquals(2, columnStatsByRecordCount.size());
// The file written before a was re-added has no stats for the new a.
assertEquals(
- Arrays.asList(intColumnStat(id, 1, 2, 2), intColumnStat(aOld, 10, 20,
2)),
+ Arrays.asList(columnStat(id, 1, 2, 0, 2), columnStat(aOld, 10, 20, 0,
2)),
columnStatsByRecordCount.get(2L));
assertEquals(
Arrays.asList(
- intColumnStat(id, 3, 3, 1),
- intColumnStat(aOld, 30, 30, 1),
- intColumnStat(a, 300, 300, 1)),
+ columnStat(id, 3, 3, 0, 1),
+ columnStat(aOld, 30, 30, 0, 1),
+ columnStat(a, 300, 300, 0, 1)),
columnStatsByRecordCount.get(1L));
}
@@ -497,6 +483,69 @@ public class ITDeltaKernelConversionSource {
assertTrue(commitsBacklog.getCommitsToProcess().isEmpty());
}
+ @Test
+ void testNestedColumnStats() {
+ final String tableName = GenericTable.getTableName();
+ final Path basePath = tempDir.resolve(tableName);
+ sparkSession.sql(
+ "CREATE TABLE `"
+ + tableName
+ + "` (id INT, nested STRUCT<name: STRING, score: INT>) USING DELTA
LOCATION '"
+ + basePath
+ + "'");
+ sparkSession.sql(
+ "INSERT INTO TABLE `"
+ + tableName
+ + "` VALUES (1, named_struct('name', 'a', 'score', 10)),"
+ + " (2, named_struct('name', 'b', 'score', NULL))");
+ Engine engine = DefaultEngine.create(new Configuration());
+ long timestamp1 =
+ Table.forPath(engine,
basePath.toString()).getLatestSnapshot(engine).getTimestamp(engine);
+ sparkSession.sql(
+ "INSERT INTO TABLE `"
+ + tableName
+ + "` VALUES (3, named_struct('name', 'c', 'score', 30)),"
+ + " (4, named_struct('name', NULL, 'score', 40)),"
+ + " (5, named_struct('name', 'd', 'score', 50))");
+ DeltaKernelConversionSource conversionSource =
getConversionSource(tableName, basePath);
+
+ // The snapshot read goes through DeltaKernelDataFileExtractor.
+ InternalSnapshot snapshot = conversionSource.getCurrentSnapshot();
+ Map<String, InternalField> fieldsByPath =
+ snapshot.getTable().getReadSchema().getAllFields().stream()
+ .collect(Collectors.toMap(InternalField::getPath, field -> field));
+ InternalField id = fieldsByPath.get("id");
+ InternalField name = fieldsByPath.get("nested.name");
+ InternalField score = fieldsByPath.get("nested.score");
+ List<ColumnStat> secondFileStats =
+ Arrays.asList(
+ columnStat(id, 3, 5, 0, 3),
+ columnStat(name, "c", "d", 1, 3),
+ columnStat(score, 30, 50, 0, 3));
+ Map<Long, List<ColumnStat>> columnStatsByRecordCount =
columnStatsByRecordCount(snapshot);
+ assertEquals(2, columnStatsByRecordCount.size());
+ assertEquals(
+ Arrays.asList(
+ columnStat(id, 1, 2, 0, 2),
+ columnStat(name, "a", "b", 0, 2),
+ columnStat(score, 10, 10, 1, 2)),
+ columnStatsByRecordCount.get(2L));
+ assertEquals(secondFileStats, columnStatsByRecordCount.get(3L));
+
+ // The incremental read goes through
DeltaKernelConversionSource#getTableChangeForCommit.
+ CommitsBacklog<Long> commitsBacklog =
+ conversionSource.getCommitsBacklog(
+ InstantsForIncrementalSync.builder()
+ .lastSyncInstant(Instant.ofEpochMilli(timestamp1))
+ .build());
+ assertEquals(1, commitsBacklog.getCommitsToProcess().size());
+ TableChange tableChange =
+
conversionSource.getTableChangeForCommit(commitsBacklog.getCommitsToProcess().get(0));
+ Set<InternalDataFile> addedFiles =
tableChange.getFilesDiff().dataFilesAdded();
+ assertEquals(1, addedFiles.size());
+ assertEquals(secondFileStats,
addedFiles.iterator().next().getColumnStats());
+ }
+
@Test
public void testsShowingVacuumHasNoEffectOnIncrementalSync() {
boolean isPartitioned = true;
@@ -809,11 +858,30 @@ public class ITDeltaKernelConversionSource {
return Stream.of(Arguments.of(false), Arguments.of(true));
}
- private static ColumnStat intColumnStat(InternalField field, int min, int
max, long numValues) {
+ private DeltaKernelConversionSource getConversionSource(String tableName,
Path basePath) {
+ SourceTable tableConfig =
+ SourceTable.builder()
+ .name(tableName)
+ .basePath(basePath.toString())
+ .formatName(TableFormat.DELTA)
+ .build();
+ return conversionSourceProvider.getConversionSourceInstance(tableConfig);
+ }
+
+ /** Returns the column stats of each data file in the snapshot, keyed by its
record count. */
+ private static Map<Long, List<ColumnStat>>
columnStatsByRecordCount(InternalSnapshot snapshot) {
+ return snapshot.getPartitionedDataFiles().stream()
+ .flatMap(group -> group.getDataFiles().stream())
+ .collect(
+ Collectors.toMap(InternalDataFile::getRecordCount,
InternalDataFile::getColumnStats));
+ }
+
+ private static ColumnStat columnStat(
+ InternalField field, Object min, Object max, long numNulls, long
numValues) {
return ColumnStat.builder()
.field(field)
.range(Range.vector(min, max))
- .numNulls(0L)
+ .numNulls(numNulls)
.numValues(numValues)
.build();
}