parquet_show_bloom_filter/
parquet-show-bloom-filter.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Binary file to read bloom filter data from a Parquet file.
19//!
20//! # Install
21//!
22//! `parquet-show-bloom-filter` can be installed using `cargo`:
23//! ```
24//! cargo install parquet --features=cli
25//! ```
26//! After this `parquet-show-bloom-filter` should be available:
27//! ```
28//! parquet-show-bloom-filter XYZ.parquet id a
29//! ```
30//!
31//! The binary can also be built from the source code and run as follows:
32//! ```
33//! cargo run --features=cli --bin parquet-show-bloom-filter -- --file-name XYZ.parquet --column id --values a
34//! ```
35
36use clap::Parser;
37use parquet::file::{
38    properties::ReaderProperties,
39    reader::{FileReader, SerializedFileReader},
40    serialized_reader::ReadOptionsBuilder,
41};
42use std::{fs::File, path::Path};
43
44#[derive(Debug, Parser)]
45#[clap(author, version, about("Binary file to read bloom filter data from a Parquet file"), long_about = None)]
46struct Args {
47    #[clap(help("Path to the parquet file"))]
48    file_name: String,
49    #[clap(help("Check the bloom filter indexes for the given column"))]
50    column: String,
51    #[clap(
52        help(
53            "Check if the given values match bloom filter, the values will be evaluated as strings"
54        ),
55        required = true
56    )]
57    values: Vec<String>,
58}
59
60fn main() {
61    let args = Args::parse();
62    let file_name = args.file_name;
63    let path = Path::new(&file_name);
64    let file = File::open(path).expect("Unable to open file");
65
66    let file_reader = SerializedFileReader::new_with_options(
67        file,
68        ReadOptionsBuilder::new()
69            .with_reader_properties(
70                ReaderProperties::builder()
71                    .set_read_bloom_filter(true)
72                    .build(),
73            )
74            .build(),
75    )
76    .expect("Unable to open file as Parquet");
77    let metadata = file_reader.metadata();
78    for (ri, row_group) in metadata.row_groups().iter().enumerate() {
79        println!("Row group #{ri}");
80        println!("{}", "=".repeat(80));
81        if let Some((column_index, _)) = row_group
82            .columns()
83            .iter()
84            .enumerate()
85            .find(|(_, column)| column.column_path().string() == args.column)
86        {
87            let row_group_reader = file_reader
88                .get_row_group(ri)
89                .expect("Unable to read row group");
90            if let Some(sbbf) = row_group_reader.get_column_bloom_filter(column_index) {
91                args.values.iter().for_each(|value| {
92                    println!(
93                        "Value {} is {} in bloom filter",
94                        value,
95                        if sbbf.check(&value.as_str()) {
96                            "present"
97                        } else {
98                            "absent"
99                        }
100                    )
101                });
102            } else {
103                println!("No bloom filter found for column {}", args.column);
104            }
105        } else {
106            println!(
107                "No column named {} found, candidate columns are: {}",
108                args.column,
109                row_group
110                    .columns()
111                    .iter()
112                    .map(|c| c.column_path().string())
113                    .collect::<Vec<_>>()
114                    .join(", ")
115            );
116        }
117    }
118}