-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add an example of embedding indexes inside a parquet file #16395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
793abb9
1f480ee
2a0ecac
4e61d0e
a8da658
c1ab4b9
baf0311
18e7028
66dc5e4
eb9b62e
310576e
fbeecfe
88fc6a6
32abcb9
284510c
0410bd8
56ad7f6
12ce9c2
0c093ac
a789084
9c75814
13c1706
06d6f08
6bd7d3e
23d7125
13b74ac
1b0501c
c344843
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,243 @@ | ||
// 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: embedding a "distinct values" index in a Parquet file's metadata | ||
//! | ||
//! 1. Read existing Parquet files | ||
//! 2. Compute distinct values for a target column using DataFusion | ||
//! 3. Serialize the distinct index to bytes and write to the new Parquet file | ||
//! with these encoded bytes appended as a custom metadata entry | ||
//! 4. Read each new parquet file, extract and deserialize the index from footer | ||
//! 5. Use the distinct index to prune files when querying | ||
use arrow::array::{ArrayRef, StringArray}; | ||
use arrow::record_batch::RecordBatch; | ||
use arrow_schema::{DataType, Field, Schema, SchemaRef}; | ||
use async_trait::async_trait; | ||
use base64::engine::general_purpose; | ||
use base64::Engine; | ||
use datafusion::catalog::{Session, TableProvider}; | ||
use datafusion::common::{HashMap, HashSet, Result}; | ||
use datafusion::datasource::listing::PartitionedFile; | ||
use datafusion::datasource::memory::DataSourceExec; | ||
use datafusion::datasource::physical_plan::{FileScanConfigBuilder, ParquetSource}; | ||
use datafusion::datasource::TableType; | ||
use datafusion::execution::object_store::ObjectStoreUrl; | ||
use datafusion::logical_expr::{Operator, TableProviderFilterPushDown}; | ||
use datafusion::parquet::arrow::ArrowWriter; | ||
use datafusion::parquet::file::metadata::KeyValue; | ||
use datafusion::parquet::file::properties::WriterProperties; | ||
use datafusion::parquet::file::reader::{FileReader, SerializedFileReader}; | ||
use datafusion::physical_plan::ExecutionPlan; | ||
use datafusion::prelude::*; | ||
use datafusion::scalar::ScalarValue; | ||
use std::fs::{create_dir_all, read_dir, File}; | ||
use std::path::{Path, PathBuf}; | ||
use std::sync::Arc; | ||
use tempfile::TempDir; | ||
|
||
/// Example creating parquet file that | ||
/// contains specialized indexes that | ||
/// are ignored by other readers | ||
/// | ||
/// ```text | ||
/// ┌──────────────────────┐ | ||
/// │┌───────────────────┐ │ | ||
/// ││ DataPage │ │ Standard Parquet | ||
/// │└───────────────────┘ │ Data / pages | ||
/// │┌───────────────────┐ │ | ||
/// ││ DataPage │ │ | ||
/// │└───────────────────┘ │ | ||
/// │ ... │ | ||
/// │ │ | ||
/// │┌───────────────────┐ │ | ||
/// ││ DataPage │ │ | ||
/// │└───────────────────┘ │ | ||
/// │┏━━━━━━━━━━━━━━━━━━━┓ │ | ||
/// │┃ ┃ │ key/value metadata | ||
/// │┃ Special Index ┃◀┼──── that points at the | ||
/// │┃ ┃ │ │ special index | ||
/// │┗━━━━━━━━━━━━━━━━━━━┛ │ | ||
/// │╔═══════════════════╗ │ │ | ||
/// │║ ║ │ | ||
/// │║ Parquet Footer ║ │ │ Footer includes | ||
/// │║ ║ ┼────── thrift-encoded | ||
/// │║ ║ │ ParquetMetadata | ||
/// │╚═══════════════════╝ │ | ||
/// └──────────────────────┘ | ||
/// | ||
/// Parquet File | ||
/// ``` | ||
/// DistinctIndexTable is a custom TableProvider that reads Parquet files | ||
#[derive(Debug)] | ||
struct DistinctIndexTable { | ||
schema: SchemaRef, | ||
index: HashMap<String, HashSet<String>>, | ||
dir: PathBuf, | ||
} | ||
|
||
impl DistinctIndexTable { | ||
/// Scan a directory, read each file's footer metadata into a map | ||
fn try_new(dir: impl Into<PathBuf>, schema: SchemaRef) -> Result<Self> { | ||
let dir = dir.into(); | ||
let mut index = HashMap::new(); | ||
for entry in read_dir(&dir)? { | ||
let p = entry?.path(); | ||
if p.extension().and_then(|s| s.to_str()) != Some("parquet") { | ||
continue; | ||
} | ||
let name = p.file_name().unwrap().to_string_lossy().into_owned(); | ||
let reader = SerializedFileReader::new(File::open(&p)?)?; | ||
if let Some(kv) = reader.metadata().file_metadata().key_value_metadata() { | ||
if let Some(e) = kv.iter().find(|kv| kv.key == "distinct_index_data") { | ||
let raw = general_purpose::STANDARD_NO_PAD | ||
.decode(e.value.as_deref().unwrap()) | ||
.unwrap(); | ||
let s = String::from_utf8(raw).unwrap(); | ||
let set = s.lines().map(|l| l.to_string()).collect(); | ||
println!("Inserting File: {name}, Distinct Values: {set:?}"); | ||
index.insert(name, set); | ||
} | ||
} | ||
} | ||
Ok(Self { schema, index, dir }) | ||
} | ||
} | ||
|
||
// Write a Parquet file and embed its distinct "category" values in footer metadata | ||
fn write_file_with_index(path: &Path, values: &[&str]) -> Result<()> { | ||
let field = Field::new("category", DataType::Utf8, false); | ||
let schema = Arc::new(Schema::new(vec![field.clone()])); | ||
let arr: ArrayRef = Arc::new(StringArray::from(values.to_vec())); | ||
let batch = RecordBatch::try_new(schema.clone(), vec![arr])?; | ||
|
||
// Compute distinct values, serialize & Base64‑encode | ||
let distinct: HashSet<_> = values.iter().copied().collect(); | ||
let serialized = distinct.iter().cloned().collect::<Vec<_>>().join("\n"); | ||
let b64 = general_purpose::STANDARD_NO_PAD.encode(serialized.as_bytes()); | ||
|
||
let props = WriterProperties::builder() | ||
.set_key_value_metadata(Some(vec![KeyValue::new( | ||
"distinct_index_data".into(), | ||
b64, | ||
)])) | ||
.build(); | ||
|
||
let file = File::create(path)?; | ||
let mut writer = ArrowWriter::try_new(file, schema, Some(props))?; | ||
writer.write(&batch)?; | ||
writer.finish()?; | ||
Ok(()) | ||
} | ||
|
||
/// Implement TableProvider for DistinctIndexTable, using the distinct index to prune files | ||
#[async_trait] | ||
impl TableProvider for DistinctIndexTable { | ||
fn as_any(&self) -> &dyn std::any::Any { | ||
self | ||
} | ||
fn schema(&self) -> SchemaRef { | ||
self.schema.clone() | ||
} | ||
fn table_type(&self) -> TableType { | ||
TableType::Base | ||
} | ||
|
||
/// Prune files before reading: only keep files whose distinct set contains the filter value | ||
async fn scan( | ||
&self, | ||
_ctx: &dyn Session, | ||
_proj: Option<&Vec<usize>>, | ||
filters: &[Expr], | ||
_limit: Option<usize>, | ||
) -> Result<Arc<dyn ExecutionPlan>> { | ||
// Look for a single `category = 'X'` filter | ||
let mut target: Option<String> = None; | ||
|
||
if filters.len() == 1 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can also potentially use However, doing this walk explicitly in the example might also be a good idea to show how it could be done generall |
||
if let Expr::BinaryExpr(expr) = &filters[0] { | ||
if expr.op == Operator::Eq { | ||
if let ( | ||
Expr::Column(c), | ||
Expr::Literal(ScalarValue::Utf8(Some(v)), _), | ||
) = (&*expr.left, &*expr.right) | ||
{ | ||
if c.name == "category" { | ||
println!("Filtering for category: {v}"); | ||
target = Some(v.clone()); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
// Determine which files to scan | ||
let keep: Vec<String> = self | ||
.index | ||
.iter() | ||
.filter(|(_f, set)| target.as_ref().is_none_or(|v| set.contains(v))) | ||
.map(|(f, _)| f.clone()) | ||
.collect(); | ||
|
||
println!("Pruned files: {:?}", keep.clone()); | ||
|
||
// Build ParquetSource for kept files | ||
let url = ObjectStoreUrl::parse("file://")?; | ||
let source = Arc::new(ParquetSource::default()); | ||
let mut builder = FileScanConfigBuilder::new(url, self.schema.clone(), source); | ||
for file in keep { | ||
let path = self.dir.join(&file); | ||
let len = std::fs::metadata(&path)?.len(); | ||
builder = builder.with_file(PartitionedFile::new( | ||
path.to_str().unwrap().to_string(), | ||
len, | ||
)); | ||
} | ||
Ok(DataSourceExec::from_data_source(builder.build())) | ||
} | ||
|
||
fn supports_filters_pushdown( | ||
&self, | ||
fs: &[&Expr], | ||
) -> Result<Vec<TableProviderFilterPushDown>> { | ||
// Mark as inexact since pruning is file‑granular | ||
Ok(vec![TableProviderFilterPushDown::Inexact; fs.len()]) | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
// 1. Create temp dir and write 3 Parquet files with different category sets | ||
let tmp = TempDir::new()?; | ||
let dir = tmp.path(); | ||
create_dir_all(dir)?; | ||
write_file_with_index(&dir.join("a.parquet"), &["foo", "bar", "foo"])?; | ||
write_file_with_index(&dir.join("b.parquet"), &["baz", "qux"])?; | ||
write_file_with_index(&dir.join("c.parquet"), &["foo", "quux", "quux"])?; | ||
|
||
// 2. Register our custom TableProvider | ||
let field = Field::new("category", DataType::Utf8, false); | ||
let schema_ref = Arc::new(Schema::new(vec![field])); | ||
let provider = Arc::new(DistinctIndexTable::try_new(dir, schema_ref.clone())?); | ||
let ctx = SessionContext::new(); | ||
ctx.register_table("t", provider)?; | ||
|
||
// 3. Run a query: only files containing 'foo' get scanned | ||
let df = ctx.sql("SELECT * FROM t WHERE category = 'foo'").await?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that is very cool |
||
df.show().await?; | ||
|
||
Ok(()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this writes the index into the footer itself (as an opaque string)
This has at least 2 downsides
Is it possible to write the binary data directly into the parquet file?
Specifically, so then the metadata looks something like
I am not sure how easy this would be to do with the current API
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good point @alamb! Thank you.
I will try to find a better solution, i agree the following downsides.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried today, but found it's hard for current API to support this, will try it again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try to using low level API, but it only works when we disable page index, if we setting page index, it will follow up the real row group data, and it conflicts with our embedding indexes.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code is here:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks super cool @zhuqi-lucas
I don't fully understand this concern -- I would probably have to play around with it some more
Are you willing to update this PR with this new example? I have some ideas on the various APIs we could use (like we could potentially encapsulate the index writing some more)
We could also then file a ticket upstream i arrow-rs with a description of what wasn't working with page indexes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @alamb , updated the code without page index using low level API, i will continue debugging the case that our self defined index with page index.