chamikaramj commented on code in PR #38706:
URL: https://github.com/apache/beam/pull/38706#discussion_r3309571217


##########
sdks/java/io/delta/src/main/java/org/apache/beam/sdk/io/delta/BeamParquetHandler.java:
##########
@@ -0,0 +1,367 @@
+/*
+ * 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.beam.sdk.io.delta;
+
+import io.delta.kernel.data.FilteredColumnarBatch;
+import 
io.delta.kernel.defaults.internal.parquet.ParquetFileReader.BatchReadSupport;
+import io.delta.kernel.engine.FileReadResult;
+import io.delta.kernel.engine.ParquetHandler;
+import io.delta.kernel.expressions.Column;
+import io.delta.kernel.expressions.Predicate;
+import io.delta.kernel.types.MetadataColumnSpec;
+import io.delta.kernel.types.StructType;
+import io.delta.kernel.utils.CloseableIterator;
+import io.delta.kernel.utils.DataFileStatus;
+import io.delta.kernel.utils.FileStatus;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.Optional;
+import java.util.Set;
+import org.apache.beam.sdk.io.range.OffsetRange;
+import org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.Path;
+import org.apache.parquet.column.page.PageReadStore;
+import org.apache.parquet.filter2.compat.FilterCompat;
+import org.apache.parquet.format.converter.ParquetMetadataConverter;
+import org.apache.parquet.hadoop.ParquetFileReader;
+import org.apache.parquet.hadoop.api.InitContext;
+import org.apache.parquet.hadoop.api.ReadSupport;
+import org.apache.parquet.hadoop.metadata.FileMetaData;
+import org.apache.parquet.hadoop.metadata.ParquetMetadata;
+import org.apache.parquet.hadoop.util.HadoopInputFile;
+import org.apache.parquet.io.ColumnIOFactory;
+import org.apache.parquet.io.MessageColumnIO;
+import org.apache.parquet.io.RecordReader;
+import org.apache.parquet.io.api.RecordMaterializer;
+import org.apache.parquet.schema.MessageType;
+
+/**
+ * A Beam specific {@link ParquetHandler} that delegates row group claiming to 
a {@link
+ * DeltaReadTaskTracker}.
+ */
+public class BeamParquetHandler implements ParquetHandler {
+  private final Configuration conf;
+  private final ParquetHandler delegate;
+  private final RestrictionTracker<OffsetRange, Long> tracker;
+  private static final long DEFAULT_START_RG_INDEX = 0L;
+
+  public BeamParquetHandler(
+      Configuration conf, ParquetHandler delegate, 
RestrictionTracker<OffsetRange, Long> tracker) {
+    this.conf = conf;
+    this.delegate = delegate;
+    this.tracker = tracker;
+  }
+
+  private boolean claimFailed = false;
+
+  /**
+   * A method that is expected to be called after the first file processing is 
done. It returns
+   * whether the last file process resulted in a claim failure. This allows 
the caller to skip
+   * trying to read the remaining files of the task which would result in 
claim failures for each
+   * row group within them.
+   *
+   * @return true, if the last file process resulted in a claim failure. 
Returns false otherwise.
+   */
+  public boolean hasClaimFailed() {
+    return claimFailed;
+  }
+
+  @Override
+  public CloseableIterator<FileReadResult> readParquetFiles(
+      CloseableIterator<FileStatus> fileIter,
+      StructType physicalSchema,
+      Optional<Predicate> predicate)
+      throws IOException {
+    return readParquetFiles(fileIter, physicalSchema, predicate, 
DEFAULT_START_RG_INDEX);
+  }
+
+  /**
+   * Reads Parquet files starting from a given row group index.
+   *
+   * <p>This takes the {@code RestrictionTracker} referenced by the current 
{@code ParquetReader}
+   * into consideration when reading by performing the following.
+   *
+   * <p>* Skips blocks of the set of files till the given start row group 
index or the start point
+   * of the {@code RestrictionTracker}, whatever is higher. * Invokes {@code 
tryClaim} when reading
+   * a specific block stops reading if a {@code tryClaim} fails. * Stops 
reading if the end of the
+   * range of the {@code RestrictionTracker} is reached.
+   *
+   * <p>If {@code tryClaim} fails during reading, subsequent {@code 
hasClaimFailed} calls will
+   * return {@code true}, so the caller can skip reading subsequent files that 
are in the range
+   * being considered for reading.
+   */
+  public CloseableIterator<FileReadResult> readParquetFiles(
+      CloseableIterator<FileStatus> fileIter,
+      StructType physicalSchema,
+      Optional<Predicate> predicate,
+      long startRgIndex)
+      throws IOException {
+
+    List<CloseableIterator<FileReadResult>> results = new ArrayList<>();
+    boolean hasRowIndexCol = 
physicalSchema.contains(MetadataColumnSpec.ROW_INDEX);
+
+    long currentRgIndex = startRgIndex;
+
+    while (fileIter.hasNext()) {
+      FileStatus fileStatus = fileIter.next();
+      Path hadoopPath = new Path(fileStatus.getPath());
+      ParquetMetadata metadata =
+          ParquetFileReader.readFooter(conf, hadoopPath, 
ParquetMetadataConverter.NO_FILTER);
+      long fileBlocks = metadata.getBlocks().size();
+
+      if (currentRgIndex + fileBlocks <= 
tracker.currentRestriction().getFrom()) {
+        // Skipping all blocks for the current file since they are located 
before the
+        // start index of the tracker.
+        currentRgIndex += fileBlocks;
+        continue;
+      }
+
+      if (currentRgIndex >= tracker.currentRestriction().getTo()) {
+        // Skipping all blocks for the current file since they are located 
after the
+        // end index of the tracker.
+        currentRgIndex += fileBlocks;
+        continue;
+      }
+
+      results.add(
+          readParquetFileDirect(
+              fileStatus,
+              hadoopPath,
+              metadata,
+              physicalSchema,
+              hasRowIndexCol,
+              currentRgIndex,
+              fileBlocks));
+
+      currentRgIndex += fileBlocks;
+    }

Review Comment:
   Done.



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