-
Notifications
You must be signed in to change notification settings - Fork 118
Fix list-of-struct nested projection #6012
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
Open
godnight10061
wants to merge
8
commits into
vortex-data:develop
Choose a base branch
from
godnight10061:fix-list-of-struct-projection
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,701
−49
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
405f9df
Fix nested projection for list-of-struct columns
godnight10061 0e94337
Add unit regression for list-of-struct get_item
godnight10061 11f06b6
Fix lints in list layout and projections
godnight10061 586550e
Refactor list projection internals
godnight10061 3e0575a
Refactor GetItem list projections
godnight10061 87d9553
Use map expression for list-of-struct projection
godnight10061 66f104e
Improve ListReader split planning for FixedSizeList
godnight10061 51cfa4f
Use get_item_list for list-of-struct projection
godnight10061 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 |
|---|---|---|
|
|
@@ -33,15 +33,28 @@ use crate::expr::Pack; | |
| use crate::expr::ReduceCtx; | ||
| use crate::expr::ReduceNode; | ||
| use crate::expr::ReduceNodeRef; | ||
| use crate::expr::SimplifyCtx; | ||
| use crate::expr::StatsCatalog; | ||
| use crate::expr::VTable; | ||
| use crate::expr::VTableExt; | ||
| use crate::expr::exprs::root::root; | ||
| use crate::expr::get_item_list; | ||
| use crate::expr::lit; | ||
| use crate::expr::stats::Stat; | ||
|
|
||
| pub struct GetItem; | ||
|
|
||
| fn propagate_nullability(parent_nullability: Nullability, field_dtype: DType) -> DType { | ||
| if matches!( | ||
| (parent_nullability, field_dtype.nullability()), | ||
| (Nullability::Nullable, Nullability::NonNullable) | ||
| ) { | ||
| return field_dtype.with_nullability(Nullability::Nullable); | ||
| } | ||
|
|
||
| field_dtype | ||
| } | ||
|
|
||
|
Comment on lines
+47
to
+57
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.
|
||
| impl VTable for GetItem { | ||
| type Options = FieldName; | ||
|
|
||
|
|
@@ -85,23 +98,24 @@ impl VTable for GetItem { | |
| } | ||
|
|
||
| fn return_dtype(&self, field_name: &FieldName, arg_dtypes: &[DType]) -> VortexResult<DType> { | ||
| let struct_dtype = &arg_dtypes[0]; | ||
| let field_dtype = struct_dtype | ||
| .as_struct_fields_opt() | ||
| .and_then(|st| st.field(field_name)) | ||
| .ok_or_else(|| { | ||
| let input_dtype = &arg_dtypes[0]; | ||
|
|
||
| // Struct field access: `$.a`. | ||
| if let Some(struct_fields) = input_dtype.as_struct_fields_opt() { | ||
| let field_dtype = struct_fields.field(field_name).ok_or_else(|| { | ||
| vortex_err!("Couldn't find the {} field in the input scope", field_name) | ||
| })?; | ||
|
|
||
| // Match here to avoid cloning the dtype if nullability doesn't need to change | ||
| if matches!( | ||
| (struct_dtype.nullability(), field_dtype.nullability()), | ||
| (Nullability::Nullable, Nullability::NonNullable) | ||
| ) { | ||
| return Ok(field_dtype.with_nullability(Nullability::Nullable)); | ||
| return Ok(propagate_nullability( | ||
| input_dtype.nullability(), | ||
| field_dtype, | ||
| )); | ||
| } | ||
|
|
||
| Ok(field_dtype) | ||
| Err(vortex_err!( | ||
| "Expected struct dtype for child of GetItem expression, got {}", | ||
| input_dtype | ||
| )) | ||
| } | ||
|
|
||
| fn evaluate( | ||
|
|
@@ -110,34 +124,75 @@ impl VTable for GetItem { | |
| expr: &Expression, | ||
| scope: &ArrayRef, | ||
| ) -> VortexResult<ArrayRef> { | ||
| let input = expr.children()[0].evaluate(scope)?.to_struct(); | ||
| let field = input.field_by_name(field_name).cloned()?; | ||
| let input = expr.children()[0].evaluate(scope)?; | ||
|
|
||
| // Struct field access: `$.a`. | ||
| if input.dtype().is_struct() { | ||
| let input = input.to_struct(); | ||
| let field = input.field_by_name(field_name).cloned()?; | ||
|
|
||
| match input.dtype().nullability() { | ||
| Nullability::NonNullable => Ok(field), | ||
| Nullability::Nullable => mask(&field, &input.validity_mask().not()), | ||
| return match input.dtype().nullability() { | ||
| Nullability::NonNullable => Ok(field), | ||
| Nullability::Nullable => mask(&field, &input.validity_mask().not()), | ||
| }; | ||
| } | ||
|
|
||
| Err(vortex_err!( | ||
| "Expected struct scope for GetItem evaluation, got {}", | ||
| input.dtype() | ||
| )) | ||
| } | ||
|
|
||
| fn execute(&self, field_name: &FieldName, mut args: ExecutionArgs) -> VortexResult<Datum> { | ||
| let struct_dtype = args.dtypes[0] | ||
| .as_struct_fields_opt() | ||
| .ok_or_else(|| vortex_err!("Expected struct dtype for child of GetItem expression"))?; | ||
| let field_idx = struct_dtype | ||
| .find(field_name) | ||
| .ok_or_else(|| vortex_err!("Field {} not found in struct dtype", field_name))?; | ||
|
|
||
| match args.datums.pop().vortex_expect("missing input") { | ||
| Datum::Scalar(s) => { | ||
| let mut field = s.as_struct().field(field_idx); | ||
| field.mask_validity(s.is_valid()); | ||
| Ok(Datum::Scalar(field)) | ||
| } | ||
| Datum::Vector(v) => { | ||
| let mut field = v.as_struct().fields()[field_idx].clone(); | ||
| field.mask_validity(v.validity()); | ||
| Ok(Datum::Vector(field)) | ||
| } | ||
| let input_dtype = &args.dtypes[0]; | ||
|
|
||
| // Struct field access: `$.a`. | ||
| if let Some(struct_dtype) = input_dtype.as_struct_fields_opt() { | ||
| let field_idx = struct_dtype | ||
| .find(field_name) | ||
| .ok_or_else(|| vortex_err!("Field {} not found in struct dtype", field_name))?; | ||
|
|
||
| return match args.datums.pop().vortex_expect("missing input") { | ||
| Datum::Scalar(s) => { | ||
| let mut field = s.as_struct().field(field_idx); | ||
| field.mask_validity(s.is_valid()); | ||
| Ok(Datum::Scalar(field)) | ||
| } | ||
| Datum::Vector(v) => { | ||
| let mut field = v.as_struct().fields()[field_idx].clone(); | ||
| field.mask_validity(v.validity()); | ||
| Ok(Datum::Vector(field)) | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| Err(vortex_err!( | ||
| "Expected struct dtype for child of GetItem expression, got {}", | ||
| input_dtype | ||
| )) | ||
| } | ||
|
|
||
| fn simplify( | ||
| &self, | ||
| field_name: &FieldName, | ||
| expr: &Expression, | ||
| ctx: &dyn SimplifyCtx, | ||
| ) -> VortexResult<Option<Expression>> { | ||
| let child = expr.child(0); | ||
| let child_dtype = ctx.return_dtype(child)?; | ||
|
|
||
| let element_dtype = match child_dtype { | ||
| DType::List(element_dtype, _) => Some(element_dtype), | ||
| DType::FixedSizeList(element_dtype, ..) => Some(element_dtype), | ||
| _ => None, | ||
| }; | ||
|
|
||
| if let Some(element_dtype) = element_dtype | ||
| && element_dtype.as_struct_fields_opt().is_some() | ||
| { | ||
| Ok(Some(get_item_list(field_name.clone(), child.clone()))) | ||
| } else { | ||
| Ok(None) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -261,6 +316,8 @@ pub fn get_item(field: impl Into<FieldName>, child: Expression) -> Expression { | |
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::sync::Arc; | ||
|
|
||
| use vortex_buffer::buffer; | ||
| use vortex_dtype::DType; | ||
| use vortex_dtype::FieldNames; | ||
|
|
@@ -272,9 +329,12 @@ mod tests { | |
|
|
||
| use crate::Array; | ||
| use crate::IntoArray; | ||
| use crate::arrays::FixedSizeListArray; | ||
| use crate::arrays::ListArray; | ||
| use crate::arrays::StructArray; | ||
| use crate::expr::exprs::binary::checked_add; | ||
| use crate::expr::exprs::get_item::get_item; | ||
| use crate::expr::exprs::get_item_list::get_item_list; | ||
| use crate::expr::exprs::literal::lit; | ||
| use crate::expr::exprs::pack::pack; | ||
| use crate::expr::exprs::root::root; | ||
|
|
@@ -322,6 +382,182 @@ mod tests { | |
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn get_item_list_of_struct() { | ||
| let element_dtype = Arc::new(DType::Struct( | ||
| [ | ||
| ("a", DType::Primitive(PType::I32, NonNullable)), | ||
| ("b", DType::Utf8(NonNullable)), | ||
| ] | ||
| .into_iter() | ||
| .collect(), | ||
| NonNullable, | ||
| )); | ||
|
|
||
| let row_count = 4; | ||
| let items = ListArray::from_iter_opt_slow::<u32, _, _>( | ||
| [ | ||
| Some(vec![ | ||
| Scalar::struct_( | ||
| (*element_dtype).clone(), | ||
| vec![ | ||
| Scalar::primitive(1i32, NonNullable), | ||
| Scalar::utf8("x", NonNullable), | ||
| ], | ||
| ), | ||
| Scalar::struct_( | ||
| (*element_dtype).clone(), | ||
| vec![ | ||
| Scalar::primitive(2i32, NonNullable), | ||
| Scalar::utf8("y", NonNullable), | ||
| ], | ||
| ), | ||
| ]), | ||
| Some(Vec::new()), | ||
| None, | ||
| Some(vec![Scalar::struct_( | ||
| (*element_dtype).clone(), | ||
| vec![ | ||
| Scalar::primitive(3i32, NonNullable), | ||
| Scalar::utf8("z", NonNullable), | ||
| ], | ||
| )]), | ||
| ], | ||
| element_dtype, | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| let ids = buffer![0i32, 1, 2, 3].into_array(); | ||
|
|
||
| let data = StructArray::new( | ||
| FieldNames::from(["id", "items"]), | ||
| vec![ids, items], | ||
| row_count, | ||
| Validity::NonNullable, | ||
| ) | ||
| .into_array(); | ||
|
|
||
| // Regression for nested field projection on list-of-struct: `items.a`. | ||
| let projection = get_item_list("a", get_item("items", root())); | ||
| let out = data.apply(&projection).expect("apply"); | ||
|
|
||
| assert_eq!( | ||
| out.dtype(), | ||
| &DType::List( | ||
| Arc::new(DType::Primitive(PType::I32, NonNullable)), | ||
| Nullability::Nullable | ||
| ) | ||
| ); | ||
|
|
||
| assert_eq!( | ||
| out.scalar_at(0).as_list().elements().unwrap().to_vec(), | ||
| vec![ | ||
| Scalar::primitive(1i32, NonNullable), | ||
| Scalar::primitive(2i32, NonNullable), | ||
| ] | ||
| ); | ||
| assert!(out.scalar_at(1).as_list().elements().unwrap().is_empty()); | ||
| assert!(out.scalar_at(2).is_null()); | ||
| assert_eq!( | ||
| out.scalar_at(3).as_list().elements().unwrap().to_vec(), | ||
| vec![Scalar::primitive(3i32, NonNullable)] | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn get_item_fixed_size_list_of_struct() { | ||
| let n_lists: usize = 3; | ||
| let list_size: u32 = 2; | ||
| let n_elements = n_lists * list_size as usize; | ||
|
|
||
| let struct_elems = StructArray::try_new( | ||
| FieldNames::from(["a", "b"]), | ||
| vec![ | ||
| buffer![1i32, 2, 3, 4, 5, 6].into_array(), | ||
| buffer![10i64, 20, 30, 40, 50, 60].into_array(), | ||
| ], | ||
| n_elements, | ||
| Validity::from_iter([true, false, true, true, false, true]), | ||
| ) | ||
| .unwrap() | ||
| .into_array(); | ||
|
|
||
| let items = FixedSizeListArray::try_new( | ||
| struct_elems, | ||
| list_size, | ||
| Validity::from_iter([true, false, true]), | ||
| n_lists, | ||
| ) | ||
| .unwrap() | ||
| .into_array(); | ||
|
|
||
| let ids = buffer![0i32, 1, 2].into_array(); | ||
|
|
||
| let data = StructArray::new( | ||
| FieldNames::from(["id", "items"]), | ||
| vec![ids, items], | ||
| n_lists, | ||
| Validity::NonNullable, | ||
| ) | ||
| .into_array(); | ||
|
|
||
| // FixedSizeList-of-struct projection: `items.a`, including struct-level nulls inside the list. | ||
| let projection = get_item_list("a", get_item("items", root())); | ||
| let out = data.apply(&projection).expect("apply"); | ||
|
|
||
| assert_eq!( | ||
| out.dtype(), | ||
| &DType::FixedSizeList( | ||
| Arc::new(DType::Primitive(PType::I32, Nullability::Nullable)), | ||
| list_size, | ||
| Nullability::Nullable | ||
| ) | ||
| ); | ||
|
|
||
| assert_eq!( | ||
| out.scalar_at(0).as_list().elements().unwrap().to_vec(), | ||
| vec![ | ||
| Scalar::primitive(1i32, Nullability::Nullable), | ||
| Scalar::null(DType::Primitive(PType::I32, Nullability::Nullable)), | ||
| ] | ||
| ); | ||
| assert!(out.scalar_at(1).is_null()); | ||
| assert_eq!( | ||
| out.scalar_at(2).as_list().elements().unwrap().to_vec(), | ||
| vec![ | ||
| Scalar::null(DType::Primitive(PType::I32, Nullability::Nullable)), | ||
| Scalar::primitive(6i32, Nullability::Nullable), | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn get_item_list_of_struct_desugars_to_get_item_list_on_optimize() { | ||
| let scope_dtype = DType::Struct( | ||
| [ | ||
| ("id", DType::from(PType::I32)), | ||
| ( | ||
| "items", | ||
| DType::List( | ||
| Arc::new(DType::Struct( | ||
| [("a", DType::from(PType::I32))].into_iter().collect(), | ||
| NonNullable, | ||
| )), | ||
| Nullability::Nullable, | ||
| ), | ||
| ), | ||
| ] | ||
| .into_iter() | ||
| .collect(), | ||
| NonNullable, | ||
| ); | ||
|
|
||
| let expr = get_item("a", get_item("items", root())); | ||
| let optimized = expr.optimize_recursive(&scope_dtype).unwrap(); | ||
|
|
||
| assert!(optimized.is::<crate::expr::exprs::get_item_list::GetItemList>()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_pack_get_item_rule() { | ||
| // Create: pack(a: lit(1), b: lit(2)).get_item("b") | ||
|
|
||
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.
do we need these changes?