Skip to content

Add ColumnarValue::values_to_arrays, deprecate columnar_values_to_array - #9114

Merged
alamb merged 3 commits into
apache:mainfrom
alamb:alamb/values_to_arrays
Feb 5, 2024
Merged

Add ColumnarValue::values_to_arrays, deprecate columnar_values_to_array#9114
alamb merged 3 commits into
apache:mainfrom
alamb:alamb/values_to_arrays

Conversation

@alamb

@alambalamb commented Feb 2, 2024

Copy link
Copy Markdown
Contributor

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-actionsgithub-actionsBot added logical-expr Logical plan and expressions physical-expr Changes to the physical-expr crates 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
Copy Markdown
ContributorAuthor

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
Copy Markdown
ContributorAuthor

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
Copy Markdown
ContributorAuthor

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
Copy Markdown
Contributor

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 viiryaFebruary 2, 2024 21:52
Comment threaddatafusion/expr/src/columnar_value.rs Outdated
Comment threaddatafusion/expr/src/columnar_value.rs Outdated

@viiryaviirya 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.

Thank you @alamb. Looks good to me.

Co-authored-by: Liang-Chi Hsieh <viirya@gmail.com>
@alambalamb added the api change Changes the API exposed to users of the crate label Feb 3, 2024
@alamb
alamb merged commit dfb6435 into apache:mainFeb 5, 2024
@alamb

alamb commented Feb 5, 2024

Copy link
Copy Markdown
ContributorAuthor

Thanks again for the review @viirya

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

Labels

api changeChanges the API exposed to users of the cratelogical-exprLogical plan and expressionsoptimizerOptimizer rulesphysical-exprChanges to the physical-expr crates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@alamb@Omega359@viirya