Skip to content

Commit 5109daf

Browse files
committed
Add helper function to create dictionary array with non-null keys and null values
1 parent 522b1aa commit 5109daf

File tree

1 file changed

+16
-12
lines changed
  • datafusion/functions-aggregate/src

1 file changed

+16
-12
lines changed

datafusion/functions-aggregate/src/count.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,20 @@ mod tests {
771771
use datafusion_physical_expr::{expressions::Column, LexOrdering};
772772
use std::sync::Arc;
773773

774+
/// Helper function to create a dictionary array with non-null keys but some null values
775+
/// Returns a dictionary array where:
776+
/// - keys are [0, 1, 2, 0, 1] (all non-null)
777+
/// - values are ["a", null, "c"]
778+
/// - so the keys reference: "a", null, "c", "a", null
779+
fn create_dictionary_with_null_values() -> Result<DictionaryArray<Int32Type>> {
780+
let values = StringArray::from(vec![Some("a"), None, Some("c")]);
781+
let keys = Int32Array::from(vec![0, 1, 2, 0, 1]); // references "a", null, "c", "a", null
782+
Ok(DictionaryArray::<Int32Type>::try_new(
783+
keys,
784+
Arc::new(values),
785+
)?)
786+
}
787+
774788
#[test]
775789
fn count_accumulator_nulls() -> Result<()> {
776790
let mut accumulator = CountAccumulator::new();
@@ -823,12 +837,7 @@ mod tests {
823837

824838
#[test]
825839
fn count_distinct_accumulator_dictionary_with_null_values() -> Result<()> {
826-
// Create a dictionary array where:
827-
// - keys aren't null
828-
// - but values referenced by some keys are null
829-
let values = StringArray::from(vec![Some("a"), None, Some("c")]);
830-
let keys = Int32Array::from(vec![0, 1, 2, 0, 1]); // references "a", null, "c", "a", null
831-
let dict_array = DictionaryArray::<Int32Type>::try_new(keys, Arc::new(values))?;
840+
let dict_array = create_dictionary_with_null_values()?;
832841

833842
// The expected behavior is that count_distinct should count only non-null values
834843
// which in this case are "a" and "c" (appearing as 0 and 2 in keys)
@@ -846,12 +855,7 @@ mod tests {
846855

847856
#[test]
848857
fn count_accumulator_dictionary_with_null_values() -> Result<()> {
849-
// Create a dictionary array where:
850-
// - keys aren't null
851-
// - but values referenced by some keys are null
852-
let values = StringArray::from(vec![Some("a"), None, Some("c")]);
853-
let keys = Int32Array::from(vec![0, 1, 2, 0, 1]); // references "a", null, "c", "a", null
854-
let dict_array = DictionaryArray::<Int32Type>::try_new(keys, Arc::new(values))?;
858+
let dict_array = create_dictionary_with_null_values()?;
855859

856860
// The expected behavior is that count should only count non-null values
857861
let mut accumulator = CountAccumulator::new();

0 commit comments

Comments
 (0)