cockroachdb / cockroachdb/cockroach
opt: decorrelation rule can cause SQL error
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
The decorrelation rule `TryDecorrelateScalarGroupBy` pulls a ScalarGroupBy from the input of a correlated InnerJoin. As part of this transformation, the ScalarGroupBy becomes a GroupBy and the join becomes a LeftJoin to recover the "row-preserving" behavior of joining against an expression that will always have one row.
As part of this rule, it is necessary to make sure that the GroupBy aggregate functions result in NULL for rows that are null-extended by the LeftJoin. This is accomplished using CASE expressions for functions that don't "transmit" NULL values. However, the rule currently doesn't do anything to handle functions that produce an error on NULL input. For example, `json_object_agg` returns an error when the `key` argument is NULL. Because of this, `TryDecorrelateScalarGroupBy` can cause a query to return an error even if it's written in such a way that the error shouldn't be possible. Here's an example:
```
CREATE TABLE t1 (a INT, b INT);
CREATE TABLE t2 (c INT, d INT);
INSERT INTO t1 VALUES (1, 2), (3, 4), (5, 6);
INSERT INTO t2 VALUES (1, 2), (3, 4);
SELECT * FROM t1, LATERAL (SELECT json_object_agg(t2.c::TEXT, t2.d::TEXT) FROM t2 WHERE t2.c = t1.a)
```
Postgres gives this result:
```
a | b | json_object_agg
---+---+-----------------
1 | 2 | { "1" : "2" }
3 | 4 | { "3" : "4" }
5 | 6 |
(3 rows)
```
While CRDB currently gives: `(22004) null value not allowed for object key`
Jira issue: CRDB-57163
Contributor guide
Assessment
This issue has not been assessed yet.