Skip to content

Convert list array and non-list array to scalars - #7862

Closed
jayzhan211 wants to merge 7 commits into
apache:mainfrom
jayzhan211:convert-general-arr-to-scalar
Closed

Convert list array and non-list array to scalars#7862
jayzhan211 wants to merge 7 commits into
apache:mainfrom
jayzhan211:convert-general-arr-to-scalar

Conversation

@jayzhan211

@jayzhan211jayzhan211 commented Oct 19, 2023

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Closes #.

Rationale for this change

convert_array_to_scalar_vec can convert well for list array, but we will get Vec<Vec<ScalarValue>> for non-list array (primitive array).
It would be nice to have another conversion that returns nicely Vec<ScalarValue> for the non-list array. I would need this for #7835 too.

What changes are included in this PR?

convert_list_array_to_scalar_vec for ListArray
convert_non_list_array_to_scalar_vec for non-ListArray

The nested array for DistinctArrayAggAccumulator (update_batch) is removed so as to test.

Are these changes tested?

Doc test

Are there any user-facing changes?

@github-actionsgithub-actionsBot added physical-expr Changes to the physical-expr crates core Core DataFusion crate labels Oct 19, 2023
@jayzhan211
jayzhan211 marked this pull request as ready for review October 19, 2023 14:32

@Weijun-HWeijun-H left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks @jayzhan211 👍

@alambalamb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment threaddatafusion/common/src/scalar.rs Outdated
Comment threaddatafusion/common/src/scalar.rs Outdated
Comment threaddatafusion/common/src/scalar.rs
Comment threaddatafusion/physical-expr/src/aggregate/array_agg_distinct.rs
@jayzhan211jayzhan211 changed the title Minor: Convert list array and non-list array to scalarsConvert list array and non-list array to scalarsOct 25, 2023
@jayzhan211
jayzhan211 marked this pull request as draft October 25, 2023 00:58
@jayzhan211
jayzhan211force-pushed the convert-general-arr-to-scalar branch from e7fd4ca to c9f4ea1CompareOctober 25, 2023 01:42
@github-actionsgithub-actionsBot added sqllogictest SQL Logic Tests (.slt) and removed sqllogictest SQL Logic Tests (.slt) labels Oct 25, 2023
@jayzhan211

jayzhan211 commented Oct 25, 2023

Copy link
Copy Markdown
ContributorAuthor

Summary of changes

  1. Only accept ListArray for convert_list_array, only accept non-ListArray for convert_non_list
  2. Add nested list test for convert_list_array
  3. extend wrap_into_list for multiple arrays
  4. keep the nested list test for distinct array agg removed, since update_batch only convert-non-list cases.

@jayzhan211
jayzhan211 marked this pull request as ready for review October 25, 2023 12:44
@alamb
alamb marked this pull request as draft October 30, 2023 20:13
@alamb

Copy link
Copy Markdown
Contributor

I think this is not waiting on any more feedback

Comment threaddatafusion/common/src/scalar.rs Outdated
Comment threaddatafusion/common/src/scalar.rs Outdated
@jayzhan211
jayzhan211 marked this pull request as ready for review November 11, 2023 08:01
@jayzhan211
jayzhan211 marked this pull request as draft November 17, 2023 13:37
@jayzhan211
jayzhan211force-pushed the convert-general-arr-to-scalar branch from 542226d to 6b56441CompareNovember 17, 2023 13:43
@jayzhan211

Copy link
Copy Markdown
ContributorAuthor

wait on #8253

@jayzhan211
jayzhan211force-pushed the convert-general-arr-to-scalar branch from d17a682 to 29ac936CompareDecember 9, 2023 12:04
@jayzhan211

Copy link
Copy Markdown
ContributorAuthor

wait on #8439

Signed-off-by: jayzhan211 <jayzhan211@gmail.com>
Signed-off-by: jayzhan211 <jayzhan211@gmail.com>
Signed-off-by: jayzhan211 <jayzhan211@gmail.com>
@jayzhan211
jayzhan211force-pushed the convert-general-arr-to-scalar branch from 29ac936 to aee4effCompareDecember 13, 2023 14:22
Signed-off-by: jayzhan211 <jayzhan211@gmail.com>
@jayzhan211
jayzhan211 marked this pull request as ready for review December 13, 2023 14:26
Signed-off-by: jayzhan211 <jayzhan211@gmail.com>
Comment threaddatafusion/physical-expr/src/aggregate/array_agg_distinct.rs
Signed-off-by: jayzhan211 <jayzhan211@gmail.com>
@jayzhan211

Copy link
Copy Markdown
ContributorAuthor

@alamb I think this is ready to go, thanks

@alambalamb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @jayzhan211 -- this is looking quite close I think

Comment threaddatafusion/common/src/scalar.rs Outdated
/// 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 It might also help to explain why we need two different signatures. It took me a while to grok things too

Suggested change
/// Convert `ListArray` to `Vec<Vec<ScalarValue>>`, first `Vec` is for rows, second `Vec` is for elements in the list
/// 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

Comment on lines +2042 to +2043
/// Retrieve `ScalarValue` for each row in `array`
///

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems left over

Suggested change
/// Retrieve `ScalarValue` for each row in `array`
///

/// ```
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>(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be easier to use if it didn't mix generic and non generic code

Maybe something like

Suggested change
pubfn convert_list_array_to_scalar_vec<O:OffsetSizeTrait>(
pubfn convert_list_array_to_scalar_vec(
array:&dyn Array,
) -> Result<Vec<Vec<Self>>> {
ifletSome(arr) = array.as_list_opt::<i32>{
Self::convert_list_array_to_scalar_vec_internal(arr)
}elseifletSome(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>>> {

Signed-off-by: jayzhan211 <jayzhan211@gmail.com>
@alamb

alamb commented Dec 17, 2023

Copy link
Copy Markdown
Contributor

(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

@jayzhan211

jayzhan211 commented Dec 17, 2023

Copy link
Copy Markdown
ContributorAuthor

(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...

And, this API is actually not breaking the previous one convert_array_to_scalar_vec, we can have three versions of them. convert_array_to_scalar_vec for the place that we can't ensure the type for array.

Anyway, this change is just improvement, not fixing a critical error, so I will close it.

@alamb

Copy link
Copy Markdown
Contributor

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.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

coreCore DataFusion cratephysical-exprChanges to the physical-expr crates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@jayzhan211@alamb@Weijun-H