vinishjail97 commented on code in PR #801: URL: https://github.com/apache/incubator-xtable/pull/801#discussion_r3331836192
########## 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); Review Comment: Addressed — the redundant 3-arg overload was removed; there is now a single `applySnapshot` signature exercised by both production and tests. ########## xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelActionsConverter.java: ########## @@ -50,6 +50,47 @@ public static DeltaKernelActionsConverter getInstance() { return INSTANCE; } + /** + * Converts AddFile to InternalDataFile using cached table base path (most efficient). + * + * @param tableBasePath cached table base path from table.getPath(engine) + */ + public InternalDataFile convertAddActionToInternalDataFile( + AddFile addFile, + String tableBasePath, + FileFormat fileFormat, + List<InternalPartitionField> partitionFields, + List<InternalField> fields, + boolean includeColumnStats, + DeltaKernelPartitionExtractor partitionExtractor, + DeltaKernelStatsExtractor fileStatsExtractor, + Map<String, String> partitionValues) { + FileStats fileStats = fileStatsExtractor.getColumnStatsForFile(addFile, fields); + List<ColumnStat> columnStats = + includeColumnStats ? fileStats.getColumnStats() : Collections.emptyList(); + long recordCount = fileStats.getNumRecords(); + + Map<String, String> scalaMap = partitionValues; Review Comment: Addressed — the misleading `scalaMap` aliases were removed; `partitionValues` is used directly. ########## xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelActionsConverter.java: ########## @@ -50,6 +50,47 @@ public static DeltaKernelActionsConverter getInstance() { return INSTANCE; } + /** + * Converts AddFile to InternalDataFile using cached table base path (most efficient). + * + * @param tableBasePath cached table base path from table.getPath(engine) + */ + public InternalDataFile convertAddActionToInternalDataFile( + AddFile addFile, + String tableBasePath, + FileFormat fileFormat, + List<InternalPartitionField> partitionFields, + List<InternalField> fields, + boolean includeColumnStats, + DeltaKernelPartitionExtractor partitionExtractor, + DeltaKernelStatsExtractor fileStatsExtractor, + Map<String, String> partitionValues) { + FileStats fileStats = fileStatsExtractor.getColumnStatsForFile(addFile, fields); + List<ColumnStat> columnStats = + includeColumnStats ? fileStats.getColumnStats() : Collections.emptyList(); + long recordCount = fileStats.getNumRecords(); + + Map<String, String> scalaMap = partitionValues; + + return InternalDataFile.builder() + .physicalPath(getFullPathToFile(addFile.getPath(), tableBasePath)) + .fileFormat(fileFormat) + .fileSizeBytes(addFile.getSize()) + .lastModified(addFile.getModificationTime()) + .partitionValues(partitionExtractor.partitionValueExtraction(scalaMap, partitionFields)) + .columnStats(columnStats) + .recordCount(recordCount) + .build(); + } + + /** + * Converts AddFile to InternalDataFile (deprecated - inefficient). + * + * @deprecated Use {@link #convertAddActionToInternalDataFile(AddFile, String, FileFormat, List, + * List, boolean, DeltaKernelPartitionExtractor, DeltaKernelStatsExtractor, Map)} with cached + * tableBasePath instead to avoid creating new Engine per call + */ + @Deprecated Review Comment: Addressed — the dead `@Deprecated` `Table`-taking overloads were removed. -- 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]
