-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Ensure null inputs to array setop functions return null output #19683
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 1 commit
Commits
Show all changes
4 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
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 |
|---|---|---|
|
|
@@ -19,8 +19,7 @@ | |
|
|
||
| use crate::utils::make_scalar_function; | ||
| use arrow::array::{ | ||
| Array, ArrayRef, GenericListArray, LargeListArray, ListArray, OffsetSizeTrait, | ||
| new_null_array, | ||
| Array, ArrayRef, GenericListArray, OffsetSizeTrait, new_empty_array, new_null_array, | ||
| }; | ||
| use arrow::buffer::{NullBuffer, OffsetBuffer}; | ||
| use arrow::compute; | ||
|
|
@@ -69,7 +68,7 @@ make_udf_expr_and_func!( | |
|
|
||
| #[user_doc( | ||
| doc_section(label = "Array Functions"), | ||
| description = "Returns an array of elements that are present in both arrays (all elements from both arrays) with out duplicates.", | ||
| description = "Returns an array of elements that are present in both arrays (all elements from both arrays) without duplicates.", | ||
| syntax_example = "array_union(array1, array2)", | ||
| sql_example = r#"```sql | ||
| > select array_union([1, 2, 3, 4], [5, 6, 3, 4]); | ||
|
|
@@ -136,8 +135,7 @@ impl ScalarUDFImpl for ArrayUnion { | |
| let [array1, array2] = take_function_args(self.name(), arg_types)?; | ||
| match (array1, array2) { | ||
| (Null, Null) => Ok(DataType::new_list(Null, true)), | ||
| (Null, dt) => Ok(dt.clone()), | ||
| (dt, Null) => Ok(dt.clone()), | ||
| (Null, dt) | (dt, Null) => Ok(dt.clone()), | ||
| (dt, _) => Ok(dt.clone()), | ||
| } | ||
| } | ||
|
|
@@ -221,8 +219,7 @@ impl ScalarUDFImpl for ArrayIntersect { | |
| let [array1, array2] = take_function_args(self.name(), arg_types)?; | ||
| match (array1, array2) { | ||
| (Null, Null) => Ok(DataType::new_list(Null, true)), | ||
| (Null, dt) => Ok(dt.clone()), | ||
| (dt, Null) => Ok(dt.clone()), | ||
| (Null, dt) | (dt, Null) => Ok(dt.clone()), | ||
| (dt, _) => Ok(dt.clone()), | ||
| } | ||
| } | ||
|
|
@@ -363,23 +360,19 @@ fn generic_set_lists<OffsetSize: OffsetSizeTrait>( | |
|
|
||
| let mut offsets = vec![OffsetSize::usize_as(0)]; | ||
| let mut new_arrays = vec![]; | ||
| let mut new_null_buf = vec![]; | ||
| let converter = RowConverter::new(vec![SortField::new(l.value_type())])?; | ||
| for (first_arr, second_arr) in l.iter().zip(r.iter()) { | ||
| let mut ele_should_be_null = false; | ||
| for (l_arr, r_arr) in l.iter().zip(r.iter()) { | ||
| let last_offset = *offsets.last().unwrap(); | ||
|
|
||
| let l_values = if let Some(first_arr) = first_arr { | ||
| converter.convert_columns(&[first_arr])? | ||
| } else { | ||
| ele_should_be_null = true; | ||
| converter.empty_rows(0, 0) | ||
| }; | ||
|
|
||
| let r_values = if let Some(second_arr) = second_arr { | ||
| converter.convert_columns(&[second_arr])? | ||
| } else { | ||
| ele_should_be_null = true; | ||
| converter.empty_rows(0, 0) | ||
| let (l_values, r_values) = match (l_arr, r_arr) { | ||
| (Some(l_arr), Some(r_arr)) => ( | ||
| converter.convert_columns(&[l_arr])?, | ||
| converter.convert_columns(&[r_arr])?, | ||
| ), | ||
| _ => { | ||
|
Comment on lines
-371
to
+372
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. Little drive-by refactor here to skip the set logic below for nulls |
||
| offsets.push(last_offset); | ||
| continue; | ||
| } | ||
| }; | ||
|
|
||
| let l_iter = l_values.iter().sorted().dedup(); | ||
|
|
@@ -405,11 +398,6 @@ fn generic_set_lists<OffsetSize: OffsetSizeTrait>( | |
| } | ||
| } | ||
|
|
||
| let last_offset = match offsets.last() { | ||
| Some(offset) => *offset, | ||
| None => return internal_err!("offsets should not be empty"), | ||
| }; | ||
|
|
||
| offsets.push(last_offset + OffsetSize::usize_as(rows.len())); | ||
| let arrays = converter.convert_rows(rows)?; | ||
| let array = match arrays.first() { | ||
|
|
@@ -419,18 +407,21 @@ fn generic_set_lists<OffsetSize: OffsetSizeTrait>( | |
| } | ||
| }; | ||
|
|
||
| new_null_buf.push(!ele_should_be_null); | ||
| new_arrays.push(array); | ||
| } | ||
|
|
||
| let offsets = OffsetBuffer::new(offsets.into()); | ||
| let new_arrays_ref: Vec<_> = new_arrays.iter().map(|v| v.as_ref()).collect(); | ||
| let values = compute::concat(&new_arrays_ref)?; | ||
| let values = if new_arrays_ref.is_empty() { | ||
| new_empty_array(&l.value_type()) | ||
| } else { | ||
| compute::concat(&new_arrays_ref)? | ||
| }; | ||
| let arr = GenericListArray::<OffsetSize>::try_new( | ||
| field, | ||
| offsets, | ||
| values, | ||
| Some(NullBuffer::new(new_null_buf.into())), | ||
| NullBuffer::union(l.nulls(), r.nulls()), | ||
| )?; | ||
| Ok(Arc::new(arr)) | ||
| } | ||
|
|
@@ -440,59 +431,13 @@ fn general_set_op( | |
| array2: &ArrayRef, | ||
| set_op: SetOp, | ||
| ) -> Result<ArrayRef> { | ||
| fn empty_array(data_type: &DataType, len: usize, large: bool) -> Result<ArrayRef> { | ||
| let field = Arc::new(Field::new_list_field(data_type.clone(), true)); | ||
| let values = new_null_array(data_type, len); | ||
| if large { | ||
| Ok(Arc::new(LargeListArray::try_new( | ||
| field, | ||
| OffsetBuffer::new_zeroed(len), | ||
| values, | ||
| None, | ||
| )?)) | ||
| } else { | ||
| Ok(Arc::new(ListArray::try_new( | ||
| field, | ||
| OffsetBuffer::new_zeroed(len), | ||
| values, | ||
| None, | ||
| )?)) | ||
| } | ||
| } | ||
|
|
||
| let len = array1.len(); | ||
| match (array1.data_type(), array2.data_type()) { | ||
| (Null, Null) => Ok(Arc::new(ListArray::new_null( | ||
| Arc::new(Field::new_list_field(Null, true)), | ||
| array1.len(), | ||
| ))), | ||
| (Null, List(field)) => { | ||
| if set_op == SetOp::Intersect { | ||
| return empty_array(field.data_type(), array1.len(), false); | ||
| } | ||
| let array = as_list_array(&array2)?; | ||
| general_array_distinct::<i32>(array, field) | ||
| } | ||
| (List(field), Null) => { | ||
| if set_op == SetOp::Intersect { | ||
| return empty_array(field.data_type(), array1.len(), false); | ||
| } | ||
| let array = as_list_array(&array1)?; | ||
| general_array_distinct::<i32>(array, field) | ||
| } | ||
| (Null, LargeList(field)) => { | ||
| if set_op == SetOp::Intersect { | ||
| return empty_array(field.data_type(), array1.len(), true); | ||
| } | ||
| let array = as_large_list_array(&array2)?; | ||
| general_array_distinct::<i64>(array, field) | ||
| } | ||
| (LargeList(field), Null) => { | ||
| if set_op == SetOp::Intersect { | ||
| return empty_array(field.data_type(), array1.len(), true); | ||
| } | ||
| let array = as_large_list_array(&array1)?; | ||
| general_array_distinct::<i64>(array, field) | ||
| } | ||
| (Null, Null) => Ok(new_null_array(&DataType::new_list(Null, true), len)), | ||
| (Null, dt @ List(_)) | ||
| | (Null, dt @ LargeList(_)) | ||
| | (dt @ List(_), Null) | ||
| | (dt @ LargeList(_), Null) => Ok(new_null_array(dt, len)), | ||
| (List(field), List(_)) => { | ||
| let array1 = as_list_array(&array1)?; | ||
| let array2 = as_list_array(&array2)?; | ||
|
|
||
Oops, something went wrong.
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.
Did you intentionally want to shadow
iwhich is used in the outer for loop?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 wasn't intentional, refactored the names to clear things up 👍