Apache DataFusion SQL Query Engine
Support better dictionary coercion (#3688)
Andrew Lambd0760f3d4e45
diff --git a/datafusion/core/tests/sql/predicates.rs b/datafusion/core/tests/sql/predicates.rsindex e075433ac3..34c9cc98ae 100644--- a/datafusion/core/tests/sql/predicates.rs+++ b/datafusion/core/tests/sql/predicates.rs@@ -297,6 +297,49 @@ async fn test_regexp_is_match() -> Result<()> {Ok(())}+#[tokio::test]+async fn string_coercion() -> Result<()> {+ let vendor_id_utf8: StringArray =+ vec![Some("124"), Some("345")].into_iter().collect();++ let vendor_id_dict: DictionaryArray<Int16Type> =+ vec![Some("124"), Some("345")].into_iter().collect();++ let batch = RecordBatch::try_from_iter(vec![+ ("vendor_id_utf8", Arc::new(vendor_id_utf8) as _),+ ("vendor_id_dict", Arc::new(vendor_id_dict) as _),+ ])+ .unwrap();++ let ctx = SessionContext::new();+ ctx.register_batch("t", batch)?;++ let expected = vec![+ "+----------------+----------------+",+ "| vendor_id_utf8 | vendor_id_dict |",+ "+----------------+----------------+",+ "| 124 | 124 |",+ "+----------------+----------------+",+ ];++ // Compare utf8 column with numeric constant+ let sql = "SELECT * from t where vendor_id_utf8 = 124";+ let actual = execute_to_batches(&ctx, sql).await;+ assert_batches_eq!(expected, &actual);++ // Compare dictionary encoded utf8 column with numeric constant+ let sql = "SELECT * from t where vendor_id_dict = 124";+ let actual = execute_to_batches(&ctx, sql).await;+ assert_batches_eq!(expected, &actual);++ // Compare dictionary encoded utf8 column with numeric constant with explicit cast+ let sql = "SELECT * from t where cast(vendor_id_utf8 as varchar) = 124";+ let actual = execute_to_batches(&ctx, sql).await;+ assert_batches_eq!(expected, &actual);++ Ok(())+}+#[tokio::test]async fn except_with_null_not_equal() {let sql = "SELECT * FROM (SELECT null AS id1, 1 AS id2) t1diff --git a/datafusion/expr/src/binary_rule.rs b/datafusion/expr/src/binary_rule.rsindex 06240b6155..507ab90c0c 100644--- a/datafusion/expr/src/binary_rule.rs+++ b/datafusion/expr/src/binary_rule.rs@@ -454,14 +454,6 @@ fn both_numeric_or_null_and_numeric(lhs_type: &DataType, rhs_type: &DataType) ->}}-/// Coercion rules for dictionary values (aka the type of the dictionary itself)-fn dictionary_value_coercion(- lhs_type: &DataType,- rhs_type: &DataType,-) -> Option<DataType> {- numerical_coercion(lhs_type, rhs_type).or_else(|| string_coercion(lhs_type, rhs_type))-}-/// Coercion rules for Dictionaries: the type that both lhs and rhs/// can be casted to for the purpose of a computation.///@@ -476,7 +468,7 @@ fn dictionary_coercion((DataType::Dictionary(_lhs_index_type, lhs_value_type),DataType::Dictionary(_rhs_index_type, rhs_value_type),- ) => dictionary_value_coercion(lhs_value_type, rhs_value_type),+ ) => comparison_coercion(lhs_value_type, rhs_value_type),(d @ DataType::Dictionary(_, value_type), other_type)| (other_type, d @ DataType::Dictionary(_, value_type))if preserve_dictionaries && value_type.as_ref() == other_type =>@@ -484,10 +476,10 @@ fn dictionary_coercion(Some(d.clone())}(DataType::Dictionary(_index_type, value_type), _) => {- dictionary_value_coercion(value_type, rhs_type)+ comparison_coercion(value_type, rhs_type)}(_, DataType::Dictionary(_index_type, value_type)) => {- dictionary_value_coercion(lhs_type, value_type)+ comparison_coercion(lhs_type, value_type)}_ => None,}@@ -801,8 +793,14 @@ mod tests {Some(Int32));+ // Since we can coerce values of Int16 to Utf8 can support thislet lhs_type = Dictionary(Box::new(Int8), Box::new(Utf8));let rhs_type = Dictionary(Box::new(Int8), Box::new(Int16));+ assert_eq!(dictionary_coercion(&lhs_type, &rhs_type, true), Some(Utf8));++ // Can not coerce values of Binary to int, cannot support this+ let lhs_type = Dictionary(Box::new(Int8), Box::new(Utf8));+ let rhs_type = Dictionary(Box::new(Int8), Box::new(Binary));assert_eq!(dictionary_coercion(&lhs_type, &rhs_type, true), None);let lhs_type = Dictionary(Box::new(Int8), Box::new(Utf8));