Skip to content

test: Add sqllogictests for field metadata through coalesce, nvl2 and CASE - #26895

Merged
alamb merged 5 commits into
apache:mainfrom
alamb:alamb/case-metadata-slt
Nov 18, 2026
Merged

alamb merged 5 commits into
apache:mainfrom
alamb:alamb/case-metadata-slt

Conversation

@alamb

@alamb alamb commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

While reviewing #24865 I found that coalesce, nvl, ifnull and nvl2 are rewritten to CASE during simplification, and CASE drops field metadata

This 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.slt for coalesce, nvl, ifnull, nvl2 and CASE that document existing behavior with some comments explaining what the expected results are

What is the testing strategy for this PR?

Tests only.

Are there any user-facing changes?

No.

… 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 kosiew left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@alamb, thanks for adding end-to-end metadata coverage.

The tests correctly characterize metadata loss through CASE simplification without changing production behavior.

Just one minor documentation suggestions.

bar
baz

# The CASE operand and WHEN conditions are only tested; their metadata does not leak.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you -- that is a good call -- I added in 185affd

@alamb
alamb enabled auto-merge November 18, 2026 09:58
@alamb
alamb added this pull request to the merge queue Nov 18, 2026
Merged via the queue into apache:main with commit 83dfc08 Nov 18, 2026
41 of 42 checks passed
@alamb
alamb deleted the alamb/case-metadata-slt branch November 18, 2026 10:09
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sqllogictest SQL Logic Tests (.slt) v57.0.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants