Skip to content

Commit 55e56c4

Browse files
authored
Report current operation when coercion fails (#13628)
This makes for a more actionable error message.
1 parent c62ab39 commit 55e56c4

File tree

5 files changed

+43
-20
lines changed

5 files changed

+43
-20
lines changed

datafusion/expr/src/type_coercion/functions.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ pub fn data_types_with_scalar_udf(
6464
return Ok(current_types.to_vec());
6565
}
6666

67-
try_coerce_types(valid_types, current_types, &signature.type_signature)
67+
try_coerce_types(
68+
func.name(),
69+
valid_types,
70+
current_types,
71+
&signature.type_signature,
72+
)
6873
}
6974

7075
/// Performs type coercion for aggregate function arguments.
@@ -100,7 +105,12 @@ pub fn data_types_with_aggregate_udf(
100105
return Ok(current_types.to_vec());
101106
}
102107

103-
try_coerce_types(valid_types, current_types, &signature.type_signature)
108+
try_coerce_types(
109+
func.name(),
110+
valid_types,
111+
current_types,
112+
&signature.type_signature,
113+
)
104114
}
105115

106116
/// Performs type coercion for window function arguments.
@@ -133,7 +143,12 @@ pub fn data_types_with_window_udf(
133143
return Ok(current_types.to_vec());
134144
}
135145

136-
try_coerce_types(valid_types, current_types, &signature.type_signature)
146+
try_coerce_types(
147+
func.name(),
148+
valid_types,
149+
current_types,
150+
&signature.type_signature,
151+
)
137152
}
138153

139154
/// Performs type coercion for function arguments.
@@ -144,6 +159,7 @@ pub fn data_types_with_window_udf(
144159
/// For more details on coercion in general, please see the
145160
/// [`type_coercion`](crate::type_coercion) module.
146161
pub fn data_types(
162+
function_name: impl AsRef<str>,
147163
current_types: &[DataType],
148164
signature: &Signature,
149165
) -> Result<Vec<DataType>> {
@@ -166,7 +182,12 @@ pub fn data_types(
166182
return Ok(current_types.to_vec());
167183
}
168184

169-
try_coerce_types(valid_types, current_types, &signature.type_signature)
185+
try_coerce_types(
186+
function_name,
187+
valid_types,
188+
current_types,
189+
&signature.type_signature,
190+
)
170191
}
171192

172193
fn is_well_supported_signature(type_signature: &TypeSignature) -> bool {
@@ -187,6 +208,7 @@ fn is_well_supported_signature(type_signature: &TypeSignature) -> bool {
187208
}
188209

189210
fn try_coerce_types(
211+
function_name: impl AsRef<str>,
190212
valid_types: Vec<Vec<DataType>>,
191213
current_types: &[DataType],
192214
type_signature: &TypeSignature,
@@ -218,7 +240,8 @@ fn try_coerce_types(
218240

219241
// none possible -> Error
220242
plan_err!(
221-
"Coercion from {:?} to the signature {:?} failed.",
243+
"Failed to coerce arguments to satisfy a call to {} function: coercion from {:?} to the signature {:?} failed.",
244+
function_name.as_ref(),
222245
current_types,
223246
type_signature
224247
)
@@ -971,23 +994,23 @@ mod tests {
971994
Volatility::Stable,
972995
);
973996

974-
let coerced_data_types = data_types(&current_types, &signature).unwrap();
997+
let coerced_data_types = data_types("test", &current_types, &signature).unwrap();
975998
assert_eq!(coerced_data_types, current_types);
976999

9771000
// make sure it can't coerce to a different size
9781001
let signature = Signature::exact(
9791002
vec![DataType::FixedSizeList(Arc::clone(&inner), 3)],
9801003
Volatility::Stable,
9811004
);
982-
let coerced_data_types = data_types(&current_types, &signature);
1005+
let coerced_data_types = data_types("test", &current_types, &signature);
9831006
assert!(coerced_data_types.is_err());
9841007

9851008
// make sure it works with the same type.
9861009
let signature = Signature::exact(
9871010
vec![DataType::FixedSizeList(Arc::clone(&inner), 2)],
9881011
Volatility::Stable,
9891012
);
990-
let coerced_data_types = data_types(&current_types, &signature).unwrap();
1013+
let coerced_data_types = data_types("test", &current_types, &signature).unwrap();
9911014
assert_eq!(coerced_data_types, current_types);
9921015

9931016
Ok(())

datafusion/optimizer/src/analyzer/type_coercion.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,7 +1357,7 @@ mod test {
13571357

13581358
let err = Projection::try_new(vec![udaf], empty).err().unwrap();
13591359
assert!(
1360-
err.strip_backtrace().starts_with("Error during planning: Error during planning: Coercion from [Utf8] to the signature Uniform(1, [Float64]) failed")
1360+
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")
13611361
);
13621362
Ok(())
13631363
}
@@ -1407,7 +1407,7 @@ mod test {
14071407
.err()
14081408
.unwrap()
14091409
.strip_backtrace();
1410-
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."));
1410+
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."));
14111411
Ok(())
14121412
}
14131413

datafusion/sqllogictest/test_files/aggregate.slt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,26 +76,26 @@ statement error DataFusion error: Schema error: Schema contains duplicate unqual
7676
SELECT approx_distinct(c9) count_c9, approx_distinct(cast(c9 as varchar)) count_c9_str FROM aggregate_test_100
7777

7878
# csv_query_approx_percentile_cont_with_weight
79-
statement error DataFusion error: Error during planning: Error during planning: Coercion from \[Utf8, Int8, Float64\] to the signature OneOf(.*) failed(.|\n)*
79+
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)*
8080
SELECT approx_percentile_cont_with_weight(c1, c2, 0.95) FROM aggregate_test_100
8181

82-
statement error DataFusion error: Error during planning: Error during planning: Coercion from \[Int16, Utf8, Float64\] to the signature OneOf(.*) failed(.|\n)*
82+
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)*
8383
SELECT approx_percentile_cont_with_weight(c3, c1, 0.95) FROM aggregate_test_100
8484

85-
statement error DataFusion error: Error during planning: Error during planning: Coercion from \[Int16, Int8, Utf8\] to the signature OneOf(.*) failed(.|\n)*
85+
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)*
8686
SELECT approx_percentile_cont_with_weight(c3, c2, c1) FROM aggregate_test_100
8787

8888
# csv_query_approx_percentile_cont_with_histogram_bins
8989
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\)\.
9090
SELECT c1, approx_percentile_cont(c3, 0.95, -1000) AS c3_p95 FROM aggregate_test_100 GROUP BY 1 ORDER BY 1
9191

