Skip to content
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

Add ColumnarValue::values_to_arrays, deprecate columnar_values_to_array #9114

Merged
merged 3 commits into from
Feb 5, 2024

Conversation

alamb
Copy link
Contributor

@alamb alamb commented Feb 2, 2024

Which issue does this PR close?

Part of #8045

Rationale for this change

While working on implementing ArrayToString in #9113 I found that many of the array functions support arbitrary combinations of arguments that are ColumnarValue to Array by converting all the arguments to arrays and then calling a function that works on arrays.

I think this is a common desire when implementing functions, but it is not always clear how to do this.

For example I think @Omega359 had to implement something similar in to_date in #9040

@viirya added a helper in #8962 which improves the situation, but I think this function may be hard to find (it is in datafusion_physical_expr) and it doesn't handle the case where the arguments are a mix of ColumnarValue::Scalar and ColumnarValue::Array

What changes are included in this PR?

  1. Move logic to ColumnarValue::values_to_arrays
  2. Support mixed Scalar + ColumnarValues
  3. Add documentation
  4. Add unit tests
  5. Deprecate the old function columnar_values_to_array

Are these changes tested?

Yes, new tests

Are there any user-facing changes?

New function, better docs and tests

@github-actions github-actions bot added logical-expr Logical plan and expressions physical-expr Physical Expressions optimizer Optimizer rules labels Feb 2, 2024
let args = columnar_values_to_array(args)?;
// Expand the arguments to arrays (this is simple, but inefficient for
// single constant values).
let args = ColumnarValue::values_to_arrays(args)?;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the basic change -- make the logic a function of ColumnarValue

/// # Errors
///
/// If there are multiple array arguments that have different lengths
pub fn values_to_arrays(args: &[ColumnarValue]) -> Result<Vec<ArrayRef>> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a different algorithm than columnar_values_to_array as it also handles mixed ScalarValue and ArrayRefs

use super::*;

#[test]
fn values_to_arrays() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

new tests

@alamb alamb marked this pull request as ready for review February 2, 2024 15:57
@Omega359
Copy link
Contributor

Omega359 commented Feb 2, 2024

In the make_date code I was attempting to not actually expand the scalar into a full array but rather use in place. It was one of the reasons why I was asking about a SingleValueArray as with something like that we could have the best of both - easy processing and no data duplication. I feel though that would likely be a lot more work than it seems it would be otherwise

@alamb alamb requested a review from viirya February 2, 2024 21:52
Copy link
Member

@viirya viirya left a comment

Choose a reason for hiding this comment

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

Thank you @alamb. Looks good to me.

Co-authored-by: Liang-Chi Hsieh <viirya@gmail.com>
@alamb alamb added the api change Changes the API exposed to users of the crate label Feb 3, 2024
@alamb alamb merged commit dfb6435 into apache:main Feb 5, 2024
22 checks passed
@alamb
Copy link
Contributor Author

alamb commented Feb 5, 2024

Thanks again for the review @viirya

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api change Changes the API exposed to users of the crate logical-expr Logical plan and expressions optimizer Optimizer rules physical-expr Physical Expressions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants