FrankChen021 commented on code in PR #19510: URL: https://github.com/apache/druid/pull/19510#discussion_r3957975309
########## extensions-contrib/druid-iceberg-extensions/src/main/java/org/apache/druid/iceberg/input/IcebergArrowInputSourceReader.java: ########## @@ -0,0 +1,400 @@ +/* + * 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 com.google.common.collect.Maps; +import org.apache.arrow.vector.BigIntVector; +import org.apache.arrow.vector.BitVector; +import org.apache.arrow.vector.DateDayVector; +import org.apache.arrow.vector.DecimalVector; +import org.apache.arrow.vector.FieldVector; +import org.apache.arrow.vector.FixedSizeBinaryVector; +import org.apache.arrow.vector.Float4Vector; +import org.apache.arrow.vector.Float8Vector; +import org.apache.arrow.vector.IntVector; +import org.apache.arrow.vector.SmallIntVector; +import org.apache.arrow.vector.TimeMicroVector; +import org.apache.arrow.vector.TimeStampMicroTZVector; +import org.apache.arrow.vector.TimeStampMicroVector; +import org.apache.arrow.vector.TimeStampMilliTZVector; +import org.apache.arrow.vector.TimeStampMilliVector; +import org.apache.arrow.vector.TimeStampNanoTZVector; +import org.apache.arrow.vector.TimeStampNanoVector; +import org.apache.arrow.vector.TinyIntVector; +import org.apache.arrow.vector.VarBinaryVector; +import org.apache.arrow.vector.VarCharVector; +import org.apache.druid.data.input.ColumnsFilter; +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.InputSourceReader; +import org.apache.druid.data.input.InputStats; +import org.apache.druid.data.input.MapBasedInputRow; +import org.apache.druid.iceberg.filter.IcebergFilter; +import org.apache.druid.java.util.common.parsers.CloseableIterator; +import org.apache.iceberg.CombinedScanTask; +import org.apache.iceberg.Table; +import org.apache.iceberg.TableScan; +import org.apache.iceberg.arrow.vectorized.ArrowReader; +import org.apache.iceberg.arrow.vectorized.ColumnarBatch; +import org.apache.iceberg.io.CloseableIterable; +import org.apache.iceberg.types.Types; +import org.apache.iceberg.util.TableScanUtil; +import org.joda.time.DateTime; + +import javax.annotation.Nullable; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +/** + * Reads an Iceberg table via iceberg-arrow's {@link ArrowReader}, yielding {@link InputRow} objects. + * + * Delete application (V2 equality and positional deletes), type coercion, and schema evolution are Review Comment: [P2] Delete-file snapshots are unsupported Iceberg 1.11's ArrowReader.open rejects any CombinedScanTask with delete files by throwing UnsupportedOperationException; its API contract requires that there be no delete files. This class currently claims V2 equality and positional deletes are applied, and the Arrow delegate does not detect or route around them, so enabling useArrowReader on a valid Iceberg snapshot with deletes fails ingestion instead of applying deletes or giving a clear documented fallback. Detect delete tasks and use the standard path or reject them explicitly, and update this claim. ########## extensions-contrib/druid-iceberg-extensions/src/main/java/org/apache/druid/iceberg/input/IcebergArrowInputSourceReader.java: ########## @@ -0,0 +1,400 @@ +/* + * 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 com.google.common.collect.Maps; +import org.apache.arrow.vector.BigIntVector; +import org.apache.arrow.vector.BitVector; +import org.apache.arrow.vector.DateDayVector; +import org.apache.arrow.vector.DecimalVector; +import org.apache.arrow.vector.FieldVector; +import org.apache.arrow.vector.FixedSizeBinaryVector; +import org.apache.arrow.vector.Float4Vector; +import org.apache.arrow.vector.Float8Vector; +import org.apache.arrow.vector.IntVector; +import org.apache.arrow.vector.SmallIntVector; +import org.apache.arrow.vector.TimeMicroVector; +import org.apache.arrow.vector.TimeStampMicroTZVector; +import org.apache.arrow.vector.TimeStampMicroVector; +import org.apache.arrow.vector.TimeStampMilliTZVector; +import org.apache.arrow.vector.TimeStampMilliVector; +import org.apache.arrow.vector.TimeStampNanoTZVector; +import org.apache.arrow.vector.TimeStampNanoVector; +import org.apache.arrow.vector.TinyIntVector; +import org.apache.arrow.vector.VarBinaryVector; +import org.apache.arrow.vector.VarCharVector; +import org.apache.druid.data.input.ColumnsFilter; +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.InputSourceReader; +import org.apache.druid.data.input.InputStats; +import org.apache.druid.data.input.MapBasedInputRow; +import org.apache.druid.iceberg.filter.IcebergFilter; +import org.apache.druid.java.util.common.parsers.CloseableIterator; +import org.apache.iceberg.CombinedScanTask; +import org.apache.iceberg.Table; +import org.apache.iceberg.TableScan; +import org.apache.iceberg.arrow.vectorized.ArrowReader; +import org.apache.iceberg.arrow.vectorized.ColumnarBatch; +import org.apache.iceberg.io.CloseableIterable; +import org.apache.iceberg.types.Types; +import org.apache.iceberg.util.TableScanUtil; +import org.joda.time.DateTime; + +import javax.annotation.Nullable; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +/** + * Reads an Iceberg table via iceberg-arrow's {@link ArrowReader}, yielding {@link InputRow} objects. + * + * Delete application (V2 equality and positional deletes), type coercion, and schema evolution are + * handled entirely by the Iceberg library. Druid only consumes the resulting {@link ColumnarBatch} + * batches and maps them to {@link MapBasedInputRow}. + * + * Column projection and predicate push-down are applied at scan planning time so only requested + * columns and matching files are read from storage. + * + * Note: iceberg-arrow currently supports Parquet data files only. ORC and Avro files will throw + * {@link UnsupportedOperationException} at read time; use the standard delegate path for those. + */ +public class IcebergArrowInputSourceReader implements InputSourceReader +{ + static final int DEFAULT_BATCH_SIZE = 1024; + + private final Table table; + @Nullable + private final IcebergFilter icebergFilter; + @Nullable + private final DateTime snapshotTime; + private final boolean caseSensitive; + private final InputRowSchema schema; + private final int batchSize; + + public IcebergArrowInputSourceReader( + final Table table, + @Nullable final IcebergFilter icebergFilter, + @Nullable final DateTime snapshotTime, + final boolean caseSensitive, + final InputRowSchema schema, + final int batchSize + ) + { + this.table = table; + this.icebergFilter = icebergFilter; + this.snapshotTime = snapshotTime; + this.caseSensitive = caseSensitive; + this.schema = schema; + this.batchSize = batchSize; + } + + @Override + public CloseableIterator<InputRow> read(@Nullable final InputStats inputStats) throws IOException + { + final TableScan scan = buildScan(); + final CloseableIterable<CombinedScanTask> tasks = TableScanUtil.planTasks( + scan.planFiles(), + scan.targetSplitSize(), + scan.splitLookback(), + scan.splitOpenFileCost() + ); + final ArrowReader arrowReader = new ArrowReader(scan, batchSize, true); + final org.apache.iceberg.io.CloseableIterator<ColumnarBatch> batchIter = arrowReader.open(tasks); + return new ArrowInputRowIterator( + batchIter, + arrowReader, + tasks, + inputStats != null ? inputStats : new NoopInputStats() + ); + } + + @Override + public CloseableIterator<InputRowListPlusRawValues> sample() throws IOException + { + final CloseableIterator<InputRow> rows = read(new NoopInputStats()); + return new CloseableIterator<InputRowListPlusRawValues>() + { + @Override + public boolean hasNext() + { + return rows.hasNext(); + } + + @Override + public InputRowListPlusRawValues next() + { + final InputRow row = rows.next(); + return InputRowListPlusRawValues.of(row, ((MapBasedInputRow) row).getEvent()); + } + + @Override + public void close() throws IOException + { + rows.close(); + } + }; + } + + private TableScan buildScan() + { + TableScan scan = table.newScan().caseSensitive(caseSensitive); + + final List<String> projection = projectedColumns(); + if (projection != null) { + scan = scan.select(projection); + } + if (icebergFilter != null) { + scan = icebergFilter.filter(scan); + } + if (snapshotTime != null) { + scan = scan.asOfTime(snapshotTime.getMillis()); + } + return scan; + } + + /** Projection authority is ColumnsFilter, not DimensionsSpec. Mirrors DeltaInputSource#pruneSchema. */ + @Nullable + private List<String> projectedColumns() + { + final ColumnsFilter filter = schema.getColumnsFilter(); + final List<String> allColumns = table.schema().columns().stream() + .map(Types.NestedField::name) + .collect(Collectors.toList()); + final List<String> filtered = allColumns.stream() + .filter(filter::apply) + .collect(Collectors.toList()); + if (filtered.equals(allColumns)) { + return null; + } + final String tsCol = schema.getTimestampSpec().getTimestampColumn(); + if (tsCol != null && allColumns.contains(tsCol) && !filtered.contains(tsCol)) { + filtered.add(tsCol); + } + return filtered; + } + + private InputRow batchRowToInputRow(final ColumnarBatch batch, final int rowIdx) + { + final int numCols = batch.numCols(); + final Map<String, Object> event = Maps.newHashMapWithExpectedSize(numCols); + for (int col = 0; col < numCols; col++) { + final FieldVector vec = batch.column(col).getFieldVector(); Review Comment: [P1] Decode dictionary-backed vectors before extraction ColumnVector.getFieldVector() is explicitly the potentially dictionary-encoded vector. For a Parquet dictionary-encoded string (including repeated values), Iceberg can expose an IntVector of dictionary IDs here; the IntVector branch at lines 242-244 then writes that ID into the Druid event instead of the string. The test even avoids asserting filtered string values for this reason. Use ColumnVector.getArrowVector() or the typed ColumnVector accessors before building the row, and add a repeated/partitioned-string assertion, otherwise useArrowReader=true silently corrupts dimensions. -- 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]
