Skip to content

feat: column support for array_append, array_prepend, array_position and array_positions - #6805

Merged
alamb merged 5 commits into
apache:mainfrom
izveigor:array_functions_with_columns
Jul 5, 2023
Merged

feat: column support for array_append, array_prepend, array_position and array_positions#6805
alamb merged 5 commits into
apache:mainfrom
izveigor:array_functions_with_columns

Conversation

@izveigor

@izveigorizveigor commented Jun 30, 2023

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Part of #6804

Rationale for this change

This is the first PR for solving the column issue.

What changes are included in this PR?

Four array functions now start to support column data:
array_append,
array_prepend,
array_position,
array_positions

Are these changes tested?

Yes

Are there any user-facing changes?

Yes

@izveigor
izveigor marked this pull request as draft June 30, 2023 09:22
@github-actionsgithub-actionsBot added core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) labels Jun 30, 2023
@izveigorizveigor changed the title feat: column support for array functionsfeat: column support for array_append, array_prepend, array_position and array_positionsJul 3, 2023
@github-actionsgithub-actionsBot added logical-expr Logical plan and expressions physical-expr Changes to the physical-expr crates labels Jul 3, 2023
}
None => {
return Err(DataFusionError::Internal(
"initial position must not be null".to_string(),

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.

PostgreSQL does not support NULL like the start position. Should we do that or is it better to translate NULL to0?

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 following postgres seems reasonable to me

Some(arr) => {
let child_array = downcast_arg!(arr, $ARRAY_TYPE);
values = downcast_arg!(
compute::concat(&[

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.

arrow-rs does not support into_builder method for GenericListArray. Is the concat method better than builder. Or is there a great third option? 🤔

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.

When concatenating two arrays I don't think there is any difference.

however, you can see in the concat implementation it uses MutableArrayData: https://docs.rs/arrow-select/43.0.0/src/arrow_select/concat.rs.html#86-91 -- maybe that is an approach to pursue?

@tustvold do you know of any reason we can't add a info_builder method for GenericListArray? If not, I'll file a ticket request and see if anyone in the community wants to try.

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.

There is no major reason why we couldn't support into_builder for ListArray, however, I should highlight this only works if there are no other references. I've not read the rest of the PR but this may not be something we can rely on here. MutableArrayData seems like the best option here if there is no selection kernel (concat, take, interleave) that suits

----
[] [] [[], []]

# make_array with columns #1

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.

All outputs match PostgreSQL engine results.
See the examples: #6804

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.

We can automatically ensure they stay in sync by using the postgres compatability mode if you wanted: https://github.com/apache/arrow-datafusion/tree/main/datafusion/core/tests/sqllogictests#running-tests-postgres-compatibility

@izveigor
izveigor marked this pull request as ready for review July 3, 2023 13:20
@izveigor

Copy link
Copy Markdown
ContributorAuthor

Ready for the review @alamb
Problem solution will be divided into several independent PR so as not to cause the same problem that you mentioned in #6384

@alamb

alamb commented Jul 3, 2023

Copy link
Copy Markdown
Contributor

This is on my list to review, but I might not have time for it until later in the week (Wednesday) -- I am somewhat occupied with #6800 at the moment

@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 @izveigor -- I went through this PR and it all looks great. I had a few minor comments but nothing I think should stop it from merging

----
[] [] [[], []]

# make_array with columns #1

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.

We can automatically ensure they stay in sync by using the postgres compatability mode if you wanted: https://github.com/apache/arrow-datafusion/tree/main/datafusion/core/tests/sqllogictests#running-tests-postgres-compatibility

BuiltinScalarFunction::ArrayPosition => Ok(UInt64),
BuiltinScalarFunction::ArrayPositions => {
Ok(List(Arc::new(Field::new("item", UInt8, true))))
Ok(List(Arc::new(Field::new("item", UInt64, true))))

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.

👍

Some(arr) => {
let child_array = downcast_arg!(arr, $ARRAY_TYPE);
values = downcast_arg!(
compute::concat(&[

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.

When concatenating two arrays I don't think there is any difference.

however, you can see in the concat implementation it uses MutableArrayData: https://docs.rs/arrow-select/43.0.0/src/arrow_select/concat.rs.html#86-91 -- maybe that is an approach to pursue?

@tustvold do you know of any reason we can't add a info_builder method for GenericListArray? If not, I'll file a ticket request and see if anyone in the community wants to try.

}
None => {
return Err(DataFusionError::Internal(
"initial position must not be null".to_string(),

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 following postgres seems reasonable to me

}
}
None => {
return Err(DataFusionError::Internal(

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 error can be generated by bad user input (and not a bug in DataFusion) so I don't think it should be an internal error:

Suggested change
returnErr(DataFusionError::Internal(
returnErr(DataFusionError::Execution(

@alamb
alamb merged commit 9a27d84 into apache:mainJul 5, 2023
@alamb

alamb commented Jul 5, 2023

Copy link
Copy Markdown
Contributor

Thanks @izveigor

@izveigorizveigor mentioned this pull request Jul 6, 2023
11 tasks
alamb pushed a commit to alamb/datafusion that referenced this pull request Jul 6, 2023
…tion` and `array_positions` (apache#6805)
* test: sqllogictests with columns for array_append, array_prepend, array_position and array_positions
* feat: column support for array_append and array_prepend
* feat: column support for array_position and array_positions
* fix: error type
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

coreCore DataFusion cratelogical-exprLogical plan and expressionsphysical-exprChanges to the physical-expr cratessqllogictestSQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@izveigor@alamb@tustvold