FrankChen021 commented on code in PR #19266: URL: https://github.com/apache/druid/pull/19266#discussion_r3259155168
########## 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: [P2] V2 delete path only reads Parquet files The new v2 path bypasses the configured inputFormat and always opens the data file with Parquet.read(...), and the delete readers do the same for delete files. The extension still documents Iceberg data files as Parquet, ORC, or Avro via the delegated warehouseSource/inputFormat path, so any ORC/Avro Iceberg table with delete files will now route through this native reader and fail to parse the files instead of using the configured format-specific reader. Please either dispatch by Iceberg FileFormat / reader support or explicitly reject/document v2 delete support as Parquet-only. -- 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]
