adriangb commented on code in PR #10020: URL: https://github.com/apache/arrow-rs/pull/10020#discussion_r3351595102
########## parquet/src/column/page_store.rs: ########## @@ -0,0 +1,226 @@ +// 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. + +//! Pluggable storage for completed, serialized page blobs. +//! +//! While a row group is being written the [`ArrowWriter`] must buffer every +//! column's encoded pages, because Parquet requires each column chunk to be +//! contiguous on disk while record batches arrive with all columns interleaved. +//! By default that buffer lives on the heap, so the writer's peak memory grows +//! with the row group size. A [`PageStore`] lets the buffer live somewhere else +//! — a local temp file, object storage, etc. — bounding peak write memory +//! independently of the row group size. +//! +//! [`ArrowWriter`]: crate::arrow::arrow_writer::ArrowWriter + +use std::fmt::Debug; + +use bytes::Bytes; + +use crate::errors::{ParquetError, Result}; + +/// An opaque, store-allocated handle to a blob held by a [`PageStore`]. +/// +/// Handles are allocated by the store — densely and sequentially — and are only +/// meaningful to the store that produced them. The caller treats them as opaque +/// tokens and decides what they *mean* (ordering, which one is the dictionary +/// page, etc.). +/// +/// Letting the store allocate the handle (rather than the caller choosing keys) +/// lets each backend pick the cheapest possible locator with no hashing: an +/// in-memory backend uses the handle as an index into a `Vec`, a temp-file +/// backend as an index into a `Vec<(offset, len)>`, and so on. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct PageKey(u64); + +impl PageKey { + /// Create a handle wrapping `raw`. + /// + /// A [`PageStore`] implementation calls this to mint the handle it returns + /// from [`put`](PageStore::put). The value is opaque to the caller, so a + /// store is free to use a dense counter, a packed locator, or anything else + /// it can later resolve in [`take`](PageStore::take). + pub const fn new(raw: u64) -> Self { + Self(raw) + } + + /// The raw value passed to [`new`](Self::new). + pub const fn get(self) -> u64 { + self.0 + } +} + +/// A pluggable store for completed, serialized page blobs. +/// +/// The store is intentionally "dumb": it only maps an opaque [`PageKey`] to a +/// blob of bytes. It knows nothing about pages, dictionaries, ordering, or +/// offsets. The caller keeps the handles it gets back from [`put`](Self::put) +/// and decides what they mean. +/// +/// Each store instance is owned by a single column writer and mutated by one +/// thread at a time (both methods take `&mut self`), so it needs no internal +/// synchronization — hence only `Send`, not `Sync`. +/// +/// The default ([`InMemoryPageStore`]) keeps blobs on the heap. Configure a +/// different backend via +/// [`ArrowWriterOptions::with_page_store_factory`](crate::arrow::arrow_writer::ArrowWriterOptions::with_page_store_factory). +pub trait PageStore: Send { + /// Store `value`, returning a handle that can later be passed to + /// [`take`](Self::take). + fn put(&mut self, value: Bytes) -> Result<PageKey>; + + /// Take back the blob previously stored under `key`. + /// + /// The caller takes ownership of the returned bytes and will **not** request + /// `key` again, so the store may release any resources backing it — eagerly + /// here, or when the store is dropped. + fn take(&mut self, key: PageKey) -> Result<Bytes>; + + /// The number of bytes this store currently holds **in memory** (resident + /// on the heap), used to report the writer's memory footprint. + /// + /// The default is `0`, which is exactly right for a backend that moves + /// every blob off-heap (a temp file, object storage): the bytes it has been + /// handed no longer occupy heap. The in-memory backend overrides this to + /// report its resident blobs. A backend that keeps a partial in-memory + /// buffer should report that buffer's size. + fn memory_size(&self) -> usize { + 0 + } +} + +/// Creates a fresh [`PageStore`] for each column chunk. +/// +/// See +/// [`ArrowWriterOptions::with_page_store_factory`](crate::arrow::arrow_writer::ArrowWriterOptions::with_page_store_factory). +pub trait PageStoreFactory: Send + Sync + Debug { + /// Create a new, empty [`PageStore`] for the leaf column at `column_index`. + /// + /// `column_index` is a hint a backend may use to e.g. name spill files or + /// shard across a bounded pool; it carries no ordering or coordination + /// requirement. + fn create(&self, column_index: usize) -> Result<Box<dyn PageStore>>; Review Comment: Done in e02a74d360 — `create` now takes a `PageStoreArgs<'_>` instead of a bare `column_index`. The fields are private with no public constructor (only the writer builds one), so we can add fields in future minor releases without breaking implementations. It already carries `column_index()` and `column_descriptor()` (physical/logical type, path, max def/rep levels) — both call sites already had the `ColumnDescPtr` in hand — so a backend can, e.g., spill only large `BYTE_ARRAY` columns and keep small fixed-width ones on the heap. ########## parquet/src/column/page_store.rs: ########## @@ -0,0 +1,226 @@ +// 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. + +//! Pluggable storage for completed, serialized page blobs. +//! +//! While a row group is being written the [`ArrowWriter`] must buffer every +//! column's encoded pages, because Parquet requires each column chunk to be +//! contiguous on disk while record batches arrive with all columns interleaved. +//! By default that buffer lives on the heap, so the writer's peak memory grows +//! with the row group size. A [`PageStore`] lets the buffer live somewhere else +//! — a local temp file, object storage, etc. — bounding peak write memory +//! independently of the row group size. +//! +//! [`ArrowWriter`]: crate::arrow::arrow_writer::ArrowWriter + +use std::fmt::Debug; + +use bytes::Bytes; + +use crate::errors::{ParquetError, Result}; + +/// An opaque, store-allocated handle to a blob held by a [`PageStore`]. +/// +/// Handles are allocated by the store — densely and sequentially — and are only +/// meaningful to the store that produced them. The caller treats them as opaque +/// tokens and decides what they *mean* (ordering, which one is the dictionary +/// page, etc.). +/// +/// Letting the store allocate the handle (rather than the caller choosing keys) +/// lets each backend pick the cheapest possible locator with no hashing: an +/// in-memory backend uses the handle as an index into a `Vec`, a temp-file +/// backend as an index into a `Vec<(offset, len)>`, and so on. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct PageKey(u64); + +impl PageKey { + /// Create a handle wrapping `raw`. + /// + /// A [`PageStore`] implementation calls this to mint the handle it returns + /// from [`put`](PageStore::put). The value is opaque to the caller, so a + /// store is free to use a dense counter, a packed locator, or anything else + /// it can later resolve in [`take`](PageStore::take). + pub const fn new(raw: u64) -> Self { + Self(raw) + } + + /// The raw value passed to [`new`](Self::new). + pub const fn get(self) -> u64 { + self.0 + } +} + +/// A pluggable store for completed, serialized page blobs. +/// +/// The store is intentionally "dumb": it only maps an opaque [`PageKey`] to a +/// blob of bytes. It knows nothing about pages, dictionaries, ordering, or +/// offsets. The caller keeps the handles it gets back from [`put`](Self::put) +/// and decides what they mean. +/// +/// Each store instance is owned by a single column writer and mutated by one +/// thread at a time (both methods take `&mut self`), so it needs no internal +/// synchronization — hence only `Send`, not `Sync`. +/// +/// The default ([`InMemoryPageStore`]) keeps blobs on the heap. Configure a +/// different backend via +/// [`ArrowWriterOptions::with_page_store_factory`](crate::arrow::arrow_writer::ArrowWriterOptions::with_page_store_factory). +pub trait PageStore: Send { + /// Store `value`, returning a handle that can later be passed to + /// [`take`](Self::take). + fn put(&mut self, value: Bytes) -> Result<PageKey>; + + /// Take back the blob previously stored under `key`. Review Comment: Good points — splitting them: - **Guarding double-`take`**: the trait already documents that the caller will not request a key twice, so I’ve left the contract as-is rather than pay for `Vec<Option<Bytes>>` enforcement. Happy to harden it if you’d rather it be enforced than documented. - **Drop keys, return an iterator of pages**: I’d like to keep this open and tie it to the double-buffering discussion in the `GenericColumnWriter`/`PageStore` threads. The caller currently *needs* keyed (random-access) reads because on the Arrow path the dictionary page is produced **last but written first** — `append_to_row_group` deliberately reorders it, which a FIFO page iterator can’t express. Whether keys can go away depends on whether the store becomes page-aware, which is exactly the simplification you raised there. So I think the right place to settle the key-vs-iterator question is that thread. -- 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]
