Skip to content

Commit cf034ba

Browse files
committed
feat: Coerce strings to any type in functions
Signed-off-by: Alex Qyoun-ae <[email protected]>
1 parent 48ff058 commit cf034ba

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

datafusion/core/tests/sql/errors.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ async fn csv_query_error() -> Result<()> {
2424
register_aggregate_csv(&ctx).await?;
2525
let sql = "SELECT sin(c1) FROM aggregate_test_100";
2626
let plan = ctx.create_logical_plan(sql);
27-
assert!(plan.is_err());
27+
// NOTE(cubesql): this coercion is supported
28+
// assert!(plan.is_err());
29+
assert!(plan.is_ok());
2830
Ok(())
2931
}
3032

datafusion/expr/src/type_coercion.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,18 @@ pub fn data_types(
5050
}
5151
let valid_types = get_valid_types(&signature.type_signature, current_types)?;
5252

53-
if valid_types
54-
.iter()
55-
.any(|data_type| data_type == current_types)
56-
{
57-
return Ok(current_types.to_vec());
53+
if let Some(types) = valid_types.iter().find(|data_types| {
54+
if data_types.len() != current_types.len() {
55+
return false;
56+
}
57+
data_types
58+
.iter()
59+
.zip(current_types)
60+
.all(|(data_type, current_type)| {
61+
data_type == current_type || matches!(current_type, DataType::Null)
62+
})
63+
}) {
64+
return Ok(types.clone());
5865
}
5966

6067
for valid_types in valid_types {
@@ -145,6 +152,10 @@ fn maybe_data_types(
145152
/// See the module level documentation for more detail on coercion.
146153
pub fn can_coerce_from(type_into: &DataType, type_from: &DataType) -> bool {
147154
use self::DataType::*;
155+
// Strings can be converted to most types implicitly
156+
if matches!(type_from, Utf8 | LargeUtf8) {
157+
return true;
158+
}
148159
// Null can convert to most of types
149160
match type_into {
150161
Int8 => matches!(type_from, Null | Int8),

0 commit comments

Comments
 (0)