Skip to content

Commit 2730423

Browse files
authored
fix: make ScalarValue::Dictionary with NULL values produce NULL arrays (#11908)
Update the way ScalarValue::Dictionary values are turned into arrays such that: scalar_value.is_null() == scalar_value.to_array()?.is_null(0) Previously the dictionary would be created with a valid key entry pointing to a NULL value. https://arrow.apache.org/docs/format/Columnar.html#dictionary-encoded-layout suggests that this does not constitute a NULL entry.
1 parent 79fa6f9 commit 2730423

File tree

1 file changed

+18
-3
lines changed
  • datafusion/common/src/scalar

1 file changed

+18
-3
lines changed

datafusion/common/src/scalar/mod.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -801,9 +801,13 @@ fn dict_from_scalar<K: ArrowDictionaryKeyType>(
801801
let values_array = value.to_array_of_size(1)?;
802802

803803
// Create a key array with `size` elements, each of 0
804-
let key_array: PrimitiveArray<K> = std::iter::repeat(Some(K::default_value()))
805-
.take(size)
806-
.collect();
804+
let key_array: PrimitiveArray<K> = std::iter::repeat(if value.is_null() {
805+
None
806+
} else {
807+
Some(K::default_value())
808+
})
809+
.take(size)
810+
.collect();
807811

808812
// create a new DictionaryArray
809813
//
@@ -6674,4 +6678,15 @@ mod tests {
66746678
);
66756679
assert!(dense_scalar.is_null());
66766680
}
6681+
6682+
#[test]
6683+
fn null_dictionary_scalar_produces_null_dictionary_array() {
6684+
let dictionary_scalar = ScalarValue::Dictionary(
6685+
Box::new(DataType::Int32),
6686+
Box::new(ScalarValue::Null),
6687+
);
6688+
assert!(dictionary_scalar.is_null());
6689+
let dictionary_array = dictionary_scalar.to_array().unwrap();
6690+
assert!(dictionary_array.is_null(0));
6691+
}
66776692
}

0 commit comments

Comments
 (0)