cockroachdb / cockroach

CockroachDB — the cloud native, distributed SQL database designed for high availability, effortless scale, and control over data placement.
https://www.cockroachlabs.com
Other
30.14k stars 3.81k forks source link

sql: allow typed DNull datums #78358

Open mgartner opened 2 years ago

mgartner commented 2 years ago

Currently, we use the same tree.DNull singleton to represent the NULL datum for all types. However, this causes inconsistencies with Postgres.

In Postgres, the NULL datum seems to carry some type other than unknown with it:

marcus=# CREATE TYPE typ AS ENUM ('foo', 'bar', 'baz');
CREATE TYPE

marcus=# SELECT enum_range(NULL::typ);
  enum_range
---------------
 {foo,bar,baz}
(1 row)

In CRDB, evaluating the same expression is impossible because NULL's type is always unknown:

defaultdb> CREATE TYPE typ AS ENUM ('foo', 'bar', 'baz');
CREATE TYPE

defaultdb> SELECT enum_range(NULL::typ);
ERROR: enum_range(): argument cannot be NULL
SQLSTATE: 22004

NOTE: I'm not exactly sure that Postgres NULL datums carry some type with them during evaluation. There could be some other mechanism at play that allow evaluation of the example above.

Epic CRDB-2474 Jira issue: CRDB-14090

jordanlewis commented 2 years ago

I think this issue has some prior art/conversation: https://github.com/cockroachdb/cockroach/issues/23101