-
Notifications
You must be signed in to change notification settings - Fork 2k
correct parquet leaf index mapping when schema contains struct cols #20698
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
adriangb
merged 2 commits into
apache:main
from
pydantic:fix-parquet-leaf-index-struct-cols
Mar 5, 2026
+140
−35
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -242,10 +242,10 @@ impl FilterCandidateBuilder { | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| let root_indices: Vec<_> = | ||||||||||||||||||||||||
| required_columns.required_columns.into_iter().collect(); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| let leaf_indices = leaf_indices_for_roots( | ||||||||||||||||||||||||
| &root_indices, | ||||||||||||||||||||||||
| metadata.file_metadata().schema_descr(), | ||||||||||||||||||||||||
| required_columns.nested, | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| let projected_schema = Arc::new(self.file_schema.project(&root_indices)?); | ||||||||||||||||||||||||
|
|
@@ -277,8 +277,6 @@ struct PushdownChecker<'schema> { | |||||||||||||||||||||||
| projected_columns: bool, | ||||||||||||||||||||||||
| /// Indices into the file schema of columns required to evaluate the expression. | ||||||||||||||||||||||||
| required_columns: Vec<usize>, | ||||||||||||||||||||||||
| /// Tracks the nested column behavior found during traversal. | ||||||||||||||||||||||||
| nested_behavior: NestedColumnSupport, | ||||||||||||||||||||||||
| /// Whether nested list columns are supported by the predicate semantics. | ||||||||||||||||||||||||
| allow_list_columns: bool, | ||||||||||||||||||||||||
| /// The Arrow schema of the parquet file. | ||||||||||||||||||||||||
|
|
@@ -291,7 +289,6 @@ impl<'schema> PushdownChecker<'schema> { | |||||||||||||||||||||||
| non_primitive_columns: false, | ||||||||||||||||||||||||
| projected_columns: false, | ||||||||||||||||||||||||
| required_columns: Vec::new(), | ||||||||||||||||||||||||
| nested_behavior: NestedColumnSupport::PrimitiveOnly, | ||||||||||||||||||||||||
| allow_list_columns, | ||||||||||||||||||||||||
| file_schema, | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
@@ -324,16 +321,11 @@ impl<'schema> PushdownChecker<'schema> { | |||||||||||||||||||||||
| /// `None` if the type is supported and pushdown can continue. | ||||||||||||||||||||||||
| fn handle_nested_type(&mut self, data_type: &DataType) -> Option<TreeNodeRecursion> { | ||||||||||||||||||||||||
| if self.is_nested_type_supported(data_type) { | ||||||||||||||||||||||||
| // Update to ListsSupported if we haven't encountered unsupported types yet | ||||||||||||||||||||||||
| if self.nested_behavior == NestedColumnSupport::PrimitiveOnly { | ||||||||||||||||||||||||
| self.nested_behavior = NestedColumnSupport::ListsSupported; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| None | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| // Block pushdown for unsupported nested types: | ||||||||||||||||||||||||
| // - Structs (regardless of predicate support) | ||||||||||||||||||||||||
| // - Lists without supported predicates | ||||||||||||||||||||||||
| self.nested_behavior = NestedColumnSupport::Unsupported; | ||||||||||||||||||||||||
| self.non_primitive_columns = true; | ||||||||||||||||||||||||
| Some(TreeNodeRecursion::Jump) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
@@ -368,7 +360,6 @@ impl<'schema> PushdownChecker<'schema> { | |||||||||||||||||||||||
| self.required_columns.dedup(); | ||||||||||||||||||||||||
| PushdownColumns { | ||||||||||||||||||||||||
| required_columns: self.required_columns, | ||||||||||||||||||||||||
| nested: self.nested_behavior, | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
@@ -391,29 +382,13 @@ impl TreeNodeVisitor<'_> for PushdownChecker<'_> { | |||||||||||||||||||||||
| /// | ||||||||||||||||||||||||
| /// This enum makes explicit the different states a predicate can be in | ||||||||||||||||||||||||
| /// with respect to nested column handling during Parquet decoding. | ||||||||||||||||||||||||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||||||||||||||||||||||||
| enum NestedColumnSupport { | ||||||||||||||||||||||||
| /// Expression references only primitive (non-nested) columns. | ||||||||||||||||||||||||
| /// These can always be pushed down to the Parquet decoder. | ||||||||||||||||||||||||
| PrimitiveOnly, | ||||||||||||||||||||||||
| /// Expression references list columns with supported predicates | ||||||||||||||||||||||||
| /// (e.g., array_has, array_has_all, IS NULL). | ||||||||||||||||||||||||
| /// These can be pushed down to the Parquet decoder. | ||||||||||||||||||||||||
| ListsSupported, | ||||||||||||||||||||||||
| /// Expression references unsupported nested types (e.g., structs) | ||||||||||||||||||||||||
| /// or list columns without supported predicates. | ||||||||||||||||||||||||
| /// These cannot be pushed down and must be evaluated after decoding. | ||||||||||||||||||||||||
| Unsupported, | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /// Result of checking which columns are required for filter pushdown. | ||||||||||||||||||||||||
| #[derive(Debug)] | ||||||||||||||||||||||||
| struct PushdownColumns { | ||||||||||||||||||||||||
| /// Sorted, unique column indices into the file schema required to evaluate | ||||||||||||||||||||||||
| /// the filter expression. Must be in ascending order for correct schema | ||||||||||||||||||||||||
| /// projection matching. | ||||||||||||||||||||||||
| required_columns: Vec<usize>, | ||||||||||||||||||||||||
| nested: NestedColumnSupport, | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /// Checks if a given expression can be pushed down to the parquet decoder. | ||||||||||||||||||||||||
|
|
@@ -437,15 +412,13 @@ fn pushdown_columns( | |||||||||||||||||||||||
| fn leaf_indices_for_roots( | ||||||||||||||||||||||||
| root_indices: &[usize], | ||||||||||||||||||||||||
| schema_descr: &SchemaDescriptor, | ||||||||||||||||||||||||
| nested: NestedColumnSupport, | ||||||||||||||||||||||||
| ) -> Vec<usize> { | ||||||||||||||||||||||||
| // For primitive-only columns, root indices ARE the leaf indices | ||||||||||||||||||||||||
| if nested == NestedColumnSupport::PrimitiveOnly { | ||||||||||||||||||||||||
| return root_indices.to_vec(); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // For List columns, expand to the single leaf column (item field) | ||||||||||||||||||||||||
| // For Struct columns (unsupported), this would expand to multiple leaves | ||||||||||||||||||||||||
| // Always map root (Arrow) indices to Parquet leaf indices via the schema | ||||||||||||||||||||||||
| // descriptor. Arrow root indices only equal Parquet leaf indices when the | ||||||||||||||||||||||||
| // schema has no group columns (Struct, Map, etc.); when group columns | ||||||||||||||||||||||||
| // exist, their children become separate leaves and shift all subsequent | ||||||||||||||||||||||||
| // leaf indices. | ||||||||||||||||||||||||
|
Comment on lines
+416
to
+420
Contributor
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.
Suggested change
|
||||||||||||||||||||||||
| // Struct columns are unsupported. | ||||||||||||||||||||||||
| let root_set: BTreeSet<_> = root_indices.iter().copied().collect(); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| (0..schema_descr.num_columns()) | ||||||||||||||||||||||||
|
|
@@ -1088,6 +1061,91 @@ mod test { | |||||||||||||||||||||||
| .expect("parsing schema") | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /// Regression test: when a schema has Struct columns, Arrow field indices diverge | ||||||||||||||||||||||||
| /// from Parquet leaf indices (Struct children become separate leaves). The | ||||||||||||||||||||||||
| /// `PrimitiveOnly` fast-path in `leaf_indices_for_roots` assumes they are equal, | ||||||||||||||||||||||||
| /// so a filter on a primitive column *after* a Struct gets the wrong leaf index. | ||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||
| /// Schema: | ||||||||||||||||||||||||
| /// Arrow indices: col_a=0 struct_col=1 col_b=2 | ||||||||||||||||||||||||
| /// Parquet leaves: col_a=0 struct_col.x=1 struct_col.y=2 col_b=3 | ||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||
| /// A filter on col_b should project Parquet leaf 3, but the bug causes it to | ||||||||||||||||||||||||
| /// project leaf 2 (struct_col.y). | ||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||
| fn test_filter_pushdown_leaf_index_with_struct_in_schema() { | ||||||||||||||||||||||||
| use arrow::array::{Int32Array, StringArray, StructArray}; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| let schema = Arc::new(Schema::new(vec![ | ||||||||||||||||||||||||
| Field::new("col_a", DataType::Int32, false), | ||||||||||||||||||||||||
| Field::new( | ||||||||||||||||||||||||
| "struct_col", | ||||||||||||||||||||||||
| DataType::Struct( | ||||||||||||||||||||||||
| vec![ | ||||||||||||||||||||||||
| Arc::new(Field::new("x", DataType::Int32, true)), | ||||||||||||||||||||||||
| Arc::new(Field::new("y", DataType::Int32, true)), | ||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||
| .into(), | ||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||
| true, | ||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||
| Field::new("col_b", DataType::Utf8, false), | ||||||||||||||||||||||||
| ])); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| let col_a = Arc::new(Int32Array::from(vec![1, 2, 3])); | ||||||||||||||||||||||||
| let struct_col = Arc::new(StructArray::from(vec![ | ||||||||||||||||||||||||
| ( | ||||||||||||||||||||||||
| Arc::new(Field::new("x", DataType::Int32, true)), | ||||||||||||||||||||||||
| Arc::new(Int32Array::from(vec![10, 20, 30])) as _, | ||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||
| ( | ||||||||||||||||||||||||
| Arc::new(Field::new("y", DataType::Int32, true)), | ||||||||||||||||||||||||
| Arc::new(Int32Array::from(vec![100, 200, 300])) as _, | ||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||
| ])); | ||||||||||||||||||||||||
| let col_b = Arc::new(StringArray::from(vec!["aaa", "target", "zzz"])); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| let batch = | ||||||||||||||||||||||||
| RecordBatch::try_new(Arc::clone(&schema), vec![col_a, struct_col, col_b]) | ||||||||||||||||||||||||
| .unwrap(); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| let file = NamedTempFile::new().expect("temp file"); | ||||||||||||||||||||||||
| let mut writer = | ||||||||||||||||||||||||
| ArrowWriter::try_new(file.reopen().unwrap(), Arc::clone(&schema), None) | ||||||||||||||||||||||||
| .expect("writer"); | ||||||||||||||||||||||||
| writer.write(&batch).expect("write batch"); | ||||||||||||||||||||||||
| writer.close().expect("close writer"); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| let reader_file = file.reopen().expect("reopen file"); | ||||||||||||||||||||||||
| let builder = ParquetRecordBatchReaderBuilder::try_new(reader_file) | ||||||||||||||||||||||||
| .expect("reader builder"); | ||||||||||||||||||||||||
| let metadata = builder.metadata().clone(); | ||||||||||||||||||||||||
| let file_schema = builder.schema().clone(); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // sanity check: 4 Parquet leaves, 3 Arrow fields | ||||||||||||||||||||||||
| assert_eq!(metadata.file_metadata().schema_descr().num_columns(), 4); | ||||||||||||||||||||||||
| assert_eq!(file_schema.fields().len(), 3); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // build a filter candidate for `col_b = 'target'` through the public API | ||||||||||||||||||||||||
| let expr = col("col_b").eq(Expr::Literal( | ||||||||||||||||||||||||
| ScalarValue::Utf8(Some("target".to_string())), | ||||||||||||||||||||||||
| None, | ||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||
| let expr = logical2physical(&expr, &file_schema); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| let candidate = FilterCandidateBuilder::new(expr, file_schema) | ||||||||||||||||||||||||
| .build(&metadata) | ||||||||||||||||||||||||
| .expect("building candidate") | ||||||||||||||||||||||||
| .expect("filter on primitive col_b should be pushable"); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // col_b is Parquet leaf 3 (shifted by struct_col's two children). | ||||||||||||||||||||||||
| assert_eq!( | ||||||||||||||||||||||||
| candidate.projection.leaf_indices, | ||||||||||||||||||||||||
| vec![3], | ||||||||||||||||||||||||
| "leaf_indices should be [3] for col_b" | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /// Sanity check that the given expression could be evaluated against the given schema without any errors. | ||||||||||||||||||||||||
| /// This will fail if the expression references columns that are not in the schema or if the types of the columns are incompatible, etc. | ||||||||||||||||||||||||
| fn check_expression_can_evaluate_against_schema( | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
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
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.
Just because a filter only references primitive columns doesn't mean Arrow indices equal Parquet leaf indices.
Struct columns elsewhere in the schema still shift the leaf numbering. The enum encoded the wrong signal (and was only used here), so I removed it and always do the proper mapping