vinishjail97 commented on code in PR #801:
URL: https://github.com/apache/incubator-xtable/pull/801#discussion_r3331835005


##########
xtable-core/src/main/java/org/apache/xtable/delta/DeltaStatsUtils.java:
##########
@@ -0,0 +1,339 @@
+/*
+ * 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.xtable.delta;
+
+import java.io.IOException;
+import java.util.ArrayDeque;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Queue;
+import java.util.Set;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+import lombok.AllArgsConstructor;
+import lombok.Value;
+import lombok.extern.log4j.Log4j2;
+
+import org.apache.commons.lang3.StringUtils;
+
+import com.fasterxml.jackson.annotation.JsonAnySetter;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.annotations.VisibleForTesting;
+
+import org.apache.xtable.collectors.CustomCollectors;
+import org.apache.xtable.model.exception.ParseException;
+import org.apache.xtable.model.schema.InternalField;
+import org.apache.xtable.model.schema.InternalSchema;
+import org.apache.xtable.model.schema.InternalType;
+import org.apache.xtable.model.stat.ColumnStat;
+import org.apache.xtable.model.stat.FileStats;
+import org.apache.xtable.model.stat.Range;
+
+/**
+ * Shared utility for Delta stats conversion between XTable internal format 
and Delta format. Used
+ * by both Delta Standalone and Delta Kernel implementations.
+ */
+@Log4j2
+public class DeltaStatsUtils {
+  private static final Set<InternalType> FIELD_TYPES_WITH_STATS_SUPPORT =
+      new HashSet<>(
+          Arrays.asList(
+              InternalType.BOOLEAN,
+              InternalType.DATE,
+              InternalType.DECIMAL,
+              InternalType.DOUBLE,
+              InternalType.INT,
+              InternalType.LONG,
+              InternalType.FLOAT,
+              InternalType.STRING,
+              InternalType.TIMESTAMP,
+              InternalType.TIMESTAMP_NTZ));
+
+  private static final ObjectMapper MAPPER = new ObjectMapper();
+
+  /* this data structure collects type names of all unrecognized Delta Lake 
stats. For instance
+  data file stats in presence of delete vectors would contain 'tightBounds' 
stat which is
+  currently not handled by XTable */
+  private static final Set<String> unsupportedStats = new HashSet<>();

Review Comment:
   Addressed — `unsupportedStats` is now `ConcurrentHashMap.newKeySet()` 
(thread-safe), and a `@VisibleForTesting clear()` is exposed so tests can reset 
the shared state.



##########
xtable-core/src/main/java/org/apache/xtable/delta/DeltaStatsUtils.java:
##########
@@ -0,0 +1,339 @@
+/*
+ * 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.xtable.delta;
+
+import java.io.IOException;
+import java.util.ArrayDeque;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Queue;
+import java.util.Set;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+import lombok.AllArgsConstructor;
+import lombok.Value;
+import lombok.extern.log4j.Log4j2;
+
+import org.apache.commons.lang3.StringUtils;
+
+import com.fasterxml.jackson.annotation.JsonAnySetter;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.annotations.VisibleForTesting;
+
+import org.apache.xtable.collectors.CustomCollectors;
+import org.apache.xtable.model.exception.ParseException;
+import org.apache.xtable.model.schema.InternalField;
+import org.apache.xtable.model.schema.InternalSchema;
+import org.apache.xtable.model.schema.InternalType;
+import org.apache.xtable.model.stat.ColumnStat;
+import org.apache.xtable.model.stat.FileStats;
+import org.apache.xtable.model.stat.Range;
+
+/**
+ * Shared utility for Delta stats conversion between XTable internal format 
and Delta format. Used
+ * by both Delta Standalone and Delta Kernel implementations.
+ */
+@Log4j2
+public class DeltaStatsUtils {
+  private static final Set<InternalType> FIELD_TYPES_WITH_STATS_SUPPORT =
+      new HashSet<>(
+          Arrays.asList(
+              InternalType.BOOLEAN,
+              InternalType.DATE,
+              InternalType.DECIMAL,
+              InternalType.DOUBLE,
+              InternalType.INT,
+              InternalType.LONG,
+              InternalType.FLOAT,
+              InternalType.STRING,
+              InternalType.TIMESTAMP,
+              InternalType.TIMESTAMP_NTZ));
+
+  private static final ObjectMapper MAPPER = new ObjectMapper();
+
+  /* this data structure collects type names of all unrecognized Delta Lake 
stats. For instance
+  data file stats in presence of delete vectors would contain 'tightBounds' 
stat which is
+  currently not handled by XTable */
+  private static final Set<String> unsupportedStats = new HashSet<>();
+
+  private DeltaStatsUtils() {
+    // Utility class, prevent instantiation
+  }
+
+  /**
+   * Converts XTable internal column statistics to Delta format JSON string.
+   *
+   * @param schema the table schema
+   * @param numRecords the number of records in the file
+   * @param columnStats the list of column statistics
+   * @return JSON string in Delta stats format
+   * @throws JsonProcessingException if serialization fails
+   */
+  public static String convertStatsToDeltaFormat(
+      InternalSchema schema, long numRecords, List<ColumnStat> columnStats)
+      throws JsonProcessingException {
+    DeltaStats.DeltaStatsBuilder deltaStatsBuilder = DeltaStats.builder();
+    deltaStatsBuilder.numRecords(numRecords);
+    if (columnStats == null) {
+      return MAPPER.writeValueAsString(deltaStatsBuilder.build());
+    }
+    Set<String> validPaths = getPathsFromStructSchemaForMinAndMaxStats(schema);
+    List<ColumnStat> validColumnStats =
+        columnStats.stream()
+            .filter(stat -> validPaths.contains(stat.getField().getPath()))
+            .collect(Collectors.toList());
+    DeltaStats deltaStats =
+        deltaStatsBuilder
+            .minValues(getMinValues(validColumnStats))
+            .maxValues(getMaxValues(validColumnStats))
+            .nullCount(getNullCount(validColumnStats))
+            .build();
+    return MAPPER.writeValueAsString(deltaStats);
+  }
+
+  /**
+   * Parses Delta stats JSON and extracts column statistics for the specified 
fields.
+   *
+   * @param statsJson the Delta stats JSON string
+   * @param fields the fields to extract stats for
+   * @return FileStats containing column statistics and record count
+   * @throws ParseException if JSON parsing fails
+   */
+  public static FileStats parseColumnStatsFromJson(String statsJson, 
List<InternalField> fields) {
+    if (StringUtils.isEmpty(statsJson)) {
+      return 
FileStats.builder().columnStats(Collections.emptyList()).numRecords(0).build();
+    }
+
+    try {
+      DeltaStats deltaStats = MAPPER.readValue(statsJson, DeltaStats.class);
+
+      collectUnsupportedStats(deltaStats.getAdditionalStats());
+
+      Map<String, Object> fieldPathToMaxValue = 
flattenStatMap(deltaStats.getMaxValues());
+      Map<String, Object> fieldPathToMinValue = 
flattenStatMap(deltaStats.getMinValues());
+      Map<String, Object> fieldPathToNullCount = 
flattenStatMap(deltaStats.getNullCount());
+
+      List<ColumnStat> columnStats =
+          fields.stream()
+              .filter(field -> 
fieldPathToMaxValue.containsKey(field.getPath()))
+              .map(
+                  field -> {
+                    String fieldPath = field.getPath();
+                    Object minRaw = fieldPathToMinValue.get(fieldPath);
+                    Object maxRaw = fieldPathToMaxValue.get(fieldPath);
+                    Object nullCountRaw = fieldPathToNullCount.get(fieldPath);
+
+                    Object minValue =
+                        minRaw != null
+                            ? 
DeltaValueConverter.convertFromDeltaColumnStatValue(
+                                minRaw, field.getSchema())
+                            : null;
+                    Object maxValue =
+                        maxRaw != null
+                            ? 
DeltaValueConverter.convertFromDeltaColumnStatValue(
+                                maxRaw, field.getSchema())
+                            : null;
+                    long nullCount =
+                        nullCountRaw instanceof Number ? ((Number) 
nullCountRaw).longValue() : 0;
+
+                    Range range = Range.vector(minValue, maxValue);
+                    return ColumnStat.builder()
+                        .field(field)
+                        .numValues(deltaStats.getNumRecords())
+                        .numNulls(nullCount)
+                        .range(range)
+                        .build();
+                  })
+              .collect(CustomCollectors.toList(fields.size()));
+
+      return FileStats.builder()
+          .columnStats(columnStats)
+          .numRecords(deltaStats.getNumRecords())
+          .build();
+    } catch (IOException ex) {
+      throw new ParseException("Unable to parse stats json", ex);
+    }
+  }
+
+  private static Set<String> 
getPathsFromStructSchemaForMinAndMaxStats(InternalSchema schema) {
+    return schema.getAllFields().stream()
+        .filter(
+            field -> {
+              InternalType type = field.getSchema().getDataType();
+              return FIELD_TYPES_WITH_STATS_SUPPORT.contains(type);
+            })
+        .map(InternalField::getPath)
+        .collect(Collectors.toSet());
+  }
+
+  private static Map<String, Object> getMinValues(List<ColumnStat> 
validColumnStats) {
+    return getValues(validColumnStats, columnStat -> 
columnStat.getRange().getMinValue());
+  }
+
+  private static Map<String, Object> getMaxValues(List<ColumnStat> 
validColumnStats) {
+    return getValues(validColumnStats, columnStat -> 
columnStat.getRange().getMaxValue());
+  }
+
+  private static Map<String, Object> getValues(
+      List<ColumnStat> validColumnStats, Function<ColumnStat, Object> 
valueExtractor) {
+    Map<String, Object> jsonObject = new HashMap<>();
+    validColumnStats.forEach(
+        columnStat -> {
+          InternalField field = columnStat.getField();
+          String[] pathParts = field.getPathParts();
+          insertValueAtPath(
+              jsonObject,
+              pathParts,
+              DeltaValueConverter.convertToDeltaColumnStatValue(
+                  valueExtractor.apply(columnStat), field.getSchema()));
+        });
+    return jsonObject;
+  }
+
+  private static Map<String, Object> getNullCount(List<ColumnStat> 
validColumnStats) {
+    // TODO: Additional work needed to track nulls maps & arrays.
+    Map<String, Object> jsonObject = new HashMap<>();
+    validColumnStats.forEach(
+        columnStat -> {
+          String[] pathParts = columnStat.getField().getPathParts();
+          insertValueAtPath(jsonObject, pathParts, columnStat.getNumNulls());
+        });
+    return jsonObject;
+  }
+
+  private static void insertValueAtPath(
+      Map<String, Object> jsonObject, String[] pathParts, Object value) {
+    if (pathParts == null || pathParts.length == 0) {
+      return;
+    }
+    Map<String, Object> currObject = jsonObject;
+    for (int i = 0; i < pathParts.length; i++) {
+      String part = pathParts[i];
+      if (i == pathParts.length - 1) {
+        currObject.put(part, value);
+      } else {
+        if (!currObject.containsKey(part)) {
+          currObject.put(part, new HashMap<String, Object>());
+        }
+        try {
+          currObject = (HashMap<String, Object>) currObject.get(part);
+        } catch (ClassCastException e) {
+          throw new RuntimeException(

Review Comment:
   Addressed — the cast failure now throws `ParseException` instead of a 
generic `RuntimeException`.



##########
xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelDataFileUpdatesExtractor.java:
##########
@@ -0,0 +1,361 @@
+/*
+ * 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.xtable.kernel;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Stream;
+
+import lombok.Builder;
+
+import scala.collection.JavaConverters;
+import scala.collection.Seq;
+
+import io.delta.kernel.Snapshot;
+import io.delta.kernel.Table;
+import io.delta.kernel.data.MapValue;
+import io.delta.kernel.data.Row;
+import io.delta.kernel.engine.Engine;
+import io.delta.kernel.expressions.Column;
+import io.delta.kernel.expressions.Literal;
+import io.delta.kernel.internal.actions.AddFile;
+import io.delta.kernel.internal.actions.RemoveFile;
+import io.delta.kernel.internal.actions.RowBackedAction;
+import io.delta.kernel.internal.util.VectorUtils;
+import io.delta.kernel.statistics.DataFileStatistics;
+import io.delta.kernel.types.StructType;
+
+import org.apache.xtable.collectors.CustomCollectors;
+import org.apache.xtable.exception.ReadException;
+import org.apache.xtable.model.schema.InternalField;
+import org.apache.xtable.model.schema.InternalSchema;
+import org.apache.xtable.model.schema.InternalType;
+import org.apache.xtable.model.stat.ColumnStat;
+import org.apache.xtable.model.stat.Range;
+import org.apache.xtable.model.storage.FilesDiff;
+import org.apache.xtable.model.storage.InternalDataFile;
+import org.apache.xtable.model.storage.InternalFile;
+import org.apache.xtable.model.storage.InternalFilesDiff;
+import org.apache.xtable.model.storage.PartitionFileGroup;
+import org.apache.xtable.paths.PathUtils;
+import org.apache.xtable.spi.extractor.DataFileIterator;
+
+@Builder
+public class DeltaKernelDataFileUpdatesExtractor {
+  @Builder.Default
+  private final DeltaKernelStatsExtractor deltaStatsExtractor =
+      DeltaKernelStatsExtractor.getInstance();
+
+  @Builder.Default
+  private final DeltaKernelPartitionExtractor deltaKernelPartitionExtractor =
+      DeltaKernelPartitionExtractor.getInstance();
+
+  @Builder.Default
+  private final DeltaKernelDataFileExtractor dataFileExtractor =
+      DeltaKernelDataFileExtractor.builder().build();
+
+  private final Engine engine;
+  private final String basePath;
+  private final boolean includeColumnStats;
+
+  /**
+   * Applies snapshot changes, loading the snapshot fresh.
+   *
+   * @param table the Delta table
+   * @param partitionedDataFiles the new data files to sync
+   * @param tableSchema the internal schema
+   * @return sequence of Delta actions (AddFile/RemoveFile)
+   */
+  public Seq<RowBackedAction> applySnapshot(
+      Table table, List<PartitionFileGroup> partitionedDataFiles, 
InternalSchema tableSchema) {
+    return applySnapshot(table, partitionedDataFiles, tableSchema, null);
+  }
+
+  /**
+   * Applies snapshot changes with an optional cached snapshot to avoid 
redundant loads.
+   *
+   * @param table the Delta table
+   * @param partitionedDataFiles the new data files to sync
+   * @param tableSchema the internal schema
+   * @param cachedSnapshot optional pre-loaded snapshot (null to load fresh)
+   * @return sequence of Delta actions (AddFile/RemoveFile)
+   */
+  public Seq<RowBackedAction> applySnapshot(
+      Table table,
+      List<PartitionFileGroup> partitionedDataFiles,
+      InternalSchema tableSchema,
+      Snapshot cachedSnapshot) {
+
+    // all files in the current delta snapshot are potential candidates for 
remove actions, i.e. if
+    // the file is not present in the new snapshot (addedFiles) then the file 
is considered removed
+    Map<String, RowBackedAction> previousFiles = new HashMap<>();
+    StructType physicalSchema;
+
+    // Use cached snapshot if provided, otherwise load it
+    boolean tableExists = cachedSnapshot != null || checkTableExists(table);
+
+    if (tableExists) {
+      Snapshot snapshot = cachedSnapshot != null ? cachedSnapshot : 
table.getLatestSnapshot(engine);
+
+      // Reuse DeltaKernelDataFileExtractor to iterate through existing files
+      // This avoids duplicating the scan logic for reading Delta files
+      try (DataFileIterator fileIterator =
+          dataFileExtractor.iterator(snapshot, table, engine, tableSchema)) {
+
+        while (fileIterator.hasNext()) {
+          InternalDataFile internalFile = fileIterator.next();
+
+          // Convert InternalDataFile back to AddFile to create RemoveFile 
action
+          AddFile addFile =
+              createAddFileAction(internalFile, tableSchema, basePath, 
snapshot.getSchema());
+          RemoveFile removeFile =
+              new RemoveFile(addFile.toRemoveFileRow(false, 
Optional.of(snapshot.getVersion())));
+          // Use optimized path construction with Engine (reuses existing 
engine, no new
+          // Configuration)
+          String fullPath =
+              
DeltaKernelActionsConverter.getFullPathToFile(removeFile.getPath(), engine, 
table);
+          previousFiles.put(fullPath, removeFile);
+        }
+      } catch (Exception e) {
+        throw new ReadException("Failed to scan existing Delta files", e);
+      }
+
+      physicalSchema = snapshot.getSchema();
+
+    } else {
+
+      // Table doesn't exist yet - no previous files to remove
+      // Convert InternalSchema to StructType for physical schema
+      DeltaKernelSchemaExtractor schemaExtractor = 
DeltaKernelSchemaExtractor.getInstance();
+      physicalSchema = schemaExtractor.fromInternalSchema(tableSchema);
+    }
+
+    FilesDiff<InternalFile, RowBackedAction> diff =
+        InternalFilesDiff.findNewAndRemovedFiles(partitionedDataFiles, 
previousFiles);
+
+    return applyDiff(
+        diff.getFilesAdded(), diff.getFilesRemoved(), tableSchema, basePath, 
physicalSchema);
+  }
+
+  private boolean checkTableExists(Table table) {
+    return DeltaKernelUtils.tableExists(engine, table.getPath(engine));
+  }
+
+  public Seq<RowBackedAction> applyDiff(
+      InternalFilesDiff internalFilesDiff,
+      InternalSchema tableSchema,
+      String tableBasePath,
+      StructType physicalSchema) {
+    List<RowBackedAction> removeActions =
+        internalFilesDiff.dataFilesRemoved().stream()
+            .map(dFile -> createAddFileAction(dFile, tableSchema, 
tableBasePath, physicalSchema))
+            .map(addFile -> new RemoveFile(addFile.toRemoveFileRow(false, 
Optional.empty())))
+            
.collect(CustomCollectors.toList(internalFilesDiff.dataFilesRemoved().size()));
+    return applyDiff(
+        internalFilesDiff.dataFilesAdded(),
+        removeActions,
+        tableSchema,
+        tableBasePath,
+        physicalSchema);
+  }
+
+  private Seq<RowBackedAction> applyDiff(
+      Set<? extends InternalFile> filesAdded,
+      Collection<RowBackedAction> removeFileActions,
+      InternalSchema tableSchema,
+      String tableBasePath,
+      StructType physicalSchema) {
+    Stream<RowBackedAction> addActions =
+        filesAdded.stream()
+            .filter(InternalDataFile.class::isInstance)
+            .map(file -> (InternalDataFile) file)
+            .map(dFile -> createAddFileAction(dFile, tableSchema, 
tableBasePath, physicalSchema));
+    int totalActions = filesAdded.size() + removeFileActions.size();
+    List<RowBackedAction> allActions =
+        Stream.concat(addActions, removeFileActions.stream())
+            .collect(CustomCollectors.toList(totalActions));
+    return JavaConverters.asScalaBuffer(allActions).toSeq();
+  }
+
+  private AddFile createAddFileAction(
+      InternalDataFile dataFile,
+      InternalSchema tableSchema,
+      String tableBasePath,
+      StructType physicalSchema) {
+    // Convert partition values from Map<String, String> to MapValue
+    Map<String, String> partitionValuesMap =
+        deltaKernelPartitionExtractor.partitionValueSerialization(dataFile);
+    MapValue partitionValues = convertToMapValue(partitionValuesMap);
+
+    // Generate column stats if enabled
+    Optional<DataFileStatistics> stats = Optional.empty();
+    if (includeColumnStats) {
+      stats =
+          Optional.of(
+              convertToDataFileStatistics(dataFile.getRecordCount(), 
dataFile.getColumnStats()));
+    }
+
+    Row addFileRow =
+        AddFile.createAddFileRow(
+            physicalSchema,
+            // Delta Lake supports relative and absolute paths in theory but 
relative paths seem
+            // more commonly supported by query engines in our testing
+            PathUtils.getRelativePath(dataFile.getPhysicalPath(), 
tableBasePath),
+            partitionValues,
+            dataFile.getFileSizeBytes(),
+            dataFile.getLastModified(),
+            true, // dataChange
+            Optional.empty(), // deletionVector
+            Optional.empty(), // tags
+            Optional.empty(), // baseRowId
+            Optional.empty(), // defaultRowCommitVersion
+            stats // stats - converted from InternalDataFile column stats
+            );
+
+    // Wrap the Row back into an AddFile object so we can use its methods
+    return new AddFile(addFileRow);
+  }
+
+  /**
+   * Converts XTable's internal column statistics to Delta Kernel's 
DataFileStatistics format.
+   *
+   * @param recordCount the number of records in the file
+   * @param columnStats the list of column statistics from InternalDataFile
+   * @return DataFileStatistics object for Delta Kernel
+   */
+  private DataFileStatistics convertToDataFileStatistics(
+      long recordCount, List<ColumnStat> columnStats) {
+    if (columnStats == null || columnStats.isEmpty()) {
+      return new DataFileStatistics(recordCount, new HashMap<>(), new 
HashMap<>(), new HashMap<>());
+    }
+
+    Map<Column, Literal> minValues = new HashMap<>();
+    Map<Column, Literal> maxValues = new HashMap<>();
+    Map<Column, Long> nullCounts = new HashMap<>();
+
+    for (ColumnStat columnStat : columnStats) {
+      InternalField field = columnStat.getField();
+      InternalType dataType = field.getSchema().getDataType();
+
+      // Only process supported types for statistics
+      if (!isSupportedStatsType(dataType)) {
+        continue;
+      }
+
+      // Create Column reference from field path
+      Column column = new Column(field.getPathParts());
+
+      // Extract min/max from range
+      Range range = columnStat.getRange();
+      if (range != null) {
+        Object minValue = range.getMinValue();
+        Object maxValue = range.getMaxValue();
+
+        if (minValue != null) {
+          Literal minLiteral = convertToLiteral(minValue, field.getSchema());
+          if (minLiteral != null) {
+            minValues.put(column, minLiteral);
+          }
+        }
+
+        if (maxValue != null) {
+          Literal maxLiteral = convertToLiteral(maxValue, field.getSchema());
+          if (maxLiteral != null) {
+            maxValues.put(column, maxLiteral);
+          }
+        }
+      }
+
+      // Add null count
+      nullCounts.put(column, columnStat.getNumNulls());
+    }
+
+    return new DataFileStatistics(recordCount, minValues, maxValues, 
nullCounts);
+  }
+
+  /** Checks if a data type is supported for statistics. */
+  private boolean isSupportedStatsType(InternalType type) {
+    return type == InternalType.BOOLEAN
+        || type == InternalType.DATE
+        || type == InternalType.DECIMAL
+        || type == InternalType.DOUBLE
+        || type == InternalType.INT
+        || type == InternalType.LONG
+        || type == InternalType.FLOAT
+        || type == InternalType.STRING
+        || type == InternalType.TIMESTAMP
+        || type == InternalType.TIMESTAMP_NTZ;
+  }
+
+  /** Converts an XTable value to a Delta Kernel Literal based on the field 
schema. */
+  private Literal convertToLiteral(Object value, InternalSchema fieldSchema) {
+    InternalType dataType = fieldSchema.getDataType();
+
+    switch (dataType) {
+      case BOOLEAN:
+        return Literal.ofBoolean((Boolean) value);
+      case INT:
+        return Literal.ofInt(((Number) value).intValue());
+      case LONG:
+        return Literal.ofLong(((Number) value).longValue());
+      case FLOAT:
+        return Literal.ofFloat(((Number) value).floatValue());
+      case DOUBLE:
+        return Literal.ofDouble(((Number) value).doubleValue());
+      case STRING:
+        return Literal.ofString((String) value);
+      case DATE:
+        // XTable stores dates as days since epoch (int)
+        return Literal.ofDate(((Number) value).intValue());
+      case TIMESTAMP:
+        // XTable stores timestamps as microseconds since epoch
+        return Literal.ofTimestamp(((Number) value).longValue());
+      case TIMESTAMP_NTZ:
+        // XTable stores timestamp_ntz as microseconds since epoch
+        return Literal.ofTimestampNtz(((Number) value).longValue());
+      case DECIMAL:
+        // Extract precision and scale from schema metadata
+        Map<InternalSchema.MetadataKey, Object> metadata = 
fieldSchema.getMetadata();
+        int precision = 10; // default precision

Review Comment:
   Addressed — the `precision=10, scale=0` default was removed; it now throws 
`IllegalStateException` when precision/scale metadata is missing for a DECIMAL 
field.



-- 
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]

Reply via email to