Skip to content

Commit

Permalink
Report current operation when coercion fails
Browse files Browse the repository at this point in the history
This makes for a more actionable error message.
  • Loading branch information
findepi committed Dec 3, 2024
1 parent 172e557 commit 766c348
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
39 changes: 31 additions & 8 deletions datafusion/expr/src/type_coercion/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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(
operation: impl AsRef<str>,
current_types: &[DataType],
signature: &Signature,
) -> Result<Vec<DataType>> {
Expand All @@ -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(
operation,
valid_types,
current_types,
&signature.type_signature,
)
}

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

fn try_coerce_types(
operation: impl AsRef<str>,
valid_types: Vec<Vec<DataType>>,
current_types: &[DataType],
type_signature: &TypeSignature,
Expand Down Expand Up @@ -218,7 +240,8 @@ fn try_coerce_types(

// none possible -> Error
plan_err!(
"Coercion from {:?} to the signature {:?} failed.",
"{}: coercion from {:?} to the signature {:?} failed.",
operation.as_ref(),
current_types,
type_signature
)
Expand Down Expand Up @@ -971,23 +994,23 @@ 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
let signature = Signature::exact(
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.
let signature = Signature::exact(
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(())
Expand Down
4 changes: 2 additions & 2 deletions datafusion/optimizer/src/analyzer/type_coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: MY_AVG: coercion from [Utf8] to the signature Uniform(1, [Float64]) failed")
);
Ok(())
}
Expand Down Expand Up @@ -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: avg: coercion from [Utf8] to the signature Uniform(1, [Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64, Float32, Float64]) failed."));
Ok(())
}

Expand Down
12 changes: 6 additions & 6 deletions datafusion/sqllogictest/test_files/aggregate.slt
Original file line number Diff line number Diff line change
Expand Up @@ -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: approx_percentile_cont_with_weight: 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: approx_percentile_cont_with_weight: 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: approx_percentile_cont_with_weight: 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: approx_percentile_cont: 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: approx_percentile_cont: 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: approx_percentile_cont: 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
Expand Down
6 changes: 3 additions & 3 deletions datafusion/sqllogictest/test_files/errors.slt
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,19 @@ 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: regr_slope: 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: regr_slope: coercion from
select
c9,
regr_slope(c11, '2') over () as min1
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: nth_value: 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
Expand Down
2 changes: 1 addition & 1 deletion datafusion/sqllogictest/test_files/expr.slt
Original file line number Diff line number Diff line change
Expand Up @@ -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: repeat: coercion from \[Utf8, Float64\] to the signature
select repeat('-1.2', 3.2);

query T
Expand Down

0 comments on commit 766c348

Please sign in to comment.