the-other-tim-brown commented on code in PR #729:
URL: https://github.com/apache/incubator-xtable/pull/729#discussion_r2217842319


##########
xtable-core/src/main/java/org/apache/xtable/delta/DeltaKernelDataFileExtractor.java:
##########
@@ -0,0 +1,152 @@
+/*
+ * 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 scala.collection.Map;
+import java.util.*;
+import java.util.stream.Collectors;
+
+import lombok.Builder;
+
+import org.apache.hadoop.conf.Configuration;
+
+import io.delta.kernel.Scan;
+import io.delta.kernel.Snapshot;
+import io.delta.kernel.data.FilteredColumnarBatch;
+import io.delta.kernel.data.Row;
+import io.delta.kernel.defaults.engine.DefaultEngine;
+import io.delta.kernel.engine.Engine;
+import io.delta.kernel.internal.InternalScanFileUtils;
+import io.delta.kernel.internal.SnapshotImpl;
+import io.delta.kernel.types.StructField;
+import io.delta.kernel.types.StructType;
+import io.delta.kernel.utils.CloseableIterator;
+import io.delta.kernel.utils.FileStatus;
+
+import org.apache.xtable.model.schema.InternalField;
+import org.apache.xtable.model.schema.InternalPartitionField;
+import org.apache.xtable.model.schema.InternalSchema;
+import org.apache.xtable.model.storage.FileFormat;
+import org.apache.xtable.model.storage.InternalDataFile;
+import org.apache.xtable.spi.extractor.DataFileIterator;
+
+/** DeltaDataFileExtractor lets the consumer iterate over partitions. */
+@Builder
+public class DeltaKernelDataFileExtractor {
+
+  @Builder.Default
+  private final DeltaKernelPartitionExtractor partitionExtractor =
+      DeltaKernelPartitionExtractor.getInstance();
+
+  @Builder.Default
+  private final DeltaKernelStatsExtractor fileStatsExtractor =
+      DeltaKernelStatsExtractor.getInstance();
+
+  @Builder.Default
+  private final DeltaKernelActionsConverter actionsConverter =
+      DeltaKernelActionsConverter.getInstance();
+
+  private final String basePath;
+
+  /**
+   * Initializes an iterator for Delta Lake files.
+   *
+   * @return Delta table file iterator
+   */
+  public DataFileIterator iterator(Snapshot deltaSnapshot, InternalSchema 
schema) {
+    return new DeltaDataFileIterator(deltaSnapshot, schema, true);
+  }
+
+  public class DeltaDataFileIterator implements DataFileIterator {
+    private final FileFormat fileFormat;
+    private final List<InternalField> fields;
+    private final List<InternalPartitionField> partitionFields;
+    private Iterator<InternalDataFile> dataFilesIterator = 
Collections.emptyIterator();
+
+    private DeltaDataFileIterator(
+        Snapshot snapshot, InternalSchema schema, boolean includeColumnStats) {
+      String provider = ((SnapshotImpl) 
snapshot).getMetadata().getFormat().getProvider();
+      this.fileFormat = actionsConverter.convertToFileFormat(provider);
+
+      this.fields = schema.getFields();
+
+      StructType fullSchema = snapshot.getSchema(); // The full table schema
+      List<String> partitionColumns = snapshot.getPartitionColumnNames(); // 
List<String>
+
+      List<StructField> partitionFields_strfld =
+          fullSchema.fields().stream()
+              .filter(field -> partitionColumns.contains(field.getName()))
+              .collect(Collectors.toList());
+
+      StructType partitionSchema = new StructType(partitionFields_strfld);
+
+      this.partitionFields =
+          partitionExtractor.convertFromDeltaPartitionFormat(schema, 
partitionSchema);
+      Configuration hadoopConf = new Configuration();
+      Engine engine = DefaultEngine.create(hadoopConf);
+
+      Scan myScan = snapshot.getScanBuilder().build();
+      CloseableIterator<FilteredColumnarBatch> scanFiles = 
myScan.getScanFiles(engine);
+      this.dataFilesIterator =
+          Collections
+              .emptyIterator(); // Initialize the dataFilesIterator by 
iterating over the scan files
+      while (scanFiles.hasNext()) {
+        FilteredColumnarBatch scanFileColumnarBatch = scanFiles.next();
+        CloseableIterator<Row> scanFileRows = scanFileColumnarBatch.getRows();
+        while (scanFileRows.hasNext()) {
+          Row scanFileRow = scanFileRows.next();

Review Comment:
   In that same test linked above, they use the AddFile object which may be 
useful to you:
   ```
             AddFile addFile = new 
AddFile(scanFileRow.getStruct(scanFileRow.getSchema().indexOf("add")));
             addFile.getStatsJson();
   ```
   It is important to note that the provided json parsing currently [does not 
extract the 
stats](https://github.com/delta-io/delta/blob/master/kernel/kernel-api/src/main/java/io/delta/kernel/internal/util/StatsUtils.java#L32)
 so we will still need to rely on our own 
[parsing](https://github.com/apache/incubator-xtable/blob/main/xtable-core/src/main/java/org/apache/xtable/delta/DeltaStatsExtractor.java#L195)
 of the string.



##########
xtable-core/src/main/java/org/apache/xtable/delta/DeltaKernelDataFileExtractor.java:
##########
@@ -0,0 +1,152 @@
+/*
+ * 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 scala.collection.Map;
+import java.util.*;
+import java.util.stream.Collectors;
+
+import lombok.Builder;
+
+import org.apache.hadoop.conf.Configuration;
+
+import io.delta.kernel.Scan;
+import io.delta.kernel.Snapshot;
+import io.delta.kernel.data.FilteredColumnarBatch;
+import io.delta.kernel.data.Row;
+import io.delta.kernel.defaults.engine.DefaultEngine;
+import io.delta.kernel.engine.Engine;
+import io.delta.kernel.internal.InternalScanFileUtils;
+import io.delta.kernel.internal.SnapshotImpl;
+import io.delta.kernel.types.StructField;
+import io.delta.kernel.types.StructType;
+import io.delta.kernel.utils.CloseableIterator;
+import io.delta.kernel.utils.FileStatus;
+
+import org.apache.xtable.model.schema.InternalField;
+import org.apache.xtable.model.schema.InternalPartitionField;
+import org.apache.xtable.model.schema.InternalSchema;
+import org.apache.xtable.model.storage.FileFormat;
+import org.apache.xtable.model.storage.InternalDataFile;
+import org.apache.xtable.spi.extractor.DataFileIterator;
+
+/** DeltaDataFileExtractor lets the consumer iterate over partitions. */
+@Builder
+public class DeltaKernelDataFileExtractor {
+
+  @Builder.Default
+  private final DeltaKernelPartitionExtractor partitionExtractor =
+      DeltaKernelPartitionExtractor.getInstance();
+
+  @Builder.Default
+  private final DeltaKernelStatsExtractor fileStatsExtractor =
+      DeltaKernelStatsExtractor.getInstance();
+
+  @Builder.Default
+  private final DeltaKernelActionsConverter actionsConverter =
+      DeltaKernelActionsConverter.getInstance();
+
+  private final String basePath;
+
+  /**
+   * Initializes an iterator for Delta Lake files.
+   *
+   * @return Delta table file iterator
+   */
+  public DataFileIterator iterator(Snapshot deltaSnapshot, InternalSchema 
schema) {
+    return new DeltaDataFileIterator(deltaSnapshot, schema, true);
+  }
+
+  public class DeltaDataFileIterator implements DataFileIterator {
+    private final FileFormat fileFormat;
+    private final List<InternalField> fields;
+    private final List<InternalPartitionField> partitionFields;
+    private Iterator<InternalDataFile> dataFilesIterator = 
Collections.emptyIterator();
+
+    private DeltaDataFileIterator(
+        Snapshot snapshot, InternalSchema schema, boolean includeColumnStats) {
+      String provider = ((SnapshotImpl) 
snapshot).getMetadata().getFormat().getProvider();
+      this.fileFormat = actionsConverter.convertToFileFormat(provider);
+
+      this.fields = schema.getFields();
+
+      StructType fullSchema = snapshot.getSchema(); // The full table schema
+      List<String> partitionColumns = snapshot.getPartitionColumnNames(); // 
List<String>
+
+      List<StructField> partitionFields_strfld =
+          fullSchema.fields().stream()
+              .filter(field -> partitionColumns.contains(field.getName()))
+              .collect(Collectors.toList());
+
+      StructType partitionSchema = new StructType(partitionFields_strfld);
+
+      this.partitionFields =
+          partitionExtractor.convertFromDeltaPartitionFormat(schema, 
partitionSchema);
+      Configuration hadoopConf = new Configuration();
+      Engine engine = DefaultEngine.create(hadoopConf);
+
+      Scan myScan = snapshot.getScanBuilder().build();
+      CloseableIterator<FilteredColumnarBatch> scanFiles = 
myScan.getScanFiles(engine);

Review Comment:
   ```suggestion
         ScanImpl myScan = (ScanImpl) snapshot.getScanBuilder().build();
         CloseableIterator<FilteredColumnarBatch> scanFiles = 
myScan.getScanFiles(engine, includeColumnStats);
   ```
   
   In order to get the stats, we need to pass in another option that is only 
available on the ScanImpl. Check out their 
[tests](https://github.com/delta-io/delta/blob/master/kernel/kernel-defaults/src/test/scala/io/delta/kernel/defaults/DeltaTableWritesSuite.scala#L1040)
 for a sample



-- 
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: commits-unsubscr...@xtable.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to