-
Notifications
You must be signed in to change notification settings - Fork 1.9k
adapt filter expressions to file schema during parquet scan #16461
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
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
f81559b
wip
adriangb 30ebecc
adapt filter expressions to file schema during parquet scan
adriangb c3b825b
handle partition values
adriangb 636fc7f
add more comments
adriangb 4cee1e9
add a new test
adriangb 35b306f
better test?
adriangb b38b9ca
fmt
adriangb 06813c2
remove schema adapters
adriangb ce64271
fmt
adriangb 403a98b
address PR feedback
adriangb 15b030f
cleanup
adriangb 8f5f150
remove unecessary reassign
adriangb b1d3b0c
fmt
adriangb a0dcae9
better comments
adriangb d1e6800
Revert "remove unecessary reassign"
adriangb 7f0de73
handle indexes internally
adriangb f141fa9
reafactor
adriangb c97fa32
fix
adriangb 4dcb3d2
fix
adriangb ce47bd8
add comment
adriangb dc1005d
clippy
adriangb 866c444
add example for RecordBatch adaptation
adriangb 9c71b6d
fmt
adriangb 20aa835
comments
adriangb 5e09d68
add equivalent test
adriangb fa1f621
fmt
adriangb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -37,6 +37,7 @@ use datafusion_common::pruning::{ | |
| }; | ||
| use datafusion_common::{exec_err, Result}; | ||
| use datafusion_datasource::PartitionedFile; | ||
| use datafusion_physical_expr::PhysicalExprSchemaRewriter; | ||
| use datafusion_physical_expr_common::physical_expr::PhysicalExpr; | ||
| use datafusion_physical_optimizer::pruning::PruningPredicate; | ||
| use datafusion_physical_plan::metrics::{Count, ExecutionPlanMetricsSet, MetricBuilder}; | ||
|
|
@@ -117,7 +118,6 @@ impl FileOpener for ParquetOpener { | |
|
|
||
| let projected_schema = | ||
| SchemaRef::from(self.logical_file_schema.project(&self.projection)?); | ||
| let schema_adapter_factory = Arc::clone(&self.schema_adapter_factory); | ||
| let schema_adapter = self | ||
| .schema_adapter_factory | ||
| .create(projected_schema, Arc::clone(&self.logical_file_schema)); | ||
|
|
@@ -159,7 +159,7 @@ impl FileOpener for ParquetOpener { | |
| if let Some(pruning_predicate) = pruning_predicate { | ||
| // The partition column schema is the schema of the table - the schema of the file | ||
| let mut pruning = Box::new(PartitionPruningStatistics::try_new( | ||
| vec![file.partition_values], | ||
| vec![file.partition_values.clone()], | ||
| partition_fields.clone(), | ||
| )?) | ||
| as Box<dyn PruningStatistics>; | ||
|
|
@@ -248,10 +248,27 @@ impl FileOpener for ParquetOpener { | |
| } | ||
| } | ||
|
|
||
| // Adapt the predicate to the physical file schema. | ||
| // This evaluates missing columns and inserts any necessary casts. | ||
| let predicate = predicate | ||
| .map(|p| { | ||
| PhysicalExprSchemaRewriter::new( | ||
| &physical_file_schema, | ||
| &logical_file_schema, | ||
| ) | ||
| .with_partition_columns( | ||
| partition_fields.to_vec(), | ||
| file.partition_values, | ||
| ) | ||
| .rewrite(p) | ||
| .map_err(ArrowError::from) | ||
| }) | ||
| .transpose()?; | ||
|
|
||
| // Build predicates for this specific file | ||
| let (pruning_predicate, page_pruning_predicate) = build_pruning_predicates( | ||
| predicate.as_ref(), | ||
| &logical_file_schema, | ||
| &physical_file_schema, | ||
| &predicate_creation_errors, | ||
| ); | ||
|
|
||
|
|
@@ -288,11 +305,9 @@ impl FileOpener for ParquetOpener { | |
| let row_filter = row_filter::build_row_filter( | ||
| &predicate, | ||
| &physical_file_schema, | ||
| &logical_file_schema, | ||
| builder.metadata(), | ||
| reorder_predicates, | ||
| &file_metrics, | ||
| &schema_adapter_factory, | ||
| ); | ||
|
|
||
| match row_filter { | ||
|
|
@@ -879,4 +894,115 @@ mod test { | |
| assert_eq!(num_batches, 0); | ||
| assert_eq!(num_rows, 0); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_prune_on_partition_value_and_data_value() { | ||
| let store = Arc::new(InMemory::new()) as Arc<dyn ObjectStore>; | ||
|
|
||
| // Note: number 3 is missing! | ||
| let batch = record_batch!(("a", Int32, vec![Some(1), Some(2), Some(4)])).unwrap(); | ||
| let data_size = | ||
| write_parquet(Arc::clone(&store), "part=1/file.parquet", batch.clone()).await; | ||
|
|
||
| let file_schema = batch.schema(); | ||
| let mut file = PartitionedFile::new( | ||
| "part=1/file.parquet".to_string(), | ||
| u64::try_from(data_size).unwrap(), | ||
| ); | ||
| file.partition_values = vec![ScalarValue::Int32(Some(1))]; | ||
|
|
||
| let table_schema = Arc::new(Schema::new(vec![ | ||
| Field::new("part", DataType::Int32, false), | ||
| Field::new("a", DataType::Int32, false), | ||
| ])); | ||
|
|
||
| let make_opener = |predicate| { | ||
| ParquetOpener { | ||
| partition_index: 0, | ||
| projection: Arc::new([0]), | ||
| batch_size: 1024, | ||
| limit: None, | ||
| predicate: Some(predicate), | ||
| logical_file_schema: file_schema.clone(), | ||
| metadata_size_hint: None, | ||
| metrics: ExecutionPlanMetricsSet::new(), | ||
| parquet_file_reader_factory: Arc::new( | ||
| DefaultParquetFileReaderFactory::new(Arc::clone(&store)), | ||
| ), | ||
| partition_fields: vec![Arc::new(Field::new( | ||
| "part", | ||
| DataType::Int32, | ||
| false, | ||
| ))], | ||
| pushdown_filters: true, // note that this is true! | ||
| reorder_filters: true, | ||
| enable_page_index: false, | ||
| enable_bloom_filter: false, | ||
| schema_adapter_factory: Arc::new(DefaultSchemaAdapterFactory), | ||
| enable_row_group_stats_pruning: false, // note that this is false! | ||
| coerce_int96: None, | ||
| } | ||
| }; | ||
|
|
||
| let make_meta = || FileMeta { | ||
| object_meta: ObjectMeta { | ||
| location: Path::from("part=1/file.parquet"), | ||
| last_modified: Utc::now(), | ||
| size: u64::try_from(data_size).unwrap(), | ||
| e_tag: None, | ||
| version: None, | ||
| }, | ||
| range: None, | ||
| extensions: None, | ||
| metadata_size_hint: None, | ||
| }; | ||
|
|
||
| // Filter should match the partition value and data value | ||
| let expr = col("part").eq(lit(1)).or(col("a").eq(lit(1))); | ||
| let predicate = logical2physical(&expr, &table_schema); | ||
| let opener = make_opener(predicate); | ||
| let stream = opener | ||
| .open(make_meta(), file.clone()) | ||
| .unwrap() | ||
| .await | ||
| .unwrap(); | ||
| let (num_batches, num_rows) = count_batches_and_rows(stream).await; | ||
| assert_eq!(num_batches, 1); | ||
| assert_eq!(num_rows, 3); | ||
|
|
||
| // Filter should match the partition value but not the data value | ||
| let expr = col("part").eq(lit(1)).or(col("a").eq(lit(3))); | ||
| let predicate = logical2physical(&expr, &table_schema); | ||
| let opener = make_opener(predicate); | ||
| let stream = opener | ||
| .open(make_meta(), file.clone()) | ||
| .unwrap() | ||
| .await | ||
| .unwrap(); | ||
| let (num_batches, num_rows) = count_batches_and_rows(stream).await; | ||
| assert_eq!(num_batches, 1); | ||
| assert_eq!(num_rows, 3); | ||
|
|
||
| // Filter should not match the partition value but match the data value | ||
| let expr = col("part").eq(lit(2)).or(col("a").eq(lit(1))); | ||
| let predicate = logical2physical(&expr, &table_schema); | ||
| let opener = make_opener(predicate); | ||
| let stream = opener | ||
| .open(make_meta(), file.clone()) | ||
| .unwrap() | ||
| .await | ||
| .unwrap(); | ||
| let (num_batches, num_rows) = count_batches_and_rows(stream).await; | ||
| assert_eq!(num_batches, 1); | ||
| assert_eq!(num_rows, 1); | ||
|
Contributor
Author
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. This assertion fails on |
||
|
|
||
| // Filter should not match the partition value or the data value | ||
| let expr = col("part").eq(lit(2)).or(col("a").eq(lit(3))); | ||
| let predicate = logical2physical(&expr, &table_schema); | ||
| let opener = make_opener(predicate); | ||
| let stream = opener.open(make_meta(), file).unwrap().await.unwrap(); | ||
| let (num_batches, num_rows) = count_batches_and_rows(stream).await; | ||
| assert_eq!(num_batches, 0); | ||
| assert_eq!(num_rows, 0); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
😍