cockroachdb / cockroachdb/cockroach
sub-select fails to run `json_object_agg` safely
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
**Describe the problem**
When run as a projected single-column sub-select, `json_object_agg()` appears to fail when there are no rows being aggregated on, but only when the filtering condition uses columns from the correlated outer query.
The query:
```
select
(
select
-- count(y) <- this works, and returns [0, 1]
json_object_agg('how could this', 'possibly fail?')
from unnest('{0,1}'::text[]) as u(y)
where
t.x = u.y
)
from unnest('{1,2}'::text[]) as t(x)
```
fails with the error:
```
ERROR: field name must not be null
```
For comparison, when using instead `where false` the result is two rows of `NULL`, as expected.
**To Reproduce**
The query is self-contained. Simply run it with `psql` or similar.
**Expected behavior**
Postgres produces this output for the query:
```
json_object_agg
-----------------------------------------
{ "how could this" : "possibly fail?" }
(2 rows)
```
**Additional data / screenshots**
**Environment:**
- CockroachDB version v22.2.8 (via docker image)
- Server OS: Linux Nixos
- Client app : psql
**Additional context**
What was the impact?
Add any other context about the problem here.
Jira issue: CRDB-31121
Contributor guide
Assessment
This issue has not been assessed yet.