JingsongLi commented on code in PR #9148: URL: https://github.com/apache/paimon/pull/9148#discussion_r3771892660
########## paimon-python/pypaimon/read/reader/blob_view_read_support.py: ########## @@ -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. + +"""Helpers for eager blob-view/descriptor inline conversion on read.""" + +from typing import List + +from pypaimon.common.options.core_options import CoreOptions +from pypaimon.read.reader.iface.record_reader import RecordReader +from pypaimon.schema.data_types import DataField, PyarrowFieldParser + + +def needs_blob_inline_convert(table) -> bool: + return ( + (CoreOptions.blob_view_fields(table.options) + and CoreOptions.blob_view_resolve_enabled(table.options)) + or (not CoreOptions.blob_as_descriptor(table.options) + and CoreOptions.blob_descriptor_fields(table.options)) + ) + + +def wrap_record_reader_with_blob_inline_convert( + reader: RecordReader, + split_read, + read_fields: List[DataField], +) -> RecordReader: + from pypaimon.read.reader.auth_masking_reader import ( + BatchToRecordReaderAdapter, RecordReaderToBatchAdapter) + from pypaimon.read.reader.blob_descriptor_convert_reader import BlobInlineConvertReader + + schema = PyarrowFieldParser.from_paimon_schema(read_fields) + batch_reader = RecordReaderToBatchAdapter(reader, schema) + batch_reader = BlobInlineConvertReader( + batch_reader, + split_read.table, + prescan_reader_factory=lambda names: split_read._create_blob_view_prescan_reader(names), + blob_parallelism=split_read._blob_parallelism, + ) + return BatchToRecordReaderAdapter(batch_reader) Review Comment: [P1] Please preserve the typed field metadata when converting this batch reader back to rows. `BatchToRecordReaderAdapter._ArrowBatchIterator` currently creates each `OffsetRow` without `file_io`, BLOB/descriptor/vector indices, or the view lookup. On this new `MergeFileSplitRead` bridge, a valid descriptor/view BLOB is materialized, but `to_iterator()` consumers then get `TypeError: Field ... is not a BLOB field` from `row.get_blob(pos)`. This is reachable for Java-created primary-key BLOB tables. Please propagate the converted reader metadata into every `OffsetRow` and add a merge-read `to_iterator() + get_blob()` regression test. ########## paimon-python/pypaimon/read/split_read.py: ########## @@ -1003,6 +1047,23 @@ def _build_merge_function(self): value_field_names=[f.name for f in self.value_fields], ) + def _create_blob_view_prescan_reader(self, field_names: set): + value_fields = self.read_fields[-self.value_arity:] + prescan_fields = [f for f in value_fields if f.name in field_names] + if not prescan_fields: + return EmptyFileRecordReader() Review Comment: [P1] Please return an `EmptyRecordBatchReader` here. `BlobInlineConvertReader` unconditionally calls `read_arrow_batch()` on its prescan reader, while `EmptyFileRecordReader` only implements `read_batch()`. When a BLOB-view table is read through this merge path with a projection that excludes every configured view field, `prescan_fields` is empty and the query fails because the returned reader has no `read_arrow_batch()` method. Please add a projection regression test for that case. ########## paimon-python/pypaimon/write/blob_format_writer.py: ########## @@ -290,17 +290,38 @@ def _write_blob_data(self, blob_value: Blob, crc32: int): data = blob_value.to_data() crc32 = self._write_with_crc(data, crc32) else: + expected_length = None + if type(blob_value) is BlobRef: + descriptor_length = blob_value.to_descriptor().length + if descriptor_length >= 0: + expected_length = descriptor_length stream = blob_value.new_input_stream() try: - chunk = stream.read(self.copy_buffer_size) - while chunk: - crc32 = self._write_with_crc(chunk, crc32) + if expected_length is not None: + crc32 = self._copy_exactly(stream, expected_length, crc32) + else: chunk = stream.read(self.copy_buffer_size) + while chunk: + crc32 = self._write_with_crc(chunk, crc32) + chunk = stream.read(self.copy_buffer_size) finally: stream.close() Review Comment: [P2] Please preserve the truncation `EOFError` when closing the source stream also fails. If a known-length descriptor expects 10 bytes, the source ends after 3, and a remote/native stream `close()` raises, this `finally` block replaces the useful `EOFError` with the close exception; `OffsetInputStream` can then attempt a second close during destruction. Please retain the copy/read exception while best-effort closing, and surface the close error only when copying succeeded. A short, close-failing source test would cover this failure path. ########## paimon-python/pypaimon/table/row/offset_row.py: ########## @@ -55,12 +62,57 @@ def get_field(self, pos: int): raise IndexError(f"Position {pos} is out of bounds for row arity {self.arity}") return self.row_tuple[self.offset + pos] - def get_blob(self, pos: int): + @staticmethod + def _normalize_blob_bytes(value): + if value is None: + return None + if hasattr(value, 'as_py'): + value = value.as_py() + if isinstance(value, str): + value = value.encode('utf-8') + if isinstance(value, bytearray): + value = bytes(value) + return value + + def _resolve_blob_view_struct(self, view_struct): from pypaimon.table.row.blob import Blob + if self._blob_view_lookup is not None: + if self._blob_view_lookup.resolve_to_null(view_struct): + return None + descriptor = self._blob_view_lookup.resolve_descriptor(view_struct) + uri_reader = self._blob_view_lookup.resolve_uri_reader(view_struct) + return Blob.from_descriptor(uri_reader, descriptor) + return Blob.from_view(view_struct) + + def _blob_from_descriptor_field_bytes(self, raw: bytes): + from pypaimon.table.row.blob import Blob, BlobDescriptor + + if BlobDescriptor.is_blob_descriptor(raw): + return Blob.from_descriptor_bytes(raw, self._file_io) + if BlobDescriptor.parse_if_serialized(raw) is not None: + return Blob.from_descriptor_bytes(raw, self._file_io) + try: + # Accept v1/v2 descriptors with trailing padding (Java deserialize). + return Blob.from_descriptor_bytes(raw, self._file_io) + except ValueError: Review Comment: [P2] Please do not downgrade descriptor parsing failures to `BlobData` here. Membership in `descriptor_field_indices` already establishes that the schema/storage context requires a descriptor. With this fallback, a truncated v1 descriptor or corrupted/future v2 descriptor is silently returned as raw payload by the row API, while `BlobInlineConvertReader` raises `ValueError` for the same bytes. This contradicts the PR stated fail-fast behavior and makes row and batch reads inconsistent. Materialized payloads are already represented by clearing the descriptor indices in `BlobInlineConvertReader`, so descriptor-indexed values should use `from_descriptor_bytes` strictly. -- 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]
