Skip to content
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

Nullif of NULL Predicate is not NULL #3589

Closed
tustvold opened this issue Jan 23, 2023 · 2 comments
Closed

Nullif of NULL Predicate is not NULL #3589

tustvold opened this issue Jan 23, 2023 · 2 comments
Labels
bug development-process Related to development process of arrow-rs question Further information is requested

Comments

@tustvold
Copy link
Contributor

Describe the bug

SQL NULLIF returns NULL if the predicate evaluates to NULL.

i.e.

> SELECT NULLIF(NULL, 1)
NULL

Whereas the nullif kernel in arrow preserves the nullability of the input.

This appears to date from the initial implementation of this kernel in https://github.com/apache/arrow/pull/8688/files#diff-d3f27fc6d1b5bf9dcc0eec10fb37f9f13e9dcdf5f608b932d0e0c0151a06b91aR253 by @velvia

To Reproduce

Expected behavior

I'm not really sure, I think it is surprising that we break SQL compatibility in this way, but we have been doing so for a while so I'm not sure if this is intentional or just nobody has noticed.

Additional context

@tustvold
Copy link
Contributor Author

tustvold commented Jan 23, 2023

Looking at how this is typically used I realise the current logic is correct. In particular the common pattern is

let cond_array = eq_dyn(lhs, rhs)?;
// Now, invoke nullif on the result
let array = nullif(lhs, as_boolean_array(&cond_array)?)?;

In particular if lhs or rhs is null, cond_array will also be null.

The result should only be null if lhs is null, which is the case if NULL passed to nullif preserves the nullability of lhs in the event of rhs being null.

>>> duckdb.query("Select nullif(null, 1)").fetchall()
[(None,)]
>>> duckdb.query("Select nullif(true, 1)").fetchall()
[(None,)]
>>> duckdb.query("Select nullif(false, 1)").fetchall()
[(False,)]
>>> duckdb.query("Select nullif(null, null)").fetchall()
[(None,)]
>>> duckdb.query("Select nullif(null, 1)").fetchall()
[(None,)]
>>> duckdb.query("Select nullif(1, null)").fetchall()
[(1,)]

@tustvold tustvold closed this as not planned Won't fix, can't repro, duplicate, stale Jan 23, 2023
@alamb
Copy link
Contributor

alamb commented Jan 23, 2023

Posgres agrees:

postgres=# Select nullif(null, null);
 nullif 
--------
 
(1 row)

postgres=# Select nullif(null, 1)
postgres-# ;
 nullif 
--------
       
(1 row)

postgres=# Select nullif(1, null);
 nullif 
--------
      1
(1 row)

🤯

@tustvold tustvold added the development-process Related to development process of arrow-rs label Jan 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug development-process Related to development process of arrow-rs question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants