diff --git a/datafusion/common/src/scalar/mod.rs b/datafusion/common/src/scalar/mod.rs index 286df339adcf..fd0c11ed0ab0 100644 --- a/datafusion/common/src/scalar/mod.rs +++ b/datafusion/common/src/scalar/mod.rs @@ -801,9 +801,13 @@ fn dict_from_scalar( let values_array = value.to_array_of_size(1)?; // Create a key array with `size` elements, each of 0 - let key_array: PrimitiveArray = std::iter::repeat(Some(K::default_value())) - .take(size) - .collect(); + let key_array: PrimitiveArray = std::iter::repeat(if value.is_null() { + None + } else { + Some(K::default_value()) + }) + .take(size) + .collect(); // create a new DictionaryArray // @@ -6674,4 +6678,15 @@ mod tests { ); assert!(dense_scalar.is_null()); } + + #[test] + fn null_dictionary_scalar_produces_null_dictionary_array() { + let dictionary_scalar = ScalarValue::Dictionary( + Box::new(DataType::Int32), + Box::new(ScalarValue::Null), + ); + assert!(dictionary_scalar.is_null()); + let dictionary_array = dictionary_scalar.to_array().unwrap(); + assert!(dictionary_array.is_null(0)); + } }