JingsongLi commented on code in PR #6766: URL: https://github.com/apache/paimon/pull/6766#discussion_r2596962254
########## paimon-python/pypaimon/deletionvectors/apply_deletion_vector_reader.py: ########## @@ -0,0 +1,129 @@ +# 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. + +from typing import Optional + +import pyarrow +from pyarrow import RecordBatch + +from pypaimon.read.reader.iface.record_batch_reader import RecordBatchReader +from pypaimon.read.reader.iface.record_iterator import RecordIterator +from pypaimon.deletionvectors.deletion_vector import DeletionVector + +from pyroaring import BitMap + +from pypaimon.read.reader.iface.record_reader import RecordReader + + +class ApplyDeletionVectorReader(RecordBatchReader): + """ + A RecordReader which applies DeletionVector to filter records. + """ + + def __init__(self, reader: RecordReader, deletion_vector: DeletionVector): + """ + Initialize an ApplyDeletionVectorReader. + + Args: + reader: The underlying record reader. + deletion_vector: The deletion vector to apply. + """ + self._reader = reader + self._deletion_vector = deletion_vector + + def reader(self) -> RecordReader: + return self._reader + + def deletion_vector(self) -> DeletionVector: + return self._deletion_vector + + def read_arrow_batch(self) -> Optional[RecordBatch]: + self._reader: RecordBatchReader + arrow_batch = self._reader.read_arrow_batch() + if arrow_batch is None: + return None + # Remove the deleted rows from the batch + range_bitmap = BitMap( + range(self._reader.return_batch_pos() - arrow_batch.num_rows, self._reader.return_batch_pos())) + intersection_bitmap = range_bitmap - self._deletion_vector.bit_map() + added_row_list = [x - (self._reader.return_batch_pos() - arrow_batch.num_rows) for x in + list(intersection_bitmap)] + return arrow_batch.take(pyarrow.array(added_row_list, type=pyarrow.int32())) + Review Comment: It is not easy to get an empty deletion vector, so I think it is OK without empty check. -- 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]
