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 UnionArray::into_parts #5585

Merged
merged 5 commits into from
Apr 9, 2024

Conversation

mbrobbel
Copy link
Contributor

@mbrobbel mbrobbel commented Apr 3, 2024

Rationale for this change

Most arrays have an into_parts method to destruct the array into its constituent parts: https://docs.rs/arrow-array/latest/arrow_array/?search=into_parts

What changes are included in this PR?

This adds that into_parts method to UnionArray.

Are there any user-facing changes?

  • arrow_array::array::UnionArray::into_parts

@github-actions github-actions bot added the arrow Changes to the arrow crate label Apr 3, 2024
Comment on lines 338 to 354
#[allow(clippy::type_complexity)]
pub fn into_parts(
self,
) -> (
DataType,
ScalarBuffer<i8>,
Option<ScalarBuffer<i32>>,
Vec<Option<ArrayRef>>,
) {
let Self {
data_type,
type_ids,
offsets,
fields,
} = self;
(data_type, type_ids, offsets, fields)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems PrimitiveArray is the only other array type that returns a DataType as part of its into_parts()

Maybe just return the fields, akin to how StructArray does it? (Though unsure about the Sparse/Dense enum)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good suggestion. In that case I think we should also return UnionMode (instead of users having to check if the offsets buffer for Some or None to figure that out):

(UnionFields, UnionMode, ScalarBuffer<i8>, Option<ScalarBuffer<i32>>, Vec<Option<ArrayRef>>)

DataType,
ScalarBuffer<i8>,
Option<ScalarBuffer<i32>>,
Vec<Option<ArrayRef>>,
Copy link
Contributor

Choose a reason for hiding this comment

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

This is exposing what is effectively an implementation detail of UnionArray, that it uses a sparse vec to encode type ids. We may want to revisit this in future, e.g. to better handle large type ids

@tustvold
Copy link
Contributor

tustvold commented Apr 4, 2024

I'm honestly a little torn on this as UnionArray is a bit of an oddity for two reasons:

  • Its constructors don't look anything like the other arrays (for historical reasons) and don't match into_parts
  • The internal storage of a UnionArray is somewhat an implementation detail w.r.t. how it encodes type ids

I think it would be good to understand the motivating use-case for this, as AFAICT the output of into_parts wouldn't be useful for reconstructing a UnionArray

@mbrobbel
Copy link
Contributor Author

mbrobbel commented Apr 4, 2024

I think it would be good to understand the motivating use-case for this, as AFAICT the output of into_parts wouldn't be useful for reconstructing a UnionArray

My use-case is zero-copy conversion to a different union array type (test example here).

I'm not using it to reconstruct an arrow_array::array::UnionArray, however the added test in this PR shows how that can be done.

Comment on lines 1302 to 1305
field
.into_iter()
.zip(fields.into_iter().flatten())
.collect(),
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is correct if the type ids in the signature are out of order

assert_eq!(fields.len(), 2);

let result = UnionArray::try_new(
&[0, 1],
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be worth showing this works with type ids of [6, 2] or something

Copy link
Contributor Author

Choose a reason for hiding this comment

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

&[0, 1],
type_ids.into_inner(),
offsets.map(ScalarBuffer::into_inner),
field
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this flattening should probably be an implementation detail of into_parts

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe the fields argument of into_parts could be changed to impl Iterator<Item = (i8, ArrayRef)> to hide implementation details?

Copy link
Contributor

@tustvold tustvold Apr 4, 2024

Choose a reason for hiding this comment

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

I think it should probably reconstruct the arrays in the order expected by the constructor and return these as a Vec

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. The constructor expects the order (of the child arrays) to match the provided fields ids, however, if unknown, one could get these type_ids from the UnionFields iterator, which has no order guarantee:

/// Returns an iterator over the fields and type ids in this [`UnionFields`]
///
/// Note: the iteration order is not guaranteed
pub fn iter(&self) -> impl Iterator<Item = (i8, &FieldRef)> + '_ {
self.0.iter().map(|(id, f)| (*id, f))
}

@mbrobbel mbrobbel requested a review from tustvold April 8, 2024 11:17
@tustvold
Copy link
Contributor

tustvold commented Apr 9, 2024

I took the liberty of removing UnionMode as it is implied by the presence or abscence of the offsets

I also filed #5613 to track cleaning up these constructors

@tustvold tustvold merged commit c203785 into apache:master Apr 9, 2024
25 checks passed
@mbrobbel mbrobbel deleted the arrow-array/union/into_parts branch April 9, 2024 10:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrow Changes to the arrow crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants