github-advanced-security[bot] commented on code in PR #20451: URL: https://github.com/apache/druid/pull/20451#discussion_r4136433656
########## extensions-contrib/druid-iceberg-extensions/src/main/java/org/apache/druid/iceberg/input/IcebergArrowRowsAndColumns.java: ########## @@ -0,0 +1,226 @@ +/* + * 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.query.rowsandcols.RowsAndColumns; +import org.apache.druid.query.rowsandcols.column.Column; +import org.apache.druid.query.rowsandcols.column.ColumnAccessor; +import org.apache.druid.query.rowsandcols.column.ColumnAccessorBasedColumn; +import org.apache.druid.segment.column.ColumnType; +import org.apache.iceberg.Schema; +import org.apache.iceberg.arrow.vectorized.ColumnVector; +import org.apache.iceberg.arrow.vectorized.ColumnarBatch; +import org.apache.iceberg.types.Type; +import org.apache.iceberg.types.Types; + +import javax.annotation.Nullable; +import java.math.BigDecimal; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +final class IcebergArrowRowsAndColumns implements RowsAndColumns +{ + private final ColumnarBatch batch; + private final Layout layout; + private final Column[] columns; + + IcebergArrowRowsAndColumns(final ColumnarBatch batch, final Layout layout) + { + this.batch = batch; + this.layout = layout; + this.columns = new Column[batch.numCols()]; + for (int i = 0; i < batch.numCols(); i++) { + columns[i] = new ColumnAccessorBasedColumn( + new IcebergArrowColumnAccessor(batch.column(i), layout.fields.get(i).type(), batch.numRows()) + ); + } + } + + @Override + public Collection<String> getColumnNames() + { + return layout.columnNames; + } + + @Override + public int numRows() + { + return batch.numRows(); + } + + @Nullable + @Override + public Column findColumn(final String name) + { + final Integer position = layout.columnPositions.get(name); + return position == null ? null : columns[position]; Review Comment: ## CodeQL / Improper validation of user-provided array index This index depends on a [user-provided value](1) which can cause an ArrayIndexOutOfBoundsException. This index depends on a [user-provided value](2) which can cause an ArrayIndexOutOfBoundsException. This index depends on a [user-provided value](3) which can cause an ArrayIndexOutOfBoundsException. [Show more details](https://github.com/apache/druid/security/code-scanning/12034) ########## processing/src/main/java/org/apache/druid/data/input/BatchToInputRowIterator.java: ########## @@ -0,0 +1,212 @@ +/* + * 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.data.input; + +import org.apache.druid.data.input.impl.MapInputRowParser; +import org.apache.druid.data.input.impl.TimestampSpec; +import org.apache.druid.guice.annotations.UnstableApi; +import org.apache.druid.java.util.common.ISE; +import org.apache.druid.java.util.common.parsers.CloseableIterator; +import org.apache.druid.query.rowsandcols.RowsAndColumns; +import org.apache.druid.query.rowsandcols.column.Column; +import org.apache.druid.query.rowsandcols.column.ColumnAccessor; +import org.joda.time.DateTime; + +import javax.annotation.Nullable; +import java.io.IOException; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.NoSuchElementException; + +/** + * Adapts batches to a reusable row cursor. A returned row is valid only until this iterator advances. + */ +@UnstableApi +public class BatchToInputRowIterator implements CloseableIterator<InputRow> +{ + private final CloseableIterator<RowsAndColumns> batches; + private final InputRowSchema inputRowSchema; + private final BatchBackedInputRow rowCursor; + + @Nullable + private BatchContext currentBatch; + private int nextRowNumber; + + public BatchToInputRowIterator( + final CloseableIterator<RowsAndColumns> batches, + final InputRowSchema inputRowSchema + ) + { + this.batches = batches; + this.inputRowSchema = inputRowSchema; + this.rowCursor = new BatchBackedInputRow(inputRowSchema.getTimestampSpec()); + } + + @Override + public boolean hasNext() + { + while (currentBatch == null || nextRowNumber >= currentBatch.numRows()) { + if (!batches.hasNext()) { + return false; + } + currentBatch = new BatchContext(batches.next(), inputRowSchema); + nextRowNumber = 0; + } + return true; + } + + @Override + public InputRow next() + { + if (!hasNext()) { + throw new NoSuchElementException(); + } + rowCursor.moveTo(currentBatch, nextRowNumber++); + return rowCursor; + } + + @Override + public void close() throws IOException + { + batches.close(); + } + + private static class BatchContext + { + private final RowsAndColumns rowsAndColumns; + private final Map<String, ColumnAccessor> accessors; + private final List<String> dimensions; + + BatchContext(final RowsAndColumns rowsAndColumns, final InputRowSchema inputRowSchema) + { + this.rowsAndColumns = rowsAndColumns; + this.accessors = new LinkedHashMap<>(); + for (final String columnName : rowsAndColumns.getColumnNames()) { + final Column column = rowsAndColumns.findColumn(columnName); + if (column == null) { + throw new ISE("Column [%s] is listed but cannot be found", columnName); + } + accessors.put(columnName, column.toAccessor()); + } + this.dimensions = MapInputRowParser.findDimensions( + inputRowSchema.getTimestampSpec(), + inputRowSchema.getDimensionsSpec(), + new HashSet<>(accessors.keySet()) + ); + } + + int numRows() + { + return rowsAndColumns.numRows(); + } + + @Nullable + Object getRaw(final String columnName, final int rowNumber) + { + final ColumnAccessor accessor = accessors.get(columnName); + return accessor == null ? null : accessor.getObject(rowNumber); + } + + Map<String, Object> asMap(final int rowNumber) + { + final Map<String, Object> row = new LinkedHashMap<>(); + accessors.forEach((columnName, accessor) -> row.put(columnName, accessor.getObject(rowNumber))); + return row; + } + } + + private static class BatchBackedInputRow implements InputRow Review Comment: ## CodeQL / Inconsistent compareTo This class declares [compareTo](1) but inherits equals; the two could be inconsistent. [Show more details](https://github.com/apache/druid/security/code-scanning/12035) -- 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]
