You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "jayzhan211 (via GitHub)" <gi...@apache.org> on 2023/10/19 01:43:20 UTC
[PR] Minor: Convert list array and non-list array to scalars [arrow-datafusion]
jayzhan211 opened a new pull request, #7862:
URL: https://github.com/apache/arrow-datafusion/pull/7862
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
Closes #.
## Rationale for this change
`convert_array_to_scalar_vec` can convert well for list array, but we will get 2D array for non-list array (primitive array).
It would be nice to have another conversion for non-list array. I would need this for #7835 too.
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
## What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
## Are these changes tested?
TODO: Add test and doc test.
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
-->
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
<!--
If there are any breaking changes to public APIs, please add the `api change` label.
-->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Minor: Convert list array and non-list array to scalars [arrow-datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #7862:
URL: https://github.com/apache/arrow-datafusion/pull/7862#discussion_r1369268008
##########
datafusion/common/src/scalar.rs:
##########
@@ -2130,6 +2129,8 @@ impl ScalarValue {
/// Retrieve ScalarValue for each row in `array`
///
+ /// Convert ListArray to `Vec<Vec<ScalarValue>>`, first `Vec` is for rows, second `Vec` is for elements in the list
Review Comment:
👍
##########
datafusion/physical-expr/src/aggregate/array_agg_distinct.rs:
##########
@@ -308,146 +304,4 @@ mod tests {
check_merge_distinct_array_agg(col1, col2, expected, DataType::Int32)
}
-
- #[test]
- fn distinct_array_agg_nested() -> Result<()> {
Review Comment:
I don't understand the rationale for deleting these tests -- is the functionality covered elsewhere? If we deleted this code, won't we have less test coverage?
##########
datafusion/common/src/scalar.rs:
##########
@@ -2156,30 +2157,58 @@ impl ScalarValue {
///
/// assert_eq!(scalar_vec, expected);
/// ```
- pub fn convert_array_to_scalar_vec(array: &dyn Array) -> Result<Vec<Vec<Self>>> {
- let mut scalars = Vec::with_capacity(array.len());
+ pub fn convert_list_array_to_scalar_vec(array: &dyn Array) -> Result<Vec<Vec<Self>>> {
+ let mut scalars_vec = Vec::with_capacity(array.len());
+
+ if as_list_array(array).is_ok() {
+ let list_arr = as_list_array(array)?;
+ for index in 0..list_arr.len() {
+ let scalars = match list_arr.is_null(index) {
+ true => Vec::new(),
+ false => {
+ let nested_array = list_arr.value(index);
+ ScalarValue::convert_list_array_to_scalar_vec(&nested_array)?
+ .into_iter()
+ .flatten()
+ .collect()
+ }
+ };
+ scalars_vec.push(scalars);
+ }
+ } else {
+ let scalars = ScalarValue::convert_non_list_array_to_scalars(array)?;
Review Comment:
This seems like it would result in some very confusing behavior -- a single row vec -- If I pass a non `ListArray` to a function called `convert_list_array_to_scalar_vec` I would prefer to get an error back so I can track down the bug in my code rather than get back something unexpected and have to know to check for that
##########
datafusion/common/src/scalar.rs:
##########
@@ -2156,30 +2157,58 @@ impl ScalarValue {
///
/// assert_eq!(scalar_vec, expected);
/// ```
- pub fn convert_array_to_scalar_vec(array: &dyn Array) -> Result<Vec<Vec<Self>>> {
- let mut scalars = Vec::with_capacity(array.len());
+ pub fn convert_list_array_to_scalar_vec(array: &dyn Array) -> Result<Vec<Vec<Self>>> {
+ let mut scalars_vec = Vec::with_capacity(array.len());
+
+ if as_list_array(array).is_ok() {
Review Comment:
In general, I think it would be better to avoid ignoring errors (which is what `is_ok()` does as each error requires at least one string allocation. This particular usage isn't likely performance critical but the pattern is not great and we have had trouble with it in the past.
It makes sense to me that we would need two different functions for Arrays to ScalarValue (as the return type of the ListArray needs to be different).
I think it would be less surprising (and thus less error prone) if this function simply returned an error if the input was not a list array
##########
datafusion/common/src/scalar.rs:
##########
@@ -2130,6 +2129,8 @@ impl ScalarValue {
/// Retrieve ScalarValue for each row in `array`
///
+ /// Convert ListArray to `Vec<Vec<ScalarValue>>`, first `Vec` is for rows, second `Vec` is for elements in the list
Review Comment:
👍
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Convert list array and non-list array to scalars [arrow-datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #7862:
URL: https://github.com/apache/arrow-datafusion/pull/7862#issuecomment-1785963930
I think this is not waiting on any more feedback
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Convert list array and non-list array to scalars [arrow-datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on PR #7862:
URL: https://github.com/apache/arrow-datafusion/pull/7862#issuecomment-1829828236
wait on #8253
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Convert list array and non-list array to scalars [arrow-datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #7862:
URL: https://github.com/apache/arrow-datafusion/pull/7862#issuecomment-1861339998
I am sorry -- I feel really bad that we can't find more review bandwidth to match your contributions @jayzhan211 -- your efforts so far have been great and are really appreciated.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Convert list array and non-list array to scalars [arrow-datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on PR #7862:
URL: https://github.com/apache/arrow-datafusion/pull/7862#issuecomment-1848402449
wait on #8439
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Minor: Convert list array and non-list array to scalars [arrow-datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #7862:
URL: https://github.com/apache/arrow-datafusion/pull/7862#discussion_r1369340102
##########
datafusion/physical-expr/src/aggregate/array_agg_distinct.rs:
##########
@@ -308,146 +304,4 @@ mod tests {
check_merge_distinct_array_agg(col1, col2, expected, DataType::Int32)
}
-
- #[test]
- fn distinct_array_agg_nested() -> Result<()> {
Review Comment:
@alamb
Since update_batch() of DistinctArrayAggAccumulator only accepts non-list after the change. Nested array in these tests is not allowed.
Should I just delete the test or add an assertion to ensure we get Err in this case?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Convert list array and non-list array to scalars [arrow-datafusion]
Posted by "Weijun-H (via GitHub)" <gi...@apache.org>.
Weijun-H commented on code in PR #7862:
URL: https://github.com/apache/arrow-datafusion/pull/7862#discussion_r1425480957
##########
datafusion/physical-expr/src/aggregate/array_agg_distinct.rs:
##########
@@ -137,9 +137,18 @@ impl Accumulator for DistinctArrayAggAccumulator {
assert_eq!(values.len(), 1, "batch input should only include 1 column!");
let array = &values[0];
- let scalars = ScalarValue::convert_array_to_scalar_vec(array)?;
- for scalar in scalars {
- self.values.extend(scalar)
+ match array.data_type() {
+ DataType::List(_) => {
+ let scalar_vec =
+ ScalarValue::convert_list_array_to_scalar_vec::<i32>(array)?;
+ for scalars in scalar_vec {
+ self.values.extend(scalars);
+ }
+ }
+ _ => {
Review Comment:
As `convert_list_array_to_scalar_vec ` can accept `OffSizeTrait`, why don't you also handle `LargeList` here?
```suggestion
DataType::LargeList(_) => {
let scalar_vec =
ScalarValue::convert_list_array_to_scalar_vec::<i64>(array)?;
for scalars in scalar_vec {
self.values.extend(scalars);
}
}
_ => {
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Convert list array and non-list array to scalars [arrow-datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #7862:
URL: https://github.com/apache/arrow-datafusion/pull/7862#issuecomment-1859145381
(BTW sorry for being so nit picky on this PR -- I think `ScalarValue` is a key and important API of DataFusion so avoiding API churn (continually changing the same API over and over again) is likely valuable
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Minor: Convert list array and non-list array to scalars [arrow-datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #7862:
URL: https://github.com/apache/arrow-datafusion/pull/7862#discussion_r1369344751
##########
datafusion/common/src/scalar.rs:
##########
@@ -2156,30 +2157,58 @@ impl ScalarValue {
///
/// assert_eq!(scalar_vec, expected);
/// ```
- pub fn convert_array_to_scalar_vec(array: &dyn Array) -> Result<Vec<Vec<Self>>> {
- let mut scalars = Vec::with_capacity(array.len());
+ pub fn convert_list_array_to_scalar_vec(array: &dyn Array) -> Result<Vec<Vec<Self>>> {
+ let mut scalars_vec = Vec::with_capacity(array.len());
+
+ if as_list_array(array).is_ok() {
Review Comment:
I think it would be nice to change both functions to ensure only ListArray is accepted in `convert_list_array_to_scalar_vec ` and non-ListArray is accepted in `convert_non_list_array_to_scalar_vec `.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Convert list array and non-list array to scalars [arrow-datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #7862:
URL: https://github.com/apache/arrow-datafusion/pull/7862#discussion_r1428801835
##########
datafusion/common/src/scalar.rs:
##########
@@ -2039,7 +2039,11 @@ impl ScalarValue {
}
}
- /// Retrieve ScalarValue for each row in `array`
+ /// Retrieve `ScalarValue` for each row in `array`
+ ///
Review Comment:
This seems left over
```suggestion
```
##########
datafusion/common/src/scalar.rs:
##########
@@ -2039,7 +2039,11 @@ impl ScalarValue {
}
}
- /// Retrieve ScalarValue for each row in `array`
+ /// Retrieve `ScalarValue` for each row in `array`
+ ///
+ /// Convert `ListArray` to `Vec<Vec<ScalarValue>>`, first `Vec` is for rows, second `Vec` is for elements in the list
Review Comment:
👍 It might also help to explain why we need two different signatures. It took me a while to grok things too
```suggestion
/// Convert `ListArray` into a 2 dimensional to `Vec<Vec<ScalarValue>>`, first `Vec` is for rows,
/// second `Vec` is for elements in the list.
///
/// See [`Self::convert_non_list_array_to_scalars`] for converting non Lists
///
/// This method is an optimization to unwrap nested ListArrays to nested Rust structures without
/// converting them twice
```
##########
datafusion/common/src/scalar.rs:
##########
@@ -2067,30 +2071,78 @@ impl ScalarValue {
///
/// assert_eq!(scalar_vec, expected);
/// ```
- pub fn convert_array_to_scalar_vec(array: &dyn Array) -> Result<Vec<Vec<Self>>> {
- let mut scalars = Vec::with_capacity(array.len());
+ pub fn convert_list_array_to_scalar_vec<O: OffsetSizeTrait>(
Review Comment:
I think this would be easier to use if it didn't mix generic and non generic code
Maybe something like
```suggestion
pub fn convert_list_array_to_scalar_vec(
array: &dyn Array,
) -> Result<Vec<Vec<Self>>> {
if let Some(arr) = array.as_list_opt::<i32> {
Self::convert_list_array_to_scalar_vec_internal(arr)
} else if let Some(arr) = array.as_list_opt::<64> {
Self::convert_list_array_to_scalar_vec_internal(arr)
} else {
_internal_err!("Expected GenericListArray but found: {array:?}")
}
}
```
And then internally pass in the cast Array by changing
```
fn convert_list_array_to_scalar_vec_internal<O: OffsetSizeTrait>(
array: &dyn Array,
) -> Result<Vec<Vec<Self>>> {
```
to
```
fn convert_list_array_to_scalar_vec_internal<O: OffsetSizeTrait>(
array: &GenericListArray<O>,
) -> Result<Vec<Vec<Self>>> {
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Convert list array and non-list array to scalars [arrow-datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on PR #7862:
URL: https://github.com/apache/arrow-datafusion/pull/7862#issuecomment-1779159454
Summary of changes
1. Only accept ListArray for convert_list_array, only accept non-ListArray for convert_non_list
2. extend wrap_into_list for multiple arrays
3. keep the nested list test for distinct array agg removed, since `update_batch` only convert-non-list cases.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Convert list array and non-list array to scalars [arrow-datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #7862:
URL: https://github.com/apache/arrow-datafusion/pull/7862#discussion_r1390146278
##########
datafusion/common/src/scalar.rs:
##########
@@ -1203,7 +1202,7 @@ impl ScalarValue {
scalars.into_iter().map(|x| match x {
ScalarValue::List(arr) => {
// `ScalarValue::List` contains a single element `ListArray`.
- let list_arr = as_list_array(&arr);
+ let list_arr = as_list_array(&arr).unwrap();
Review Comment:
Cant use `?` in macro
##########
datafusion/common/src/scalar.rs:
##########
@@ -320,8 +319,8 @@ impl PartialOrd for ScalarValue {
(Fixedsizelist(_, _, _), _) => None,
(List(arr1), List(arr2)) => {
if arr1.data_type() == arr2.data_type() {
- let list_arr1 = as_list_array(arr1);
- let list_arr2 = as_list_array(arr2);
+ let list_arr1 = as_list_array(arr1).unwrap();
Review Comment:
Cant use `?` in macro
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Convert list array and non-list array to scalars [arrow-datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 closed pull request #7862: Convert list array and non-list array to scalars
URL: https://github.com/apache/arrow-datafusion/pull/7862
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Convert list array and non-list array to scalars [arrow-datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on PR #7862:
URL: https://github.com/apache/arrow-datafusion/pull/7862#issuecomment-1859325308
> (BTW sorry for being so nit picky on this PR -- I think `ScalarValue` is a key and important API of DataFusion so avoiding API churn (continually changing the same API over and over again) is important
This comment is a bit too late...
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Convert list array and non-list array to scalars [arrow-datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on PR #7862:
URL: https://github.com/apache/arrow-datafusion/pull/7862#issuecomment-1858651153
@alamb I think this is ready to go, thanks
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Minor: Convert list array and non-list array to scalars [arrow-datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #7862:
URL: https://github.com/apache/arrow-datafusion/pull/7862#discussion_r1369344751
##########
datafusion/common/src/scalar.rs:
##########
@@ -2156,30 +2157,58 @@ impl ScalarValue {
///
/// assert_eq!(scalar_vec, expected);
/// ```
- pub fn convert_array_to_scalar_vec(array: &dyn Array) -> Result<Vec<Vec<Self>>> {
- let mut scalars = Vec::with_capacity(array.len());
+ pub fn convert_list_array_to_scalar_vec(array: &dyn Array) -> Result<Vec<Vec<Self>>> {
+ let mut scalars_vec = Vec::with_capacity(array.len());
+
+ if as_list_array(array).is_ok() {
Review Comment:
I think it is nice that only ListArray is accepted in `convert_list_array_to_scalar_vec ` and non-ListArray is accepted in `convert_non_list_array_to_scalar_vec `.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org