Skip to content

Conversation

@pepijnve
Copy link
Contributor

@pepijnve pepijnve commented Sep 28, 2025

Which issue does this PR close?

Rationale for this change

#17357 introduced a change that replaces coalesce function calls with case expressions. In the current implementation these two differ in the way they report their nullability. coalesce is more precise than case all will report itself as not nullable in situations where the equivalent case does report being nullable.

The rest of the codebase currently does not expect the nullability property of an expression to change as a side effect of expression simplification. This PR is a first attempt to align the nullability of coalesce and case.

What changes are included in this PR?

Tweaks to the nullable logic for the logical and physical case expression code to report case as being not nullable in more situations.

  • For logical case, a best effort const evaluation of 'when' expressions is done to determine 'then' reachability. The code errs on the conservative side wrt nullability.
  • For physical case, const evaluation of 'when' expressions using a placeholder record batch is attempted to determine 'then' reachability. Again if const evaluation is not possible, the code errs on the conservative side.
  • The optimizer schema check has been relaxed slightly to allow nullability to be removed by optimizer passes without having to disable the schema check entirely
  • The panic'ing benchmark has been reenabled

Are these changes tested?

Additional unit tests have been added to test the new logic.

Are there any user-facing changes?

No

@github-actions github-actions bot added logical-expr Logical plan and expressions physical-expr Changes to the physical-expr crates core Core DataFusion crate labels Sep 28, 2025
@pepijnve pepijnve marked this pull request as ready for review September 29, 2025 10:52
@pepijnve pepijnve force-pushed the issue_17801 branch 3 times, most recently from 4bbaa82 to 7f8d7cf Compare September 29, 2025 12:56
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thank you for this PR @pepijnve --

I am not quite sure about this implementation (I am hoping #17628 might solve the problem too with more sophisticated case folding)

However, I verified it does solve the problem with running the benchmarks so from that perspective I think we should proceed

My only real concern is that the newly added tests cover only the new code, and not the "end to end" behavior you tracked down (namely that the case pattern with coalesce changes the nullability).

Would it be possible to add some of the cases as expr simplification tests too? Somewhere like here?

Comment on lines 924 to 925
when(binary_expr(col("foo"), Operator::Eq, lit(5)), col("foo"))
.otherwise(lit(0))?,
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor: You can probably make this more concise using the eq method, something like this:

Suggested change
when(binary_expr(col("foo"), Operator::Eq, lit(5)), col("foo"))
.otherwise(lit(0))?,
when(col("foo").eq(lit(5))), col("foo")).otherwise(lit(0))?,

Copy link
Contributor

Choose a reason for hiding this comment

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

likewise there is Expr::and for ands that could be used as well below

However, the current setup of using and as a prefix is pretty clear too, so maybe what you have here is actually more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah I missed that. I was looking for prefix versions, and hadn't realised infix ones existed too.

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 ended up sticking with prefix notation for the boolean combinators and infix for the rest. Using infix for the boolean made it hard to read. I've also added the SQL equivalent as a comment.

assert!(expr.nullable(&get_schema(false)).unwrap());
}

