Support for IS DISTINCT FROM and IS NOT DISTINCT FROM operators #12612
Labels
topic: operators
Features & issues about comparing values inside of WHERE
type: feature
For issues and PRs. For new features. Never breaking changes.
Feature Description
IS DISTINCT FROM
and the relatedIS NOT DISTINCT FROM
operators are defined in SQL:2003 as a null-safe way to compare two values. Their advantage over<>
and=
, respectively, is thatx IS DISTINCT FROM NULL
evaluates toFALSE
for all non-null x whilex <> NULL
evaluates toNULL
for all x (includingNULL
).Supported in Postgres since at least 8.0: https://www.postgresql.org/docs/8.0/functions-comparison.html
MySQL has a related "null-safe equals operator"
<=>
: https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_equal-toSQLite has an
IS
operator that behaves identically: https://www.sqlite.org/lang_expr.html#isisnot. Note that this is more general than theIS NULL/TRUE/FALSE/UNKNOWN
operators that Postgres and other database systems implement.To my knowledge, SQL Server has nothing comparable: https://docs.microsoft.com/en-us/sql/t-sql/language-elements/comparison-operators-transact-sql?view=sql-server-ver15
Is your feature request related to a problem? Please describe.
Finding all rows that match a predicate, without worrying about the predicate being NULL
Describe the solution you'd like
I see 2 potential solutions. The simplest would be to add a
distinctFrom
operator. The alternative would be to amend the existingis
operator to evaluate toIS DISTINCT FROM
on Postgres and<=>
on MySQL.On Postgres,
IS
must be followed byNULL
,TRUE
,FALSE
, orUNKNOWN
(or aNOT
and then one ofNULL
,TRUE
,FALSE
, orUNKNOWN
). In my testing, a clause like{[Op.is]: 'foo'}
will outputIS 'foo'
, which is a syntax error. To match the user's intent, Sequelize should be outputtingIS DISTINCT FROM 'foo'
.I assume Sequelize currently has similar behavior on MySQL (outputting
foo IS 'foo'
instead offoo <=> 'foo'
), but I haven't tested it.Why should this be in Sequelize
Adds a construct implemented in one form or another by most supported database systems.
Describe alternatives/workarounds you've considered
This can be worked around by using equality comparison coupled with
IS NULL
. For example, to find all values not equal tox
:Additional context
https://wiki.postgresql.org/wiki/Is_distinct_from
https://www.postgresql.org/docs/current/functions-comparison.html
https://www.sqlite.org/lang_expr.html#:~:text=In%20other%20words%2C%20the%20operator,it%20does%20in%20ANSI%2DC.&text=The%20IS%20and%20IS%20NOT,evaluates%20to%200%20(false).
https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_equal-to
https://mariadb.com/kb/en/null-safe-equal
Feature Request Checklist
Is this feature dialect-specific?
Would you be willing to implement this feature by submitting a Pull Request?
The text was updated successfully, but these errors were encountered: