Skip to content

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

Merged
kosiew merged 1 commit intoapache:mainfrom
polarsignals:asubiotto/cast
Mar 23, 2026
Merged

fix[physical-expr-adapter]: support casting structs nested inside complex types#20907
kosiew merged 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

@alamb
Copy link
Contributor

alamb commented Mar 22, 2026

Is this ready to merge?

@kosiew kosiew added this pull request to the merge queue Mar 23, 2026
Merged via the queue into apache:main with commit abf8f61 Mar 23, 2026
34 checks passed
de-bgunter pushed a commit to de-bgunter/datafusion that referenced this pull request Mar 24, 2026
…plex types (apache#20907)

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.

## Which issue does this PR close?
- Closes apache#20906 

## Rationale for this change

<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->

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?

<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes 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?

<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->

<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->
Queries that would previously error, now complete successfully.

Signed-off-by: Alfonso Subiotto Marques <alfonso.subiotto@polarsignals.com>
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

3 participants