aokolnychyi commented on code in PR #4888: URL: https://github.com/apache/iceberg/pull/4888#discussion_r929392782
########## spark/v3.2/spark/src/main/java/org/apache/iceberg/spark/data/vectorized/ColumnVectorBuilder.java: ########## @@ -0,0 +1,53 @@ +/* + * 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.iceberg.spark.data.vectorized; + +import org.apache.iceberg.arrow.vectorized.VectorHolder; +import org.apache.iceberg.arrow.vectorized.VectorHolder.ConstantVectorHolder; +import org.apache.iceberg.types.Types; +import org.apache.spark.sql.vectorized.ColumnVector; + +class ColumnVectorBuilder { + private boolean[] isDeleted; + private int[] rowIdMapping; + + public ColumnVectorBuilder withDeletedRows(int[] rowIdMappingArray, boolean[] isDeletedArray) { Review Comment: nit: I feel we better make this a constructor and pass these arrays only once during the construction. ########## spark/v3.2/spark/src/main/java/org/apache/iceberg/spark/data/vectorized/ColumnarBatchReader.java: ########## @@ -66,37 +69,53 @@ public final ColumnarBatch read(ColumnarBatch reuse, int numRowsToRead) { closeVectors(); } - ColumnBatchLoader batchLoader = new ColumnBatchLoader(numRowsToRead); + ColumnarBatch columnarBatch = new ColumnBatchLoader(numRowsToRead).loadDataToColumnBatch(); rowStartPosInBatch += numRowsToRead; - return batchLoader.columnarBatch; + return columnarBatch; } private class ColumnBatchLoader { - private int[] rowIdMapping; // the rowId mapping to skip deleted rows for all column vectors inside a batch - private int numRows; - private ColumnarBatch columnarBatch; + private final int numRowsToRead; + // the rowId mapping to skip deleted rows for all column vectors inside a batch, it is null when there is no deletes + private int[] rowIdMapping; + // the array to indicate if a row is deleted or not, it is null when there is no "_deleted" metadata column + private boolean[] isDeleted; ColumnBatchLoader(int numRowsToRead) { - initRowIdMapping(numRowsToRead); - loadDataToColumnBatch(numRowsToRead); + Preconditions.checkArgument(numRowsToRead > 0, "Invalid number of rows to read: %s", numRowsToRead); + this.numRowsToRead = numRowsToRead; + if (hasIsDeletedColumn) { + isDeleted = new boolean[numRowsToRead]; + } } - ColumnarBatch loadDataToColumnBatch(int numRowsToRead) { - Preconditions.checkArgument(numRowsToRead > 0, "Invalid number of rows to read: %s", numRowsToRead); - ColumnVector[] arrowColumnVectors = readDataToColumnVectors(numRowsToRead); + ColumnarBatch loadDataToColumnBatch() { + int numRowsUndeleted = initRowIdMapping(); - columnarBatch = new ColumnarBatch(arrowColumnVectors); - columnarBatch.setNumRows(numRows); + ColumnVector[] arrowColumnVectors = readDataToColumnVectors(); + + ColumnarBatch newColumnarBatch = new ColumnarBatch(arrowColumnVectors); + newColumnarBatch.setNumRows(numRowsUndeleted); if (hasEqDeletes()) { - applyEqDelete(); + applyEqDelete(newColumnarBatch); + } + + if (hasIsDeletedColumn && rowIdMapping != null) { + // reset the row id mapping array, so that it doesn't filter out the deleted rows + for (int i = 0; i < numRowsToRead; i++) { + rowIdMapping[i] = i; + } + newColumnarBatch.setNumRows(numRowsToRead); } - return columnarBatch; + + return newColumnarBatch; } - ColumnVector[] readDataToColumnVectors(int numRowsToRead) { + ColumnVector[] readDataToColumnVectors() { ColumnVector[] arrowColumnVectors = new ColumnVector[readers.length]; + ColumnVectorBuilder columnVectorBuilder = new ColumnVectorBuilder(); Review Comment: nit: This is probably where you can pass `rowIdMapping` and `isDeleted` as those two don't change. ########## spark/v3.2/spark/src/main/java/org/apache/iceberg/spark/data/vectorized/ColumnarBatchReader.java: ########## @@ -66,35 +78,50 @@ public final ColumnarBatch read(ColumnarBatch reuse, int numRowsToRead) { closeVectors(); } - ColumnBatchLoader batchLoader = new ColumnBatchLoader(numRowsToRead); + ColumnarBatch columnarBatch = new ColumnBatchLoader(numRowsToRead).loadDataToColumnBatch(); rowStartPosInBatch += numRowsToRead; - return batchLoader.columnarBatch; + return columnarBatch; } private class ColumnBatchLoader { - private int[] rowIdMapping; // the rowId mapping to skip deleted rows for all column vectors inside a batch - private int numRows; - private ColumnarBatch columnarBatch; + private final int numRowsToRead; + // the rowId mapping to skip deleted rows for all column vectors inside a batch, it is null when there is no deletes + private int[] rowIdMapping; + // the array to indicate if a row is deleted or not, it is null when there is no "_deleted" metadata column + private boolean[] isDeleted; ColumnBatchLoader(int numRowsToRead) { - initRowIdMapping(numRowsToRead); - loadDataToColumnBatch(numRowsToRead); + Preconditions.checkArgument(numRowsToRead > 0, "Invalid number of rows to read: %s", numRowsToRead); + this.numRowsToRead = numRowsToRead; + if (hasIsDeletedColumn) { + isDeleted = new boolean[numRowsToRead]; + } } - ColumnarBatch loadDataToColumnBatch(int numRowsToRead) { - Preconditions.checkArgument(numRowsToRead > 0, "Invalid number of rows to read: %s", numRowsToRead); - ColumnVector[] arrowColumnVectors = readDataToColumnVectors(numRowsToRead); + ColumnarBatch loadDataToColumnBatch() { + int numRowsUndeleted = initRowIdMapping(); + + ColumnVector[] arrowColumnVectors = readDataToColumnVectors(); - columnarBatch = new ColumnarBatch(arrowColumnVectors); - columnarBatch.setNumRows(numRows); + ColumnarBatch newColumnarBatch = new ColumnarBatch(arrowColumnVectors); + newColumnarBatch.setNumRows(numRowsUndeleted); if (hasEqDeletes()) { - applyEqDelete(); + applyEqDelete(newColumnarBatch); } - return columnarBatch; + + if (hasIsDeletedColumn && rowIdMapping != null) { + // reset the row id mapping array, so that it doesn't filter out the deleted rows + for (int i = 0; i < numRowsToRead; i++) { + rowIdMapping[i] = i; Review Comment: Sounds good to me. ########## spark/v3.3/spark/src/main/java/org/apache/iceberg/spark/data/vectorized/ColumnVectorBuilder.java: ########## @@ -0,0 +1,53 @@ +/* + * 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.iceberg.spark.data.vectorized; + +import org.apache.iceberg.arrow.vectorized.VectorHolder; +import org.apache.iceberg.arrow.vectorized.VectorHolder.ConstantVectorHolder; +import org.apache.iceberg.types.Types; +import org.apache.spark.sql.vectorized.ColumnVector; + +class ColumnVectorBuilder { + private boolean[] isDeleted; + private int[] rowIdMapping; + + public ColumnVectorBuilder withDeletedRows(int[] rowIdMappingArray, boolean[] isDeletedArray) { Review Comment: nit: Same for 3.3 -- 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]
