Skip to content

Commit 1f806b2

Browse files
committed
revert native to original and add anynative
1 parent f519d92 commit 1f806b2

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

datafusion/expr-common/src/signature.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,19 @@ pub enum TypeSignature {
126126
Exact(Vec<DataType>),
127127
/// One or more arguments belonging to the [`TypeSignatureClass`], in order.
128128
///
129-
/// `Coercible(vec![TypeSignatureClass::Native(...)])` accepts any type castable to the
129+
/// `Coercible(vec![TypeSignatureClass::AnyNative(...)])` accepts any type castable to the
130130
/// target `NativeType` through the explicit set of type conversion rules defined in
131-
/// `NativeType::default_cast_for`
131+
/// `NativeType::default_cast_for`.
132132
///
133-
/// For example, `Coercible(vec![TypeSignatureClass::Native(logical_float64())])` accepts
133+
/// For example, `Coercible(vec![TypeSignatureClass::AnyNative(logical_float64())])` accepts
134134
/// arguments like `vec![Int32]` or `vec![Float32]` since i32 and f32 can be cast to f64.
135135
///
136+
/// `Coercible(vec![TypeSignatureClass::Native(...)])` is designed to cast between the same
137+
/// logical type.
138+
///
139+
/// For example, `Coercible(vec![TypeSignatureClass::Native(logical_int64())])` accepts
140+
/// arguments like `vec![Int32]` since i32 is the same logical type as i64.
141+
///
136142
/// Coercible(vec![TypeSignatureClass::Integer(...)])` accepts any
137143
/// integer type (`NativeType::is_integer`) castable to the target integer `NativeType`.
138144
///
@@ -222,9 +228,9 @@ pub enum TypeSignatureClass {
222228
Interval,
223229
Duration,
224230
Native(LogicalTypeRef),
231+
AnyNative(LogicalTypeRef),
232+
Numeric(LogicalTypeRef),
225233
Integer(LogicalTypeRef),
226-
// TODO:
227-
// Numeric
228234
}
229235

230236
impl Display for TypeSignatureClass {
@@ -385,9 +391,10 @@ impl TypeSignature {
385391
TypeSignature::Coercible(types) => types
386392
.iter()
387393
.map(|logical_type| match logical_type {
388-
TypeSignatureClass::Native(l) | TypeSignatureClass::Integer(l) => {
389-
get_data_types(l.native())
390-
}
394+
TypeSignatureClass::Native(l)
395+
| TypeSignatureClass::AnyNative(l)
396+
| TypeSignatureClass::Numeric(l)
397+
| TypeSignatureClass::Integer(l) => get_data_types(l.native()),
391398
TypeSignatureClass::Timestamp => {
392399
vec![
393400
DataType::Timestamp(TimeUnit::Nanosecond, None),

datafusion/expr/src/type_coercion/functions.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,9 +540,32 @@ fn get_valid_types(
540540

541541
match target_type_class {
542542
TypeSignatureClass::Native(native_type) => {
543+
let target_type = native_type.native();
544+
if &logical_type == target_type
545+
|| logical_type == NativeType::Null
546+
|| (target_type.is_numeric() && logical_type.is_numeric())
547+
{
548+
target_type.default_cast_for(current_type)
549+
} else {
550+
internal_err!(
551+
"Expect {target_type_class} but received {current_type}"
552+
)
553+
}
554+
}
555+
TypeSignatureClass::AnyNative(native_type) => {
543556
let target_type = native_type.native();
544557
target_type.default_cast_for(current_type)
545558
}
559+
TypeSignatureClass::Numeric(native_type) => {
560+
let target_type = native_type.native();
561+
if target_type.is_numeric() && logical_type.is_numeric() {
562+
target_type.default_cast_for(current_type)
563+
} else {
564+
internal_err!(
565+
"Expect {target_type_class} but received {current_type}"
566+
)
567+
}
568+
}
546569
TypeSignatureClass::Integer(native_type) => {
547570
let target_type = native_type.native();
548571
if target_type.is_integer() && logical_type.is_integer() {
@@ -591,7 +614,7 @@ fn get_valid_types(
591614
// Following the behavior of `TypeSignature::String`, we find the common string type.
592615
let string_indices: Vec<_> = target_types.iter().enumerate()
593616
.filter(|(_, t)| {
594-
matches!(t, TypeSignatureClass::Native(n) if n.native() == &NativeType::String)
617+
matches!(t, TypeSignatureClass::Native(n) | TypeSignatureClass::AnyNative(n) if n.native() == &NativeType::String)
595618
})
596619
.map(|(i, _)| i)
597620
.collect();

0 commit comments

Comments
 (0)