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

findepi 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 55e56c47a6 Report current operation when coercion fails (#13628)
55e56c47a6 is described below

commit 55e56c47a61ee0321f971ab8660af7468f9e9687
Author: Piotr Findeisen <[email protected]>
AuthorDate: Wed Dec 4 13:18:05 2024 +0100

    Report current operation when coercion fails (#13628)
    
    This makes for a more actionable error message.
---
 datafusion/expr/src/type_coercion/functions.rs     | 39 +++++++++++++++++-----
 datafusion/optimizer/src/analyzer/type_coercion.rs |  4 +--
 datafusion/sqllogictest/test_files/aggregate.slt   | 12 +++----
 datafusion/sqllogictest/test_files/errors.slt      |  6 ++--
 datafusion/sqllogictest/test_files/expr.slt        |  2 +-
 5 files changed, 43 insertions(+), 20 deletions(-)

diff --git a/datafusion/expr/src/type_coercion/functions.rs 
b/datafusion/expr/src/type_coercion/functions.rs
index 3dbffce3c9..9d15d96939 100644
--- a/datafusion/expr/src/type_coercion/functions.rs
+++ b/datafusion/expr/src/type_coercion/functions.rs
@@ -64,7 +64,12 @@ pub fn data_types_with_scalar_udf(
         return Ok(current_types.to_vec());
     }
 
-    try_coerce_types(valid_types, current_types, &signature.type_signature)
+    try_coerce_types(
+        func.name(),
+        valid_types,
+        current_types,
+        &signature.type_signature,
+    )
 }
 
 /// Performs type coercion for aggregate function arguments.
@@ -100,7 +105,12 @@ pub fn data_types_with_aggregate_udf(
         return Ok(current_types.to_vec());
     }
 
-    try_coerce_types(valid_types, current_types, &signature.type_signature)
+    try_coerce_types(
+        func.name(),
+        valid_types,
+        current_types,
+        &signature.type_signature,
+    )
 }
 
 /// Performs type coercion for window function arguments.
@@ -133,7 +143,12 @@ pub fn data_types_with_window_udf(
         return Ok(current_types.to_vec());
     }
 
-    try_coerce_types(valid_types, current_types, &signature.type_signature)
+    try_coerce_types(
+        func.name(),
+        valid_types,
+        current_types,
+        &signature.type_signature,
+    )
 }
 
 /// Performs type coercion for function arguments.
