Skip to content

fix: keep cast nullability consistent with execution - #26752

Draft
Dodothereal wants to merge 1 commit into
apache:mainfrom
Dodothereal:fix/cast-extension-nullability
Draft

Dodothereal wants to merge 1 commit into
apache:mainfrom
Dodothereal:fix/cast-extension-nullability

Conversation

@Dodothereal

Copy link
Copy Markdown
Contributor

Closes #26469.

An explicit nullable cast target could be reported as nullable by the physical expression while the logical expression still kept the source's required nullability. Aggregates compare those input schemas, so casts to extension types such as UUID failed with Physical input schema should be the same as the one converted from logical input schema.

This uses the execution rule in the logical and physical expressions: type-only casts preserve the input's nullability, explicit targets are nullable if either the input or target allows nulls, and safe casts are nullable because a failed cast produces NULL. CastExpr::return_field() keeps returning the exact target field for the schema adapter.

The new sqllogictest covers count, count(DISTINCT), and GROUP BY over a two-row UUID cast, along with the source-required case. I also left the existing extension metadata tests in place.

Tests run:

  • cargo test -p datafusion-sqllogictest --test sqllogictests -- cast_extension_type_metadata --test-threads 1
  • cargo test -p datafusion-expr --lib expr_schema::tests
  • cargo test -p datafusion-physical-expr --lib expressions::cast::tests
  • cargo test -p datafusion-physical-expr-adapter --lib
  • union-nullability, aggregate-adapter, and recursive CTE integration tests
  • cargo clippy -p datafusion-expr -p datafusion-physical-expr --lib -- -D warnings
  • cargo fmt --all -- --check

The checkout is shallow and lacks the test-data submodules, so I did not treat the full sqllogictest corpus as evidence.

A cast to an explicit nullable target field kept the source nullability in both the logical expression and the physical cast. A failed safe cast can also produce NULL. Both paths reported a result that could not actually contain those nulls.

Use one conservative rule: type-only casts preserve source nullability, explicit targets are nullable when either side is nullable, and safe casts are nullable. Keep return_field() exact for the schema adapter. Add regression coverage for the aggregate queries and for the source-required case.
@github-actions github-actions Bot added logical-expr Logical plan and expressions physical-expr Changes to the physical-expr crates sqllogictest SQL Logic Tests (.slt) labels Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

logical-expr Logical plan and expressions physical-expr Changes to the physical-expr crates sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Internal error: CAST to an extension-type field over a non-null column, under an aggregate (logical vs physical nullability)

1 participant