adriangb commented on code in PR #24670:
URL: https://github.com/apache/datafusion/pull/24670#discussion_r3898881697
##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -71,18 +71,23 @@ pub trait ExprSchemable {
-> Result<(DataType, bool)>;
}
-/// Derives the output field for a cast expression from the source field.
+/// Derives the output field for a cast expression from the source field, using
+/// explicit target metadata when supplied.
/// For `TryCast`, `force_nullable` is `true` since a failed cast returns NULL.
fn cast_output_field(
source_field: &FieldRef,
- target_type: &DataType,
+ target: &CastTarget,
force_nullable: bool,
) -> Arc<Field> {
+ let metadata = target
+ .metadata()
+ .cloned()
+ .unwrap_or_else(|| source_field.metadata().clone());
let mut f = source_field
.as_ref()
.clone()
- .with_data_type(target_type.clone())
- .with_metadata(source_field.metadata().clone());
+ .with_data_type(target.data_type().clone())
+ .with_metadata(metadata);
Review Comment:
This is the only change I needed on top of `projection.rs` to make the UUID
test pass (counterfactual confirmed: reverting just this reproduces the CI
failure).
Note your callsite changes below — `cast_output_field(&src, field, false)` /
`(&src, field, true)` — work **unchanged** with this, since `Cast.field` would
go back to being a `FieldRef`. So this suggestion plus dropping `CastTarget` is
the whole edit; the other ~20 files in the stack come out with it.
```suggestion
/// Derives the output field for a cast expression from the source field.
///
/// The cast target's metadata is authoritative when it carries any;
otherwise the
/// source's metadata is inherited. This mirrors the physical `CastExpr`,
whose
/// target field is already authoritative when it is not the synthesized
type-only
/// field.
///
/// For `TryCast`, `force_nullable` is `true` since a failed cast returns
NULL.
fn cast_output_field(
source_field: &FieldRef,
target_field: &FieldRef,
force_nullable: bool,
) -> Arc<Field> {
let metadata = if target_field.metadata().is_empty() {
source_field.metadata().clone()
} else {
target_field.metadata().clone()
};
let mut f = source_field
.as_ref()
.clone()
.with_data_type(target_field.data_type().clone())
.with_metadata(metadata);
```
--
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]