cockroachdb / cockroachdb/cockroach
opt: unnest as table source should expand tuple return values
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
When the `unnest` builtin function is used as a table source and returns a tuple value, the output rows should be expanded into multiple columns. We don't currently do this; instead, the result rows are let as tuples. We currently decide whether to expand the result columns of an srf by examining its return type for tuple labels (with a special case for unnest). We should instead check whether the srf is being used as a data source, similar to what is done for UDFs in #98162.
```
root@localhost:26257/defaultdb> CREATE TABLE t(a INT, b INT);
CREATE TABLE
Time: 8ms total (execution 7ms / network 0ms)
root@localhost:26257/defaultdb> INSERT INTO t VALUES (1, 2), (3, 4), (5, 6);
INSERT 0 3
Time: 5ms total (execution 5ms / network 0ms)
root@localhost:26257/defaultdb> SELECT * FROM t, LATERAL (SELECT * FROM unnest(ARRAY[t.*])) AS foo;
a | b | unnest
----+---+---------
1 | 2 | (1,2)
3 | 4 | (3,4)
5 | 6 | (5,6)
(3 rows)
Time: 1ms total (execution 1ms / network 0ms)
```
Postgres expands the unnest column:
```
postgres=# SELECT * FROM t, LATERAL (SELECT * FROM unnest(ARRAY[t.*])) AS foo;
a | b | a | b
---+---+---+---
1 | 2 | 1 | 2
3 | 4 | 3 | 4
5 | 6 | 5 | 6
(3 rows)
```
Jira issue: CRDB-25214
Contributor guide
Research direction
Start by tracing how the unnest table-source SRF determines result columns, then compare it with the UDF handling described in #98162. Reproduce the supplied LATERAL query and verify that tuple results become separate columns, with regression coverage showing the expanded output.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, sql
- Domain
- database
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100