Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions datafusion/physical-expr/src/expressions/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ const DEFAULT_SAFE_CAST_OPTIONS: CastOptions<'static> = CastOptions {
/// planning-time validation matches runtime validation, enabling fail-fast behavior
/// instead of deferring errors to execution. Handles structs at any nesting level
/// (e.g., `List<Struct>`, `Dictionary<_, Struct>`).
fn can_cast_named_struct_types(source: &DataType, target: &DataType) -> bool {
validate_data_type_compatibility("", source, target).is_ok()
fn can_cast_named_struct_types(source: &DataType, target: &DataType) -> Result<bool> {
validate_data_type_compatibility("", source, target).map(|_| true)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This make sure that the error message is not swallowed here.

}

/// CAST expression casts an expression to a specific data type and returns a runtime error on invalid cast
Expand Down Expand Up @@ -399,7 +399,7 @@ pub fn cast_with_target_field(
// applied at planning time (now) to fail fast, rather than deferring errors
// to execution time. The name-based casting logic will be executed at runtime
// via ColumnarValue::cast_to.
can_cast_named_struct_types(&expr_type, cast_type)
can_cast_named_struct_types(&expr_type, cast_type)?
} else {
can_cast_types(&expr_type, cast_type)
};
Expand Down Expand Up @@ -1046,7 +1046,7 @@ mod tests {
let err = cast_with_options(col("a", &schema)?, &schema, invalid_target, None)
.expect_err("missing required struct field should fail");

assert!(err.to_string().contains("Unsupported CAST"));
assert!(err.to_string().contains("Cannot cast struct"));

Ok(())
}
Expand Down
6 changes: 5 additions & 1 deletion datafusion/sqllogictest/test_files/struct.slt
Original file line number Diff line number Diff line change
Expand Up @@ -887,9 +887,13 @@ SELECT CAST({a: 1, b: 2, extra: 3} AS STRUCT(a INT, b INT));
{a: 1, b: 2}

# Test no overlap with mismatched field count - should fail because no field names match
statement error DataFusion error: (Plan error|Error during planning|This feature is not implemented): (Cannot cast struct: at least one field name must match between source and target|Cannot cast struct with 3 fields to 2 fields without name overlap|Unsupported CAST from Struct)
statement error DataFusion error: Error during planning: Cannot cast struct with 3 fields to 2
SELECT CAST(struct(1, 'x', 'y') AS STRUCT(a INT, b VARCHAR));

# Cannot cast to nullable struct field to non nullable struct field
statement error Optimizer rule 'simplify_expressions' failed
select arrow_cast(struct(10), 'Struct("c0": non-null Int64)')

# Test nested struct with field reordering
query ?
SELECT CAST(
Expand Down
Loading