fn check_nullability(
Copy link
Contributor

Choose a reason for hiding this comment

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

I found this a little confusing at first, because it makes an explicit assumption that expr's will never introduce nulls (in order for !expr.nullable(&get_schema(false))?, to be true). So for example, it wouldn't do the right thing with the NULLIF function NULLIF(foo, 25) or something

Maybe some comments would help

Suggested change
fn check_nullability(
/// Verifies that `expr` has `nullable` nullability when the 'foo' column is
/// null.
/// Also assumes and verifies that `expr` is NOT nullable when 'foo' is NOT null
fn check_nullability(

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've reworked the logical plan test cases already to (hopefully) make it more obvious what's going on. I hadn't given this function much thought since it was only a test thing.

check_nullability(
when(binary_expr(col("foo"), Operator::Eq, lit(5)), col("foo"))
.otherwise(lit(0))?,
true,
Copy link
Contributor

Choose a reason for hiding this comment

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

technically this could also be reported as false, given that if foo is null, then the expr resolves to 0 (non null)

> create table t(foo int) as values (0), (NULL), (5);
0 row(s) fetched.
Elapsed 0.001 seconds.

> select foo, CASE WHEN foo=5 THEN foo ELSE 0 END from t;
+------+---------------------------------------------------------+
| foo  | CASE WHEN t.foo = Int64(5) THEN t.foo ELSE Int64(0) END |
+------+---------------------------------------------------------+
| 0    | 0                                                       |
| NULL | 0                                                       |
| 5    | 5                                                       |
+------+---------------------------------------------------------+
3 row(s) fetched.
Elapsed 0.002 seconds.

However, maybe we can improve that in a follow on PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, the const evaluation is far from complete. I tried to do something good enough for the coalesce simplification initially.
I was wondering the whole time if there isn't some existing null analysis logic somewhere in the codebase we could reuse. The best I could come up with is rewriting the full expression by replacing the then expression with literal NULL and then attempting const evaluation. But that got me worrying about planning overhead again.

when(
or(
is_not_null(col("foo")),
binary_expr(col("foo"), Operator::Eq, lit(5)),
Copy link
Contributor

Choose a reason for hiding this comment

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

as above, I don't think this expression can everr be true so this overall expression is still non nullable

col("foo"),
)
.otherwise(lit(0))?,
true,
Copy link
Contributor

Choose a reason for hiding this comment

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

Here too -- this expression is not nullabile

> select foo, CASE WHEN foo=5 OR foo IS NOT NULL THEN foo ELSE 0 END from t;
+------+------------------------------------------------------------------------------+
| foo  | CASE WHEN t.foo = Int64(5) OR t.foo IS NOT NULL THEN t.foo ELSE Int64(0) END |
+------+------------------------------------------------------------------------------+
| 0    | 0                                                                            |
| NULL | 0                                                                            |
| 5    | 5                                                                            |
+------+------------------------------------------------------------------------------+
3 row(s) fetched.
Elapsed 0.002 seconds.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FWIW, if you comment out the filter step (i.e. revert to the pre-patch version) all of these cases are reported as being nullable. The scope of this PR is to get at least some cases that are definitely not nullable reported as such, not ensure all cases are reported correctly.

.otherwise(lit(0))?,
true,
get_schema,
)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also please add a check with is_null in the OR clause (which should be null)

Something like the equivalent to

> select foo, CASE WHEN foo=5 OR foo IS NULL THEN foo ELSE 0 END from t;
+------+--------------------------------------------------------------------------+
| foo  | CASE WHEN t.foo = Int64(5) OR t.foo IS NULL THEN t.foo ELSE Int64(0) END |
+------+--------------------------------------------------------------------------+
| 0    | 0                                                                        |
| NULL | NULL                                                                     |
| 5    | 5                                                                        |
+------+--------------------------------------------------------------------------+
3 row(s) fetched.
Elapsed 0.000 seconds.

Like

      check_nullability(
            when(
                or(
                    binary_expr(col("foo"), Operator::Eq, lit(5)),
                    is_null(col("foo")),
                ),
                col("foo"),
            )
            .otherwise(lit(0))?,
            true,
            get_schema,
        )?;

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've added this test case

@pepijnve
Copy link
Contributor Author

pepijnve commented Sep 29, 2025

I am not quite sure about this implementation (I am hoping #17628 might solve the problem too with more sophisticated case folding)

I warned you it wasn't very elegant. 😄 I don't think #17628 covers the same thing though. What we're trying to do here is get ExprSchemable::nullable to report an accurate value outside of the optimiser. Ideally you would want that to work both before and after case folding.
I agree that there is a lot of overlap in the required logic though. If you were to take the case expression, replace the then expression trees with literal NULL everywhere they occur, then perform case folding, and then perform null analysis then you would get the same result.

My only real concern is that the newly added tests cover only the new code, and not the "end to end" behavior you tracked down (namely that the case pattern with coalesce changes the nullability).

Would it be possible to add some of the cases as expr simplification tests too? Somewhere like here?

I'm not sure what kind of test you have in mind. The end to end case is (admittedly very indirectly) covered by TPC-DS query 75 and the removal of the double optimisation. If you revert the production code change in this PR, but keep the test change you'll see that it fails.

For the simplifier itself, I was wondering if there shouldn't be some internal assertions that verifies that the result of calling ScalarUDF::simplify doesn't change key aspects of the expression that the schema also encodes. Both the data type and the nullability should not change since that causes the expression and the schema to be mismatched.

@pepijnve
Copy link
Contributor Author

pepijnve commented Sep 30, 2025

@alamb thinking about this a bit more. I'm going to struggle expressing myself sufficiently clearly here, but I'll try to explain the idea behind what I'm doing. Maybe that can help us figure out a better way to express the idea.

What I'm trying to do is improve the accuracy of the predicate is_nullable(expr) specifically for CASE expressions.
In the current code this predicate is implemented by checking (in pseudo code) case_expr.when_then.any?((when, then) -> is_nullable(then)) || is_nullable(case_expr.else). This results in quite a few CASE expressions being reported as nullable even though they're not.

In particular there's one interesting case (pun not intended) which results from the coalesce simplification and that is CASE WHEN x IS NOT NULL THEN x ELSE y. If the expression x is nullable, is_nullable will currently report the entire case expression as nullable. The implementation does not take into account that there is a guard clause preventing the null value of x from being returned.

What I attempted to do in this PR is to look at the more general form WHEN f(x) THEN x where f(x) is some arbitrary predicate that may or may not depend on the value of x. What the code is trying to do is to const evaluate f(NULL). If it can with 100% certainty (i.e. Some is returned) and the evaluated value is false, then predicate f guarantees this particular branch of the case expression from ever returning a NULL and we can ignore the nullability of x.

I tried to implement this in a cheap, but imprecise way. My rationale was that even though it's not perfect, it's an improvement in accuracy over the current code.
A possible alternative would be to rewrite the expression corresponding to f using the binding x = null and then attempting to const evaluate using the existing const evaluation code. That introduces a dependency from the logical expression module to the optimiser though and would probably have a longer run time than the current crude approximation.

@pepijnve
Copy link
Contributor Author

I've massaged the logical plan version of the code a bit further already to hopefully clarify what it's doing. I then ran the test cases with logging output rather than assertions before and after the extra filtering to illustrate what's being changed. After the change all tests pass. Before the patch it reports the following

CASE WHEN x IS NOT NULL THEN x ELSE Int32(0) END nullable? should be false, but was true
CASE WHEN NOT x IS NULL THEN x ELSE Int32(0) END nullable? should be false, but was true
CASE WHEN x IS NOT NULL AND x = Int32(5) THEN x ELSE Int32(0) END nullable? should be false, but was true
CASE WHEN x = Int32(5) AND x IS NOT NULL THEN x ELSE Int32(0) END nullable? should be false, but was true
CASE WHEN x = Int32(5) AND x IS NOT NULL OR x = bar AND x IS NOT NULL THEN x ELSE Int32(0) END nullable? should be false, but was true

@pepijnve
Copy link
Contributor Author

@alamb I've taken the logical expression portion of the PR another step further which ensures correct answers for the expressions you mentioned earlier. I can complete the physical expression portion as well if you like. Unless you tell me this path is a dead end.

@alamb
Copy link
Contributor

alamb commented Sep 30, 2025

@alamb I've taken the logical expression portion of the PR another step further which ensures correct answers for the expressions you mentioned earlier. I can complete the physical expression portion as well if you like. Unless you tell me this path is a dead end.

Thank you -- I will try and get to this one asap. Somehow every time i think I am getting the queue of reviews under control there are like 50 new notifications ! It is a good problem to have.

@pepijnve
Copy link
Contributor Author

Thank you -- I will try and get to this one asap. Somehow every time i think I am getting the queue of reviews under control there are like 50 new notifications ! It is a good problem to have.

No pressure from my side. I just write up my notes and move on to the next thing. Async delayed response is fine.

@pepijnve
Copy link
Contributor Author

pepijnve commented Oct 1, 2025

I experimented a bit with the rewrite + const eval approach on the physical expression side of things. While attractive and simple to implement, the downside is that it's going to be very hard to ensure the logical and physical side agree. Logical needs to work without ExecutionProps so it has less information available to it compared to the PhysicalExpr tree. I don't see a way to resolve that. As a consequence I ended up with an limited ad hoc version of const evaluation on the logical side and would have to do the same for physical which isn't really ideal from a DRY perspective.

@alamb
Copy link
Contributor

alamb commented Oct 6, 2025

Than you -- this is on my list of things to review shortly

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thank you @pepijnve -- this is looking so close -- I think we should roll back some of the GuaranteeRewriter changes to avoid API churn. If you could break them out into their own PR I think that would be good and I could review them quickly

/// ```text
/// A ∧ B │ F U T
/// ──────┼──────
/// F │ F F F
Copy link
Contributor

Choose a reason for hiding this comment

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

this truth table seems to be missing the values for A (self)

Copy link
Contributor Author

@pepijnve pepijnve Nov 19, 2025

Choose a reason for hiding this comment

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

That's the left column. Let me see if I can figure out a compact way to clarify that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These tables were based on https://en.wikipedia.org/wiki/Three-valued_logic#Kleene_and_Priest_logics

I've tweaked them a bit further to resemble those as closely as possible.

/// This method uses the following truth table.
///
/// ```text
/// A ∨ B │ F U T
Copy link
Contributor

Choose a reason for hiding this comment

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

likewise here this table is missing values for A

/// See a full example in [`ExprSimplifier::with_guarantees()`].
///
/// [`ExprSimplifier::with_guarantees()`]: crate::simplify_expressions::expr_simplifier::ExprSimplifier::with_guarantees
pub struct GuaranteeRewriter<'a> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Unless there is a good reason, I think we should avoid removing this API as it will cause unecessary churn on downstream crates

If you find rewrite_with_guarantees easier to work with, maybe you leave GuaranteeRewriter and but implement rewrite_with_guarantees in terms of that

use datafusion_common::{DataFusionError, HashMap, Result};
use datafusion_expr_common::interval_arithmetic::{Interval, NullableInterval};

struct GuaranteeRewriter<'a> {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the GuaranteeRewriter is part of the public API, so making this change would potentially cause breaking downstream changes: https://docs.rs/datafusion/latest/datafusion/optimizer/simplify_expressions/struct.GuaranteeRewriter.html

I think we should leave the GuaranteeRewriter API in place (w/ comments etc) and then make rewrite_with_guarantees a method or something

Perhaps

impl  GuaranteeRewriter { 

/// Create new guarantees from an iterator
pub fn new(
        guarantees: impl IntoIterator<Item = &'a (Expr, NullableInterval)>,
    )

/// Create new gurantees from a map
pub fn new(
    guarantees: &'a HashMap<&'a Expr, &'a NullableInterval>,
)

}

🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, it's a breaking change. It's already breaking simply because of the move from one crate to another unless we add a reexport from optimizer.

No objections to restoring public visibility of the struct though. I was just trying to follow the example/style of the order by rewrite sibling on the new module location.

///
/// See a full example in [`ExprSimplifier::with_guarantees()`].
///
/// [`ExprSimplifier::with_guarantees()`]: crate::simplify_expressions::expr_simplifier::ExprSimplifier::with_guarantees
Copy link
Contributor

Choose a reason for hiding this comment

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

You probably removed this doc link b/c the code is now in a different module that doesn't depend on optimizer.

However I think the link still adds value

What we have done in other places where we can't rely on auto links is to use the direct HTML link: https://docs.rs/datafusion/latest/datafusion/optimizer/simplify_expressions/struct.ExprSimplifier.html#method.with_guarantees

Which isn't as good as rustdoc doesn't check that the links don't get broken, but I think it is better than just removing the link totally

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, that's why I removed it here. Will restore.

@pepijnve
Copy link
Contributor Author

Thank you @pepijnve -- this is looking so close -- I think we should roll back some of the GuaranteeRewriter changes to avoid API churn. If you could break them out into their own PR I think that would be good and I could review them quickly

I'll make this a separate PR taking the comments you logged so far into account. It'll be easier to track that way.

@pepijnve
Copy link
Contributor Author

@alamb guarantee stuff moved over to #18821

github-merge-queue bot pushed a commit that referenced this pull request Nov 19, 2025
## Which issue does this PR close?

- None, break out PR of changes done in #17813

## Rationale for this change

In #17813 `GuaranteeRewriter` is used from the `datafusion_expr` crate.
In order to enable this the type needed to be moved from
`datafusion_optimizer` to `datafusion_expr`.

Additionally, during the development of #17813 some latent bugs were
discovered in `GuaranteeRewriter` that have been resolved.

## What changes are included in this PR?

- Move `GuaranteeRewriter` to `datafusion_expr`
- Fix two bugs where rewrites of 'between' expression would fail
  - when one of the bounds was untyped null
  - when the lower bound was greater than the upper bound
- Add logic to replace expressions with literal null based on provided
guarantees
- Split implementation into smaller functions for easier readability

## Are these changes tested?

- Existing tests updated
- Tests added for bugfixes

## Are there any user-facing changes?

No

---------

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
# Conflicts:
#	datafusion/expr/src/expr_rewriter/guarantees.rs
#	datafusion/expr/src/expr_rewriter/mod.rs
#	datafusion/optimizer/src/simplify_expressions/mod.rs
@github-actions github-actions bot removed the optimizer Optimizer rules label Nov 19, 2025
@pepijnve
Copy link
Contributor Author

The changes from #18821 have been merged into this PR from main. PR currently reflects only the changes relevant to the original problem description.

@alamb
Copy link
Contributor

alamb commented Nov 19, 2025

Thank you @pepijnve -- I plan to give this one a final review tomorrow morning and merge it in

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Looks great -- thank you (again) @pepijnve

I took the liberty of pushing the change to run the benchmark query again so we could really close #17833 and merging up from main

Expr::Column(c) => input_schema.nullable(c),
Expr::OuterReferenceColumn(field, _) => Ok(field.is_nullable()),
Expr::Literal(value, _) => Ok(value.is_null()),
Expr::Case(case) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

While re-reading this I can't help but think the logic is quite non trivial - and someone trying to figure out if an expression is nullable on a deeply nested function might end up calling this function many times

Not for this PR, but I think we should consider how to cache or otherwise avoid re-computing the same nullabilty (and DataType) expressions over and over again.

I'll writeup a follow on ticket

Copy link
Contributor Author

@pepijnve pepijnve Nov 20, 2025

Choose a reason for hiding this comment

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

That's absolutely correct. Performance overhead concerns were the main reason I had initially avoided rewriting the expression and instead tried to do the rewrite indirectly. Rather than rewriting using a NullableInterval::Null guarantee, I was checking this using a callback function.

It's probably feasible, but non-trivial to cache this result. What would you use as storage location?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See https://github.com/apache/datafusion/pull/17813/files#r2545958309. That already mitigates the additional calculations a little bit.

Copy link
Contributor

@alamb alamb Nov 20, 2025

Choose a reason for hiding this comment

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

It's probably feasible, but non-trivial to cache this result. What would you use as storage location?

Yes, I agree it is non trivial. I wrote up some ideas in

Copy link
Contributor Author

@pepijnve pepijnve Nov 20, 2025

Choose a reason for hiding this comment

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

I started looking at the possible options here already a bit. I don't immediately see a simple solution.

@alamb
Copy link
Contributor

alamb commented Nov 20, 2025

🤖 ./gh_compare_branch_bench.sh Benchmark Script Running
Linux aal-dev 6.14.0-1018-gcp #19~24.04.1-Ubuntu SMP Wed Sep 24 23:23:09 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
Comparing issue_17801 (ff9a41f) to 7fa2a69 diff
BENCH_NAME=sql_planner
BENCH_COMMAND=cargo bench --bench sql_planner
BENCH_FILTER=
BENCH_BRANCH_NAME=issue_17801
Results will be posted here when complete

Some(Ok(()))
}
})
.next();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The change from collect to next().is_some() does mitigate the performance overhead a little bit. As soon as one nullable branch is found the iteration will stop.

@alamb
Copy link
Contributor

alamb commented Nov 20, 2025

🤖: Benchmark completed

Details

group                                                 issue_17801                            main
-----                                                 -----------                            ----
logical_aggregate_with_join                           1.00    639.2±2.78µs        ? ?/sec    1.00    638.7±7.94µs        ? ?/sec
logical_select_all_from_1000                          1.02     11.1±0.07ms        ? ?/sec    1.00     11.0±0.04ms        ? ?/sec
logical_select_one_from_700                           1.01    422.1±2.17µs        ? ?/sec    1.00    419.9±1.94µs        ? ?/sec
logical_trivial_join_high_numbered_columns            1.00    379.3±1.42µs        ? ?/sec    1.00    377.6±1.71µs        ? ?/sec
logical_trivial_join_low_numbered_columns             1.01    366.1±1.47µs        ? ?/sec    1.00    363.8±1.98µs        ? ?/sec
physical_intersection                                 1.02    848.8±3.75µs        ? ?/sec    1.00    834.9±3.82µs        ? ?/sec
physical_join_consider_sort                           1.01   1409.6±4.86µs        ? ?/sec    1.00   1394.1±9.18µs        ? ?/sec
physical_join_distinct                                1.01    354.9±1.33µs        ? ?/sec    1.00    352.9±1.48µs        ? ?/sec
physical_many_self_joins                              1.01      9.8±0.04ms        ? ?/sec    1.00      9.7±0.09ms        ? ?/sec
physical_plan_clickbench_all                          1.00    182.8±1.77ms        ? ?/sec    1.00    182.6±1.25ms        ? ?/sec
physical_plan_clickbench_q1                           1.00      2.4±0.02ms        ? ?/sec    1.00      2.4±0.02ms        ? ?/sec
physical_plan_clickbench_q10                          1.00      3.2±0.02ms        ? ?/sec    1.00      3.2±0.03ms        ? ?/sec
physical_plan_clickbench_q11                          1.00      3.4±0.03ms        ? ?/sec    1.00      3.4±0.04ms        ? ?/sec
physical_plan_clickbench_q12                          1.00      3.5±0.03ms        ? ?/sec    1.05      3.7±0.37ms        ? ?/sec
physical_plan_clickbench_q13                          1.00      3.2±0.02ms        ? ?/sec    1.01      3.2±0.03ms        ? ?/sec
physical_plan_clickbench_q14                          1.00      3.4±0.03ms        ? ?/sec    1.00      3.4±0.05ms        ? ?/sec
physical_plan_clickbench_q15                          1.00      3.3±0.03ms        ? ?/sec    1.00      3.3±0.03ms        ? ?/sec
physical_plan_clickbench_q16                          1.00      3.1±0.03ms        ? ?/sec    1.00      3.1±0.02ms        ? ?/sec
physical_plan_clickbench_q17                          1.01      3.2±0.04ms        ? ?/sec    1.00      3.2±0.02ms        ? ?/sec
physical_plan_clickbench_q18                          1.01      2.7±0.02ms        ? ?/sec    1.00      2.7±0.01ms        ? ?/sec
physical_plan_clickbench_q19                          1.00      3.6±0.03ms        ? ?/sec    1.00      3.6±0.03ms        ? ?/sec
physical_plan_clickbench_q2                           1.00      2.7±0.02ms        ? ?/sec    1.03      2.8±0.09ms        ? ?/sec
physical_plan_clickbench_q20                          1.00      2.5±0.02ms        ? ?/sec    1.00      2.5±0.01ms        ? ?/sec
physical_plan_clickbench_q21                          1.00      2.8±0.02ms        ? ?/sec    1.00      2.8±0.02ms        ? ?/sec
physical_plan_clickbench_q22                          1.00      3.4±0.02ms        ? ?/sec    1.00      3.4±0.02ms        ? ?/sec
physical_plan_clickbench_q23                          1.01      3.7±0.03ms        ? ?/sec    1.00      3.6±0.03ms        ? ?/sec
physical_plan_clickbench_q24                          1.01      4.1±0.05ms        ? ?/sec    1.00      4.1±0.03ms        ? ?/sec
physical_plan_clickbench_q25                          1.00      2.9±0.03ms        ? ?/sec    1.00      2.9±0.02ms        ? ?/sec
physical_plan_clickbench_q26                          1.01      2.7±0.03ms        ? ?/sec    1.00      2.7±0.02ms        ? ?/sec
physical_plan_clickbench_q27                          1.00      3.0±0.03ms        ? ?/sec    1.00      3.0±0.02ms        ? ?/sec
physical_plan_clickbench_q28                          1.00      3.7±0.04ms        ? ?/sec    1.00      3.7±0.04ms        ? ?/sec
physical_plan_clickbench_q29                          1.00      4.0±0.03ms        ? ?/sec    1.00      4.0±0.04ms        ? ?/sec
physical_plan_clickbench_q3                           1.00      2.7±0.02ms        ? ?/sec    1.00      2.7±0.03ms        ? ?/sec
physical_plan_clickbench_q30                          1.01     14.9±0.15ms        ? ?/sec    1.00     14.8±0.12ms        ? ?/sec
physical_plan_clickbench_q31                          1.00      3.7±0.03ms        ? ?/sec    1.00      3.7±0.02ms        ? ?/sec
physical_plan_clickbench_q32                          1.00      3.7±0.03ms        ? ?/sec    1.00      3.7±0.03ms        ? ?/sec
physical_plan_clickbench_q33                          1.00      3.2±0.02ms        ? ?/sec    1.01      3.3±0.03ms        ? ?/sec
physical_plan_clickbench_q34                          1.00      2.9±0.02ms        ? ?/sec    1.02      3.0±0.18ms        ? ?/sec
physical_plan_clickbench_q35                          1.00      3.0±0.02ms        ? ?/sec    1.01      3.0±0.04ms        ? ?/sec
physical_plan_clickbench_q36                          1.00      3.7±0.03ms        ? ?/sec    1.00      3.7±0.03ms        ? ?/sec
physical_plan_clickbench_q37                          1.00      3.8±0.04ms        ? ?/sec    1.00      3.9±0.03ms        ? ?/sec
physical_plan_clickbench_q38                          1.00      3.8±0.04ms        ? ?/sec    1.00      3.8±0.05ms        ? ?/sec
physical_plan_clickbench_q39                          1.00      3.7±0.04ms        ? ?/sec    1.00      3.7±0.03ms        ? ?/sec
physical_plan_clickbench_q4                           1.01      2.4±0.02ms        ? ?/sec    1.00      2.4±0.01ms        ? ?/sec
physical_plan_clickbench_q40                          1.02      4.6±0.07ms        ? ?/sec    1.00      4.5±0.03ms        ? ?/sec
physical_plan_clickbench_q41                          1.01      3.9±0.04ms        ? ?/sec    1.00      3.9±0.05ms        ? ?/sec
physical_plan_clickbench_q42                          1.00      3.9±0.04ms        ? ?/sec    1.00      3.9±0.03ms        ? ?/sec
physical_plan_clickbench_q43                          1.00      4.2±0.05ms        ? ?/sec    1.00      4.3±0.03ms        ? ?/sec
physical_plan_clickbench_q44                          1.00      2.6±0.02ms        ? ?/sec    1.00      2.6±0.02ms        ? ?/sec
physical_plan_clickbench_q45                          1.00      2.6±0.02ms        ? ?/sec    1.01      2.6±0.02ms        ? ?/sec
physical_plan_clickbench_q46                          1.00      3.0±0.02ms        ? ?/sec    1.00      3.0±0.02ms        ? ?/sec
physical_plan_clickbench_q47                          1.00      3.6±0.03ms        ? ?/sec    1.01      3.6±0.04ms        ? ?/sec
physical_plan_clickbench_q48                          1.00      4.3±0.03ms        ? ?/sec    1.00      4.3±0.05ms        ? ?/sec
physical_plan_clickbench_q49                          1.00      4.6±0.04ms        ? ?/sec    1.00      4.6±0.03ms        ? ?/sec
physical_plan_clickbench_q5                           1.01      2.7±0.02ms        ? ?/sec    1.00      2.7±0.03ms        ? ?/sec
physical_plan_clickbench_q50                          1.02      4.2±0.12ms        ? ?/sec    1.00      4.1±0.03ms        ? ?/sec
physical_plan_clickbench_q51                          1.00      3.2±0.03ms        ? ?/sec    1.00      3.2±0.03ms        ? ?/sec
physical_plan_clickbench_q6                           1.00      2.7±0.02ms        ? ?/sec    1.00      2.7±0.04ms        ? ?/sec
physical_plan_clickbench_q7                           1.01      2.4±0.02ms        ? ?/sec    1.00      2.4±0.02ms        ? ?/sec
physical_plan_clickbench_q8                           1.00      3.2±0.03ms        ? ?/sec    1.01      3.3±0.03ms        ? ?/sec
physical_plan_clickbench_q9                           1.00      3.1±0.02ms        ? ?/sec    1.00      3.1±0.02ms        ? ?/sec
physical_plan_tpcds_all                               1.04   1080.1±6.37ms        ? ?/sec    1.00   1037.9±4.61ms        ? ?/sec
physical_plan_tpch_all                                1.01     65.0±0.25ms        ? ?/sec    1.00     64.4±0.30ms        ? ?/sec
physical_plan_tpch_q1                                 1.01      2.1±0.01ms        ? ?/sec    1.00      2.1±0.01ms        ? ?/sec
physical_plan_tpch_q10                                1.01      4.0±0.01ms        ? ?/sec    1.00      4.0±0.01ms        ? ?/sec
physical_plan_tpch_q11                                1.00      3.6±0.01ms        ? ?/sec    1.00      3.6±0.02ms        ? ?/sec
physical_plan_tpch_q12                                1.00  1839.3±10.93µs        ? ?/sec    1.00   1832.8±9.39µs        ? ?/sec
physical_plan_tpch_q13                                1.00   1450.0±7.49µs        ? ?/sec    1.00   1444.6±8.59µs        ? ?/sec
physical_plan_tpch_q14                                1.00      2.0±0.01ms        ? ?/sec    1.00      2.0±0.01ms        ? ?/sec
physical_plan_tpch_q16                                1.00      2.5±0.01ms        ? ?/sec    1.00      2.4±0.01ms        ? ?/sec
physical_plan_tpch_q17                                1.00      2.6±0.01ms        ? ?/sec    1.00      2.6±0.01ms        ? ?/sec
physical_plan_tpch_q18                                1.00      2.7±0.01ms        ? ?/sec    1.00      2.7±0.01ms        ? ?/sec
physical_plan_tpch_q19                                1.00      3.2±0.02ms        ? ?/sec    1.00      3.2±0.01ms        ? ?/sec
physical_plan_tpch_q2                                 1.01      5.9±0.05ms        ? ?/sec    1.00      5.9±0.02ms        ? ?/sec
physical_plan_tpch_q20                                1.01      3.2±0.01ms        ? ?/sec    1.00      3.2±0.02ms        ? ?/sec
physical_plan_tpch_q21                                1.01      4.2±0.04ms        ? ?/sec    1.00      4.1±0.01ms        ? ?/sec
physical_plan_tpch_q22                                1.00      2.9±0.01ms        ? ?/sec    1.00      2.9±0.02ms        ? ?/sec
physical_plan_tpch_q3                                 1.01      2.7±0.01ms        ? ?/sec    1.00      2.7±0.01ms        ? ?/sec
physical_plan_tpch_q4                                 1.00  1485.1±10.99µs        ? ?/sec    1.00  1482.4±15.79µs        ? ?/sec
physical_plan_tpch_q5                                 1.00      3.3±0.01ms        ? ?/sec    1.00      3.3±0.03ms        ? ?/sec
physical_plan_tpch_q6                                 1.00    874.2±6.76µs        ? ?/sec    1.00   876.6±11.93µs        ? ?/sec
physical_plan_tpch_q7                                 1.01      4.2±0.02ms        ? ?/sec    1.00      4.2±0.02ms        ? ?/sec
physical_plan_tpch_q8                                 1.01      5.5±0.02ms        ? ?/sec    1.00      5.5±0.03ms        ? ?/sec
physical_plan_tpch_q9                                 1.01      4.1±0.02ms        ? ?/sec    1.00      4.1±0.05ms        ? ?/sec
physical_select_aggregates_from_200                   1.00     16.9±0.08ms        ? ?/sec    1.00     16.8±0.07ms        ? ?/sec
physical_select_all_from_1000                         1.02     24.2±0.11ms        ? ?/sec    1.00     23.8±0.09ms        ? ?/sec
physical_select_one_from_700                          1.02  1106.9±10.45µs        ? ?/sec    1.00   1087.9±3.62µs        ? ?/sec
physical_sorted_union_order_by_10_int64               1.00      6.0±0.02ms        ? ?/sec    1.00      6.0±0.06ms        ? ?/sec
physical_sorted_union_order_by_10_uint64              1.01     12.9±0.07ms        ? ?/sec    1.00     12.8±0.05ms        ? ?/sec
physical_sorted_union_order_by_50_int64               1.00    162.3±0.91ms        ? ?/sec    1.00    162.5±1.04ms        ? ?/sec
physical_sorted_union_order_by_50_uint64              1.01    376.1±2.45ms        ? ?/sec    1.00    373.6±2.63ms        ? ?/sec
physical_theta_join_consider_sort                     1.01   1776.7±7.55µs        ? ?/sec    1.00  1754.5±17.61µs        ? ?/sec
physical_unnest_to_join                               1.00  1852.3±16.93µs        ? ?/sec    1.00  1843.8±12.45µs        ? ?/sec
physical_window_function_partition_by_12_on_values    1.00  1110.8±10.07µs        ? ?/sec    1.00  1110.9±13.58µs        ? ?/sec
physical_window_function_partition_by_30_on_values    1.00      2.3±0.01ms        ? ?/sec    1.00      2.3±0.01ms        ? ?/sec
physical_window_function_partition_by_4_on_values     1.02    682.4±4.17µs        ? ?/sec    1.00    670.8±4.37µs        ? ?/sec
physical_window_function_partition_by_7_on_values     1.01    834.0±5.28µs        ? ?/sec    1.00    827.6±5.78µs        ? ?/sec
physical_window_function_partition_by_8_on_values     1.01    893.6±8.56µs        ? ?/sec    1.00    886.8±5.06µs        ? ?/sec
with_param_values_many_columns                        1.01    654.3±5.30µs        ? ?/sec    1.00    650.4±3.87µs        ? ?/sec

@pepijnve
Copy link
Contributor Author

pepijnve commented Nov 20, 2025

@alamb the variant of the code that did not use GuaranteeRewrite is only a small delta wrt the state of this PR now. Shall I make a new branch with that variant (keeping everything else equal) so we can compare planning performance between the two?

I went ahead and made that change at pepijnve#2

@alamb
Copy link
Contributor

alamb commented Nov 20, 2025

Sounds good -- will run

@alamb
Copy link
Contributor

alamb commented Nov 20, 2025

Since this PR has been outstanding for so long and it fixes a bug and I desperately want to close it down (so we can move on) I am going to merge it as is.

@pepijnve would you be willing to make a real PR with the change from

@alamb alamb added this pull request to the merge queue Nov 20, 2025
Merged via the queue into apache:main with commit 0bd127f Nov 20, 2025
32 checks passed
@pepijnve
Copy link
Contributor Author

Since this PR has been outstanding for so long and it fixes a bug and I desperately want to close it down (so we can move on) I am going to merge it as is.

My browser just let out a sigh of relief. GitHub's UI was struggling with this one.

@pepijnve would you be willing to make a real PR with the change from

Certainly

@alamb
Copy link
Contributor

alamb commented Nov 20, 2025

Since this PR has been outstanding for so long and it fixes a bug and I desperately want to close it down (so we can move on) I am going to merge it as is.

My browser just let out a sigh of relief. GitHub's UI was struggling with this one.

😆 thank you for sticking with it -- I think the code overall (not just case reporting) is significantly better because of your work

@pepijnve would you be willing to make a real PR with the change from

Certainly

🙏

logan-keede pushed a commit to logan-keede/datafusion that referenced this pull request Nov 23, 2025
## Which issue does this PR close?

- None, break out PR of changes done in apache#17813

## Rationale for this change

In apache#17813 `GuaranteeRewriter` is used from the `datafusion_expr` crate.
In order to enable this the type needed to be moved from
`datafusion_optimizer` to `datafusion_expr`.

Additionally, during the development of apache#17813 some latent bugs were
discovered in `GuaranteeRewriter` that have been resolved.

## What changes are included in this PR?

- Move `GuaranteeRewriter` to `datafusion_expr`
- Fix two bugs where rewrites of 'between' expression would fail
  - when one of the bounds was untyped null
  - when the lower bound was greater than the upper bound
- Add logic to replace expressions with literal null based on provided
guarantees
- Split implementation into smaller functions for easier readability

## Are these changes tested?

- Existing tests updated
- Tests added for bugfixes

## Are there any user-facing changes?

No

---------

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
logan-keede pushed a commit to logan-keede/datafusion that referenced this pull request Nov 23, 2025
…e#17813)

## Which issue does this PR close?

- Closes apache#17801
- Obviates (contains) and thus Closes apache#17833
- Obviates (contains) and thus Closes apache#18536

## Rationale for this change

apache#17357 introduced a change that replaces `coalesce` function calls with
`case` expressions. In the current implementation these two differ in
the way they report their nullability. `coalesce` is more precise than
`case` all will report itself as not nullable in situations where the
equivalent `case` does report being nullable.

The rest of the codebase currently does not expect the nullability
property of an expression to change as a side effect of expression
simplification. This PR is a first attempt to align the nullability of
`coalesce` and `case`.

## What changes are included in this PR?

Tweaks to the `nullable` logic for the logical and physical `case`
expression code to report `case` as being not nullable in more
situations.

- For logical `case`, a best effort const evaluation of 'when'
expressions is done to determine 'then' reachability. The code errs on
the conservative side wrt nullability.
- For physical `case`, const evaluation of 'when' expressions using a
placeholder record batch is attempted to determine 'then' reachability.
Again if const evaluation is not possible, the code errs on the
conservative side.
- The optimizer schema check has been relaxed slightly to allow
nullability to be removed by optimizer passes without having to disable
the schema check entirely
- The panic'ing benchmark has been reenabled

## Are these changes tested?

Additional unit tests have been added to test the new logic.

## Are there any user-facing changes?

No

---------

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate 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.

Regression: error planning TPC-DS query: input schema nullability mismatch

4 participants