You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "Amogh-2404 (via GitHub)" <gi...@apache.org> on 2026/10/11 08:09:51 UTC

[PR] fix: preserve schema consistency across union and cast [datafusion]

Amogh-2404 opened a new pull request, #26200:
URL: https://github.com/apache/datafusion/pull/26200

   ## Which issue does this PR close?
   
   - Closes #26178.
   
   ## Rationale for this change
   
   `CAST(coalesce(non_null_col, lit) AS T)` under `UNION ALL` + aggregate fails planning when the column carries Arrow field metadata (`PARQUET:field_id`):
   
   ```
   Internal error: Physical input schema should be the same as the one converted
   from logical input schema. Differences:
     - field metadata at index 0 [x]: (physical) {"PARQUET:field_id": "1"} vs (logical) {}.
   ```
   
   `coalesce` on a `NOT NULL` column simplifies to the column. Child projections then advertise that metadata; the Union's stored schema still has `{}` from the pre-simplify coalesce field. Physical planning derives from the simplified exprs, so the aggregate schema check fails.
   
   ## What changes are included in this PR?
   
   `LogicalPlan::recompute_schema` for `Union` no longer keeps the old schema whenever field count is unchanged. It re-derives via `Union::try_new` so metadata/nullability track the current children. If width is unchanged but `try_new` fails (loose-typed children before coercion), the previous schema is kept; width mismatches still return the `try_new` error.
   
   ## What is the testing strategy for this PR?
   
   - Integration: `datafusion/core/tests/sql/union_field_metadata.rs` — issue query (`m=3`, `c=6`), controls (no metadata / `CAST(b)` / direct `b AS x` / no union), mixed-metadata legs.
   - Unit: `recompute_schema_refreshes_union_field_metadata`, loose-union preserve, width-mismatch error.
   
   Verified fail-before / pass-after on `coalesce_cast_union_aggregate_preserves_field_metadata` by temporarily reverting the `plan.rs` change.
   
   Commands run:
   
   ```bash
   cargo test -p datafusion-expr --lib logical_plan::plan::tests::recompute_schema
   cargo test -p datafusion --test core_integration coalesce_cast_union_aggregate
   cargo test -p datafusion --test core_integration union_
   cargo test -p datafusion-optimizer --lib
   cargo test -p datafusion --lib physical_planner
   cargo test -p datafusion-sqllogictest --test sqllogictests metadata.slt
   cargo clippy -p datafusion-expr -p datafusion --tests -- -D warnings
   cargo fmt --all -- --check
   ```
   
   Not run: full `./dev/rust_lint.sh`, full extended CI suite, full sqllogictest corpus.
   
   ## Are there any user-facing changes?
   
   No public API change. Queries that previously hit this internal error now plan. Union output may correctly carry field metadata that a stale schema had dropped after expression rewrites. CAST metadata inheritance is unchanged.
   
   Made with [Cursor](https://cursor.com)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: preserve schema consistency across union and cast [datafusion]

Posted by "Amogh-2404 (via GitHub)" <gi...@apache.org>.
Amogh-2404 commented on PR #26200:
URL: https://github.com/apache/datafusion/pull/26200#issuecomment-6107085020

   Superseded by a fresh PR (same change; reopened without the force-push timeline).


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: preserve schema consistency across union and cast [datafusion]

Posted by "Amogh-2404 (via GitHub)" <gi...@apache.org>.
Amogh-2404 closed pull request #26200: fix: preserve schema consistency across union and cast
URL: https://github.com/apache/datafusion/pull/26200


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: preserve schema consistency across union and cast [datafusion]

Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #26200:
URL: https://github.com/apache/datafusion/pull/26200#issuecomment-6107236988

   ## [Codecov](https://app.codecov.io/gh/apache/datafusion/pull/26200?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :x: Patch coverage is `72.63158%` with `26 lines` in your changes missing coverage. Please review.
   :white_check_mark: Project coverage is 82.80%. Comparing base ([`a4b8198`](https://app.codecov.io/gh/apache/datafusion/commit/a4b8198cfa2419394df966f815d284e745f06653?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`ced67e8`](https://app.codecov.io/gh/apache/datafusion/commit/ced67e897980508524678ac0fe7489706a8ddfe3?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/datafusion/pull/26200?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
   |---|---|---|
   | [datafusion/expr/src/logical\_plan/plan.rs](https://app.codecov.io/gh/apache/datafusion/pull/26200?src=pr&el=tree&filepath=datafusion%2Fexpr%2Fsrc%2Flogical_plan%2Fplan.rs&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-ZGF0YWZ1c2lvbi9leHByL3NyYy9sb2dpY2FsX3BsYW4vcGxhbi5ycw==) | 72.63% | [7 Missing and 19 partials :warning: ](https://app.codecov.io/gh/apache/datafusion/pull/26200?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   ```diff
   @@            Coverage Diff             @@
   ##             main   #26200      +/-   ##
   ==========================================
   - Coverage   82.80%   82.80%   -0.01%     
   ==========================================
     Files        1149     1149              
     Lines      452205   452296      +91     
     Branches   452205   452296      +91     
   ==========================================
   + Hits       374468   374538      +70     
   - Misses      54942    54945       +3     
   - Partials    22795    22813      +18     
   ```
   </details>
   
   [:umbrella: View full report in Codecov by Harness](https://app.codecov.io/gh/apache/datafusion/pull/26200?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).   
   :loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   - :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
   </details>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org