You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@paimon.apache.org by "XiaoHongbo-Hope (via GitHub)" <gi...@apache.org> on 2026/09/18 09:23:30 UTC
[PR] [python] Add opt-in Parquet OffsetIndex reads for row windows [paimon]
XiaoHongbo-Hope opened a new pull request, #9850:
URL: https://github.com/apache/paimon/pull/9850
### Purpose
Reading a small row-ID window currently reads and decodes whole Parquet column chunks in each selected row group. Add opt-in OffsetIndex reads so PyPaimon can fetch the data pages covering that window, plus any required dictionary pages, and continue decoding with PyArrow.
Enable per read without changing persisted table options:
```python
table = table.copy({"read.parquet.page-index.enabled": "true"})
```
The option defaults to `false`; disabled reads bypass page-index processing. The initial implementation supports flat schemas and one contiguous selected window per row group. Disjoint windows, missing indexes, nested schemas, decoded row-group cache reads, and unsupported files keep the existing path. Large or unprofitable selections also fall back. This does not implement ColumnIndex predicate filtering.
Selected encoded pages are decoded through temporary in-memory Parquet column files using the original physical schema. Source files are never rewritten. The change adds no dependencies, preserves the existing metadata cache, validates page/index bounds and row counts, and propagates I/O and decoding errors. The 32 MiB selected-page budget bounds encoded payloads, not total process memory.
### Tests
- PyArrow 19.0.1 and 24.0.0: **77 tests passed on each**, covering page-index reads, row ranges, metadata caching, the default/explicit switch states, and public `table.copy()` reads.
- Coverage includes page V1/V2, dictionary/plain/delta/byte-stream-split encodings, four compression modes, nulls, decimals, timestamps, projection/missing columns, mixed optimized/fallback row groups, concurrent readers, early close, corrupt indexes, and I/O failures.
- Java parquet-mr 1.16.0 V1/V2 fixtures matched full-read-and-slice results. Arrow 6.0.1 read those fixtures correctly through fallback with the switch both on and off.
- Repository-configured flake8, license-header checks, and `git diff --check` passed.
- PyArrow 16.1.0 was unavailable and remains unverified; no packages were installed.
Controlled DLF/Jindo/OSS A/B used an isolated 65,536-row table, one fixed snapshot, identical queries, parallelism 1, and ABBA process ordering with cold/warm reads (24 reads total). Returned IDs, payloads, row IDs, and hashes matched. Native Jindo logs supplied HEAD/GET counts and returned GET bytes:
| Query / metadata cache | GET bytes: baseline → optimized | HEAD / GET: baseline → optimized |
| --- | ---: | ---: |
| 64-row window / cold | 7,265,111 → 4,119,383 (-43.3%) | 3 / 5 → 3 / 5 |
| 64-row window / warm | 5,916,957 → 2,771,229 (-53.2%) | 1 / 3 → 1 / 3 |
| Disjoint selection / cold | 10,410,839 → 10,410,839 | 3 / 6 → 3 / 6 |
| Disjoint selection / warm | 9,062,685 → 9,062,685 | 1 / 4 → 1 / 4 |
| Full scan / cold | 10,410,839 → 10,410,839 | 3 / 5 → 3 / 5 |
| Full scan / warm | 9,062,685 → 9,062,685 | 1 / 3 → 1 / 3 |
The disjoint-range fallback avoids request amplification observed when seeking between scattered pages under Jindo prefetch. These measurements show reduced bytes for this window workload, **not fewer OSS requests or a production throughput guarantee**. OSS A/B preceded the public configuration switch; the enabled algorithm is unchanged, and switch propagation was subsequently tested locally. Temporary test tables were removed.
--
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: issues-unsubscribe@paimon.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [python] Assemble LeRobot camera windows in parallel [paimon]
Posted by "XiaoHongbo-Hope (via GitHub)" <gi...@apache.org>.
XiaoHongbo-Hope closed pull request #9850: [python] Assemble LeRobot camera windows in parallel
URL: https://github.com/apache/paimon/pull/9850
--
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: issues-unsubscribe@paimon.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [python] Read Parquet row windows with OffsetIndex [paimon]
Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
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: issues-unsubscribe@paimon.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [python] Read Parquet row windows with OffsetIndex [paimon]
Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi merged PR #9850:
URL: https://github.com/apache/paimon/pull/9850
--
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: issues-unsubscribe@paimon.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [python] Read Parquet row windows with OffsetIndex [paimon]
Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi commented on code in PR #9850:
URL: https://github.com/apache/paimon/pull/9850#discussion_r4056456298
##########
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)
+ previous = field_id
+ if field_id == 1:
+ if kind != 9:
+ raise ValueError("Invalid Parquet OffsetIndex page locations")
+ locations = self._locations()
+ else:
+ self._skip(kind)
+ if locations is None or self.parser.position != len(self.parser.data):
+ raise ValueError("Invalid Parquet OffsetIndex")
+ return locations
+
+ def _field(self, previous):
+ header = self.parser.take(1)[0]
+ if header == 0:
+ return None
+ delta, kind = header >> 4, header & 15
+ field = previous + delta if delta else self.parser.value(4)
+ if field <= 0:
+ raise ValueError("Invalid Parquet compact field")
+ return field, kind
+
+ def _collection(self):
+ header = self.parser.take(1)[0]
+ count, element = header >> 4, header & 15
+ if count == 15:
+ count = self.parser.unsigned()
+ if count > len(self.parser.data) - self.parser.position:
+ raise ValueError("Invalid Parquet compact collection size")
+ return count, element
+
+ def _locations(self):
+ count, element = self._collection()
+ if element != 12:
+ raise ValueError("Invalid Parquet OffsetIndex page locations")
+ if count > self.max_locations:
+ raise _PageIndexBudgetExceeded(
+ "Parquet OffsetIndex exceeds page-location budget")
+ self._consume(count)
+ return [self._location() for _ in range(count)]
+
+ def _location(self):
+ values = [None, None, None]
+ expected = (6, 5, 6)
+ 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 PageLocation field")
+ seen.add(field_id)
+ previous = field_id
+ if 1 <= field_id <= 3:
+ if kind != expected[field_id - 1]:
+ raise ValueError("Invalid Parquet PageLocation field type")
+ values[field_id - 1] = self.parser.value(kind)
+ else:
+ self._skip(kind)
+ if any(value is None for value in values):
+ raise ValueError("Missing Parquet PageLocation field")
+ offset, size, first_row = values
+ if offset < 0 or size <= 0 or first_row < 0:
+ raise ValueError("Invalid Parquet PageLocation")
+ return offset, size, first_row
+
+ def _consume(self, count):
+ if count > self.remaining_items:
+ raise _PageIndexBudgetExceeded(
+ "Parquet compact metadata exceeds object budget")
+ self.remaining_items -= count
+
+ def _skip_collection_value(self, kind, depth):
+ if kind in (1, 2):
+ actual = self.parser.take(1)[0]
+ if actual not in (1, 2):
+ raise ValueError("Invalid Parquet compact boolean")
+ else:
+ self._skip(kind, depth)
+
+ def _skip(self, kind, depth=0):
+ if depth > 64:
+ raise ValueError("Parquet metadata nesting exceeds 64 levels")
+ if kind in (1, 2):
+ return
+ if kind == 3:
+ self.parser.take(1)
+ return
+ if kind in (4, 5, 6):
+ self.parser.unsigned()
+ return
+ if kind == 7:
+ self.parser.take(8)
+ return
+ if kind == 8:
+ self.parser.take(self.parser.unsigned())
+ return
+ if kind in (9, 10):
+ count, element = self._collection()
+ self._consume(count)
+ for _ in range(count):
+ self._skip_collection_value(element, depth + 1)
+ return
+ if kind == 11:
+ count = self.parser.unsigned()
+ self._consume(count * 2)
+ if count:
+ kinds = self.parser.take(1)[0]
+ key_kind, value_kind = kinds >> 4, kinds & 15
+ for _ in range(count):
+ self._skip_collection_value(key_kind, depth + 1)
+ self._skip_collection_value(value_kind, depth + 1)
+ return
+ if kind == 12:
+ previous = 0
+ seen = set()
+ while True:
+ field = self._field(previous)
+ if field is None:
+ return
+ field_id, field_kind = field
+ if field_id in seen:
+ raise ValueError("Duplicate Parquet compact field")
+ seen.add(field_id)
+ previous = field_id
+ self._skip(field_kind, depth + 1)
+ raise ValueError("Unsupported Parquet compact type: {}".format(kind))
+
+
+def _decode_offset_index(data, max_locations):
+ return _OffsetIndexDecoder(data, max_locations).decode()
+
+
+def _unsigned(value):
+ result = bytearray()
+ while value >= 128:
+ result.append((value & 127) | 128)
+ value >>= 7
+ result.append(value)
+ return bytes(result)
+
+
+def _encode(kind, value):
+ if kind in (1, 2):
+ return bytes([1 if value else 2])
+ if kind in (3, 7):
+ return value
+ if kind in (4, 5, 6):
+ return _unsigned(value * 2 if value >= 0 else -value * 2 - 1)
+ if kind == 8:
+ return _unsigned(len(value)) + value
+ if kind in (9, 10):
+ element, items = value
+ size = len(items)
+ header = bytes([(min(size, 15) << 4) | element])
+ if size >= 15:
+ header += _unsigned(size)
+ return header + b"".join(_encode(element, item) for item in items)
+ if kind == 12:
+ result = bytearray()
+ previous = 0
+ for field, (field_kind, item) in sorted(value.items()):
+ delta = field - previous
+ if field_kind in (1, 2):
+ field_kind = 1 if item else 2
+ if 0 < delta < 16:
+ result.append((delta << 4) | field_kind)
+ else:
+ result.append(field_kind)
+ result.extend(_encode(4, field))
+ if field_kind not in (1, 2):
+ result.extend(_encode(field_kind, item))
+ previous = field
+ result.append(0)
+ return bytes(result)
+ raise ValueError("Unsupported Parquet compact type: {}".format(kind))
+
+
+def _get(fields, field, default=None):
+ return fields[field][1] if field in fields else default
+
+
+def _read_exact(source, offset, length):
+ if offset < 4 or length <= 0:
+ raise ValueError("Invalid Parquet page-index byte range")
+ data = source.read_at(length, offset)
+ if len(data) != length:
+ raise OSError("Truncated Parquet page-index byte range")
+ return data
+
+
+def _read_index_ranges(source, ranges):
+ groups = []
+ for key, offset, length in sorted(ranges, key=lambda item: item[1]):
+ if offset < 4 or length <= 0:
+ raise ValueError("Invalid Parquet page-index byte range")
+ end = offset + length
+ if groups and offset < groups[-1][1]:
+ raise ValueError("Overlapping Parquet page-index byte ranges")
+ if groups and offset == groups[-1][1]:
+ groups[-1][1] = end
+ groups[-1][2].append((key, offset, length))
+ else:
+ groups.append([offset, end, [(key, offset, length)]])
+ result = {}
+ for start, end, members in groups:
+ data = memoryview(_read_exact(source, start, end - start))
+ for key, offset, length in members:
+ result[key] = data[offset - start:offset - start + length]
+ return result
+
+
+class ParquetPageIndexReader:
+ def __init__(self, source, metadata, schema, footer, columns, fields, batch_size):
+ self.source = source
+ self.metadata = metadata
+ self.schema = schema
+ self.footer = footer
+ self.columns = columns
+ self.fields = fields
+ self.batch_size = batch_size
+
+ @classmethod
+ def create(cls, source, parquet_file, columns, row_groups, batch_size):
+ metadata = parquet_file.metadata
+ schema = parquet_file.schema_arrow
+ if not columns or len(set(schema.names)) != len(schema):
+ return None
+ indices = [schema.get_field_index(name) for name in columns]
+ if any(index < 0 for index in indices) or len(set(indices)) != len(indices):
+ return None
+ if not any(getattr(metadata.row_group(group).column(index),
+ "has_offset_index", False)
+ for group in row_groups for index in range(metadata.num_columns)):
+ return None
+ output = pa.BufferOutputStream()
+ metadata.write_metadata_file(output)
+ serialized = output.getvalue().to_pybytes()
+ length = struct.unpack("<I", serialized[-8:-4])[0]
+ footer = _Compact(serialized[-8 - length:-8]).value(12)
Review Comment:
[P2] Avoid materializing every unselected row group footer tree
create serializes and generic-decodes the complete FileMetaData, then retains the whole Python object tree even when row_groups selects only one group. On a valid one-column file with 5,000 row groups, create(..., row_groups=[0]) turned a 574,774-byte footer into 25,377,527 retained bytes, peaked at 26,528,093 bytes, and took 0.742 seconds locally. This is paid on the default-enabled row-range path and scales with irrelevant groups.
Please stream or retain only schema plus selected row groups, cache a bounded decoded representation, or reject oversized or over-fragmented metadata before generic materialization and fall back to the ordinary reader.
--
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: issues-unsubscribe@paimon.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [python] Read Parquet row windows with OffsetIndex [paimon]
Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi commented on code in PR #9850:
URL: https://github.com/apache/paimon/pull/9850#discussion_r4056525648
##########
paimon-python/pypaimon/read/reader/parquet_page_index_reader.py:
##########
@@ -0,0 +1,630 @@
+# 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 the generic FileMetaData tree before creating Python objects for it.
+_MAX_FOOTER_BYTES = 1024 * 1024
+_MAX_FOOTER_COLUMN_CHUNKS = 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
+ # PageLocations retain three fields each. Allow the standard optional
+ # per-page byte-count list plus a small amount of forward metadata.
+ self.remaining_items = max_locations * 6 + 16
+
+ def decode(self):
+ locations = None
+ previous = 0
+ while True:
+ field = self._field(previous)
+ if field is None:
+ break
+ field_id, kind = field
+ previous = field_id
+ if field_id == 1:
+ if locations is not None:
+ raise ValueError("Duplicate Parquet OffsetIndex field")
+ if kind != 9:
+ raise ValueError("Invalid Parquet OffsetIndex page locations")
+ locations = self._locations()
+ else:
+ self._skip(kind)
+ if locations is None or self.parser.position != len(self.parser.data):
+ raise ValueError("Invalid Parquet OffsetIndex")
+ return locations
+
+ def _field(self, previous):
+ header = self.parser.take(1)[0]
+ if header == 0:
+ return None
+ delta, kind = header >> 4, header & 15
+ field = previous + delta if delta else self.parser.value(4)
+ if field <= 0:
+ raise ValueError("Invalid Parquet compact field")
+ self._consume(1)
+ return field, kind
+
+ def _collection(self):
+ header = self.parser.take(1)[0]
+ count, element = header >> 4, header & 15
+ if count == 15:
+ count = self.parser.unsigned()
+ if count > len(self.parser.data) - self.parser.position:
+ raise ValueError("Invalid Parquet compact collection size")
+ return count, element
+
+ def _locations(self):
+ count, element = self._collection()
+ if element != 12:
+ raise ValueError("Invalid Parquet OffsetIndex page locations")
+ if count > self.max_locations:
+ raise _PageIndexBudgetExceeded(
+ "Parquet OffsetIndex exceeds page-location budget")
+ self._consume(count)
+ return [self._location() for _ in range(count)]
+
+ def _location(self):
+ values = [None, None, None]
+ expected = (6, 5, 6)
+ previous = 0
+ while True:
+ field = self._field(previous)
+ if field is None:
+ break
+ field_id, kind = field
+ previous = field_id
+ if 1 <= field_id <= 3:
+ if values[field_id - 1] is not None:
+ raise ValueError("Duplicate Parquet PageLocation field")
+ if kind != expected[field_id - 1]:
+ raise ValueError("Invalid Parquet PageLocation field type")
+ values[field_id - 1] = self.parser.value(kind)
+ else:
+ self._skip(kind)
+ if any(value is None for value in values):
+ raise ValueError("Missing Parquet PageLocation field")
+ offset, size, first_row = values
+ if offset < 0 or size <= 0 or first_row < 0:
+ raise ValueError("Invalid Parquet PageLocation")
+ return offset, size, first_row
+
+ def _consume(self, count):
+ if count > self.remaining_items:
+ raise _PageIndexBudgetExceeded(
+ "Parquet compact metadata exceeds object budget")
+ self.remaining_items -= count
+
+ def _skip_collection_value(self, kind, depth):
+ if kind in (1, 2):
+ actual = self.parser.take(1)[0]
+ if actual not in (1, 2):
+ raise ValueError("Invalid Parquet compact boolean")
+ else:
+ self._skip(kind, depth)
+
+ def _skip(self, kind, depth=0):
+ if depth > 64:
+ raise ValueError("Parquet metadata nesting exceeds 64 levels")
+ if kind in (1, 2):
+ return
+ if kind == 3:
+ self.parser.take(1)
+ return
+ if kind in (4, 5, 6):
+ self.parser.unsigned()
+ return
+ if kind == 7:
+ self.parser.take(8)
+ return
+ if kind == 8:
+ self.parser.take(self.parser.unsigned())
+ return
+ if kind in (9, 10):
+ count, element = self._collection()
+ self._consume(count)
+ for _ in range(count):
+ self._skip_collection_value(element, depth + 1)
+ return
+ if kind == 11:
+ count = self.parser.unsigned()
+ self._consume(count * 2)
+ if count:
+ kinds = self.parser.take(1)[0]
+ key_kind, value_kind = kinds >> 4, kinds & 15
+ for _ in range(count):
+ self._skip_collection_value(key_kind, depth + 1)
+ self._skip_collection_value(value_kind, depth + 1)
+ return
+ if kind == 12:
+ previous = 0
+ while True:
+ field = self._field(previous)
+ if field is None:
+ return
+ field_id, field_kind = field
+ previous = field_id
+ self._skip(field_kind, depth + 1)
+ raise ValueError("Unsupported Parquet compact type: {}".format(kind))
+
+
+def _decode_offset_index(data, max_locations):
+ return _OffsetIndexDecoder(data, max_locations).decode()
+
+
+def _unsigned(value):
+ result = bytearray()
+ while value >= 128:
+ result.append((value & 127) | 128)
+ value >>= 7
+ result.append(value)
+ return bytes(result)
+
+
+def _encode(kind, value):
+ if kind in (1, 2):
+ return bytes([1 if value else 2])
+ if kind in (3, 7):
+ return value
+ if kind in (4, 5, 6):
+ return _unsigned(value * 2 if value >= 0 else -value * 2 - 1)
+ if kind == 8:
+ return _unsigned(len(value)) + value
+ if kind in (9, 10):
+ element, items = value
+ size = len(items)
+ header = bytes([(min(size, 15) << 4) | element])
+ if size >= 15:
+ header += _unsigned(size)
+ return header + b"".join(_encode(element, item) for item in items)
+ if kind == 12:
+ result = bytearray()
+ previous = 0
+ for field, (field_kind, item) in sorted(value.items()):
+ delta = field - previous
+ if field_kind in (1, 2):
+ field_kind = 1 if item else 2
+ if 0 < delta < 16:
+ result.append((delta << 4) | field_kind)
+ else:
+ result.append(field_kind)
+ result.extend(_encode(4, field))
+ if field_kind not in (1, 2):
+ result.extend(_encode(field_kind, item))
+ previous = field
+ result.append(0)
+ return bytes(result)
+ raise ValueError("Unsupported Parquet compact type: {}".format(kind))
+
+
+def _get(fields, field, default=None):
+ return fields[field][1] if field in fields else default
+
+
+def _read_exact(source, offset, length):
+ if offset < 4 or length <= 0:
+ raise ValueError("Invalid Parquet page-index byte range")
+ data = source.read_at(length, offset)
+ if len(data) != length:
+ raise OSError("Truncated Parquet page-index byte range")
+ return data
+
+
+def _read_index_ranges(source, ranges):
+ groups = []
+ for key, offset, length in sorted(ranges, key=lambda item: item[1]):
+ if offset < 4 or length <= 0:
+ raise ValueError("Invalid Parquet page-index byte range")
+ end = offset + length
+ if groups and offset < groups[-1][1]:
+ raise ValueError("Overlapping Parquet page-index byte ranges")
+ if groups and offset == groups[-1][1]:
+ groups[-1][1] = end
+ groups[-1][2].append((key, offset, length))
+ else:
+ groups.append([offset, end, [(key, offset, length)]])
+ result = {}
+ for start, end, members in groups:
+ data = memoryview(_read_exact(source, start, end - start))
+ for key, offset, length in members:
+ result[key] = data[offset - start:offset - start + length]
+ return result
+
+
+class ParquetPageIndexReader:
+ def __init__(self, source, metadata, schema, footer, columns, fields, batch_size):
+ self.source = source
+ self.metadata = metadata
+ self.schema = schema
+ self.footer = footer
+ self.columns = columns
+ self.fields = fields
+ self.batch_size = batch_size
+
+ @classmethod
+ def create(cls, source, parquet_file, columns, row_groups, batch_size):
+ metadata = parquet_file.metadata
+ schema = parquet_file.schema_arrow
+ if not columns or len(set(schema.names)) != len(schema):
+ return None
+ indices = [schema.get_field_index(name) for name in columns]
+ if any(index < 0 for index in indices) or len(set(indices)) != len(indices):
+ return None
+ if not any(getattr(metadata.row_group(group).column(index),
+ "has_offset_index", False)
+ for group in row_groups for index in range(metadata.num_columns)):
+ return None
+ if (metadata.serialized_size > _MAX_FOOTER_BYTES
+ or metadata.num_row_groups * metadata.num_columns
+ > _MAX_FOOTER_COLUMN_CHUNKS):
+ return None
+ output = pa.BufferOutputStream()
+ metadata.write_metadata_file(output)
+ serialized = output.getvalue().to_pybytes()
+ length = struct.unpack("<I", serialized[-8:-4])[0]
+ if length > _MAX_FOOTER_BYTES:
+ return None
+ footer = _Compact(serialized[-8 - length:-8]).value(12)
+ if 8 in footer or 9 in footer:
+ return None # Encrypted pages need the original file identity/AAD.
+ elements = _get(footer, 2)[1]
+ # Parquet stores a preorder schema tree and one chunk per physical leaf.
+ # Arrow field positions cannot be used as physical column positions.
+ fields = []
+ position, leaf = 1, 0
+ for _ in range(_get(elements[0], 5)):
+ start, first_leaf, pending = position, leaf, 1
+ while pending:
+ if position >= len(elements):
+ raise ValueError("Truncated Parquet schema tree")
+ element = elements[position]
+ children = _get(element, 5, 0)
+ if children < 0 or (1 in element and children) or (1 not in element and not children):
+ raise ValueError("Invalid Parquet schema child count")
+ pending += children - 1
+ leaf += int(1 in element)
+ position += 1
+ fields.append((elements[start:position], list(range(first_leaf, leaf))))
+ if (position != len(elements) or leaf != metadata.num_columns
+ or len(fields) != len(schema)):
+ return None
+ if not any(all(getattr(metadata.row_group(group).column(leaf),
+ "has_offset_index", False)
+ for index in indices for leaf in fields[index][1])
+ for group in row_groups):
+ return None
+ return cls(source, metadata, schema, footer, indices, fields, batch_size)
+
+ def read_row_group(self, group, runs):
+ """Return selected batches, or None when the ordinary reader is cheaper."""
+ # Decide before reading indexes so scattered selections preserve the
+ # existing I/O pattern. A future multi-range path needs an I/O planner
+ # that accounts for filesystem prefetch, not just compressed page sizes.
+ if len(runs) != 1:
+ return None
+ row_group = _get(self.footer, 4)[1][group]
+ row_count = _get(row_group, 3)
+ if sum(upper - lower + 1 for lower, upper in runs) >= row_count:
+ return None
+ chunks = _get(row_group, 1)[1]
+ physical_columns = [leaf for index in self.columns for leaf in self.fields[index][1]]
+ if any(4 not in chunks[index] or 5 not in chunks[index]
+ or _get(chunks[index], 1) or 8 in chunks[index] or 9 in chunks[index]
+ or 10 in _get(chunks[index], 3) # Legacy index pages.
+ for index in physical_columns):
+ return None
+ indexed = {}
+ plans = []
+ selected_bytes = 0
+ full_bytes = 0
+ index_ranges = []
+ for index in sorted(physical_columns):
+ chunk = chunks[index]
+ index_size = _get(chunk, 5)
+ index_ranges.append((index, _get(chunk, 4), index_size))
+ index_bytes = sum(length for _, _, length in index_ranges)
+ if index_bytes > _MAX_INDEX_BYTES:
+ return None
+ raw_indexes = _read_index_ranges(self.source, index_ranges)
+ remaining_locations = _MAX_PAGE_LOCATIONS
+ for index in sorted(physical_columns):
+ chunk = chunks[index]
+ try:
+ locations = _decode_offset_index(raw_indexes[index], remaining_locations)
+ except _PageIndexBudgetExceeded:
+ return None
+ remaining_locations -= len(locations)
+ column = _get(chunk, 3)
+ data_offset = _get(column, 9)
+ dictionary_offset = _get(column, 11, data_offset)
+ chunk_end = dictionary_offset + _get(column, 7)
+ starts = [page[2] for page in locations]
+ if not starts or starts[0] != 0 or starts[-1] >= row_count:
+ raise ValueError("Invalid Parquet OffsetIndex row boundaries")
+ previous_end = data_offset
+ previous_row = -1
+ for page in locations:
+ offset, size, first_row = page
+ if (offset < previous_end or size <= 0 or offset + size > chunk_end
+ or first_row <= previous_row):
+ raise ValueError("Invalid Parquet OffsetIndex page location")
+ previous_end, previous_row = offset + size, first_row
+ if locations[0][0] != data_offset or dictionary_offset > data_offset:
+ raise ValueError("Invalid Parquet OffsetIndex first page")
+ indexed[index] = (column, dictionary_offset, data_offset - dictionary_offset,
+ locations, starts)
+ full_bytes += _get(column, 7)
+ for field in sorted(self.columns):
+ leaves = self.fields[field][1]
+ # OffsetIndex pages must start at row boundaries (repetition level 0).
+ # Keep all leaves of a field aligned so Arrow can reconstruct nesting.
+ # ponytail: common boundaries may widen to the whole group; independent
+ # leaf decoding/reassembly can recover savings if this becomes costly.
+ boundaries = set(indexed[leaves[0]][4])
+ for leaf in leaves[1:]:
+ boundaries.intersection_update(indexed[leaf][4])
+ boundaries = sorted(boundaries) + [row_count]
+ lower, upper = runs[0]
+ lower = boundaries[bisect.bisect_right(boundaries, lower) - 1]
+ end = boundaries[bisect.bisect_right(boundaries, upper)]
+ column_plans = []
+ for index in leaves:
+ column, dictionary_offset, dictionary_size, locations, starts = indexed[index]
+ selected = range(bisect.bisect_left(starts, lower),
+ bisect.bisect_left(starts, end))
+ pages, infos = [], []
+ for position in selected:
+ page = locations[position]
+ pages.append(page[:2])
+ next_row = starts[position + 1] if position + 1 < len(starts) else row_count
+ infos.append((starts[position], next_row - starts[position]))
+ selected_bytes += dictionary_size + sum(size for _, size in pages)
+ column_plans.append((index, column, dictionary_offset, dictionary_size, pages, infos))
+ plans.append((field, column_plans, [(lower, end - lower)]))
+ if (selected_bytes + index_bytes >= full_bytes
+ or selected_bytes > _MAX_PAGE_BYTES):
+ return None
+ return self._batches(plans, runs)
+
+ def _column_payload(self, plan):
+ index, column, dictionary_offset, dictionary_size, pages, infos = plan
+ ranges = ([(dictionary_offset, dictionary_size)] if dictionary_size else []) + pages
+ # Coalesce adjacent dictionary/data pages without fetching skipped pages.
+ groups = []
+ for offset, length in ranges:
+ if groups and groups[-1][0] + groups[-1][1] == offset:
+ groups[-1][1] += length
+ else:
+ groups.append([offset, length])
+ payload = b"".join(_read_exact(self.source, offset, length)
+ for offset, length in groups)
+ cursor = 0
+ uncompressed_size = 0
+ num_values = 0
+ repeated = self.metadata.schema.column(index).max_repetition_level > 0
+ for position, (_, length) in enumerate(ranges):
+ parser = _Compact(memoryview(payload)[cursor:cursor + length])
+ header = parser.value(12)
Review Comment:
OffsetIndex has been switched to a typed/bounded decoder, but the PageHeader of the data page is still decoded using the generic `_Compact.value(12)`, which fully expands unknown collections or structs into Python lists or dictionaries. `_MAX_PAGE_BYTES` limits only the encoded data size (32 MiB), not the size of the decoded objects.
--
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: issues-unsubscribe@paimon.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [python] Add opt-in Parquet OffsetIndex reads for row windows [paimon]
Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi commented on code in PR #9850:
URL: https://github.com/apache/paimon/pull/9850#discussion_r4055749231
##########
paimon-python/pypaimon/common/options/core_options.py:
##########
@@ -1128,6 +1128,19 @@ class CoreOptions:
.with_description("Read batch size for any file format if it supports.")
)
+ PARQUET_COLUMN_INDEX_ENABLED: ConfigOption[bool] = (
+ ConfigOptions.key("parquet.filter.columnindex.enabled")
+ .boolean_type()
+ .default_value(False)
Review Comment:
[P2] Preserve the existing default for the shared Parquet option
parquet.filter.columnindex.enabled is an existing parquet-mr option whose default is true, and Paimon forwards parquet.* table options to Java ParquetReadOptions. Defining the same key with a default of false makes its effective behavior differ between Java and PyPaimon when the option is absent. Please change this default to true. If the new PyPaimon OffsetIndex path must remain opt-in, it should use a separate PyPaimon-specific option instead.
--
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: issues-unsubscribe@paimon.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [python] Read Parquet row windows with OffsetIndex [paimon]
Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi commented on code in PR #9850:
URL: https://github.com/apache/paimon/pull/9850#discussion_r4056249561
##########
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, [
+ 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))
+
+
+def _unsigned(value):
+ result = bytearray()
+ while value >= 128:
+ result.append((value & 127) | 128)
+ value >>= 7
+ result.append(value)
+ return bytes(result)
+
+
+def _encode(kind, value):
+ if kind in (1, 2):
+ return bytes([1 if value else 2])
+ if kind in (3, 7):
+ return value
+ if kind in (4, 5, 6):
+ return _unsigned(value * 2 if value >= 0 else -value * 2 - 1)
+ if kind == 8:
+ return _unsigned(len(value)) + value
+ if kind in (9, 10):
+ element, items = value
+ size = len(items)
+ header = bytes([(min(size, 15) << 4) | element])
+ if size >= 15:
+ header += _unsigned(size)
+ return header + b"".join(_encode(element, item) for item in items)
+ if kind == 12:
+ result = bytearray()
+ previous = 0
+ for field, (field_kind, item) in sorted(value.items()):
+ delta = field - previous
+ if field_kind in (1, 2):
+ field_kind = 1 if item else 2
+ if 0 < delta < 16:
+ result.append((delta << 4) | field_kind)
+ else:
+ result.append(field_kind)
+ result.extend(_encode(4, field))
+ if field_kind not in (1, 2):
+ result.extend(_encode(field_kind, item))
+ previous = field
+ result.append(0)
+ return bytes(result)
+ raise ValueError("Unsupported Parquet compact type: {}".format(kind))
+
+
+def _get(fields, field, default=None):
+ return fields[field][1] if field in fields else default
+
+
+def _read_exact(source, offset, length):
+ if offset < 4 or length <= 0:
+ raise ValueError("Invalid Parquet page-index byte range")
+ data = source.read_at(length, offset)
+ if len(data) != length:
+ raise OSError("Truncated Parquet page-index byte range")
+ return data
+
+
+class ParquetPageIndexReader:
+ def __init__(self, source, metadata, schema, footer, columns, fields, batch_size):
+ self.source = source
+ self.metadata = metadata
+ self.schema = schema
+ self.footer = footer
+ self.columns = columns
+ self.fields = fields
+ self.batch_size = batch_size
+
+ @classmethod
+ def create(cls, source, parquet_file, columns, row_groups, batch_size):
+ metadata = parquet_file.metadata
+ schema = parquet_file.schema_arrow
+ if not columns or len(set(schema.names)) != len(schema):
+ return None
+ indices = [schema.get_field_index(name) for name in columns]
+ if any(index < 0 for index in indices) or len(set(indices)) != len(indices):
+ return None
+ if not any(getattr(metadata.row_group(group).column(index),
+ "has_offset_index", False)
+ for group in row_groups for index in range(metadata.num_columns)):
+ return None
+ output = pa.BufferOutputStream()
+ metadata.write_metadata_file(output)
+ serialized = output.getvalue().to_pybytes()
+ length = struct.unpack("<I", serialized[-8:-4])[0]
+ footer = _Compact(serialized[-8 - length:-8]).value(12)
+ if 8 in footer or 9 in footer:
+ return None # Encrypted pages need the original file identity/AAD.
+ elements = _get(footer, 2)[1]
+ # Parquet stores a preorder schema tree and one chunk per physical leaf.
+ # Arrow field positions cannot be used as physical column positions.
+ fields = []
+ position, leaf = 1, 0
+ for _ in range(_get(elements[0], 5)):
+ start, first_leaf, pending = position, leaf, 1
+ while pending:
+ if position >= len(elements):
+ raise ValueError("Truncated Parquet schema tree")
+ element = elements[position]
+ children = _get(element, 5, 0)
+ if children < 0 or (1 in element and children) or (1 not in element and not children):
+ raise ValueError("Invalid Parquet schema child count")
+ pending += children - 1
+ leaf += int(1 in element)
+ position += 1
+ fields.append((elements[start:position], list(range(first_leaf, leaf))))
+ if (position != len(elements) or leaf != metadata.num_columns
+ or len(fields) != len(schema)):
+ return None
+ if not any(all(getattr(metadata.row_group(group).column(leaf),
+ "has_offset_index", False)
+ for index in indices for leaf in fields[index][1])
+ for group in row_groups):
+ return None
+ return cls(source, metadata, schema, footer, indices, fields, batch_size)
+
+ def read_row_group(self, group, runs):
+ """Return selected batches, or None when the ordinary reader is cheaper."""
+ # Decide before reading indexes so scattered selections preserve the
+ # existing I/O pattern. A future multi-range path needs an I/O planner
+ # that accounts for filesystem prefetch, not just compressed page sizes.
+ if len(runs) != 1:
+ return None
+ row_group = _get(self.footer, 4)[1][group]
+ row_count = _get(row_group, 3)
+ if sum(upper - lower + 1 for lower, upper in runs) >= row_count:
+ return None
+ chunks = _get(row_group, 1)[1]
+ physical_columns = [leaf for index in self.columns for leaf in self.fields[index][1]]
+ if any(4 not in chunks[index] or 5 not in chunks[index]
+ or _get(chunks[index], 1) or 8 in chunks[index] or 9 in chunks[index]
+ or 10 in _get(chunks[index], 3) # Legacy index pages.
+ for index in physical_columns):
+ return None
+ indexed = {}
+ plans = []
+ selected_bytes = 0
+ index_bytes = 0
+ full_bytes = 0
+ for index in sorted(physical_columns):
+ chunk = chunks[index]
+ index_size = _get(chunk, 5)
+ if index_size > _MAX_INDEX_BYTES:
+ return None
+ raw = _read_exact(self.source, _get(chunk, 4), index_size)
Review Comment:
[P2] Batch OffsetIndex reads before deciding to fall back
Each physical leaf performs a separate read_at here, while selected_bytes plus index_bytes is checked only after every index has been fetched. With 200 scalar columns and a one-page row group, selecting rows 0 through 1 adds about 200 index range reads and then falls back to the full-row-group reader because no page can be skipped. parquet-mr retains decoded indexes in a row-group ColumnIndexStore and coalesces or vector-reads selected data ranges; its source also explicitly marks batching consecutive OffsetIndexes as a TODO. Please keep this default-on Python path at least no worse than that model by planning and coalescing adjacent index ranges from the footer, reusing decoded indexes, and/or rejecting the optimization from footer metadata before issuing per-column reads.
--
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: issues-unsubscribe@paimon.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [python] Read Parquet row windows with OffsetIndex [paimon]
Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
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: issues-unsubscribe@paimon.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org