@@ -144,6 +159,7 @@ pub fn data_types_with_window_udf(
 /// For more details on coercion in general, please see the
 /// [`type_coercion`](crate::type_coercion) module.
 pub fn data_types(
+    function_name: impl AsRef<str>,
     current_types: &[DataType],
     signature: &Signature,
 ) -> Result<Vec<DataType>> {
@@ -166,7 +182,12 @@ pub fn data_types(
         return Ok(current_types.to_vec());
     }
 
-    try_coerce_types(valid_types, current_types, &signature.type_signature)
+    try_coerce_types(
+        function_name,
+        valid_types,
+        current_types,
+        &signature.type_signature,
+    )
 }
 
 fn is_well_supported_signature(type_signature: &TypeSignature) -> bool {
@@ -187,6 +208,7 @@ fn is_well_supported_signature(type_signature: 
&TypeSignature) -> bool {
 }
 
 fn try_coerce_types(
+    function_name: impl AsRef<str>,
     valid_types: Vec<Vec<DataType>>,
     current_types: &[DataType],
     type_signature: &TypeSignature,
@@ -218,7 +240,8 @@ fn try_coerce_types(
 
     // none possible -> Error
     plan_err!(
-        "Coercion from {:?} to the signature {:?} failed.",
+        "Failed to coerce arguments to satisfy a call to {} function: coercion 
from {:?} to the signature {:?} failed.",
+        function_name.as_ref(),
         current_types,
         type_signature
     )
@@ -971,7 +994,7 @@ mod tests {
             Volatility::Stable,
         );
 
-        let coerced_data_types = data_types(&current_types, 
&signature).unwrap();
+        let coerced_data_types = data_types("test", &current_types, 
&signature).unwrap();
         assert_eq!(coerced_data_types, current_types);
 
         // make sure it can't coerce to a different size
@@ -979,7 +1002,7 @@ mod tests {
             vec![DataType::FixedSizeList(Arc::clone(&inner), 3)],
             Volatility::Stable,
         );
-        let coerced_data_types = data_types(&current_types, &signature);
+        let coerced_data_types = data_types("test", &current_types, 
&signature);
         assert!(coerced_data_types.is_err());
 
         // make sure it works with the same type.
@@ -987,7 +1010,7 @@ mod tests {
             vec![DataType::FixedSizeList(Arc::clone(&inner), 2)],
             Volatility::Stable,
         );
-        let coerced_data_types = data_types(&current_types, 
&signature).unwrap();
+        let coerced_data_types = data_types("test", &current_types, 
&signature).unwrap();
         assert_eq!(coerced_data_types, current_types);
 
         Ok(())
diff --git a/datafusion/optimizer/src/analyzer/type_coercion.rs 
b/datafusion/optimizer/src/analyzer/type_coercion.rs
index 71abfda5fc..628c1498f9 100644
--- a/datafusion/optimizer/src/analyzer/type_coercion.rs
+++ b/datafusion/optimizer/src/analyzer/type_coercion.rs
@@ -1357,7 +1357,7 @@ mod test {
 
         let err = Projection::try_new(vec![udaf], empty).err().unwrap();
         assert!(
-            err.strip_backtrace().starts_with("Error during planning: Error 
during planning: Coercion from [Utf8] to the signature Uniform(1, [Float64]) 
failed")
+            err.strip_backtrace().starts_with("Error during planning: Error 
during planning: Failed to coerce arguments to satisfy a call to MY_AVG 
function: coercion from [Utf8] to the signature Uniform(1, [Float64]) failed")
         );
         Ok(())
     }
@@ -1407,7 +1407,7 @@ mod test {
             .err()
             .unwrap()
             .strip_backtrace();
-        assert!(err.starts_with("Error during planning: Error during planning: 
Coercion from [Utf8] to the signature Uniform(1, [Int8, Int16, Int32, Int64, 
UInt8, UInt16, UInt32, UInt64, Float32, Float64]) failed."));
+        assert!(err.starts_with("Error during planning: Error during planning: 
Failed to coerce arguments to satisfy a call to avg function: coercion from 
[Utf8] to the signature Uniform(1, [Int8, Int16, Int32, Int64, UInt8, UInt16, 
UInt32, UInt64, Float32, Float64]) failed."));
         Ok(())
     }
 
diff --git a/datafusion/sqllogictest/test_files/aggregate.slt 
b/datafusion/sqllogictest/test_files/aggregate.slt
index 917e037682..e76c1466a5 100644
--- a/datafusion/sqllogictest/test_files/aggregate.slt
+++ b/datafusion/sqllogictest/test_files/aggregate.slt
@@ -76,26 +76,26 @@ statement error DataFusion error: Schema error: Schema 
contains duplicate unqual
 SELECT approx_distinct(c9) count_c9, approx_distinct(cast(c9 as varchar)) 
count_c9_str FROM aggregate_test_100
 
 # csv_query_approx_percentile_cont_with_weight
-statement error DataFusion error: Error during planning: Error during 
planning: Coercion from \[Utf8, Int8, Float64\] to the signature OneOf(.*) 
failed(.|\n)*
+statement error DataFusion error: Error during planning: Error during 
planning: Failed to coerce arguments to satisfy a call to 
approx_percentile_cont_with_weight function: coercion from \[Utf8, Int8, 
Float64\] to the signature OneOf(.*) failed(.|\n)*
 SELECT approx_percentile_cont_with_weight(c1, c2, 0.95) FROM aggregate_test_100
 
-statement error DataFusion error: Error during planning: Error during 
planning: Coercion from \[Int16, Utf8, Float64\] to the signature OneOf(.*) 
failed(.|\n)*
+statement error DataFusion error: Error during planning: Error during 
planning: Failed to coerce arguments to satisfy a call to 
approx_percentile_cont_with_weight function: coercion from \[Int16, Utf8, 
Float64\] to the signature OneOf(.*) failed(.|\n)*
 SELECT approx_percentile_cont_with_weight(c3, c1, 0.95) FROM aggregate_test_100
 
-statement error DataFusion error: Error during planning: Error during 
planning: Coercion from \[Int16, Int8, Utf8\] to the signature OneOf(.*) 
failed(.|\n)*
+statement error DataFusion error: Error during planning: Error during 
planning: Failed to coerce arguments to satisfy a call to 
approx_percentile_cont_with_weight function: coercion from \[Int16, Int8, 
Utf8\] to the signature OneOf(.*) failed(.|\n)*
 SELECT approx_percentile_cont_with_weight(c3, c2, c1) FROM aggregate_test_100
 
 # csv_query_approx_percentile_cont_with_histogram_bins
 statement error DataFusion error: External error: This feature is not 
implemented: Tdigest max_size value for 'APPROX_PERCENTILE_CONT' must be UInt > 
0 literal \(got data type Int64\)\.
 SELECT c1, approx_percentile_cont(c3, 0.95, -1000) AS c3_p95 FROM 
aggregate_test_100 GROUP BY 1 ORDER BY 1
 
-statement error DataFusion error: Error during planning: Error during 
planning: Coercion from \[Int16, Float64, Utf8\] to the signature OneOf(.*) 
failed(.|\n)*
+statement error DataFusion error: Error during planning: Error during 
planning: Failed to coerce arguments to satisfy a call to 
approx_percentile_cont function: coercion from \[Int16, Float64, Utf8\] to the 
signature OneOf(.*) failed(.|\n)*
 SELECT approx_percentile_cont(c3, 0.95, c1) FROM aggregate_test_100
 
-statement error DataFusion error: Error during planning: Error during 
planning: Coercion from \[Int16, Float64, Float64\] to the signature OneOf(.*) 
failed(.|\n)*
+statement error DataFusion error: Error during planning: Error during 
planning: Failed to coerce arguments to satisfy a call to 
approx_percentile_cont function: coercion from \[Int16, Float64, Float64\] to 
the signature OneOf(.*) failed(.|\n)*
 SELECT approx_percentile_cont(c3, 0.95, 111.1) FROM aggregate_test_100
 
-statement error DataFusion error: Error during planning: Error during 
planning: Coercion from \[Float64, Float64, Float64\] to the signature 
OneOf(.*) failed(.|\n)*
+statement error DataFusion error: Error during planning: Error during 
planning: Failed to coerce arguments to satisfy a call to 
approx_percentile_cont function: coercion from \[Float64, Float64, Float64\] to 
the signature OneOf(.*) failed(.|\n)*
 SELECT approx_percentile_cont(c12, 0.95, 111.1) FROM aggregate_test_100
 
 statement error DataFusion error: This feature is not implemented: Percentile 
value for 'APPROX_PERCENTILE_CONT' must be a literal
diff --git a/datafusion/sqllogictest/test_files/errors.slt 
b/datafusion/sqllogictest/test_files/errors.slt
index b99bbf3dc0..14d5678e50 100644
--- a/datafusion/sqllogictest/test_files/errors.slt
+++ b/datafusion/sqllogictest/test_files/errors.slt
@@ -108,11 +108,11 @@ query error
 select avg(c1, c12) from aggregate_test_100;
 
 # AggregateFunction with wrong argument type
-statement error Coercion
+statement error DataFusion error: Error during planning: Error during 
planning: Failed to coerce arguments to satisfy a call to regr_slope function: 
coercion from
 select regr_slope(1, '2');
 
 # WindowFunction using AggregateFunction wrong signature
-statement error Coercion
+statement error DataFusion error: Error during planning: Error during 
planning: Failed to coerce arguments to satisfy a call to regr_slope function: 
coercion from
 select
 c9,
 regr_slope(c11, '2') over () as min1
@@ -120,7 +120,7 @@ from aggregate_test_100
 order by c9
 
 # WindowFunction wrong signature
-statement error DataFusion error: Error during planning: Error during 
planning: Coercion from \[Int32, Int64, Int64\] to the signature 
OneOf\(\[Any\(0\), Any\(1\), Any\(2\)\]\) failed
+statement error DataFusion error: Error during planning: Error during 
planning: Failed to coerce arguments to satisfy a call to nth_value function: 
coercion from \[Int32, Int64, Int64\] to the signature OneOf\(\[Any\(0\), 
Any\(1\), Any\(2\)\]\) failed
 select
 c9,
 nth_value(c5, 2, 3) over (order by c9) as nv1
diff --git a/datafusion/sqllogictest/test_files/expr.slt 
b/datafusion/sqllogictest/test_files/expr.slt
index 499d279515..9b8dfc2186 100644
--- a/datafusion/sqllogictest/test_files/expr.slt
+++ b/datafusion/sqllogictest/test_files/expr.slt
@@ -560,7 +560,7 @@ select repeat('-1.2', arrow_cast(3, 'Int32'));
 ----
 -1.2-1.2-1.2
 
-query error DataFusion error: Error during planning: Error during planning: 
Coercion from \[Utf8, Float64\] to the signature
+query error DataFusion error: Error during planning: Error during planning: 
Failed to coerce arguments to satisfy a call to repeat function: coercion from 
\[Utf8, Float64\] to the signature
 select repeat('-1.2', 3.2);
 
 query T


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

Reply via email to