-
Notifications
You must be signed in to change notification settings - Fork 1.7k
#17801 Improve nullability reporting of case expressions #17813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
d2e613a
to
88a911b
Compare
88a911b
to
482d0be
Compare
4bbaa82
to
7f8d7cf
Compare
There was a problem hiding this 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?
#[test] |
datafusion/expr/src/expr_schema.rs
Outdated
when(binary_expr(col("foo"), Operator::Eq, lit(5)), col("foo")) | ||
.otherwise(lit(0))?, |
There was a problem hiding this comment.
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:
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))?, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
datafusion/expr/src/expr_schema.rs
Outdated
assert!(expr.nullable(&get_schema(false)).unwrap()); | ||
} | ||
|
||
fn check_nullability( |
There was a problem hiding this comment.
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
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( |
There was a problem hiding this comment.
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.
datafusion/expr/src/expr_schema.rs
Outdated
check_nullability( | ||
when(binary_expr(col("foo"), Operator::Eq, lit(5)), col("foo")) | ||
.otherwise(lit(0))?, | ||
true, |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
datafusion/expr/src/expr_schema.rs
Outdated
when( | ||
or( | ||
is_not_null(col("foo")), | ||
binary_expr(col("foo"), Operator::Eq, lit(5)), |
There was a problem hiding this comment.
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
datafusion/expr/src/expr_schema.rs
Outdated
col("foo"), | ||
) | ||
.otherwise(lit(0))?, | ||
true, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
datafusion/expr/src/expr_schema.rs
Outdated
.otherwise(lit(0))?, | ||
true, | ||
get_schema, | ||
)?; |
There was a problem hiding this comment.
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,
)?;
There was a problem hiding this comment.
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
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
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 |
9516262
to
a6ab83a
Compare
@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 In particular there's one interesting case (pun not intended) which results from the What I attempted to do in this PR is to look at the more general form 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. |
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
|
@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. |
No pressure from my side. I just write up my notes and move on to the next thing. Async delayed response is fine. |
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 |
Which issue does this PR close?
sql_planner
benchmark panic'ing on main #17801Rationale for this change
#17357 introduced a change that replaces
coalesce
function calls withcase
expressions. In the current implementation these two differ in the way they report their nullability.coalesce
is more precise thancase
all will report itself as not nullable in situations where the equivalentcase
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
andcase
.What changes are included in this PR?
Tweaks to the
nullable
logic for the logical and physicalcase
expression code to reportcase
as being not nullable in more situations.Are these changes tested?
Additional unit tests have been added to test the new logic.
Are there any user-facing changes?
No