92-
statement error DataFusion error: Error during planning: Error during planning: Coercion from \[Int16, Float64, Utf8\] to the signature OneOf(.*) failed(.|\n)*
92+
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)*
9393
SELECT approx_percentile_cont(c3, 0.95, c1) FROM aggregate_test_100
9494

95-
statement error DataFusion error: Error during planning: Error during planning: Coercion from \[Int16, Float64, Float64\] to the signature OneOf(.*) failed(.|\n)*
95+
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)*
9696
SELECT approx_percentile_cont(c3, 0.95, 111.1) FROM aggregate_test_100
9797

98-
statement error DataFusion error: Error during planning: Error during planning: Coercion from \[Float64, Float64, Float64\] to the signature OneOf(.*) failed(.|\n)*
98+
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)*
9999
SELECT approx_percentile_cont(c12, 0.95, 111.1) FROM aggregate_test_100
100100

101101
statement error DataFusion error: This feature is not implemented: Percentile value for 'APPROX_PERCENTILE_CONT' must be a literal

datafusion/sqllogictest/test_files/errors.slt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,19 +108,19 @@ query error
108108
select avg(c1, c12) from aggregate_test_100;
109109

110110
# AggregateFunction with wrong argument type
111-
statement error Coercion
111+
statement error DataFusion error: Error during planning: Error during planning: Failed to coerce arguments to satisfy a call to regr_slope function: coercion from
112112
select regr_slope(1, '2');
113113

114114
# WindowFunction using AggregateFunction wrong signature
115-
statement error Coercion
115+
statement error DataFusion error: Error during planning: Error during planning: Failed to coerce arguments to satisfy a call to regr_slope function: coercion from
116116
select
117117
c9,
118118
regr_slope(c11, '2') over () as min1
119119
from aggregate_test_100
120120
order by c9
121121

122122
# WindowFunction wrong signature
123-
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
123+
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
124124
select
125125
c9,
126126
nth_value(c5, 2, 3) over (order by c9) as nv1

datafusion/sqllogictest/test_files/expr.slt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ select repeat('-1.2', arrow_cast(3, 'Int32'));
560560
----
561561
-1.2-1.2-1.2
562562

563-
query error DataFusion error: Error during planning: Error during planning: Coercion from \[Utf8, Float64\] to the signature
563+
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
564564
select repeat('-1.2', 3.2);
565565

566566
query T

0 commit comments

Comments
 (0)