JingsongLi commented on code in PR #9850: URL: https://github.com/apache/paimon/pull/9850#discussion_r4056455804
########## paimon-python/pypaimon/read/reader/parquet_page_index_reader.py: ########## @@ -0,0 +1,626 @@ +# 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 page data retained by the temporary column files. +_MAX_PAGE_BYTES = 32 * 1024 * 1024 +# Bound all serialized OffsetIndexes and their retained typed PageLocations. +_MAX_INDEX_BYTES = 8 * 1024 * 1024 +_MAX_PAGE_LOCATIONS = 128 * 1024 + + +class _PageIndexBudgetExceeded(Exception): + pass + + +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, [ + self.value(self.take(1)[0] if element in (1, 2) else element, depth + 1) + for _ in range(count) + ] + if kind == 12: + fields = {} + previous = 0 + while True: + header = self.take(1)[0] + if header == 0: + return fields + delta, field_kind = header >> 4, header & 15 + field = previous + delta if delta else self.value(4) + if field in fields: + raise ValueError("Duplicate Parquet compact field") + fields[field] = field_kind, self.value(field_kind, depth + 1) + previous = field + raise ValueError("Unsupported Parquet compact type: {}".format(kind)) + + +class _OffsetIndexDecoder: + """Decode typed PageLocations without materializing generic Thrift trees.""" + + def __init__(self, data, max_locations): + self.parser = _Compact(data) + self.max_locations = max_locations + self.remaining_items = max_locations * 4 + + def decode(self): + locations = None + previous = 0 + seen = set() + while True: + field = self._field(previous) + if field is None: + break + field_id, kind = field + if field_id in seen: + raise ValueError("Duplicate Parquet OffsetIndex field") + seen.add(field_id) Review Comment: [P1] Charge struct fields against the OffsetIndex object budget The typed decoder budgets collection elements, but every decoded struct retains an unbudgeted seen set. Unknown inline-boolean fields consume no remaining_items budget, so a syntactically valid compact payload can expand far beyond the serialized cap before being rejected. I reproduced a 1,048,585-byte OffsetIndex that decoded successfully while peaking at 100,513,560 bytes; the current 8 MiB byte limit can therefore still create worker-threatening allocation on corrupt or forward-extended metadata. Please count every struct field against a global decoder budget, or avoid retaining unknown IDs, and raise _PageIndexBudgetExceeded so read_row_group falls back safely. A regression should bound allocation for many unknown fields, not only collection size. -- 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]
