You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by gi...@apache.org on 2026/09/06 04:16:09 UTC

(datafusion) branch gh-readonly-queue/main/pr-24920-a5a525e1d67a6edfb9c29b4d470cf45778e872d6 created (now 4f7ce268f8)

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

github-merge-queue[bot] pushed a change to branch gh-readonly-queue/main/pr-24920-a5a525e1d67a6edfb9c29b4d470cf45778e872d6
in repository https://gitbox.apache.org/repos/asf/datafusion.git


      at 4f7ce268f8 feat: include source field context in cast errors (#24920)

This branch includes the following new commits:

     new 4f7ce268f8 feat: include source field context in cast errors (#24920)

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org


(datafusion) 01/01: feat: include source field context in cast errors (#24920)

Posted by gi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-24920-a5a525e1d67a6edfb9c29b4d470cf45778e872d6
in repository https://gitbox.apache.org/repos/asf/datafusion.git

commit 4f7ce268f8a506a2465281fc2c7cc713cf8dd337
Author: Huaijin <ha...@gmail.com>
AuthorDate: Sun Sep 6 04:15:47 2026 +0000

    feat: include source field context in cast errors (#24920)
    
    ## Which issue does this PR close?
    
    - Closes #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 #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.
---
 datafusion/physical-expr/src/expressions/cast.rs | 69 +++++++++++++++++++-----
 datafusion/sqllogictest/test_files/errors.slt    |  2 +-
 2 files changed, 58 insertions(+), 13 deletions(-)

diff --git a/datafusion/physical-expr/src/expressions/cast.rs b/datafusion/physical-expr/src/expressions/cast.rs
index 565d396dad..845b9608d9 100644
--- a/datafusion/physical-expr/src/expressions/cast.rs
+++ b/datafusion/physical-expr/src/expressions/cast.rs
@@ -330,7 +330,22 @@ impl PhysicalExpr for CastExpr {
 
     fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> {
         let value = self.expr.evaluate(batch)?;
-        value.cast_to(self.cast_type(), Some(&self.cast_options))
+        value
+            .cast_to(self.cast_type(), Some(&self.cast_options))
+            .map_err(|error| {
+                let source = self
+                    .expr
+                    .return_field(batch.schema().as_ref())
+                    .ok()
+                    .filter(|field| !field.name().is_empty())
+                    .map(|field| format!("field '{}'", field.name()))
+                    .unwrap_or_else(|| format!("expression '{}'", self.expr));
+                error.context(format!(
+                    "Failed to cast {source} from {} to {}",
+                    value.data_type(),
+                    self.cast_type()
+                ))
+            })
     }
 
     fn return_field(&self, input_schema: &Schema) -> Result<FieldRef> {
@@ -769,7 +784,11 @@ mod tests {
         let expression =
             cast_with_options(col("a", &schema)?, &schema, Decimal128(6, 2), None)?;
         let e = expression.evaluate(&batch).unwrap_err().strip_backtrace(); // panics on OK
-        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");
+        assert_snapshot!(e, @r"
+        Failed to cast field 'a' from Decimal128(10, 3) to Decimal128(6, 2)
+        caused by
+        Arrow error: Invalid argument error: 123456.79 is too large to store in a Decimal128 of precision 6. Max is 9999.99
+        ");
         // safe cast should return null
         let expression_safe = cast_with_options(
             col("a", &schema)?,
@@ -1088,25 +1107,51 @@ mod tests {
 
     #[test]
     fn invalid_cast_with_options_error() -> Result<()> {
-        // Ensure a useful error happens at plan time if invalid casts are used
+        // Ensure a useful error happens at runtime if invalid casts are used
         let schema = Schema::new(vec![Field::new("a", Utf8, false)]);
         let a = StringArray::from(vec!["9.1"]);
         let batch = RecordBatch::try_new(Arc::new(schema.clone()), vec![Arc::new(a)])?;
         let expression = cast_with_options(col("a", &schema)?, &schema, Int32, None)?;
         let result = expression.evaluate(&batch);
 
-        match result {
-            Ok(_) => panic!("expected error"),
-            Err(e) => {
-                assert!(
-                    e.to_string()
-                        .contains("Cannot cast string '9.1' to value of Int32 type")
-                )
-            }
-        }
+        let error = result.expect_err("expected error").strip_backtrace();
+        assert_eq!(
+            error,
+            "Failed to cast field 'a' from Utf8 to Int32\n\
+             caused by\n\
+             Arrow error: Cast error: Cannot cast string '9.1' to value of Int32 type"
+        );
         Ok(())
     }
 
+    #[test]
+    fn invalid_cast_with_empty_field_name_uses_expression() {
+        let schema = Schema::new(vec![Field::new("", Utf8, false)]);
+        let batch = RecordBatch::try_new(
+            Arc::new(schema.clone()),
+            vec![Arc::new(StringArray::from(vec!["9.1"]))],
+        )
+        .expect("valid record batch");
+        let expression = cast_with_options(
+            col("", &schema).expect("valid column"),
+            &schema,
+            Int32,
+            None,
+        )
+        .expect("valid cast expression");
+
+        let error = expression
+            .evaluate(&batch)
+            .expect_err("expected error")
+            .strip_backtrace();
+        assert_eq!(
+            error,
+            "Failed to cast expression '@0' from Utf8 to Int32\n\
+             caused by\n\
+             Arrow error: Cast error: Cannot cast string '9.1' to value of Int32 type"
+        );
+    }
+
     #[test]
     fn field_aware_cast_preserves_target_field_semantics() -> Result<()> {
         // Target field metadata should be preserved exactly (no merging with source).
diff --git a/datafusion/sqllogictest/test_files/errors.slt b/datafusion/sqllogictest/test_files/errors.slt
index 5d3ddd43f0..55aa405ae3 100644
--- a/datafusion/sqllogictest/test_files/errors.slt
+++ b/datafusion/sqllogictest/test_files/errors.slt
@@ -42,7 +42,7 @@ statement error
 SELECT sin(c1) FROM aggregate_test_100
 
 # cast_expressions_error
-statement error DataFusion error: Arrow error: Cast error: Cannot cast string 'c' to value of Int32 type
+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
 SELECT CAST(c1 AS INT) FROM aggregate_test_100
 
 # aggregation_with_bad_arguments


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org