FrankChen021 commented on code in PR #19266:
URL: https://github.com/apache/druid/pull/19266#discussion_r3273988148


##########
extensions-contrib/druid-iceberg-extensions/src/main/java/org/apache/druid/iceberg/input/IcebergNativeRecordReader.java:
##########
@@ -0,0 +1,355 @@
+/*
+ * 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.druid.iceberg.input;
+
+import org.apache.druid.data.input.InputRow;
+import org.apache.druid.data.input.InputRowListPlusRawValues;
+import org.apache.druid.data.input.InputRowSchema;
+import org.apache.druid.data.input.InputSourceFactory;
+import org.apache.druid.data.input.InputSourceReader;
+import org.apache.druid.data.input.InputStats;
+import org.apache.druid.data.input.impl.MapInputRowParser;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.java.util.common.parsers.CloseableIterator;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.CatalogUtil;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.SchemaParser;
+import org.apache.iceberg.data.Record;
+import org.apache.iceberg.data.parquet.GenericParquetReaders;
+import org.apache.iceberg.hadoop.HadoopFileIO;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.io.DeleteSchemaUtil;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.parquet.Parquet;
+import org.apache.iceberg.types.Types;
+
+import javax.annotation.Nullable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.Set;
+
+/**
+ * An {@link InputSourceReader} that reads an Iceberg data file and applies
+ * associated positional and equality delete files before converting records
+ * to Druid {@link InputRow} objects.
+ *
+ * Delete application follows the Iceberg v2 spec:
+ * <ol>
+ *   <li>Positional deletes: read (file_path, pos) pairs, filter by current 
data file,
+ *       build a Set of deleted positions</li>
+ *   <li>Equality deletes: read key tuples from equality delete files, build 
Sets
+ *       of deleted key values per equality field set</li>
+ *   <li>Stream data file: for each record, skip if position-deleted or 
equality-deleted</li>
+ * </ol>
+ *
+ * All reads use Iceberg's Parquet reader with {@link GenericParquetReaders} 
for
+ * schema-aware reading. Files are accessed via Hadoop {@link Configuration}.
+ */
+public class IcebergNativeRecordReader implements InputSourceReader
+{
+  private static final Logger log = new 
Logger(IcebergNativeRecordReader.class);
+
+  private final String dataFilePath;
+  private final List<DeleteFileInfo> deleteFiles;
+  private final String tableSchemaJson;
+  private final InputSourceFactory warehouseSource;
+  private final InputRowSchema inputRowSchema;
+  private final Configuration hadoopConf;
+  private final FileIO fileIO;
+
+  public IcebergNativeRecordReader(
+      final String dataFilePath,
+      final List<DeleteFileInfo> deleteFiles,
+      final String tableSchemaJson,
+      final InputSourceFactory warehouseSource,
+      final InputRowSchema inputRowSchema,
+      @Nullable final String fileIOImpl,
+      @Nullable final Map<String, String> fileIOProperties
+  )
+  {
+    this.dataFilePath = dataFilePath;
+    this.deleteFiles = deleteFiles;
+    this.tableSchemaJson = tableSchemaJson;
+    this.warehouseSource = warehouseSource;
+    this.inputRowSchema = inputRowSchema;
+    this.hadoopConf = new Configuration();
+    this.fileIO = buildFileIO(fileIOImpl, fileIOProperties, hadoopConf);
+  }
+
+  private static FileIO buildFileIO(
+      @Nullable final String fileIOImpl,
+      @Nullable final Map<String, String> fileIOProperties,
+      final Configuration hadoopConf
+  )
+  {
+    final Map<String, String> props = fileIOProperties == null ? 
Collections.emptyMap() : fileIOProperties;
+    if (fileIOImpl == null || fileIOImpl.isEmpty()) {
+      return new HadoopFileIO(hadoopConf);
+    }
+    return CatalogUtil.loadFileIO(fileIOImpl, props, hadoopConf);
+  }
+
+  @Override
+  public CloseableIterator<InputRow> read(final InputStats inputStats) throws 
IOException
+  {
+    final Schema tableSchema = SchemaParser.fromJson(tableSchemaJson);
+
+    // Step 1: Collect positional deletes
+    final Set<Long> deletedPositions = collectPositionalDeletes();
+
+    // Step 2: Collect equality deletes
+    final List<EqualityDeleteSet> equalityDeleteSets = 
collectEqualityDeletes(tableSchema);
+
+    // Step 3: Stream data file with delete application
+    final InputFile dataInputFile = fileIO.newInputFile(dataFilePath);
+    final CloseableIterable<Record> records = Parquet.read(dataInputFile)

Review Comment:
   Thanks, this now guards the data file format, but I think one edge of the 
original concern is still open: Iceberg delete files carry their own 
`deleteFile.format()`, separate from `task.file().format()`. Right now 
`DeleteFileInfo` only carries path/content/equality fields, and 
`requireParquet(deleteFileInfo.getPath())` checks the data file format, so a 
PARQUET data file with an ORC/AVRO delete file would still reach 
`Parquet.read(deleteInputFile)` instead of being explicitly rejected. Could we 
preserve the delete-file format and guard each delete file too?
   
   Also, the user docs still say Iceberg data files can be Parquet, ORC, or 
Avro and the new v2 delete section does not call out the Parquet-only 
limitation, so it would be worth documenting that limitation there as well.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to