[Qt-interest] Comparing two NULL QVariants returns true

Robert Hairgrove evorgriahr at hispeed.ch
Sun May 29 15:08:30 CEST 2011


On Sun, 2011-05-29 at 14:33 +0200, Thiago Macieira wrote:
> On Sunday, 29 de May de 2011 13:18:44 Robert Hairgrove wrote:
> > In the internal "compare()" function in file qvariant.cpp, the case
> > where two null variants are compared is at the very bottom:
> > 
> >     if (a->is_null && b->is_null)
> >         return true;
> > 
> > I was wondering why this is implemented as such, whereas most databases
> > will return false when two NULL values are compared. What was the
> > rationale behind this?
> 
> You're saying that for databases, this happens:
> 
> 	Type a = null;
> 	Type b = a;
> then
> 	a != b;
> ?
> 
> Sounds very counter-intuitive. I've only seen that for NaNs.
> 

It is counter-intuitive. Actually, though, it is just three-valued
logic. Joe Celko gives a good example in his book "SQL For Smarties" in
the chapter on comparison of NULL values. Consider the following
statements:

SELECT * FROM SomeTable WHERE SomeColumn = 2
UNION SELECT * FROM SomeTable WHERE SomeColumn != 2;

One would expect to see all rows of "SomeTable" in the result set.
However, any rows where SomeColumn has NULL values will be missing!

In order to see all the rows, you have to write it like this:

SELECT * FROM SomeTable WHERE SomeColumn = 2
UNION SELECT * FROM SomeTable WHERE SomeColumn != 2
UNION SELECT * FROM SomeTable WHERE SomeColumn IS NULL;

But since QVariant is a full-fledged type, and two invalid QVariants
should be the same when compared bitwise, returning true is probably
more consistent from a C++ language point of view.




More information about the Qt-interest-old mailing list