This is an automated email from the ASF dual-hosted git repository.

jayzhan pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new 4b7281e9c5 fix: make get_valid_types handle TypeSignature::Numeric 
correctly (#14060)
4b7281e9c5 is described below

commit 4b7281e9c5ae42c7934ca9c9fce3aaadc5d2df48
Author: niebayes <[email protected]>
AuthorDate: Sat Jan 11 09:37:55 2025 +0800

    fix: make get_valid_types handle TypeSignature::Numeric correctly (#14060)
    
    * fix get_valid_types with TypeSignature::Numeric
    
    * fix sqllogictest
---
 datafusion/expr/src/type_coercion/functions.rs | 65 ++++++++++++++++++++++++++
 datafusion/sqllogictest/test_files/math.slt    |  6 +--
 2 files changed, 68 insertions(+), 3 deletions(-)

diff --git a/datafusion/expr/src/type_coercion/functions.rs 
b/datafusion/expr/src/type_coercion/functions.rs
index 5294cc526d..650619e6de 100644
--- a/datafusion/expr/src/type_coercion/functions.rs
+++ b/datafusion/expr/src/type_coercion/functions.rs
@@ -544,6 +544,10 @@ fn get_valid_types(
             // and their default type is double precision
             if logical_data_type == NativeType::Null {
                 valid_type = DataType::Float64;
+            } else if !logical_data_type.is_numeric() {
+                return plan_err!(
+                    "The signature expected NativeType::Numeric but received 
{logical_data_type}"
+                );
             }
 
             vec![vec![valid_type; *number]]
@@ -997,6 +1001,67 @@ mod tests {
         }
     }
 
+    #[test]
+    fn test_get_valid_types_numeric() -> Result<()> {
+        let get_valid_types_flatten =
+            |signature: &TypeSignature, current_types: &[DataType]| {
+                get_valid_types(signature, current_types)
+                    .unwrap()
+                    .into_iter()
+                    .flatten()
+                    .collect::<Vec<_>>()
+            };
+
+        // Trivial case.
+        let got = get_valid_types_flatten(&TypeSignature::Numeric(1), 
&[DataType::Int32]);
+        assert_eq!(got, [DataType::Int32]);
+
+        // Args are coerced into a common numeric type.
+        let got = get_valid_types_flatten(
+            &TypeSignature::Numeric(2),
+            &[DataType::Int32, DataType::Int64],
+        );
+        assert_eq!(got, [DataType::Int64, DataType::Int64]);
+
+        // Args are coerced into a common numeric type, specifically, int 
would be coerced to float.
+        let got = get_valid_types_flatten(
+            &TypeSignature::Numeric(3),
+            &[DataType::Int32, DataType::Int64, DataType::Float64],
+        );
+        assert_eq!(
+            got,
+            [DataType::Float64, DataType::Float64, DataType::Float64]
+        );
+
+        // Cannot coerce args to a common numeric type.
+        let got = get_valid_types(
+            &TypeSignature::Numeric(2),
+            &[DataType::Int32, DataType::Utf8],
+        )
+        .unwrap_err();
+        assert_contains!(
+            got.to_string(),
+            "The signature expected NativeType::Numeric but received 
NativeType::String"
+        );
+
+        // Fallbacks to float64 if the arg is of type null.
+        let got = get_valid_types_flatten(&TypeSignature::Numeric(1), 
&[DataType::Null]);
+        assert_eq!(got, [DataType::Float64]);
+
+        // Rejects non-numeric arg.
+        let got = get_valid_types(
+            &TypeSignature::Numeric(1),
+            &[DataType::Timestamp(TimeUnit::Second, None)],
+        )
+        .unwrap_err();
+        assert_contains!(
+            got.to_string(),
+            "The signature expected NativeType::Numeric but received 
NativeType::Timestamp(Second, None)"
+        );
+
+        Ok(())
+    }
+
     #[test]
     fn test_get_valid_types_one_of() -> Result<()> {
         let signature =
diff --git a/datafusion/sqllogictest/test_files/math.slt 
b/datafusion/sqllogictest/test_files/math.slt
index e86d78a623..37b5a378fc 100644
--- a/datafusion/sqllogictest/test_files/math.slt
+++ b/datafusion/sqllogictest/test_files/math.slt
@@ -126,15 +126,15 @@ statement error
 SELECT abs(1, 2);
 
 # abs: unsupported argument type
-query error This feature is not implemented: Unsupported data type Utf8 for 
function abs
+query error DataFusion error: Error during planning: The signature expected 
NativeType::Numeric but received NativeType::String
 SELECT abs('foo');
 
 # abs: numeric string
 # TODO: In Postgres, '-1.2' is unknown type and interpreted to float8 so they 
don't fail on this query
-query error DataFusion error: This feature is not implemented: Unsupported 
data type Utf8 for function abs
+query error DataFusion error: Error during planning: The signature expected 
NativeType::Numeric but received NativeType::String
 select abs('-1.2');
 
-query error DataFusion error: This feature is not implemented: Unsupported 
data type Utf8 for function abs
+query error DataFusion error: Error during planning: The signature expected 
NativeType::Numeric but received NativeType::String
 select abs(arrow_cast('-1.2', 'Utf8'));
 
 statement ok


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to