-
Notifications
You must be signed in to change notification settings - Fork 796
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write Bloom filters between row groups instead of the end (#5860)
* Add example script to write Parquet files with a Bloom filter * Write Bloom filters between row groups instead of the end This allows Bloom filters to not be saved in memory, which can save significant space when writing long files * Add WriterProperties::bloom_filter_position * Mutate the right row group metadata When using BloomFilterPosition::AfterRowGroup this was only writing the Bloom Filter offset to a temporary clone of the metadata, causing the Bloom Filter to never be seen by readers * Add a test for Bloom Filters written at the end * Update async writer accordingly * Undo accidental commit * Clippy * Apply suggestions from code review Improve documentation Co-authored-by: Andrew Lamb <[email protected]> * Rewrite example with constants as parameters and fewer dependencies * rustfmt * Clippy * Fix MSRV --------- Co-authored-by: Andrew Lamb <[email protected]>
- Loading branch information
Showing
7 changed files
with
277 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// 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. | ||
|
||
use std::fs::File; | ||
use std::path::PathBuf; | ||
use std::sync::Arc; | ||
use std::time::{Duration, Instant}; | ||
|
||
use arrow::array::{StructArray, UInt64Builder}; | ||
use arrow::datatypes::DataType::UInt64; | ||
use arrow::datatypes::{Field, Schema}; | ||
use clap::{Parser, ValueEnum}; | ||
use parquet::arrow::ArrowWriter as ParquetWriter; | ||
use parquet::basic::Encoding; | ||
use parquet::errors::Result; | ||
use parquet::file::properties::{BloomFilterPosition, WriterProperties}; | ||
use sysinfo::{MemoryRefreshKind, Pid, ProcessRefreshKind, RefreshKind, System}; | ||
|
||
#[derive(ValueEnum, Clone)] | ||
enum BloomFilterPositionArg { | ||
End, | ||
AfterRowGroup, | ||
} | ||
|
||
#[derive(Parser)] | ||
#[command(version)] | ||
/// Writes sequences of integers, with a Bloom Filter, while logging timing and memory usage. | ||
struct Args { | ||
#[arg(long, default_value_t = 1000)] | ||
/// Number of batches to write | ||
iterations: u64, | ||
|
||
#[arg(long, default_value_t = 1000000)] | ||
/// Number of rows in each batch | ||
batch: u64, | ||
|
||
#[arg(long, value_enum, default_value_t=BloomFilterPositionArg::AfterRowGroup)] | ||
/// Where to write Bloom Filters | ||
bloom_filter_position: BloomFilterPositionArg, | ||
|
||
/// Path to the file to write | ||
path: PathBuf, | ||
} | ||
|
||
fn now() -> String { | ||
chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string() | ||
} | ||
|
||
fn mem(system: &mut System) -> String { | ||
let pid = Pid::from(std::process::id() as usize); | ||
system.refresh_process_specifics(pid, ProcessRefreshKind::new().with_memory()); | ||
system | ||
.process(pid) | ||
.map(|proc| format!("{}MB", proc.memory() / 1_000_000)) | ||
.unwrap_or("N/A".to_string()) | ||
} | ||
|
||
fn main() -> Result<()> { | ||
let args = Args::parse(); | ||
|
||
let bloom_filter_position = match args.bloom_filter_position { | ||
BloomFilterPositionArg::End => BloomFilterPosition::End, | ||
BloomFilterPositionArg::AfterRowGroup => BloomFilterPosition::AfterRowGroup, | ||
}; | ||
|
||
let properties = WriterProperties::builder() | ||
.set_column_bloom_filter_enabled("id".into(), true) | ||
.set_column_encoding("id".into(), Encoding::DELTA_BINARY_PACKED) | ||
.set_bloom_filter_position(bloom_filter_position) | ||
.build(); | ||
let schema = Arc::new(Schema::new(vec![Field::new("id", UInt64, false)])); | ||
// Create parquet file that will be read. | ||
let file = File::create(args.path).unwrap(); | ||
let mut writer = ParquetWriter::try_new(file, schema.clone(), Some(properties))?; | ||
|
||
let mut system = | ||
System::new_with_specifics(RefreshKind::new().with_memory(MemoryRefreshKind::everything())); | ||
eprintln!( | ||
"{} Writing {} batches of {} rows. RSS = {}", | ||
now(), | ||
args.iterations, | ||
args.batch, | ||
mem(&mut system) | ||
); | ||
|
||
let mut array_builder = UInt64Builder::new(); | ||
let mut last_log = Instant::now(); | ||
for i in 0..args.iterations { | ||
if Instant::now() - last_log > Duration::new(10, 0) { | ||
last_log = Instant::now(); | ||
eprintln!( | ||
"{} Iteration {}/{}. RSS = {}", | ||
now(), | ||
i + 1, | ||
args.iterations, | ||
mem(&mut system) | ||
); | ||
} | ||
for j in 0..args.batch { | ||
array_builder.append_value(i + j); | ||
} | ||
writer.write( | ||
&StructArray::new( | ||
schema.fields().clone(), | ||
vec![Arc::new(array_builder.finish())], | ||
None, | ||
) | ||
.into(), | ||
)?; | ||
} | ||
writer.flush()?; | ||
writer.close()?; | ||
|
||
eprintln!("{} Done. RSS = {}", now(), mem(&mut system)); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.