-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
sql: NaN = NaN (in Postgres) #18860
Comments
There's some inconsistency here. Even though we define
We should either fix this to make it work like it does for |
I think the natural fix would be to treat Cc @bdarnell who tends to feel more strongly about these things (both correctness and compatibility) |
I don't feel strongly about this one. I agree with @nvanbenschoten that maintaining compatibility with past versions of cockroach is less important here since we're already kind of broken. I think ideally we'd make NaN work like NULL in an index, but I don't know how much work that would be. I'd be OK with following the postgres example and making |
vote +1 to follow the Postgres convention of NaN==NaN. Please see note below on ordering behavior as well From https://www.postgresql.org/docs/9.1/static/datatype-numeric.html
|
For comparison, mysql and sql server do not allow storage of NaN values (2010 source). Oracle implements NaN the same way postgres does, with I don't see any DB supporting NaN in the way mandated by IEEE754 (which kind of makes sense, because this behavior is available by using NULL in place of NaN, and making a second kind of value that is unequal to itself can be complex). So now I'm leaning towards following the postgres/oracle example and deviating from IEEE754. |
NULL isn't quite a replacement for NaN because the latter can be generated as the result of computations. That said, given that we already have an inconsistency that needs to be fixed, I'm inclined to make While fixing this, we should make sure that We'll need to call this out in release notes when this lands. |
If both Oracle and Postgres treat Yes, this needs to be pretty loud in our release notes. |
I'm pretty sure we don't handle |
This change adds new normalization rules: ``` ComparisonExpr{Is,v,d} where d != DNull -> AndExpr{ ComparisonExpr{EQ,v,d} ComparisonExpr{IsNot,v,DNull} } ComparisonExpr{IsNot,v,d} where d != DNull -> OrExpr{ ComparisonExpr{NE,v,d} ComparisonExpr{Is,v,DNull} } ``` This allows us to take advantage of obvious index constraints for queries like `SELECT * FROM t WHERE b IS TRUE`. It will also avoid complications being discussed in cockroachdb#18860 by addressing the root issue. There may be an argument to make this a simplification rule instead.
This change adds new normalization rules: ``` ComparisonExpr{Is,v,d} where d != DNull -> AndExpr{ ComparisonExpr{EQ,v,d} ComparisonExpr{IsNot,v,DNull} } ComparisonExpr{IsNot,v,d} where d != DNull -> OrExpr{ ComparisonExpr{NE,v,d} ComparisonExpr{Is,v,DNull} } ``` This allows us to take advantage of obvious index constraints for queries like `SELECT * FROM t WHERE b IS TRUE`. It will also avoid complications being discussed in cockroachdb#18860 by addressing the root issue. There may be an argument to make this a simplification rule instead.
To complicate this story even more, Postgres actually has
Our current behavior is (like in Go) to return false from all comparisons with |
Sounds good to me. Crossing my fingers we don't have to address this in the future. |
Yikes..
It certainly would be easier to adopt their behavior and just define |
That's an excellent point. The comparison of |
So I think I'm just going to pull this all the way back and treat |
Fixes cockroachdb#18860. This change switches `NaN` equality semantics from defining `NaN` as not-equal to itself to defining it as equal to itself. This is a very sad violation of IEEE754 in the name of Postgres compatibility.
below is the order Cockroach follows based on discussion with @nvanbenschoten
http://troels.arvin.dk/db/rdbms/#select-order_by show Postgres, DB2 and Oracle put NULL higher than any other values and MSSQL and MySQL going the other way. There isn't specific customers to cite at the moment raising this as an issue, but customers would expect Postgres API database should follow Postgres behavior. If we also assume CockroachDB customers are coming from Postgres, DB2 and Oracle and these then following Postgres behavior would make more sense. |
Fixes cockroachdb#18860. This change switches `NaN` equality semantics from defining `NaN` as not-equal to itself to defining it as equal to itself. This is a very sad violation of IEEE754 in the name of Postgres compatibility.
Fixes cockroachdb#18860. This change switches `NaN` equality semantics from defining `NaN` as not-equal to itself to defining it as equal to itself. This is a very sad violation of IEEE754 in the name of Postgres compatibility.
In Postgres,
NaN = NaN
. This is a violation of IEEE754:Cockroach provides the expected result
I'm not sure if there is anything to be done here as changing our behavior at this point would be backwards incompatible. And even if we could change it, I think the Postgres behavior is wrong. In Cockroach,
NaNs
can be detected using:The rationale for the Postgres behavior is:
In Cockroach, we allow indexing
NaNs
and using the index to excludeNaNs
from scans just as we indexNULLs
, but we maintain the IEEE754 behavior ofNaN != NaN
.The text was updated successfully, but these errors were encountered: