-
Notifications
You must be signed in to change notification settings - Fork 475
feat: add parquet nested leaf projection #7900
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
fengys1996
merged 12 commits into
GreptimeTeam:main
from
fengys1996:feat/arquet-nested-leaf-projection
Apr 10, 2026
+324
−5
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
10851f3
feat: add parquet nested leaf projection
fengys1996 1785e95
rename ParquetProjection related struct
fengys1996 f6b5f16
add some apis
fengys1996 fd974af
extract common build schema function for test
fengys1996 956a671
remove unsed method
fengys1996 1dd2e19
keep only deduped parquet root projection constructor
fengys1996 224d8f1
add more unit tests
fengys1996 f9346a6
fix: typo
fengys1996 88902fa
fix: cr
fengys1996 551ddec
fast-path parquet root projection without nested fields
fengys1996 0b3a11d
extract a build_projection_mask method
fengys1996 c735f7c
fix: cargo clippy
fengys1996 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
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 |
|---|---|---|
| @@ -0,0 +1,285 @@ | ||
| // Copyright 2023 Greptime Team | ||
| // | ||
| // Licensed 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::collections::HashMap; | ||
|
|
||
| use parquet::schema::types::SchemaDescriptor; | ||
|
|
||
| /// A nested field access path inside one parquet root column. | ||
| pub type ParquetNestedPath = Vec<String>; | ||
|
|
||
| /// The parquet columns to read. | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub struct ParquetReadColumns { | ||
| cols: Vec<ParquetReadColumn>, | ||
| } | ||
|
|
||
| impl ParquetReadColumns { | ||
| /// Builds root-column projections from root indices that are already | ||
| /// deduplicated. | ||
| /// | ||
| /// Note: this constructor does not check for duplicates. | ||
| pub fn from_deduped_root_indices(root_indices: impl IntoIterator<Item = usize>) -> Self { | ||
| let cols = root_indices | ||
| .into_iter() | ||
| .map(ParquetReadColumn::new) | ||
| .collect(); | ||
| Self { cols } | ||
| } | ||
|
|
||
| pub fn columns(&self) -> &[ParquetReadColumn] { | ||
| &self.cols | ||
| } | ||
| } | ||
|
|
||
| /// Read requirement for a single parquet root column. | ||
| /// | ||
| /// `root_index` identifies the root column in the parquet schema. | ||
| /// | ||
| /// If `nested_paths` is empty, the whole root column is read. Otherwise, only | ||
| /// leaves under the specified nested paths are read. | ||
| /// | ||
| /// To construct a [`ParquetReadColumn`]: | ||
| /// - `ParquetReadColumn::new(0)` reads the whole root column at index `0`. | ||
| /// - `ParquetReadColumn::new(0).with_nested_paths(vec![vec!["j".into(), "b".into()]])` | ||
| /// reads only leaves under `j.b`. | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub struct ParquetReadColumn { | ||
| /// Root field index in the parquet schema. | ||
| root_index: usize, | ||
| /// Nested paths to read under this root column. | ||
| /// | ||
| /// Each path includes the root column itself. For example, for a root | ||
| /// column `j`, path `["j", "a", "b"]` refers to `j.a.b`. | ||
| /// | ||
| /// If empty, the whole root column is read. | ||
| nested_paths: Vec<ParquetNestedPath>, | ||
MichaelScofield marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| impl ParquetReadColumn { | ||
| pub fn new(root_index: usize) -> Self { | ||
| Self { | ||
| root_index, | ||
| nested_paths: vec![], | ||
| } | ||
| } | ||
|
|
||
| pub fn with_nested_paths(self, nested_paths: Vec<ParquetNestedPath>) -> Self { | ||
| Self { | ||
| nested_paths, | ||
| ..self | ||
| } | ||
| } | ||
|
|
||
| pub fn root_index(&self) -> usize { | ||
| self.root_index | ||
| } | ||
|
|
||
| pub fn nested_paths(&self) -> &[ParquetNestedPath] { | ||
| &self.nested_paths | ||
| } | ||
| } | ||
|
|
||
| /// Builds parquet leaf-column indices from parquet read columns. | ||
| pub fn build_parquet_leaves_indices( | ||
| parquet_schema_desc: &SchemaDescriptor, | ||
| projection: &ParquetReadColumns, | ||
| ) -> Vec<usize> { | ||
| let mut map = HashMap::new(); | ||
fengys1996 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for col in &projection.cols { | ||
| map.insert(col.root_index, &col.nested_paths); | ||
fengys1996 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
evenyag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| let mut leaf_indices = Vec::new(); | ||
| for (leaf_idx, leaf_col) in parquet_schema_desc.columns().iter().enumerate() { | ||
| let root_idx = parquet_schema_desc.get_column_root_idx(leaf_idx); | ||
| let Some(nested_paths) = map.get(&root_idx) else { | ||
| continue; | ||
| }; | ||
fengys1996 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if nested_paths.is_empty() { | ||
| leaf_indices.push(leaf_idx); | ||
| continue; | ||
| } | ||
|
|
||
| let leaf_path = leaf_col.path().parts(); | ||
| if nested_paths | ||
| .iter() | ||
| .any(|nested_path| leaf_path.starts_with(nested_path)) | ||
| { | ||
| leaf_indices.push(leaf_idx); | ||
| } | ||
| } | ||
fengys1996 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| leaf_indices | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::sync::Arc; | ||
|
|
||
| use parquet::basic::Repetition; | ||
| use parquet::schema::types::Type; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_reads_whole_root() { | ||
| let parquet_schema_desc = build_test_nested_parquet_schema(); | ||
|
|
||
| let projection = ParquetReadColumns { | ||
| cols: vec![ParquetReadColumn { | ||
| root_index: 0, | ||
| nested_paths: vec![], | ||
| }], | ||
| }; | ||
|
|
||
| assert_eq!( | ||
| vec![0, 1, 2], | ||
| build_parquet_leaves_indices(&parquet_schema_desc, &projection) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_filters_nested_paths() { | ||
| let parquet_schema_desc = build_test_nested_parquet_schema(); | ||
|
|
||
| let projection = ParquetReadColumns { | ||
| cols: vec![ | ||
| ParquetReadColumn { | ||
| root_index: 0, | ||
| nested_paths: vec![vec!["j".to_string(), "b".to_string()]], | ||
| }, | ||
| ParquetReadColumn { | ||
| root_index: 1, | ||
| nested_paths: vec![], | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| assert_eq!( | ||
| vec![1, 2, 3], | ||
| build_parquet_leaves_indices(&parquet_schema_desc, &projection) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_reads_middle_level_path() { | ||
| let parquet_schema_desc = build_test_nested_parquet_schema(); | ||
|
|
||
| let projection = ParquetReadColumns { | ||
| cols: vec![ParquetReadColumn { | ||
| root_index: 0, | ||
| nested_paths: vec![vec!["j".to_string(), "b".to_string()]], | ||
| }], | ||
| }; | ||
|
|
||
| assert_eq!( | ||
| vec![1, 2], | ||
| build_parquet_leaves_indices(&parquet_schema_desc, &projection) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_reads_leaf_level_path() { | ||
| let parquet_schema_desc = build_test_nested_parquet_schema(); | ||
|
|
||
| let projection = ParquetReadColumns { | ||
| cols: vec![ParquetReadColumn { | ||
| root_index: 0, | ||
| nested_paths: vec![vec!["j".to_string(), "b".to_string(), "c".to_string()]], | ||
| }], | ||
| }; | ||
|
|
||
| assert_eq!( | ||
| vec![1], | ||
| build_parquet_leaves_indices(&parquet_schema_desc, &projection) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_merges_mixed_paths() { | ||
| let parquet_schema_desc = build_test_nested_parquet_schema(); | ||
|
|
||
| let projection = ParquetReadColumns { | ||
| cols: vec![ParquetReadColumn { | ||
| root_index: 0, | ||
| nested_paths: vec![ | ||
| vec!["j".to_string(), "a".to_string()], | ||
| vec!["j".to_string(), "b".to_string(), "d".to_string()], | ||
| ], | ||
| }], | ||
| }; | ||
|
|
||
| assert_eq!( | ||
| vec![0, 2], | ||
| build_parquet_leaves_indices(&parquet_schema_desc, &projection) | ||
| ); | ||
| } | ||
|
|
||
| // Test schema: | ||
| // schema | ||
| // |- j | ||
| // | |- a: INT64 | ||
| // | `- b | ||
| // | |- c: INT64 | ||
| // | `- d: INT64 | ||
| // `- k: INT64 | ||
| fn build_test_nested_parquet_schema() -> SchemaDescriptor { | ||
| let leaf_a = Arc::new( | ||
| Type::primitive_type_builder("a", parquet::basic::Type::INT64) | ||
| .with_repetition(Repetition::REQUIRED) | ||
| .build() | ||
| .unwrap(), | ||
| ); | ||
| let leaf_c = Arc::new( | ||
| Type::primitive_type_builder("c", parquet::basic::Type::INT64) | ||
| .with_repetition(Repetition::REQUIRED) | ||
| .build() | ||
| .unwrap(), | ||
| ); | ||
| let leaf_d = Arc::new( | ||
| Type::primitive_type_builder("d", parquet::basic::Type::INT64) | ||
| .with_repetition(Repetition::REQUIRED) | ||
| .build() | ||
| .unwrap(), | ||
| ); | ||
| let group_b = Arc::new( | ||
| Type::group_type_builder("b") | ||
| .with_repetition(Repetition::REQUIRED) | ||
| .with_fields(vec![leaf_c, leaf_d]) | ||
| .build() | ||
| .unwrap(), | ||
| ); | ||
| let root_j = Arc::new( | ||
| Type::group_type_builder("j") | ||
| .with_repetition(Repetition::REQUIRED) | ||
| .with_fields(vec![leaf_a, group_b]) | ||
| .build() | ||
| .unwrap(), | ||
| ); | ||
| let root_k = Arc::new( | ||
| Type::primitive_type_builder("k", parquet::basic::Type::INT64) | ||
| .with_repetition(Repetition::REQUIRED) | ||
| .build() | ||
| .unwrap(), | ||
| ); | ||
| let schema = Arc::new( | ||
| Type::group_type_builder("schema") | ||
| .with_fields(vec![root_j, root_k]) | ||
| .build() | ||
| .unwrap(), | ||
| ); | ||
|
|
||
| SchemaDescriptor::new(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
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.
Uh oh!
There was an error while loading. Please reload this page.