Repository navigation
test: Add sqllogictests for field metadata through coalesce, nvl2 and CASE - #26895
Merged
Merged
Conversation
… CASE Document the current behavior: field metadata (including Arrow extension types) is dropped by coalesce, nvl, ifnull, nvl2 and CASE expressions. The intended results are recorded in comments next to each query. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
kosiew
approved these changes
Nov 17, 2026
| bar | ||
| baz | ||
|
|
||
| # The CASE operand and WHEN conditions are only tested; their metadata does not leak. |
Contributor
There was a problem hiding this comment.
This heading mentions WHEN-condition metadata isolation, but the test only covers the CASE operand. Could we narrow the heading or add a separate condition-isolation test?
Contributor
Author
There was a problem hiding this comment.
Thank you -- that is a good call -- I added in 185affd
alamb
enabled auto-merge
November 18, 2026 09:58
bvolpato
pushed a commit
to bvolpato/datafusion
that referenced
this pull request
Nov 21, 2026
## Which issue does this PR close? - Closes apache#24860. This is a smaller alternative to apache#26145 + apache#26158, following the suggestion on apache#24865 to look for a simpler way to reach the same goal. It also includes the simplifier fix from apache#26951, which depends on the CASE fix here. ## Rationale for this change `CASE` drops the field metadata of its branches, so Arrow extension types are lost. That breaks functions that check the extension type of their argument when the argument is a `CASE`, or a `COALESCE`, `NVL`, `IFNULL` or `NVL2`, since those are rewritten to `CASE` before execution. Examples are `variant_get` over a Parquet Variant column, reported on apache#26145, and geometry and raster functions in SedonaDB. The metadata is dropped in the logical `Expr::Case` field, in the physical `CaseExpr`, which has no `return_field` override, and in simplifier rewrites that drop the branch carrying it. ## What changes are included in this PR? - **Logical `Expr::Case::to_field`** merges the fields of its THEN and ELSE branches with `coerced_fields_metadata`, the helper `COALESCE`/`NVL2` use since apache#24865. It takes the type and nullability from the same fields, so it reads each branch once. The type rule from apache#27139 moves into a helper that `get_type` also uses. - **Physical `CaseExpr::return_field`** does the same with its branches' `return_field`. - **A typed NULL ELSE that carries metadata** is no longer dropped by `CaseExpr::try_new`. It is still evaluated like no ELSE. - **CASE simplifications keep the metadata.** A literal left by a constant WHEN, such as the NULL from `CASE WHEN false THEN x END`, takes the CASE's metadata. A CASE is not simplified when that would change its metadata, and a Boolean CASE with metadata on a branch is not rewritten to AND/OR. So CASE follows the same rules as `COALESCE`: the earlier branch wins for each key, an extension name is kept together with its parameters, differing extension names are a planning error, and a branch without metadata or an untyped `NULL` does not block another branch's metadata. The operand of a simple `CASE` and the WHEN conditions do not contribute. <details> <summary>Differences from apache#26145 + apache#26158</summary> About 290 lines of non-test code, against about 700 there. That includes the simplifier changes and about 70 lines that move the existing CASE type and nullability logic into helpers. - **No stored copy of the logical field, and no protobuf change.** The physical CASE derives its field from its physical branches, so it matches the logical field whenever each branch's field does. A unit test checks that for column, literal, cast and nested CASE branches. apache#26158 instead carries the logical field into the physical CASE. - **A branch whose logical and physical fields differ therefore shows through the CASE.** Physical `NOT` passes its input's metadata through while logical `NOT` drops it (apache#27155), and an alias's metadata can be lost when the physical expression is created (apache#27156). With `NOT`, this can be a planning error that `main` doesn't raise, when the other branch has a different extension name. Those mismatches are in `NOT` and `Alias` and are not addressed here. </details> <details> <summary>Performance</summary> Local runs on a laptop against `main`, so treat them as indicative: Criterion runs of the benchmarks from apache#26145/apache#26158, and `EXPLAIN` timings with a debug build of `datafusion-cli` for planning. - **CASE evaluation and physical planning** are unchanged. - **`return_field` rebuilds the merged metadata on every call:** 954 ns vs 274 ns for a column CASE with 4 metadata keys. A function asks its CASE argument for its field once per batch; field plus evaluation for 8,192 rows took 13.6 µs vs 13.1 µs. - **Logical `to_field`** for a wide CASE without any metadata took 2–3x as long. Both were measured before the nesting changes below, which only remove work. - **Nested CASE.** `to_field`, `return_field` and the constant-WHEN simplification visit each branch once. I timed 18 nesting shapes up to 22 levels: the CASE in the THEN, the ELSE or a WHEN; nested directly, in a function call, in `||` or under a cast; with a nullable or non-nullable other branch; a simple CASE; and COALESCE nested in either argument. The plans are the same as on `main`. - 10 take the same time as on `main`. - 7 are exponential on `main` and linear here. With the CASE in a function call, 6 shapes take 16–22 s on `main` at 18 levels and 0.05 s here. With a cast between levels (found by @jayzhan211), `main` takes 13.2 s at 16 levels and this PR 0.06 s. - COALESCE nested in its first argument is exponential on both, because the simplifier rewrites it to a CASE that repeats that argument: 4.05 s vs 3.82 s at 18 levels. Fast paths can come later: reuse the metadata when only one branch has any, and return early when none has. </details> ## What is the testing strategy for this PR? - **New `metadata.slt` cases** check the metadata at execution with `arrow_metadata`, mostly with the built-in `JSON` extension type. They cover precedence, simple CASE, conflicting extension names, a branch without metadata, no ELSE, an untyped and a typed NULL ELSE, coercion to a common type, nested CASE, the CASE rewrites of `coalesce`, `nvl`, `ifnull` and `nvl2`, constant-WHEN simplification, and a Boolean CASE. Each check fails without this change. Each query uses a given CASE only once, because a repeated expression is computed in a lower projection whose field comes from the logical plan. - **Unit tests** check that the physical CASE field matches the logical one in metadata, type and nullability, and cover the CASE simplifications. That includes the rewrite of a Boolean CASE to `NOT(CASE ...)`, which SQL can't observe because physical `NOT` passes the metadata through (apache#27155). - **Existing tests:** the `datafusion-expr`, `datafusion-physical-expr` and `datafusion-optimizer` lib tests pass. The full sqllogictest suite and the `core_integration` `sql` tests show no new failures; my checkout doesn't have the test-data submodules, so the files that need that data fail the same way on `main`. - **The `metadata.slt` from apache#26158**, which includes the cases from apache#26895, also passes with this change, checked locally with the current head of apache#26158. ## Are there any user-facing changes? - `CASE` results keep their field metadata, including extension types, also when the optimizer simplifies the CASE. So do `COALESCE`, `NVL`, `IFNULL` and `NVL2` at execution. - `CASE` branches with differing extension names are now a planning error, as they already are for `COALESCE`. No public API changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
CASEexpressions drop field metadata, silently stripping Arrow extension types and breaking metadata-aware UDFs #24860Rationale for this change
While reviewing #24865 I found that
coalesce,nvl,ifnullandnvl2are rewritten toCASEduring simplification, andCASEdrops field metadataThis wasn't caught because there was no end user API coverage
These tests document the current behavior end to end so a fix can be verified against them.
What changes are included in this PR?
Add New cases in
metadata.sltforcoalesce,nvl,ifnull,nvl2andCASEthat document existing behavior with some comments explaining what the expected results areWhat is the testing strategy for this PR?
Tests only.
Are there any user-facing changes?
No.