Skip to content

Commit 92323f7

Browse files
buraksennkosiew
andauthored
fix(proto): preserve physical cast expression state (apache#25056)
## Which issue does this PR close? - Closes apache#24615. ## Rationale for this change Physical cast protobuf hooks did not serialize `CastExpr` options or explicit target-field metadata. A round trip could therefore change cast behavior, nullability, or metadata. The hooks also accessed fields individually, so future fields could be omitted silently. ## What changes are included in this PR? - Exhaustively destructure `CastExpr`, `TryCastExpr`, and their protobuf messages. - Preserve explicit target fields for `CAST` and `TRY_CAST` while keeping legacy type-only payload behavior. - Preserve all serializable Arrow cast/format options; reject custom formatter factories instead of silently dropping them. - Store decoded format strings in owned cast options. - Regenerate protobuf models and document the `CastExpr::cast_options` API adjustment. The protobuf changes are additive and backward compatible. ## What is the testing strategy for this PR? Added unit coverage for: - target-field and cast-option round trips - legacy payloads without the new fields - explicit default-shaped targets - malformed target-field types - unsupported formatter factories Validated with: - `cargo test -p datafusion-physical-expr --features proto expressions::cast::` - `cargo test -p datafusion-physical-expr --features proto expressions::try_cast::` - `cargo clippy -p datafusion-physical-expr -p datafusion-physical-expr-adapter --all-features --tests -- -D warnings` - `cargo check -p datafusion-proto --all-features` - `./ci/scripts/doc_prettier_check.sh --write --allow-dirty` ## Are there any user-facing changes? Cast expressions now retain their options and explicit output-field semantics across protobuf round trips. `CastExpr::cast_options()` returns a borrowing `CastOptions<'_>` value rather than `&CastOptions<'static>`; the upgrade guide documents migration for code rebuilding casts. --------- Co-authored-by: kosiew <[email protected]>
1 parent 7fe6d47 commit 92323f7

10 files changed

Lines changed: 1281 additions & 54 deletions

File tree

‎datafusion/common/src/scalar/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4377,7 +4377,7 @@ impl ScalarValue {
43774377
pub fn cast_to_with_options(
43784378
&self,
43794379
target_type: &DataType,
4380-
cast_options: &CastOptions<'static>,
4380+
cast_options: &CastOptions<'_>,
43814381
) -> Result<Self> {
43824382
let source_type = self.data_type();
43834383

‎datafusion/expr-common/src/columnar_value.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ impl ColumnarValue {
294294
pub fn cast_to(
295295
&self,
296296
cast_type: &DataType,
297-
cast_options: Option<&CastOptions<'static>>,
297+
cast_options: Option<&CastOptions<'_>>,
298298
) -> Result<ColumnarValue> {
299299
let cast_options = cast_options.cloned().unwrap_or(DEFAULT_CAST_OPTIONS);
300300
match self {
@@ -312,7 +312,7 @@ impl ColumnarValue {
312312
fn cast_array_by_name(
313313
array: &ArrayRef,
314314
cast_type: &DataType,
315-
cast_options: &CastOptions<'static>,
315+
cast_options: &CastOptions<'_>,
316316
) -> Result<ArrayRef> {
317317
// If types are already equal, no cast needed
318318
if array.data_type() == cast_type {

‎datafusion/physical-expr-adapter/src/schema_rewriter.rs‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -570,11 +570,9 @@ impl DefaultPhysicalExprAdapterRewriter {
570570
return Ok(None);
571571
};
572572
let mut args = get_field_expr.args().to_vec();
573-
args[0] = Arc::new(CastExpr::new_with_target_field(
574-
Arc::clone(inner),
575-
target_field,
576-
Some(cast.cast_options().clone()),
577-
));
573+
args[0] = Arc::new(
574+
cast.with_new_expr_and_target_field(Arc::clone(inner), target_field),
575+
);
578576
return Arc::clone(expr).with_new_children(args).map(Some);
579577
}
580578

@@ -598,10 +596,9 @@ impl DefaultPhysicalExprAdapterRewriter {
598596
{
599597
return Ok(Some(extracted));
600598
}
601-
Ok(Some(Arc::new(CastExpr::new_with_target_field(
599+
Ok(Some(Arc::new(cast.with_new_expr_and_target_field(
602600
extracted,
603601
logical_return_field,
604-
Some(cast.cast_options().clone()),
605602
))))
606603
}
607604

0 commit comments

Comments
 (0)