Skip to content

[Variant] add variant_to_arrow union builder - #10313

Merged
Jefffrey merged 12 commits into
apache:mainfrom
sdf-jkl:variant-to-union
Aug 16, 2026
Merged

[Variant] add variant_to_arrow union builder#10313
Jefffrey merged 12 commits into
apache:mainfrom
sdf-jkl:variant-to-union

Conversation

@sdf-jkl

@sdf-jkl sdf-jkl commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

The last type to be supported by variant_to_arrow cast.

Rationale for this change

Check issue

What changes are included in this PR?

  • Add variant_to_arrow union cast support + unit tests

Are these changes tested?

  • Yes, unit tests

Are there any user-facing changes?

  • Users can now cast Variant to Union type

Casting Variant to dense or sparse Union dispatches each value to the
union field that most exactly represents its runtime type (lossless
widening allowed, declaration order breaks ties). Null rows land in a
Null-typed child if declared, otherwise the first child, since unions
have no top-level null buffer.

Co-Authored-By: Claude Fable 5 <[email protected]>
@github-actions github-actions Bot added the parquet-variant parquet-variant* crates label Jul 9, 2026
@sdf-jkl sdf-jkl changed the title add variant_to_arrow union builder [Variant] add variant_to_arrow union builder Jul 9, 2026
@sdf-jkl
sdf-jkl marked this pull request as ready for review July 20, 2026 22:12
@Jefffrey Jefffrey added the enhancement Any new improvement worthy of a entry in the changelog label Aug 5, 2026

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

disclaimer: this is kinda my first time looking at variant code 😅

Comment thread parquet-variant-compute/src/variant_get.rs
let null_child = fields
.iter()
.position(|(_, field)| field.data_type() == &DataType::Null)
.unwrap_or(0);

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.

do we need to consider nullability of the field?

@sdf-jkl sdf-jkl Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I checked the existing Arrow union policy. UnionBuilder::append_null intentionally writes nulls into child arrays, while UnionBuilder::build still creates those child Fields with nullable = false. UnionArray::try_new does not validate field nullability, and union nullness is derived from the selected child array. Therefore the fallback does not need to select a nullable-declared field.

let mut children = Vec::with_capacity(self.fields.len());
let union_fields = self
.fields
.into_iter()
.map(
|(
name,
FieldData {
type_id,
data_type,
mut values_buffer,
slots,
mut null_buffer_builder,
},
)| {
let array_ref = make_array(unsafe {
ArrayDataBuilder::new(data_type.clone())
.add_buffer(values_buffer.finish())
.len(slots)
.nulls(null_buffer_builder.finish())
.build_unchecked()
});
children.push(array_ref);
(type_id, Arc::new(Field::new(name, data_type, false)))
},
)
.collect();
UnionArray::try_new(
union_fields,
self.type_id_builder.into(),
self.value_offset_builder.map(Into::into),
children,
)

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.

the builder thing is a known issue:

i guess its one of those things which is a bit messy in the codebase as is 🤔

Comment thread parquet-variant-compute/src/variant_to_arrow.rs
@sdf-jkl

sdf-jkl commented Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @Jefffrey 🙏

This is not the most Varianty code, mostly about understanding the Union type

@sdf-jkl

sdf-jkl commented Aug 13, 2026

Copy link
Copy Markdown
Contributor Author

@klion26 please you take a look 🙏

@Jefffrey
Jefffrey merged commit ae4becb into apache:main Aug 16, 2026
19 checks passed
@Jefffrey

Copy link
Copy Markdown
Contributor

thanks @sdf-jkl

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

Labels

enhancement Any new improvement worthy of a entry in the changelog parquet-variant parquet-variant* crates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Variant] variant_to_arrow types support

2 participants