Skip to content

fix(parquet-variant-compute): reject unsupported types in empty and all-null cast_to_variant input - #12246

Merged
sdf-jkl merged 3 commits into
apache:mainfrom
dayan02dev:fix/11946
Nov 16, 2026
Merged

sdf-jkl merged 3 commits into
apache:mainfrom
dayan02dev:fix/11946

Conversation

@dayan02dev

@dayan02dev dayan02dev commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

cast_to_variant returned Ok for duration/interval types (directly or nested) when the input was empty or all null, and the unsupported-type error as soon as one row held a value. There are two causes:

  1. The all-null fast path in cast_to_variant_with_options returned before make_arrow_to_variant_row_builder, which is the only place types are checked.
  2. UnionArrowToVariantBuilder::new looped over the per-row type_ids() buffer, so it only built (and type-checked) the children some row selects, and rebuilt that child's builder once per row.

Moving the type check before the shortcut, as the issue suggests, fixes the first cause but not the second: an empty union with a duration child, or one whose rows never select that child, is still accepted. So the union loop also changes.

What changes are included in this PR?

  • Build the row builder before the all-null fast path, so the existing recursive type check runs first. Supported types still take the bulk append_nulls fast path.
  • Create one union child builder per declared field (union_array.fields().iter()) instead of per row. This matches the comment above the loop and the behaviour before [Variant] Implement row builders for cast_to_variant #8299.

#12211 also edits the body of the same union loop, so one of the two will need a small rebase.

Are these changes tested?

Yes. test_cast_to_variant_duration_or_interval_errors now also checks empty and all-null arrays of each of the seven duration/interval types, directly and inside a list, a struct and a sparse union, plus supported controls. Without the fix it fails with List(Duration(s)) with 0 rows; with only the fast-path reorder it still fails on the empty union case.

Are there any significant user-facing changes?

  • Empty or all-null arrays of unsupported types (including nested ones), and unions that declare an unsupported child, now return the same InvalidArgumentError as populated arrays instead of Ok. No signatures change.
  • All-null map and dictionary inputs now pay builder construction (for maps, casting the keys to Utf8; for dictionaries, normalising the keys). Because a map slice's keys are the parent's whole entries array, an all-null 1000-row slice of a 1M-entry map goes from ~0.01 ms to ~17 ms, the same as a populated slice of that map already costs. Other all-null types show no measurable change.
  • Valid unions convert ~1.8× faster, since child builders are no longer rebuilt per row.

If you'd rather keep the fast path cost-free for maps, the alternatives are narrowing the map key cast to the offsets range, or a DataType-only recursive check before the shortcut.

AI usage disclosure

The patch, test, verification and the benchmark numbers above were produced with an AI coding agent (Claude Code), and a separate agent session re-checked them before submission.

@boring-cyborg boring-cyborg Bot added the parquet-variant parquet-variant* crates label Nov 13, 2026

@sdf-jkl sdf-jkl 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.

Thanks @dayan02dev this is great! Could you please address the suggestions below 🙏

input: &dyn Array,
options: &CastOptions,
) -> Result<VariantArray, ArrowError> {
// Create row builder for the input array 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.

Could this say why it has to come before the fast path, so it doesn't get moved back for performance later?

Suggested change
// Create row builder for the input array type
// Build before the all-null fast path so unsupported types fail regardless of contents.

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.

Done in e1f4642, using your wording.

}

#[test]
fn test_cast_to_variant_duration_or_interval_errors_empty_or_all_null() {

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.

This test repeats the type list and error assertion of test_cast_to_variant_duration_or_interval_errors. That test only missed the bug because all its arrays contain a value. I'd fold the new cases into it, generating the empty, all-null, list, struct and union cases from each array's data_type(), so there is one list of unsupported types

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.

Done in e1f4642. test_cast_to_variant_duration_or_interval_errors now builds the empty and all-null cases from each array's data_type(): as the type itself, as a list element, as a struct field, and as a sparse union child. So the seven types are listed once. The is_err assertion now names the generated type and the row count. The supported-type controls (Int64, List<Int64>, and a union of Int32 and Utf8) sit at the end of the same test. The test still fails without the fix (on List(Duration(s)) with 0 rows), and with only the reorder (on the empty union case).

…all-null cases into the duration/interval test
@Rich-T-kid Rich-T-kid added the bug Squash those bugs label Nov 16, 2026

@sdf-jkl sdf-jkl 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.

Thanks @dayan02dev this is a very clean fix

@sdf-jkl
sdf-jkl added this pull request to the merge queue Nov 16, 2026
Merged via the queue into apache:main with commit 7a1e79d Nov 16, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Squash those bugs parquet-variant parquet-variant* crates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Variant] cast_to_variant accepts unsupported types for empty and all-null arrays

3 participants