kosiew commented on code in PR #25056:
URL: https://github.com/apache/datafusion/pull/25056#discussion_r4237350405
##########
datafusion/physical-expr/src/expressions/cast.rs:
##########
@@ -2160,6 +2478,102 @@ mod proto_tests {
.expect("cast type should be encoded");
let data_type: DataType = arrow_type.try_into().unwrap();
assert_eq!(data_type, Int64);
+ assert!(cast_node.target_field.is_none());
+ assert!(cast_node.cast_options.is_none());
+ }
+
+ #[test]
+ fn cast_target_field_survives_proto_round_trip() {
+ let schema = Schema::new(vec![Field::new("a", Int32, true)]);
+ let target = Arc::new(Field::new("target", Int64, false).with_metadata(
+ Metadata::from([("extension".to_string(), "value".to_string())]),
+ ));
+ let cast = CastExpr::new_with_target_field(
+ col("a", &schema).unwrap(),
+ Arc::clone(&target),
+ None,
+ );
+
+ assert!(encode_cast(&cast).target_field.is_some());
+ let decoded = round_trip_cast(&cast, &schema);
+
+ assert_eq!(decoded.target_field(), &target);
+ assert_eq!(decoded.target_metadata(), Some(target.metadata()));
+ assert_eq!(decoded.target_nullable(), Some(false));
+ assert!(!decoded.return_field(&schema).unwrap().is_nullable());
+ }
+
+ #[test]
+ fn explicit_default_shaped_cast_target_is_encoded() {
+ let schema = Schema::new(vec![Field::new("a", Int32,
false).with_metadata(
+ Metadata::from([("source".to_string(), "value".to_string())]),
+ )]);
+ let cast = CastExpr::new_with_target_field(
+ col("a", &schema).unwrap(),
+ Int64.into_nullable_field_ref(),
+ None,
+ );
+
+ assert!(encode_cast(&cast).target_field.is_some());
+ let decoded = round_trip_cast(&cast, &schema);
+ assert_eq!(decoded.target_metadata(), Some(&Metadata::default()));
+ assert_eq!(decoded.target_nullable(), Some(true));
+ let output = decoded.return_field(&schema).unwrap();
+ assert!(output.metadata().is_empty());
+ assert!(output.is_nullable());
+ }
+
+ #[test]
+ fn type_only_cast_target_is_omitted_and_remains_type_only() {
+ let metadata = Metadata::from([("source".to_string(),
"value".to_string())]);
+ let schema = Schema::new(vec![
+ Field::new("a", Int32, false).with_metadata(metadata.clone()),
+ ]);
+ let cast = CastExpr::new(col("a", &schema).unwrap(), Int64, None);
+
+ assert!(encode_cast(&cast).target_field.is_none());
+ let decoded = round_trip_cast(&cast, &schema);
+
+ assert_eq!(decoded.target_metadata(), None);
+ assert_eq!(decoded.target_nullable(), None);
+ let output = decoded.return_field(&schema).unwrap();
+ assert_eq!(output.metadata(), &metadata);
+ assert!(!output.is_nullable());
+ }
+
+ #[test]
+ fn cast_options_survive_proto_round_trip() {
Review Comment:
Could we add a wire round-trip test in `datafusion/proto/tests/cases/plans/`
using a `ProjectionExec` that casts an invalid string like "9.1" from Utf8 to
Int32? It would be useful to serialize and deserialize the plan with
`physical_plan_to_bytes` and `physical_plan_from_bytes`, then verify that both
plans return NULL with `safe=true` and an error with `safe=false`. The existing
test checks option equality but not execution behavior across the public
serialization path. This can be a follow-up.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]