JingsongLi commented on code in PR #9850: URL: https://github.com/apache/paimon/pull/9850#discussion_r4056249233
########## paimon-python/pypaimon/read/reader/parquet_page_index_reader.py: ########## @@ -0,0 +1,438 @@ +# 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. + +"""Read contiguous Parquet row windows using their OffsetIndex. + +Selected encoded pages are placed in bounded, in-memory Parquet files. PyArrow +still decodes the pages, including dictionary and compression encodings. Source +files are never rewritten. Nested fields retain their complete physical schema +and align their leaf columns at common row boundaries. Files without indexes and +disjoint ranges use the normal reader: per-page seeks can amplify requests in +filesystems that prefetch remote data (including Jindo). +""" + +import base64 +import bisect +import struct + +import pyarrow as pa +import pyarrow.parquet as pq + + +# Bound encoded data retained by the temporary column files, independently of +# the number of requested rows and the size of the source row group. +_MAX_PAGE_BYTES = 32 * 1024 * 1024 +_MAX_INDEX_BYTES = 8 * 1024 * 1024 + + +class _Compact: + """Thrift compact values used by Parquet metadata (no generated bindings).""" + + def __init__(self, data): + self.data = memoryview(data) + self.position = 0 + + def take(self, size): + end = self.position + size + if size < 0 or end > len(self.data): + raise ValueError("Truncated Parquet page-index metadata") + result = self.data[self.position:end] + self.position = end + return result + + def unsigned(self): + result = 0 + for shift in range(0, 70, 7): + value = self.take(1)[0] + result |= (value & 127) << shift + if value < 128: + return result + raise ValueError("Invalid Parquet compact integer") + + def value(self, kind, depth=0): + if depth > 64: + raise ValueError("Parquet metadata nesting exceeds 64 levels") + if kind in (1, 2): + return kind == 1 + if kind == 3: + return self.take(1).tobytes() + if kind in (4, 5, 6): + value = self.unsigned() + return (value >> 1) ^ -(value & 1) + if kind == 7: + return self.take(8).tobytes() + if kind == 8: + return self.take(self.unsigned()).tobytes() + if kind in (9, 10): + header = self.take(1)[0] + count, element = header >> 4, header & 15 + if count == 15: + count = self.unsigned() + if count > len(self.data) - self.position: + raise ValueError("Invalid Parquet compact collection size") + return element, [ Review Comment: [P1] Bound decoded OffsetIndex state, not only the encoded buffer _MAX_INDEX_BYTES caps serialized bytes, but this generic decoder materializes the complete collection as Python lists, tuples, and dictionaries before the OffsetIndex/PageLocation fields are validated. For example, an encoded list of empty structs passes the remaining-byte check and can expand an 8 MiB index into hundreds of MiB before the later row-boundary validation rejects it. The parquet-mr path uses generated PageLocation decoding, which validates each required field while decoding instead of first building a generic metadata tree. Please at least align with that behavior: decode OffsetIndex into typed fields, validate each PageLocation before retaining it, and enforce an explicit page-location/object budget before constructing the collection. -- 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]
