This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/main by this push:
new dc99a52498 perf: skip validation of dictionary keys if all null
(#9322)
dc99a52498 is described below
commit dc99a52498ec25dae1fdc12618e992764486b88c
Author: albertlockett <[email protected]>
AuthorDate: Mon Feb 2 14:28:01 2026 -0500
perf: skip validation of dictionary keys if all null (#9322)
# Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax.
-->
- Closes #9321 .
# Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->
Improve the performance of creating a dictionary array that is all
nulls.
# What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->
The `try_new` constructor skips the key validation if all the keys are
null.
Adds a benchmark for creating the all null dictionary to verify the
performance improvement.
| Benchmark | Before | After | Change |
|-----------|--------|-------|---------|
| null_dict/len=128 | 133.37 ns | 98.222 ns | -25.659% |
| null_dict/len=1536 | 623.39 ns | 286.11 ns | -57.361% |
| null_dict/len=8092 | 3.0252 µs | 1.0719 µs | -66.573% |
# Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->
I added a smoke test that calls the constructor with an all nulls key
column and asserts the result is OK.
I wasn't sure about the best way to test this (open to suggestions).
# Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
If there are any breaking changes to public APIs, please call them out.
-->
no
---
arrow-array/Cargo.toml | 6 ++++-
arrow-array/benches/null_dict.rs | 41 +++++++++++++++++++++++++++++++
arrow-array/src/array/dictionary_array.rs | 32 ++++++++++++++++--------
3 files changed, 68 insertions(+), 11 deletions(-)
diff --git a/arrow-array/Cargo.toml b/arrow-array/Cargo.toml
index 0a1d6fde91..a046fea2b0 100644
--- a/arrow-array/Cargo.toml
+++ b/arrow-array/Cargo.toml
@@ -75,6 +75,10 @@ harness = false
name = "fixed_size_list_array"
harness = false
+[[bench]]
+name = "null_dict"
+harness = false
+
[[bench]]
name = "decimal_overflow"
harness = false
@@ -85,4 +89,4 @@ harness = false
[[bench]]
name = "record_batch"
-harness = false
\ No newline at end of file
+harness = false
diff --git a/arrow-array/benches/null_dict.rs b/arrow-array/benches/null_dict.rs
new file mode 100644
index 0000000000..b8e81208e1
--- /dev/null
+++ b/arrow-array/benches/null_dict.rs
@@ -0,0 +1,41 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+use arrow_array::{DictionaryArray, StringArray, UInt16Array};
+use std::sync::Arc;
+
+use criterion::*;
+
+fn criterion_benchmark(c: &mut Criterion) {
+ let dict_vals = Arc::new(StringArray::from_iter_values(["a", "b", "c"]));
+ for len in [128, 1536, 8092] {
+ c.bench_function(&format!("null_dict/len={len}"), |b| {
+ b.iter_batched(
+ || dict_vals.clone(),
+ |dict_vals| {
+ std::hint::black_box(DictionaryArray::new(
+ UInt16Array::new_null(len),
+ dict_vals.clone(),
+ ))
+ },
+ BatchSize::SmallInput,
+ );
+ });
+ }
+}
+
+criterion_group!(benches, criterion_benchmark);
+criterion_main!(benches);
diff --git a/arrow-array/src/array/dictionary_array.rs
b/arrow-array/src/array/dictionary_array.rs
index 54f8c42dd8..97e45cc5d6 100644
--- a/arrow-array/src/array/dictionary_array.rs
+++ b/arrow-array/src/array/dictionary_array.rs
@@ -292,17 +292,20 @@ impl<K: ArrowDictionaryKeyType> DictionaryArray<K> {
Box::new(values.data_type().clone()),
);
- let zero = K::Native::usize_as(0);
- let values_len = values.len();
+ // // we can skip the validating the keys if they are all null
+ let all_null = keys.null_count() == keys.len();
- if let Some((idx, v)) =
- keys.values().iter().enumerate().find(|(idx, v)| {
+ if !all_null {
+ let zero = K::Native::usize_as(0);
+ let values_len = values.len();
+
+ if let Some((idx, v)) =
keys.values().iter().enumerate().find(|(idx, v)| {
(v.is_lt(zero) || v.as_usize() >= values_len) &&
keys.is_valid(*idx)
- })
- {
- return Err(ArrowError::InvalidArgumentError(format!(
- "Invalid dictionary key {v:?} at index {idx}, expected 0 <=
key < {values_len}",
- )));
+ }) {
+ return Err(ArrowError::InvalidArgumentError(format!(
+ "Invalid dictionary key {v:?} at index {idx}, expected 0
<= key < {values_len}",
+ )));
+ }
}
Ok(Self {
@@ -1047,7 +1050,7 @@ impl<K: ArrowDictionaryKeyType> AnyDictionaryArray for
DictionaryArray<K> {
mod tests {
use super::*;
use crate::cast::as_dictionary_array;
- use crate::{Int8Array, Int16Array, Int32Array, RunArray};
+ use crate::{Int8Array, Int16Array, Int32Array, RunArray, UInt8Array};
use arrow_buffer::{Buffer, ToByteSlice};
#[test]
@@ -1528,4 +1531,13 @@ mod tests {
let dictionary = DictionaryArray::new(keys,
Arc::new(Int32Array::new_null(2)));
assert_eq!(&dictionary.normalized_keys(), &[1, 0, 1])
}
+
+ #[test]
+ fn test_all_null_dict() {
+ let all_null_dict_arr = DictionaryArray::try_new(
+ UInt8Array::new_null(10),
+ Arc::new(StringArray::from_iter_values(["a"])),
+ );
+ assert!(all_null_dict_arr.is_ok())
+ }
}