Commit fd97799
authored
Make Physical CastExpr Field-aware and unify cast semantics across physical expressions (#20814)
## Which issue does this PR close?
* Part of #20164
## Rationale for this change
Physical `CastExpr` previously stored only a target `DataType`. This
caused field-level semantics (name, nullability, and metadata) to be
lost when casts were represented in the physical layer. In contrast,
logical expressions already carry this information through `FieldRef`.
This mismatch created several issues:
* Physical and logical cast representations diverged in how they
preserve schema semantics.
* Struct casting logic behaved differently depending on whether the cast
was represented as `CastExpr` or `CastColumnExpr`.
* Downstream components (such as schema rewriting and ordering
equivalence analysis) required additional branching and duplicated
logic.
Making `CastExpr` field-aware aligns the physical representation with
logical semantics and enables consistent schema propagation across
execution planning and expression evaluation.
## What changes are included in this PR?
This PR introduces field-aware semantics to `CastExpr` and simplifies
several areas that previously relied on type-only casting.
Key changes include:
1. **Field-aware CastExpr**
* Replace the `cast_type: DataType` field with `target_field: FieldRef`.
* Add `new_with_target_field` constructor to explicitly construct
field-aware casts.
* Keep the existing `new(expr, DataType)` constructor as a compatibility
shim that creates a canonical field.
2. **Return-field and nullability behavior**
* `return_field` now returns the full `target_field`, preserving name,
nullability, and metadata.
* `nullable()` now derives its result from the resolved target field
rather than the input expression.
* Add compatibility logic for legacy type-only casts to preserve
previous behavior.
3. **Struct cast validation improvements**
* Struct-to-struct casting now validates compatibility using field
information before execution.
* Planning-time validation prevents unsupported casts from reaching
execution.
4. **Shared cast property logic**
* Introduce shared helper functions (`cast_expr_properties`,
`is_order_preserving_cast_family`) for determining ordering
preservation.
* Reuse this logic in both `CastExpr` and `CastColumnExpr` to avoid
duplicated implementations.
5. **Schema rewriter improvements**
* Refactor physical column resolution into `resolve_physical_column`.
* Simplify cast insertion logic when logical and physical fields differ.
* Pass explicit physical and logical fields to cast creation for
improved correctness.
6. **Ordering equivalence simplification**
* Introduce `substitute_cast_like_ordering` helper to unify handling of
`CastExpr` and `CastColumnExpr` in ordering equivalence analysis.
7. **Additional unit tests**
* Validate metadata propagation through `return_field`.
* Verify nullability behavior for field-aware casts.
* Ensure legacy type-only casts preserve existing semantics.
* Test struct-cast validation with nested field semantics.
## Are these changes tested?
Yes.
New unit tests were added in `physical-expr/src/expressions/cast.rs` to
verify:
* Metadata propagation through field-aware casts
* Correct nullability behavior derived from the target field
* Backward compatibility with legacy type-only constructors
* Struct cast compatibility validation using nested fields
Existing tests continue to pass and validate compatibility with the
previous API behavior.
## Are there any user-facing changes?
There are no direct user-facing behavior changes.
This change primarily improves internal schema semantics and consistency
in the physical expression layer. Existing APIs remain compatible
through the legacy constructor that accepts only a `DataType`.
## LLM-generated code disclosure
This PR includes LLM-generated code and comments. All LLM-generated
content has been manually reviewed and tested.1 parent 84a22ea commit fd97799
File tree
4 files changed
+319
-144
lines changed- datafusion
- physical-expr-adapter/src
- physical-expr/src
- equivalence/properties
- expressions
4 files changed
+319
-144
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | | - | |
| 29 | + | |
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| |||
404 | 404 | | |
405 | 405 | | |
406 | 406 | | |
407 | | - | |
408 | | - | |
409 | | - | |
410 | | - | |
411 | | - | |
412 | | - | |
413 | | - | |
414 | | - | |
415 | | - | |
416 | | - | |
417 | | - | |
418 | | - | |
419 | | - | |
420 | | - | |
421 | | - | |
422 | | - | |
423 | | - | |
424 | | - | |
425 | | - | |
426 | | - | |
427 | | - | |
428 | | - | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
429 | 415 | | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
430 | 425 | | |
431 | | - | |
432 | 426 | | |
433 | | - | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
434 | 430 | | |
435 | 431 | | |
436 | 432 | | |
437 | | - | |
438 | | - | |
439 | | - | |
| 433 | + | |
440 | 434 | | |
441 | | - | |
| 435 | + | |
442 | 436 | | |
443 | 437 | | |
444 | | - | |
445 | | - | |
446 | | - | |
447 | | - | |
448 | | - | |
449 | | - | |
450 | | - | |
| 438 | + | |
| 439 | + | |
451 | 440 | | |
452 | 441 | | |
453 | 442 | | |
454 | | - | |
| 443 | + | |
455 | 444 | | |
456 | 445 | | |
457 | | - | |
458 | | - | |
459 | | - | |
460 | | - | |
461 | | - | |
462 | | - | |
| 446 | + | |
| 447 | + | |
463 | 448 | | |
464 | 449 | | |
465 | | - | |
466 | | - | |
467 | | - | |
468 | | - | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
469 | 465 | | |
470 | | - | |
471 | | - | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
472 | 477 | | |
473 | 478 | | |
474 | 479 | | |
| |||
479 | 484 | | |
480 | 485 | | |
481 | 486 | | |
| 487 | + | |
482 | 488 | | |
483 | 489 | | |
484 | | - | |
485 | | - | |
486 | | - | |
487 | | - | |
488 | | - | |
489 | 490 | | |
490 | 491 | | |
491 | 492 | | |
492 | 493 | | |
493 | 494 | | |
494 | | - | |
| 495 | + | |
495 | 496 | | |
496 | 497 | | |
497 | 498 | | |
498 | 499 | | |
499 | 500 | | |
500 | 501 | | |
501 | 502 | | |
502 | | - | |
503 | | - | |
504 | | - | |
505 | | - | |
| 503 | + | |
| 504 | + | |
506 | 505 | | |
507 | 506 | | |
508 | 507 | | |
509 | 508 | | |
510 | | - | |
| 509 | + | |
511 | 510 | | |
512 | 511 | | |
513 | 512 | | |
| |||
516 | 515 | | |
517 | 516 | | |
518 | 517 | | |
519 | | - | |
| 518 | + | |
520 | 519 | | |
521 | 520 | | |
522 | 521 | | |
| |||
1604 | 1603 | | |
1605 | 1604 | | |
1606 | 1605 | | |
| 1606 | + | |
1607 | 1607 | | |
1608 | 1608 | | |
1609 | 1609 | | |
| |||
Lines changed: 38 additions & 27 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
| 42 | + | |
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| |||
195 | 195 | | |
196 | 196 | | |
197 | 197 | | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
198 | 231 | | |
199 | 232 | | |
200 | 233 | | |
| |||
844 | 877 | | |
845 | 878 | | |
846 | 879 | | |
847 | | - | |
848 | | - | |
849 | | - | |
850 | | - | |
851 | | - | |
852 | | - | |
853 | | - | |
854 | | - | |
855 | | - | |
856 | | - | |
857 | | - | |
858 | | - | |
859 | | - | |
860 | | - | |
861 | | - | |
862 | | - | |
863 | | - | |
864 | | - | |
865 | | - | |
866 | | - | |
867 | | - | |
868 | | - | |
869 | | - | |
870 | | - | |
871 | | - | |
872 | | - | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
873 | 884 | | |
874 | 885 | | |
875 | 886 | | |
| |||
0 commit comments