Skip to content

[Variant] When possible, cast perfectly shredded children - #9862

Closed
AdamGS wants to merge 3 commits into
apache:mainfrom
AdamGS:adamg/perfect-shredding-with-cast
Closed

[Variant] When possible, cast perfectly shredded children#9862
AdamGS wants to merge 3 commits into
apache:mainfrom
AdamGS:adamg/perfect-shredding-with-cast

Conversation

@AdamGS

@AdamGS AdamGS commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

For cases we can just cast the perfectly shredded array, this is a significant performance boost, we do that only for a subset of types where arrow's casting matches the existing cast behavior in parquet-variant-compute.

One issue here is that the casting behavior is slightly different, but that seems aligned with #8982 and other work.

What changes are included in this PR?

If an array is perfectly shredded AND can be cast according to the existing cast semantics, we cast it instead of falling back to the row builder.

Are these changes tested?

In addition to existing tests, added an additional test to make sure it works.

Are there any user-facing changes?

None.

@AdamGS
AdamGS force-pushed the adamg/perfect-shredding-with-cast branch from 2de441e to 23b785d Compare April 30, 2026 15:44
@github-actions github-actions Bot added the parquet-variant parquet-variant* crates label Apr 30, 2026
@AdamGS AdamGS changed the title [Variant] When possible, cast perfectly shredded children if [Variant] When possible, cast perfectly shredded children when possible Apr 30, 2026
@AdamGS
AdamGS force-pushed the adamg/perfect-shredding-with-cast branch from 23b785d to e154a22 Compare April 30, 2026 15:55
@AdamGS

AdamGS commented Apr 30, 2026

Copy link
Copy Markdown
Contributor Author

Ok seems like some tests are not fully deterministic and I missed that.

@AdamGS
AdamGS force-pushed the adamg/perfect-shredding-with-cast branch from e154a22 to fb87e3d Compare May 11, 2026 15:55
@AdamGS AdamGS changed the title [Variant] When possible, cast perfectly shredded children when possible [Variant] When possible, cast perfectly shredded children May 15, 2026
@AdamGS
AdamGS force-pushed the adamg/perfect-shredding-with-cast branch 3 times, most recently from 11538b5 to e01c2da Compare May 19, 2026 11:32
}

None
match from_type {

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.

Seems this overlaps with can_cast_types? can we simplify this

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.

yeah there's definitely overlap and a very similar structure, but I think we have to do this while the cast behaviors don't match.

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.

another options is just allow everything that arrow supports in the perfect cast case, and work to align the other cases enumerated in #8982 (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.

As we will align the logic between variant and arrow-cast, does this mean we can only keep the logic can_cast_types above? If can_cast_types returns tru,e we'll always try to do the cast


#[test]
fn test_perfect_shredding_list_cast_gate_uses_variant_element_semantics() {
let int64_item = Arc::new(Field::new("item", DataType::Int64, true));

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.

Do we need to add more tests to cover the logic of can_use_perfect_shredding_arrow_cast here?

variant_array: &VariantArray,
as_field: &Field,
cast_options: &CastOptions,
) -> Result<Option<ArrayRef>> {

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.

Why do we need the Result in the return value? Is it ok to use Option<arrayRef> here?

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.

there's a couple of thins here that do return a Result, I'll push a fix here soon

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.

Here we still keep the Result, 1) seems we can replace with Option<ArrayRef> if we don't change the code in L284; 2) the code here changed the logic because if we throw an error here, then in L284 we will return directly(a minimal rust-play)


// Use Arrow's vectorized cast when it cleanly matches the shredded representation. If not,
// fall back to row-wise extraction to preserve the existing variant-specific semantics.
Ok(cast_with_options(target_array.as_ref(), as_field.data_type(), cast_options).ok())

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.

Does this mean that cast_with_options throws some error if the return value here is None? do we need to add some tests to cover this

AdamGS added 2 commits May 26, 2026 14:05
Signed-off-by: Adam Gutglick <adam@spiraldb.com>
Signed-off-by: Adam Gutglick <adam@spiraldb.com>
@AdamGS
AdamGS force-pushed the adamg/perfect-shredding-with-cast branch from e01c2da to a8279d4 Compare May 26, 2026 13:29
@AdamGS

AdamGS commented Jun 16, 2026

Copy link
Copy Markdown
Contributor Author

I think its much better to just get to the point we align the casting behaviors instead of trying to pick and choose, so I'm closing this PR. Once we have full alignment there's a small subset for perfect shredding, happy to either re-open this one or open another one when we reach that point.

@AdamGS AdamGS closed this Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

parquet-variant parquet-variant* crates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants