Skip to content

Commit

Permalink
Coerce Dictionary types for scalar functions (apache#10077)
Browse files Browse the repository at this point in the history
* Coerce Dictionary types for scalar functions

* Fix

* Fix format

* Add test
  • Loading branch information
viirya authored and appletreeisyellow committed Apr 22, 2024
1 parent eedf46d commit 00eb2d0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
15 changes: 15 additions & 0 deletions datafusion/expr/src/built_in_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,4 +448,19 @@ mod tests {
.unwrap();
assert_eq!(return_type, DataType::Date32);
}

#[test]
fn test_coalesce_return_types_dictionary() {
let coalesce = BuiltinScalarFunction::Coalesce;
let return_type = coalesce
.return_type(&[
DataType::Dictionary(Box::new(DataType::Int32), Box::new(DataType::Utf8)),
DataType::Utf8,
])
.unwrap();
assert_eq!(
return_type,
DataType::Dictionary(Box::new(DataType::Int32), Box::new(DataType::Utf8))
);
}
}
25 changes: 23 additions & 2 deletions datafusion/expr/src/type_coercion/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,13 @@ fn coerced_from<'a>(
// match Dictionary first
match (type_into, type_from) {
// coerced dictionary first
(cur_type, Dictionary(_, value_type)) | (Dictionary(_, value_type), cur_type)
if coerced_from(cur_type, value_type).is_some() =>
(_, Dictionary(_, value_type))
if coerced_from(type_into, value_type).is_some() =>
{
Some(type_into.clone())
}
(Dictionary(_, value_type), _)
if coerced_from(value_type, type_from).is_some() =>
{
Some(type_into.clone())
}
Expand Down Expand Up @@ -624,4 +629,20 @@ mod tests {

Ok(())
}

#[test]
fn test_coerced_from_dictionary() {
let type_into =
DataType::Dictionary(Box::new(DataType::Int32), Box::new(DataType::UInt32));
let type_from = DataType::Int64;
assert_eq!(coerced_from(&type_into, &type_from), None);

let type_from =
DataType::Dictionary(Box::new(DataType::Int32), Box::new(DataType::UInt32));
let type_into = DataType::Int64;
assert_eq!(
coerced_from(&type_into, &type_from),
Some(type_into.clone())
);
}
}

0 comments on commit 00eb2d0

Please sign in to comment.