Skip to content

Commit def1c98

Browse files
committed
Report current operation when coercion fails
This makes for a more actionable error message.
1 parent 172e557 commit def1c98

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
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+
operation: 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+
operation,
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+
operation: 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+
"{}: coercion from {:?} to the signature {:?} failed.",
244+
operation.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: MY_AVG: 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: avg: coercion from [Utf8] to the signature Uniform(1, [Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64, Float32, Float64]) failed."));
14111411
Ok(())
14121412
}
14131413

0 commit comments

Comments
 (0)