Skip to content

Extend CastExpr and Converge on Single Cast Expression #20164

Closed

Description

@kosiew

Enhance the logical CastExpr to support struct-aware casting, field-level nullability checks, and schema-aware validation. Once complete, retire CastColumnExpr by updating the adapter to emit the enhanced CastExpr and consolidate all casting logic into one expression type.

Scope

This is a follow-up to #20163. It aims to unify casting behavior across the planner and execution layers through a single enhanced expression.

Tasks

Logical Expression Enhancement:

  • Extend CastExpr in datafusion/expr/ to support struct field casting
  • Add field-level nullability and metadata propagation to CastExpr
  • Implement schema-aware validation for target types

Physical Planning & Adapter Integration:

  • Update physical planner to convert enhanced CastExpr → optimized physical cast
  • Update PhysicalExprAdapter to emit enhanced CastExpr instead of CastColumnExpr
  • Ensure adapter-generated casts are compatible with new CastExpr semantics

Testing & Documentation:

  • Deprecate CastColumnExpr (or convert to thin wrapper)
  • Add SQL logic tests for struct casts in standard SQL queries
  • Remove or archive CastColumnExpr once fully transitioned
  • Update casting documentation to reflect unified behavior

Files/Modules

  • datafusion/expr/src/expr.rs (CastExpr extension)
  • datafusion/physical-planner/ (logical → physical conversion)
  • datafusion/physical-expr-adapter/src/schema_rewriter.rs (adapter integration)
  • datafusion/sqllogictest/test_files/ (new SLT tests for struct casting)

Acceptance Criteria

✅ Enhanced CastExpr supports all CastColumnExpr features
✅ Physical planner correctly converts enhanced CastExpr to physical cast
✅ Adapter emits enhanced CastExpr for schema adaptation
✅ CastColumnExpr is deprecated or removed
✅ SQL logic tests validate struct casts work end-to-end
✅ All existing cast tests continue to pass
✅ Code compiles without warnings

Notes

  • Proto deferred: Serialization support for enhanced CastExpr and CastColumnExpr is deferred to keep this PR focused on logical/physical expression unification.
  • Design consideration: Ensure enhanced CastExpr doesn't become too broad; keep logical cast semantics clear while allowing physical optimization.

Future: Clean-Up and Removal

Once this issue is complete and CastExpr has been adopted everywhere:

  • Archive or completely remove CastColumnExpr
  • Update documentation to guide users on struct casting in SQL
  • Consider whether other physical expressions could benefit from similar unification patterns

Activity

  1. kosiew commented on Mar 10, 2026

    @kosiew
    ContributorAuthor

    #20814 adds these changes:

    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.
  2. added a commit that references this issue on Apr 14, 2026
    0a8b044
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions