Skip to content

fix(arrow-cast): preserve null rows when casting size-1 FixedSizeList - #12533

Open
ApacheWang wants to merge 3 commits into
apache:mainfrom
ApacheWang:fix/single-element-fsl-nulls
Open

ApacheWang wants to merge 3 commits into
apache:mainfrom
ApacheWang:fix/single-element-fsl-nulls

Conversation

@ApacheWang

Copy link
Copy Markdown

Which issue does this PR close?

  • None (discovered directly during code review; no existing open issue).

Rationale for this change

Casting a size-1 FixedSizeListArray to its child (or target) type via cast_single_element_fixed_size_list_to_values currently delegates directly to cast_with_options(array.values(), to, cast_options) without consulting array.nulls().

As a result:

  • Null list rows silently expose their masked underlying child values as non-null entries in the output array (e.g. a null row whose underlying child holds 99 becomes Some(99) instead of None).
  • Under CastOptions { safe: false, .. }, invalid masked child values in null rows (such as "invalid" when casting to Int32) cause the cast to fail even though those rows are null.

What changes are included in this PR?

  • Update cast_single_element_fixed_size_list_to_values in arrow-cast/src/cast/list.rs so that when array.nulls() has at least one null:
    1. Filter array.values() to only valid parent rows before calling cast_with_options, preventing masked child values from failing strict casts.
    2. Scatter the cast values back to the original length with take using nullable indices derived from array.nulls(), preserving null rows while keeping the zero-copy fast path when null_count() == 0.
  • Add regression tests in arrow-cast/tests/cast/list.rs (test_cast_single_element_fixed_size_list_preserves_nulls).

Are these changes tested?

Yes. Added test_cast_single_element_fixed_size_list_preserves_nulls in arrow-cast/tests/cast/list.rs, covering:

  • Sliced FixedSizeList<Utf8, 1> cast to Int32 under both safe: true and safe: false with masked numeric and invalid child values alongside inner nulls.
  • Same-type FixedSizeList<Int32, 1> unwrap to Int32 with masked values under null rows.
  • All-null FixedSizeList<Int32, 1> cast to Int32.
  • FixedSizeList<Struct, 1> with a non-nullable struct child field under a null list row.
  • Casting through a Dictionary target type (safe: false).

Are there any significant user-facing changes?

No (bug fix only; null rows in size-1 FixedSizeList casts now remain null instead of exposing masked child data).

AI usage disclosure

AI assistance (Codex) helped identify the bug and draft the initial fix and test; I reviewed the edge cases (sliced buffers, all-null inputs, non-nullable struct children, dictionary targets), refined the tests, and verified the full arrow-cast test suite, rustfmt, and clippy locally.

Signed-off-by: bc15575202581 <bc15575202581@gmail.com>
@boring-cyborg boring-cyborg Bot added the arrow-kernels Crates that transform arrow data (e.g. compute, cast) label Dec 2, 2026
Comment thread arrow-cast/src/cast/list.rs Outdated

// Skip masked child values so a strict cast cannot fail on a null row.
let valid = BooleanArray::new(nulls.inner().clone(), None);
let values = filter(values.as_ref(), &valid)?;

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 probably should gate this filter to only execute for strict casts

.iter()
.map(|valid| if valid { valid_indices.next() } else { None })
.collect();
take(values.as_ref(), &indices, None)

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 wonder if there is a better way to do this null masking; especially for the strict = false case, we could probably just operate directly on the null buffers. there is a caveat that some array types wont have a null buffer (run array & union array) so probably can keep this type of fallback for them

Signed-off-by: bc15575202581 <bc15575202581@gmail.com>
@ApacheWang
ApacheWang force-pushed the fix/single-element-fsl-nulls branch from 94d3c0f to 8a8d135 Compare December 3, 2026 05:11
@ApacheWang

Copy link
Copy Markdown
Author

Good suggestions, thanks @Jefffrey! Updated in 8a8d135:

  • Gated the filter + cast + take path to strict casts where a conversion actually happens (!cast_options.safe && values.data_type() != to).
  • For safe casts and same-type unwraps, cast directly and apply parent nulls via apply_null_mask:
    • DataType::Null: return as-is without attaching a null buffer.
    • DataType::RunEndEncoded(_, _) | DataType::Union(_, _): fall back to take with null indices.
    • All other types: combine directly with NullBuffer::union(Some(nulls), values.nulls()) and ArrayDataBuilder::nulls(...) without copying element buffers.
  • Expanded test_cast_single_element_fixed_size_list_preserves_nulls to cover sliced same-type unwraps, safe and strict dictionary casts, NullArray unwraps, and strict cast failures on invalid values in valid rows.

@Ap-95671 Ap-95671 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I reproduced a safe-cast regression at 8a8d135 and verified that the same case passes on 071e3a1. The inline comment describes the masked child case and a tested fallback. AI-assisted review with OpenAI Codex.

Comment thread arrow-cast/src/cast/list.rs Outdated
return take(values.as_ref(), &indices, None);
}

let values = cast_with_options(values, to, cast_options)?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

A safe cast can return an error here before apply_null_mask runs. I tested a size-one FixedSizeList<Struct<number: Utf8 not null>> with child strings ["2", "invalid"] and parent validity [true, false]. Casting to Struct<number: Int32 not null> should return [{number: 2}, null], but 8a8d135 reports an unmasked null in number. The first PR revision passes the same test because it filters the null list row first. Could the direct path fall back to the existing filter/cast/take path on error? That variant passed the arrow-cast suite with the regression case.

Signed-off-by: bc15575202581 <bc15575202581@gmail.com>
@ApacheWang

Copy link
Copy Markdown
Author

Good catch, thanks for reproducing that case, @Ap-95671! Updated in f6c93c2:

  • Kept the direct cast_with_options + apply_null_mask fast path for safe = true, and fall back to the filter + cast_with_options + take helper when cast_with_options returns an error on a different target type (such as an invalid masked child inside a non-nullable Struct field).
  • Added regression tests covering FixedSizeList<Struct<number: Utf8 not null>> -> Struct<number: Int32 not null> with masked invalid rows (both safe = true and safe = false), all-null masked invalid rows, and verifying that an invalid non-nullable field in a valid row still errors.

This branch has not been deployed

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

Labels

arrow-kernels Crates that transform arrow data (e.g. compute, cast)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants