Skip to content

Commit 4f7ce26

Browse files
authored
feat: include source field context in cast errors (apache#24920)
## Which issue does this PR close? - Closes apache#24919. ## Rationale for this change Runtime cast errors currently identify the invalid value and target type but not the source field. This makes failures difficult to diagnose when a query contains multiple casts or an application processes many fields. Including the source field in the error provides actionable context while retaining the original Arrow error as the cause. ## What changes are included in this PR? This PR adds context to errors returned by `CastExpr::evaluate`: - Resolves the child expression's output field from the input schema when a cast fails. - Reports the source field name and the source and target data types. - Falls back to the physical expression when a field name cannot be resolved. - Preserves the original cast error as the underlying cause. For example, the error now looks like: ```text Failed to cast field 'a' from Utf8View to Int32 caused by Arrow error: Cast error: Cannot cast string '2619.200945' to value of Int32 type ``` ## Are these changes tested? Yes. The existing invalid cast unit test now verifies the complete error, including the source field, source and target data types, and original Arrow error. The following checks pass: ```shell cargo fmt --all cargo test -p datafusion-physical-expr invalid_cast_with_options_error cargo clippy --all-targets --all-features -- -D warnings ``` The behavior was also verified end to end with the DataFusion CLI using the reproduction from apache#24919. ## Are there any user-facing changes? Yes. Runtime cast error messages now include the source field and source and target data types. The original cast error remains available as the cause. There are no public API changes.
1 parent a5a525e commit 4f7ce26

2 files changed

Lines changed: 58 additions & 13 deletions

File tree

‎datafusion/physical-expr/src/expressions/cast.rs‎

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,22 @@ impl PhysicalExpr for CastExpr {
330330

331331
fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> {
332332
let value = self.expr.evaluate(batch)?;
333-
value.cast_to(self.cast_type(), Some(&self.cast_options))
333+
value
334+
.cast_to(self.cast_type(), Some(&self.cast_options))
335+
.map_err(|error| {
336+
let source = self
337+
.expr
338+
.return_field(batch.schema().as_ref())
339+
.ok()
340+
.filter(|field| !field.name().is_empty())
341+
.map(|field| format!("field '{}'", field.name()))
342+
.unwrap_or_else(|| format!("expression '{}'", self.expr));
343+
error.context(format!(
344+
"Failed to cast {source} from {} to {}",
345+
value.data_type(),
346+
self.cast_type()
347+
))
348+
})
334349
}
335350

336351
fn return_field(&self, input_schema: &Schema) -> Result<FieldRef> {
@@ -769,7 +784,11 @@ mod tests {
769784
let expression =
770785
cast_with_options(col("a", &schema)?, &schema, Decimal128(6, 2), None)?;
771786
let e = expression.evaluate(&batch).unwrap_err().strip_backtrace(); // panics on OK
772-
assert_snapshot!(e, @"Arrow error: Invalid argument error: 123456.79 is too large to store in a Decimal128 of precision 6. Max is 9999.99");
787+
assert_snapshot!(e, @r"
788+
Failed to cast field 'a' from Decimal128(10, 3) to Decimal128(6, 2)
789+
caused by
790+
Arrow error: Invalid argument error: 123456.79 is too large to store in a Decimal128 of precision 6. Max is 9999.99
791+
");
773792
// safe cast should return null
774793
let expression_safe = cast_with_options(
775794
col("a", &schema)?,
@@ -1088,25 +1107,51 @@ mod tests {
10881107

10891108
#[test]
10901109
fn invalid_cast_with_options_error() -> Result<()> {
1091-
// Ensure a useful error happens at plan time if invalid casts are used
1110+
// Ensure a useful error happens at runtime if invalid casts are used
10921111
let schema = Schema::new(vec![Field::new("a", Utf8, false)]);
10931112
let a = StringArray::from(vec!["9.1"]);
10941113
let batch = RecordBatch::try_new(Arc::new(schema.clone()), vec![Arc::new(a)])?;
10951114
let expression = cast_with_options(col("a", &schema)?, &schema, Int32, None)?;
10961115
let result = expression.evaluate(&batch);
10971116

1098-
match result {
1099-
Ok(_) => panic!("expected error"),
1100-
Err(e) => {
1101-
assert!(
1102-
e.to_string()
1103-
.contains("Cannot cast string '9.1' to value of Int32 type")
1104-
)
1105-
}
1106-
}
1117+
let error = result.expect_err("expected error").strip_backtrace();
1118+
assert_eq!(
1119+
error,
1120+
"Failed to cast field 'a' from Utf8 to Int32\n\
1121+
caused by\n\
1122+
Arrow error: Cast error: Cannot cast string '9.1' to value of Int32 type"
1123+
);
11071124
Ok(())
11081125
}
11091126

1127+
#[test]
1128+
fn invalid_cast_with_empty_field_name_uses_expression() {
1129+
let schema = Schema::new(vec![Field::new("", Utf8, false)]);
1130+
let batch = RecordBatch::try_new(
1131+
Arc::new(schema.clone()),
1132+
vec![Arc::new(StringArray::from(vec!["9.1"]))],
1133+
)
1134+
.expect("valid record batch");
1135+
let expression = cast_with_options(
1136+
col("", &schema).expect("valid column"),
1137+
&schema,
1138+
Int32,
1139+
None,
1140+
)
1141+
.expect("valid cast expression");
1142+
1143+
let error = expression
1144+
.evaluate(&batch)
1145+
.expect_err("expected error")
1146+
.strip_backtrace();
1147+
assert_eq!(
1148+
error,
1149+
"Failed to cast expression '@0' from Utf8 to Int32\n\
1150+
caused by\n\
1151+
Arrow error: Cast error: Cannot cast string '9.1' to value of Int32 type"
1152+
);
1153+
}
1154+
11101155
#[test]
11111156
fn field_aware_cast_preserves_target_field_semantics() -> Result<()> {
11121157
// Target field metadata should be preserved exactly (no merging with source).

‎datafusion/sqllogictest/test_files/errors.slt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ statement error
4242
SELECT sin(c1) FROM aggregate_test_100
4343

4444
# cast_expressions_error
45-
statement error DataFusion error: Arrow error: Cast error: Cannot cast string 'c' to value of Int32 type
45+
statement error DataFusion error: Failed to cast field 'c1' from Utf8View to Int32\ncaused by\nArrow error: Cast error: Cannot cast string 'c' to value of Int32 type
4646
SELECT CAST(c1 AS INT) FROM aggregate_test_100
4747

4848
# aggregation_with_bad_arguments

0 commit comments

Comments
 (0)