Skip to content

GH-50338: [C++] Add ComputeLogicalNullCount to Datum - #50347

Merged
pitrou merged 3 commits into
apache:mainfrom
goel-skd:gh-50338-datum-compute-logical-null-count
Jul 29, 2026
Merged

GH-50338: [C++] Add ComputeLogicalNullCount to Datum#50347
pitrou merged 3 commits into
apache:mainfrom
goel-skd:gh-50338-datum-compute-logical-null-count

Conversation

@goel-skd

@goel-skd goel-skd commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Rationale for this change

Datum::null_count() only counts physical nulls, so it gives the wrong answer for union, run-end encoded and dictionary data. Array, ArrayData, ArraySpan and ChunkedArray already have ComputeLogicalNullCount() for this, but Datum doesn't.

What changes are included in this PR?

  • Datum::ComputeLogicalNullCount(), which delegates to the existing ArrayData/ChunkedArray implementations for array-like data.
  • Scalar::IsLogicalNull(), which is !is_valid for most types. DictionaryScalar overrides it because a valid index can still refer to a null dictionary value. Ill-formed scalars (e.g. an out-of-bounds index) are not handled defensively — the result is undefined for them, and validation rejects them. Like the array path, it doesn't recurse into nested values.

Are these changes tested?

Yes, in datum_test.cc and scalar_test.cc: union (sparse and dense), run-end encoded, dictionary and chunked inputs, scalars obtained from Array::GetScalar(), and a check that the scalar and array paths agree element by element.

Are there any user-facing changes?

The two new APIs above. The new virtual on Scalar changes the C++ ABI, so this shouldn't be backported to a patch release.

@goel-skd
goel-skd requested a review from pitrou as a code owner July 2, 2026 22:09
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

⚠️ GitHub issue #50338 has been automatically assigned in GitHub to PR creator.

@goel-skd
goel-skd force-pushed the gh-50338-datum-compute-logical-null-count branch from 2e82f92 to 30f92df Compare July 2, 2026 22:35
Comment thread cpp/src/arrow/datum.cc Outdated
Comment thread cpp/src/arrow/datum.h
Comment thread cpp/src/arrow/datum_test.cc Outdated
Comment thread cpp/src/arrow/datum_test.cc
@github-actions github-actions Bot added awaiting committer review Awaiting committer review and removed awaiting review Awaiting review labels Jul 7, 2026
Comment thread cpp/src/arrow/scalar.cc
@goel-skd
goel-skd force-pushed the gh-50338-datum-compute-logical-null-count branch from f6dbdc7 to ecb27c9 Compare July 8, 2026 14:47
@goel-skd

goel-skd commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

@pitrou - the s3fs test times out. Seems unrelated to this change and an CI infra red herring!

@goel-skd
goel-skd requested a review from pitrou July 15, 2026 04:25
@goel-skd

Copy link
Copy Markdown
Contributor Author

@pitrou - This is ready to be reviewed. Thanks.

@pitrou pitrou 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 for the update! Here a couple more comments.

Comment thread cpp/src/arrow/scalar.cc
Comment thread cpp/src/arrow/scalar.cc Outdated
Comment thread cpp/src/arrow/scalar.cc Outdated
return false;
}
auto index_value = DictionaryIndexValue(*value.index);
if (!index_value.ok() || *index_value < 0 || *index_value >= dict->length()) {

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.

Same here: not sure it's worth checking for these invalid cases, and it may actually be better to let them crash rather than return a seemingly valid value.

Comment thread cpp/src/arrow/datum_test.cc
goel-skd added 2 commits July 24, 2026 14:54
Datum::null_count() does not account for types that carry logical
nulls without a validity bitmap (union, dictionary and run-end
encoded types). Add Datum::ComputeLogicalNullCount(), delegating to
ArrayData::ComputeLogicalNullCount() and
ChunkedArray::ComputeLogicalNullCount() for array-like data; for
scalars, is_valid already reflects logical validity.
@goel-skd
goel-skd force-pushed the gh-50338-datum-compute-logical-null-count branch from ecb27c9 to ec4e1fa Compare July 29, 2026 04:15
@goel-skd
goel-skd requested a review from pitrou July 29, 2026 04:19
@pitrou
pitrou merged commit e1dfbef into apache:main Jul 29, 2026
57 of 61 checks passed
@pitrou pitrou removed the awaiting committer review Awaiting committer review label Jul 29, 2026
@pitrou

pitrou commented Jul 29, 2026

Copy link
Copy Markdown
Member

Thank you @goel-skd !

@conbench-apache-arrow

Copy link
Copy Markdown

After merging your PR, Conbench analyzed the 4 benchmarking runs that have been run so far on merge-commit e1dfbef.

There were no benchmark performance regressions. 🎉

The full Conbench report has more details. It also includes information about 9 possible false positives for unstable benchmarks that are known to sometimes produce them.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants