Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2.3k
Support make_array null handling in nested version#7207
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
+111
−100
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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 |
|---|---|---|
| @@ -212,6 +212,12 @@ fn compute_array_dims(arr: Option<ArrayRef>) -> Result<Option<Vec<Option<u64>>>> | ||
| } | ||
| } | ||
| #[derive(Debug)] | ||
| enum ListOrNull<'a> { | ||
| List(&'a dyn Array), | ||
| Null, | ||
| } | ||
| /// Convert one or more [`ArrayRef`] of the same type into a | ||
| /// `ListArray` | ||
| /// | ||
| @@ -238,18 +244,18 @@ fn compute_array_dims(arr: Option<ArrayRef>) -> Result<Option<Vec<Option<u64>>>> | ||
| /// | ||
| /// Calling `array(col1, col2)` where col1 and col2 are lists | ||
| /// would return a single new `ListArray`, where each row was a list | ||
| /// of the corresponding elements of col1 and col2 flattened. | ||
| /// of the corresponding elements of col1 and col2. | ||
| /// | ||
| /// ``` text | ||
| /// ┌──────────────┐ ┌──────────────┐ ┌────────────────────────┐ | ||
| /// │ ┌──────────┐ │ │ ┌──────────┐ │ │ ┌────────────────────┐ │ | ||
| /// │ │ [A, X] │ │ │ │ [] │ │ │ │ [A, X]│ │ | ||
| /// │ ├──────────┤ │ │ ├──────────┤ │ │ ├────────────────────┤ │ | ||
| /// │ │[NULL, Y] │ │ │ │[Q, R, S] │ │───────▶│ │ [NULL, Y, Q, R, S] │ │ | ||
| /// │ ├──────────┤ │ │ ├──────────┤ │ │ ├────────────────────┤ │ | ||
| /// │ │ [C, Z] │ │ │ │ NULL │ │ │ │ [C, Z, NULL] │ │ | ||
| /// │ └──────────┘ │ │ └──────────┘ │ │ └────────────────────┘ │ | ||
| /// └──────────────┘ └──────────────┘ └────────────────────────┘ | ||
| /// ┌──────────────┐ ┌──────────────┐ ┌─────────────────────────────┐ | ||
| /// │ ┌──────────┐ │ │ ┌──────────┐ │ │ ┌────────────────────────┐ │ | ||
| /// │ │ [A, X] │ │ │ │ [] │ │ │ │ [[A, X], []] │ │ | ||
| /// │ ├──────────┤ │ │ ├──────────┤ │ │ ├────────────────────────┤ │ | ||
| /// │ │[NULL, Y] │ │ │ │[Q, R, S] │ │───────▶│ │ [[NULL, Y], [Q, R, S]] │ │ | ||
| /// │ ├──────────┤ │ │ ├──────────┤ │ │ ├────────────────────────│ │ | ||
| /// │ │ [C, Z] │ │ │ │ NULL │ │ │ │ [[C, Z], NULL] │ │ | ||
| /// │ └──────────┘ │ │ └──────────┘ │ │ └────────────────────────┘ │ | ||
| /// └──────────────┘ └──────────────┘ └─────────────────────────────┘ | ||
| /// col1 col2 output | ||
| /// ``` | ||
| fn array_array(args: &[ArrayRef], data_type: DataType) -> Result<ArrayRef> { | ||
| @@ -260,23 +266,53 @@ fn array_array(args: &[ArrayRef], data_type: DataType) -> Result<ArrayRef> { | ||
| let res = match data_type { | ||
| DataType::List(..) => { | ||
| let arrays = | ||
| downcast_vec!(args, ListArray).collect::<Result<Vec<&ListArray>>>()?; | ||
| // Assume number of rows is the same for all arrays | ||
| let row_count = arrays[0].len(); | ||
| let mut arrays = vec![]; | ||
| let mut row_count = 0; | ||
| for arg in args { | ||
| let list_arr = arg.as_list_opt::<i32>(); | ||
| if let Some(list_arr) = list_arr { | ||
| // Assume number of rows is the same for all arrays | ||
| row_count = list_arr.len(); | ||
| arrays.push(ListOrNull::List(list_arr)); | ||
| } else if arg.as_any().downcast_ref::<NullArray>().is_some() { | ||
| arrays.push(ListOrNull::Null); | ||
| } else { | ||
| return Err(DataFusionError::Internal( | ||
| "Unsupported argument type for array".to_string(), | ||
| )); | ||
| } | ||
| } | ||
| let capacity = Capacities::Array(arrays.iter().map(|a| a.len()).sum()); | ||
| let array_data = arrays.iter().map(|a| a.to_data()).collect::<Vec<_>>(); | ||
| let mut total_capacity = 0; | ||
| let mut array_data = vec![]; | ||
| for arr in arrays.iter() { | ||
| if let ListOrNull::List(arr) = arr { | ||
| total_capacity += arr.len(); | ||
| array_data.push(arr.to_data()); | ||
| } | ||
| } | ||
| let capacity = Capacities::Array(total_capacity); | ||
| let array_data = array_data.iter().collect(); | ||
| let mut mutable = | ||
| MutableArrayData::with_capacities(array_data, true, capacity); | ||
| for i in 0..row_count { | ||
| for (j, _) in arrays.iter().enumerate() { | ||
| mutable.extend(j, i, i + 1); | ||
| let mut nulls = 0; | ||
| for (j, arr) in arrays.iter().enumerate() { | ||
| match arr { | ||
| ListOrNull::List(_) => { | ||
| mutable.extend(j - nulls, i, i + 1); | ||
| } | ||
| ListOrNull::Null => { | ||
| mutable.extend_nulls(1); | ||
| nulls += 1; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| let list_data_type = | ||
| DataType::List(Arc::new(Field::new("item", data_type, true))); | ||
| @@ -327,21 +363,36 @@ fn array(values: &[ColumnarValue]) -> Result<ColumnarValue> { | ||
| }) | ||
| .collect(); | ||
| let mut data_type = DataType::Null; | ||
| let mut data_type = None; | ||
| for arg in &arrays { | ||
| let arg_data_type = arg.data_type(); | ||
| if !arg_data_type.equals_datatype(&DataType::Null) { | ||
| data_type = arg_data_type.clone(); | ||
| data_type = Some(arg_data_type.clone()); | ||
| break; | ||
| } else { | ||
| data_type = Some(DataType::Null); | ||
| } | ||
| } | ||
| match data_type { | ||
| DataType::Null => Ok(ColumnarValue::Scalar(ScalarValue::new_list( | ||
| // empty array | ||
| None => Ok(ColumnarValue::Scalar(ScalarValue::new_list( | ||
| Some(vec![]), | ||
| DataType::Null, | ||
| ))), | ||
| _ => Ok(ColumnarValue::Array(array_array( | ||
| // all nulls, set default data type as int32 | ||
| Some(DataType::Null) => { | ||
jayzhan211 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| let nulls = arrays.len(); | ||
| let null_arr = Int32Array::from(vec![None; nulls]); | ||
| let field = Arc::new(Field::new("item", DataType::Int32, true)); | ||
| let offsets = OffsetBuffer::from_lengths([nulls]); | ||
| let values = Arc::new(null_arr) as ArrayRef; | ||
| let nulls = None; | ||
| Ok(ColumnarValue::Array(Arc::new(ListArray::new( | ||
| field, offsets, values, nulls, | ||
| )))) | ||
| } | ||
| Some(data_type) => Ok(ColumnarValue::Array(array_array( | ||
| arrays.as_slice(), | ||
| data_type, | ||
| )?)), | ||
| @@ -2047,61 +2098,6 @@ mod tests { | ||
| ); | ||
| } | ||
| #[test] | ||
| fn test_array_with_nulls() { | ||
| // make_array(NULL, 1, NULL, 2, NULL, 3, NULL, NULL, 4, 5) = [NULL, 1, NULL, 2, NULL, 3, NULL, NULL, 4, 5] | ||
| let args = [ | ||
| ColumnarValue::Scalar(ScalarValue::Null), | ||
| ColumnarValue::Scalar(ScalarValue::Int64(Some(1))), | ||
| ColumnarValue::Scalar(ScalarValue::Null), | ||
| ColumnarValue::Scalar(ScalarValue::Int64(Some(2))), | ||
| ColumnarValue::Scalar(ScalarValue::Null), | ||
| ColumnarValue::Scalar(ScalarValue::Int64(Some(3))), | ||
| ColumnarValue::Scalar(ScalarValue::Null), | ||
| ColumnarValue::Scalar(ScalarValue::Null), | ||
| ColumnarValue::Scalar(ScalarValue::Int64(Some(4))), | ||
| ColumnarValue::Scalar(ScalarValue::Int64(Some(5))), | ||
| ]; | ||
| let array = array(&args) | ||
| .expect("failed to initialize function array") | ||
| .into_array(1); | ||
| let result = as_list_array(&array).expect("failed to initialize function array"); | ||
| assert_eq!(result.len(), 1); | ||
| assert_eq!( | ||
| &[0, 1, 0, 2, 0, 3, 0, 0, 4, 5], | ||
| result | ||
| .value(0) | ||
| .as_any() | ||
| .downcast_ref::<Int64Array>() | ||
| .unwrap() | ||
| .values() | ||
| ) | ||
| } | ||
| #[test] | ||
| fn test_array_all_nulls() { | ||
| // make_array(NULL, NULL, NULL) = [] | ||
| let args = [ | ||
| ColumnarValue::Scalar(ScalarValue::Null), | ||
| ColumnarValue::Scalar(ScalarValue::Null), | ||
| ColumnarValue::Scalar(ScalarValue::Null), | ||
| ]; | ||
| let array = array(&args) | ||
| .expect("failed to initialize function array") | ||
| .into_array(1); | ||
| let result = as_list_array(&array).expect("failed to initialize function array"); | ||
| assert_eq!(result.len(), 1); | ||
| assert_eq!( | ||
| 0, | ||
| result | ||
| .value(0) | ||
| .as_any() | ||
| .downcast_ref::<NullArray>() | ||
| .unwrap() | ||
| .null_count() | ||
| ) | ||
| } | ||
| #[test] | ||
| fn test_array_element() { | ||
| // array_element([1, 2, 3, 4], 1) = 1 | ||
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.
Uh oh!
There was an error while loading. Please reload this page.