Skip to content

fix(arrow-select): preserve nullif nulls for REE and Union - #10991

Open
yongster wants to merge 3 commits into
apache:mainfrom
yongster:fix/nullif-ree-union
Open

yongster wants to merge 3 commits into
apache:mainfrom
yongster:fix/nullif-ree-union

Conversation

@yongster

@yongster yongster commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

nullif creates nulls by adding a top-level validity bitmap. This works for
most arrays, but RunEndEncoded and Union arrays derive logical nullability from
their child arrays instead.

As a result, nullif previously returned non-null values at positions where
the condition was true for these array types.

Closes #10990

  • Delegate nullif for RunEndEncoded and Union arrays to take, using null
    indices for the positions selected by the condition.
  • Return an error when a RunEndEncoded values field is non-nullable and cannot
    represent the requested nulls.
  • Return an error when a Union has no nullable child that can represent null
    indices.
  • Prefer a nullable Union child when take needs to represent a null index.
  • Add coverage for nullable and non-nullable REE, plus dense and sparse Union
    cases.

@github-actions github-actions Bot added arrow Changes to the arrow crate arrow-select labels Sep 4, 2026
@Jefffrey Jefffrey added the bug label Sep 5, 2026
Comment thread arrow-select/src/nullif.rs Outdated
return Ok(make_array(left_data));
match left.data_type() {
DataType::RunEndEncoded(_, values) => {
if !values.is_nullable() && right.iter().any(|value| value == Some(true)) {

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.

Suggested change
if !values.is_nullable() && right.iter().any(|value| value == Some(true)) {
if !values.is_nullable() && right.has_true() {

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 9ec63d5. The should-null bitmap is computed once (right.values() & validity), then this check uses BooleanBuffer::has_true() on that mask.

Comment thread arrow-select/src/nullif.rs Outdated
}

/// Applies `nullif` to arrays that represent logical nulls in their children.
fn nullif_take(left: &dyn Array, right: &BooleanArray) -> Result<ArrayRef, ArrowError> {

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.

perhaps we can reuse the computation we do here:

// left=0 (null) right=null output bitmap=null
// left=0 right=1 output bitmap=null
// left=1 (set) right=null output bitmap=set (passthrough)
// left=1 right=1 & comp=true output bitmap=null
// left=1 right=1 & comp=false output bitmap=set
//
// Thus: result = left null bitmap & (!right_values | !right_bitmap)
// OR left null bitmap & !(right_values & right_bitmap)
// Compute right_values & right_bitmap
let right = match right.nulls() {
Some(nulls) => right.values() & nulls.inner(),
None => right.values().clone(),
};

instead of needing to check validity in this iterator

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. should_null is now computed once and reused by both the generic nullif path and nullif_take, so we no longer inspect is_valid/value per index.

Comment thread arrow-select/src/take.rs

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.

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.

Yes — the take.rs changes here are a subset of #10994. I'll wait for that to land, then rebase this PR so it only contains the nullif.rs changes.

Compute the should-null bitmap once and reuse it for both the generic
nullif path and the take-based REE/Union path. This avoids iterating
BooleanArray validity in nullif_take and uses BooleanBuffer::has_true
for the non-nullable REE check.
@yongster

yongster commented Sep 5, 2026

Copy link
Copy Markdown
Contributor Author

@Jefffrey on the relationship with #10994:

This PR's nullif path for RunEndEncoded and Union delegates to take with null indices. That only produces correct logical nulls if take itself:

  • prefers a nullable Union child for a null index
  • errors when a REE values field is non-nullable
  • errors when a Sparse Union has non-nullable fields

Those take fixes live in #10994. The small take.rs diff here is a subset of that PR, included so this branch still works against current main.

I'd like to wait for #10994 to land, then rebase this PR so it only contains the nullif.rs changes.

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 Changes to the arrow crate arrow-select bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

nullif does not introduce nulls for RunEndEncoded and Union arrays

2 participants