etseidl commented on code in PR #10842: URL: https://github.com/apache/arrow-rs/pull/10842#discussion_r3918289694
########## parquet/examples/custom_page_index.rs: ########## @@ -0,0 +1,307 @@ +// 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. + +//! Example of implementing a custom PageIndexProvider +//! +//! This example demonstrates how to create a custom page index provider that: +//! - Only stores page indexes for specified columns (selective storage) +//! - Uses nested HashMaps for efficient storage and lookup +//! - Implements all required PageIndexProvider trait methods +//! +//! This approach can significantly reduce memory usage when working with wide +//! tables where only a few columns need page-level statistics. + +use bytes::Bytes; +use parquet::DecodeResult; +use parquet::errors::{ParquetError, Result}; +use parquet::file::metadata::page_index::PageIndexProvider; +use parquet::file::metadata::{PageIndexPolicy, ParquetMetaData, ParquetMetaDataPushDecoder}; +use parquet::file::page_index::column_index::ColumnIndexMetaData; +use parquet::file::page_index::index_reader::{decode_column_index, decode_offset_index}; +use parquet::file::page_index::offset_index::OffsetIndexMetaData; +use std::collections::HashMap; +use std::collections::hash_map::Entry; +use std::fs::File; +use std::path::PathBuf; +use std::sync::Arc; +use tempfile::TempDir; + +////////////////////////////////////////////// +// helper functions + +fn print_page_index(metadata: &ParquetMetaData, row_group_idx: usize) -> Result<()> { + if let Some(page_index) = metadata.page_index() { + let num_columns = metadata.file_metadata().schema_descr().num_columns(); + + println!("\nIndexes for row group {row_group_idx}:"); + for col_idx in 0..num_columns { + println!( + " Column {col_idx}: has offset idx {}, has column idx {}", + page_index.offset_index(row_group_idx, col_idx).is_some(), + page_index.column_index(row_group_idx, col_idx).is_some() + ); + } + println!(); + } else { + println!("No page index in metadata"); + println!("Note: This example requires a file with page indexes."); + println!("Try using alltypes_tiny_pages.parquet or another file with page indexes."); + return Err(ParquetError::General("no page index".to_string())); + } + Ok(()) +} + +fn create_sample_file(temp_path: &PathBuf) -> Result<()> { + use arrow::array::{Int32Array, StringArray}; + use arrow::datatypes::{DataType, Field, Schema}; + use arrow::record_batch::RecordBatch; + use parquet::arrow::ArrowWriter; + use parquet::file::properties::{EnabledStatistics, WriterProperties}; + + println!("Creating sample file: {}", temp_path.display()); + + // Create a sample dataset with multiple columns + let schema = Arc::new(Schema::new(vec![ + Field::new("id", DataType::Int32, false), + Field::new("value", DataType::Int32, false), + Field::new("name", DataType::Utf8, false), + Field::new("score", DataType::Int32, false), + Field::new("category", DataType::Utf8, false), + Field::new("amount", DataType::Int32, false), + ])); + + // Create multiple row groups with multiple pages + let file = File::create(temp_path)?; + let props = WriterProperties::builder() + .set_statistics_enabled(EnabledStatistics::Page) + .set_data_page_size_limit(100) // Small pages for demonstration + .set_write_batch_size(10) + .build(); + + let mut writer = ArrowWriter::try_new(file, schema.clone(), Some(props))?; + + // Write several row groups + for row_group in 0..3 { + for batch_num in 0..5 { + let offset = (row_group * 50) + (batch_num * 10); + let batch = RecordBatch::try_new( + schema.clone(), + vec![ + Arc::new(Int32Array::from( + (offset..offset + 10).collect::<Vec<i32>>(), + )), + Arc::new(Int32Array::from( + (offset..offset + 10).map(|x| x * 2).collect::<Vec<i32>>(), + )), + Arc::new(StringArray::from( + (offset..offset + 10) + .map(|x| format!("name{x}")) + .collect::<Vec<String>>(), + )), + Arc::new(Int32Array::from( + (offset..offset + 10).map(|x| x * 3).collect::<Vec<i32>>(), + )), + Arc::new(StringArray::from( + (offset..offset + 10) + .map(|x| if x % 2 == 0 { "even" } else { "odd" }) + .collect::<Vec<&str>>(), + )), + Arc::new(Int32Array::from( + (offset..offset + 10).map(|x| x * 4).collect::<Vec<i32>>(), + )), + ], + )?; + writer.write(&batch)?; + } + writer.flush()?; + } + + writer.close()?; + Ok(()) +} + +////////////////////////////////////////////// +// our custom provider + +// A custom PageIndexProvider that only stores indexes for a subset of columns +// +// This provider contains the parsed footer metadata and the entire contents of a +// Parquet file. Indexes are lazily populated as they are requested. +#[derive(Debug, Clone)] +pub struct OnDemandPageIndexProvider { + metadata: ParquetMetaData, + file_bytes: Bytes, + column_indexes: Option<HashMap<usize, HashMap<usize, ColumnIndexMetaData>>>, Review Comment: done in https://github.com/apache/arrow-rs/pull/10842/commits/0caa8b4b768b991b2359e08429b8757c42542566 -- 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]
