XiaoHongbo-Hope commented on code in PR #6766: URL: https://github.com/apache/paimon/pull/6766#discussion_r2597039260
########## paimon-python/pypaimon/deletionvectors/deletion_vector.py: ########## @@ -0,0 +1,142 @@ +# 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 abc import ABC, abstractmethod + +from pypaimon.common.file_io import FileIO +from pypaimon.table.source.deletion_file import DeletionFile + + +class DeletionVector(ABC): + """ + The DeletionVector can efficiently record the positions of rows that are deleted in a file, + which can then be used to filter out deleted rows when processing the file. + """ + + @abstractmethod + def bit_map(self): + """ + Returns the bitmap of the DeletionVector. + """ + pass + + @abstractmethod + def delete(self, position: int) -> None: + """ + Marks the row at the specified position as deleted. + + Args: + position: The position of the row to be marked as deleted. + """ + pass + + @abstractmethod + def is_deleted(self, position: int) -> bool: + """ + Checks if the row at the specified position is deleted. + + Args: + position: The position of the row to check. + + Returns: + True if the row is deleted, False otherwise. + """ + pass + + @abstractmethod + def is_empty(self) -> bool: + """ + Determines if the deletion vector is empty, indicating no deletions. + + Returns: + True if the deletion vector is empty, False if it contains deletions. + """ + pass + + @abstractmethod + def get_cardinality(self) -> int: + """ + Returns the number of distinct integers added to the DeletionVector. + + Returns: + The number of deleted positions. + """ + pass + + @abstractmethod + def merge(self, deletion_vector: 'DeletionVector') -> None: + """ + Merge another DeletionVector to this current one. + + Args: + deletion_vector: The other DeletionVector to merge. + """ + pass + + def checked_delete(self, position: int) -> bool: + """ + Marks the row at the specified position as deleted. + + Args: + position: The position of the row to be marked as deleted. + + Returns: + True if the added position wasn't already deleted. False otherwise. + """ + if self.is_deleted(position): + return False + else: + self.delete(position) + return True + + @staticmethod + def read(file_io: FileIO, deletion_file: DeletionFile) -> 'DeletionVector': + """ + Read a DeletionVector from a file. + """ + from pypaimon.deletionvectors.bitmap_deletion_vector import BitmapDeletionVector + + with file_io.new_input_stream(deletion_file.dv_index_path) as f: + f.seek(deletion_file.offset) + + # Read bitmap length + bitmap_length_bytes = f.read(4) + bitmap_length = int.from_bytes(bitmap_length_bytes, byteorder='big') + + # Read magic number + magic_number_bytes = f.read(4) + magic_number = int.from_bytes(magic_number_bytes, byteorder='big') + + if magic_number == BitmapDeletionVector.MAGIC_NUMBER: + if deletion_file.length is not None and bitmap_length != deletion_file.length: + raise RuntimeError( + f"Size not match, actual size: {bitmap_length}, expected size: {deletion_file.length}" + ) + + # Magic number has been read, read remaining bytes + remaining_bytes = bitmap_length - BitmapDeletionVector.MAGIC_NUMBER_SIZE_BYTES + data = f.read(remaining_bytes) + + # Skip CRC (4 bytes) + f.read(4) + + return BitmapDeletionVector.deserialize_from_bytes(data) + else: + raise RuntimeError( + f"Invalid magic number: {magic_number}, " + f"expected: {BitmapDeletionVector.MAGIC_NUMBER}" + ) Review Comment: > Not now, it is just for iceberg compatibility. 👌 ########## 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]
