[MDEV-28420] For null comparison there should also be a <!=> for not equal Created: 2022-04-27  Updated: 2022-05-20  Resolved: 2022-05-20

Status: Closed
Project: MariaDB Server
Component/s: OTHER
Affects Version/s: None
Fix Version/s: N/A

Type: Bug Priority: Major
Reporter: Tom Miller Assignee: Sergei Golubchik
Resolution: Not a Bug Votes: 0
Labels: None


 Description   

To overcome this, certain operators are specifically designed for use with NULL values. To cater for testing equality between two values that may contain NULLs, there's <=>, NULL-safe equal.

https://mariadb.com/kb/en/null-values/

There should also be a test for not equal.



 Comments   
Comment by Daniel Black [ 2022-04-27 ]

Something from a SQL standard that didn't exist at the time NULL-safe operators where implement, is IS [NOT] DISTINCT FROM that has the same function.

Comment by Tom Miller [ 2022-04-27 ]

Just doing some research, I found this:
"Comparisons Involving null
Comparisons (<, >, =, …) to null are neither true nor false but instead return the third logical value of SQL: unknown. The following example removes all rows—even those where col is actually null—because the where clause treats unknown like false."

The article had links to SQL-2016 standard documentation. If you compare "a"= NULL the result is unknown or False. Not True. Why does MariaDB default this to True?

Comment by Daniel Black [ 2022-04-27 ]

Looks like unknown in the form of NULL to me:

ref: fiddle.

Comment by Tom Miller [ 2022-04-27 ]

That doesn't mean it meets SQL ISO specs.

"a" = Null => False
"a" != Null => True
"a" > Null => Error not a valid comparison
"a" < Null => Error not a valid comparison

Comment by Sergei Golubchik [ 2022-05-05 ]

1222tmiller, I don't understand what you're saying here.

If a <=> b is a test for a and b being equal, then NOT a <=> b is a test for them being not equal. Is that what you mean?

Why does MariaDB default this to True?

MariaDB defaults what to True?

Comment by Tom Miller [ 2022-05-05 ]

I was trying to do just that inside a column IF statement. IF(true, UseValueIfTrue, UseValueIfFalse);

It read better as IF(a != b, x, y); It started failing with NULL as I tested this early on and it worked.

So I did exactly as you suggested and IF(NOT(a <=> a), x, y) failed. So I just flipped it to get around the bug:

IF(a <=> b, y, x);

The ANSI standard is NULL = NULL => True Anything else is false. An unassigned value is not equal to "Tom".

NULL = 'Tom' => False Why I get a true back all of a sudden is a mystery. I am using the new 3.0 connector, so it could be that.

Comment by Sergei Golubchik [ 2022-05-20 ]

Could you clarify that, please?

  • what do you mean by "failed"?
  • Where can I read that "The ANSI standard is NULL = NULL => True Anything else is false" ?
Comment by Tom Miller [ 2022-05-20 ]

IF(a != b, x, y);

If a = "MyTestString" b = NULL

a is not equal to b so the result should be true. But it returns false and uses x as the result.

This is a bug.

Comment by Sergei Golubchik [ 2022-05-20 ]

No, this is not a bug.

See the MariaDB Knowledge Base: https://mariadb.com/kb/en/null-values/

Or, literally, the very first results in internet search for "comparison with null": https://learnsql.com/blog/null-comparison-operators/

Generated at Thu Feb 08 10:00:36 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.