Skip to content

fix[physical-expr-adapter]: support casting structs nested inside complex types#20907

Open
asubiotto wants to merge 1 commit intoapache:mainfrom
polarsignals:asubiotto/cast
Open

fix[physical-expr-adapter]: support casting structs nested inside complex types#20907
asubiotto wants to merge 1 commit intoapache:mainfrom
polarsignals:asubiotto/cast

Conversation

@asubiotto
Copy link
Contributor

Previously, DefaultPhysicalExprAdapterRewriter.create_cast_column_expr would only allow struct evolution for top-level structs. However, it is valid to have structs that are nested inside other complex types (e.g. Dictionary<_, Struct>, List). These cases would previously return an error although they were valid.

This commit handles Structs that are arbitrarily nested within certain complex types (Lists, ListViews, and Dicts). Other complex types can be supported in the future, but I think this is good enough for now.

Which issue does this PR close?

Rationale for this change

Struct evolution is supported, this PR fixes an edge case where struct evolution is not supported when structs are nested inside other complex types.

What changes are included in this PR?

Are these changes tested?

Yes, I initially wrote parametrized tests, but I think that's probably overkill.

Are there any user-facing changes?

Queries that would previously error, now complete successfully.

@github-actions github-actions bot added logical-expr Logical plan and expressions physical-expr Changes to the physical-expr crates common Related to common crate labels Mar 12, 2026
@asubiotto
Copy link
Contributor Author

cc @kosiew since you touched the code most recently. cc @alamb in case this needs to be routed to somebody else.

/// # Arguments
/// * `source_col` - The source array to cast
/// * `target_field` - The target field definition (including type and metadata)
/// * `target_type` - The target data type to cast to
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This refactor made sense. target_field was not used at all, only its inner DataType. There were several callsites that constructed "mock" fields just to be able to call this function with a datatype.

@asubiotto asubiotto changed the title physical-expr-adapter: support casting structs nested inside complex types fix[physical-expr-adapter]: support casting structs nested inside complex types Mar 12, 2026
Copy link
Contributor

@kosiew kosiew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asubiotto
Thanks for working on this.

assert!(b_col.is_null(0));
assert!(b_col.is_null(1));
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new functionality adds fairly intricate ListView and dictionary rebuild logic, but the new tests exercise only the List<Struct> execution path and dictionary compatibility validation.

Do you think It would be worth adding tests for Dictionary<_, Struct> and for ListView<Struct>?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially had parameterized test cases for all types but it was a fair amount of code. I'm not sure, but I'm happy to add them back and we can decide.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added concrete tests instead. Let me know what you think

@asubiotto
Copy link
Contributor Author

Thanks for the review! I will address the comments and have a new version by the middle of next week.

…types

Previously, DefaultPhysicalExprAdapterRewriter.create_cast_column_expr would
only allow struct evolution for top-level structs. However, it is valid to
have structs that are nested inside other complex types (e.g.
Dictionary<_, Struct>, List<Struct>). These cases would previously return an
error although they were valid.

This commit handles Structs that are arbitrarily nested within certain complex
types (Lists, ListViews, and Dicts). Other complex types can be supported in
the future, but I think this is good enough for now.

Signed-off-by: Alfonso Subiotto Marques <alfonso.subiotto@polarsignals.com>
@asubiotto
Copy link
Contributor Author

Addressed the comments. I'm realizing now that this struct casting is not supported if you're also doing an outer cast (list->listview). That seems like a more involved change though, so will open an issue for a future PR.

Copy link
Contributor

@kosiew kosiew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

common Related to common crate logical-expr Logical plan and expressions physical-expr Changes to the physical-expr crates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DefaultPhysicalExprAdapterRewriter doesn't handle structs inside complex types

2 participants