chamikaramj commented on code in PR #30805: URL: https://github.com/apache/beam/pull/30805#discussion_r1561896932
########## sdks/java/io/iceberg/src/main/java/org/apache/beam/io/iceberg/CombinedScanReader.java: ########## @@ -0,0 +1,196 @@ +/* + * 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.io.iceberg; + +import static org.apache.beam.sdk.util.Preconditions.checkStateNotNull; + +import java.io.IOException; +import java.util.ArrayDeque; +import java.util.NoSuchElementException; +import java.util.Queue; +import javax.annotation.Nullable; +import org.apache.beam.sdk.io.BoundedSource; +import org.apache.beam.sdk.schemas.Schema; +import org.apache.beam.sdk.values.Row; +import org.apache.iceberg.CombinedScanTask; +import org.apache.iceberg.DataFile; +import org.apache.iceberg.FileScanTask; +import org.apache.iceberg.Table; +import org.apache.iceberg.avro.Avro; +import org.apache.iceberg.data.Record; +import org.apache.iceberg.data.avro.DataReader; +import org.apache.iceberg.data.orc.GenericOrcReader; +import org.apache.iceberg.data.parquet.GenericParquetReaders; +import org.apache.iceberg.encryption.EncryptionManager; +import org.apache.iceberg.encryption.InputFilesDecryptor; +import org.apache.iceberg.io.CloseableIterable; +import org.apache.iceberg.io.CloseableIterator; +import org.apache.iceberg.io.FileIO; +import org.apache.iceberg.io.InputFile; +import org.apache.iceberg.orc.ORC; +import org.apache.iceberg.parquet.Parquet; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class CombinedScanReader extends BoundedSource.BoundedReader<Row> { + private static final Logger LOG = LoggerFactory.getLogger(CombinedScanReader.class); + + private final IcebergBoundedSource source; + private final Table table; + + private final CombinedScanTask task; + + private final Schema schema; + + transient @Nullable org.apache.iceberg.Schema project; + transient @Nullable FileIO io; + transient @Nullable InputFilesDecryptor decryptor; + transient @Nullable Queue<FileScanTask> fileScanTasks; + transient @Nullable CloseableIterator<Record> currentIterator; + transient @Nullable Record current; + + public CombinedScanReader(IcebergBoundedSource source, CombinedScanTask task, Schema schema) { + this.source = source; + this.table = + checkStateNotNull( + source.table(), "CombinedScanReader requires an IcebergBoundedSource with a table"); + this.task = task; + this.schema = schema; + if (this.schema != null) { + project = SchemaHelper.convert(this.schema); + } + } + + @Override + public boolean start() throws IOException { + EncryptionManager encryptionManager = table.encryption(); + io = table.io(); + decryptor = new InputFilesDecryptor(task, io, encryptionManager); + + fileScanTasks = new ArrayDeque<>(); + fileScanTasks.addAll(task.files()); + + return advance(); + } + + @Override + public boolean advance() throws IOException { + Queue<FileScanTask> fileScanTasks = + checkStateNotNull(this.fileScanTasks, "files null in advance() - did you call start()?"); + InputFilesDecryptor decryptor = + checkStateNotNull(this.decryptor, "decryptor null in adance() - did you call start()?"); + + // This nullness annotation is incorrect, but the most expedient way to work with Iceberg's APIs + // which are not null-safe. + @SuppressWarnings("nullness") + org.apache.iceberg.@NonNull Schema project = this.project; + + do { + // If our current iterator is working... do that. + if (currentIterator != null && currentIterator.hasNext()) { + current = currentIterator.next(); + return true; + } + + // Close out the current iterator and try to open a new one + if (currentIterator != null) { + currentIterator.close(); + currentIterator = null; + } + + LOG.info("Trying to open new file."); + if (fileScanTasks.isEmpty()) { + LOG.info("We have exhausted all available files in this CombinedScanTask"); + break; + } + + // We have a new file to start reading + FileScanTask fileTask = fileScanTasks.remove(); + DataFile file = fileTask.file(); + InputFile input = decryptor.getInputFile(fileTask); + + CloseableIterable<Record> iterable; + switch (file.format()) { + case ORC: + LOG.info("Preparing ORC input"); + iterable = + ORC.read(input) + .split(fileTask.start(), fileTask.length()) + .project(project) + .createReaderFunc(fileSchema -> GenericOrcReader.buildReader(project, fileSchema)) + .filter(fileTask.residual()) + .build(); + break; + case PARQUET: + LOG.info("Preparing Parquet input."); + iterable = + Parquet.read(input) + .split(fileTask.start(), fileTask.length()) + .project(project) + .createReaderFunc( + fileSchema -> GenericParquetReaders.buildReader(project, fileSchema)) + .filter(fileTask.residual()) + .build(); + break; + case AVRO: + LOG.info("Preparing Avro input."); + iterable = + Avro.read(input) + .split(fileTask.start(), fileTask.length()) + .project(project) + .createReaderFunc(DataReader::create) + .build(); + break; + default: + throw new UnsupportedOperationException("Cannot read format: " + file.format()); + } + currentIterator = iterable.iterator(); + + } while (true); + + return false; + } + + @Override + public Row getCurrent() throws NoSuchElementException { + if (current == null) { + throw new NoSuchElementException(); + } + return RowHelper.recordToRow(schema, current); + } + + @Override + public void close() throws IOException { + if (currentIterator != null) { + currentIterator.close(); + } + if (fileScanTasks != null) { + fileScanTasks.clear(); + fileScanTasks = null; + } + if (io != null) { + io.close(); + } + } + + @Override + public BoundedSource<Row> getCurrentSource() { Review Comment: This can be in a future PR since we want to get this in by release cut. -- 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: github-unsubscr...@beam.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org