cockroachdb / cockroachdb/cockroach

IN predicate nested tuple type support

Open
#102,017 2 comments 0 reactions 0 assignees View on GitHub
C-bug T-sql-queries
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

**Is your feature request related to a problem? Please describe.**
Postgres supports IN subqueries such as `SELECT * FROM ab WHERE ROW(ROW(a, b)) IN (SELECT (x, y) FROM xy);` while this fails in CRDB during type checking.
Similar query `SELECT * FROM ab WHERE (a,b) IN (SELECT (x, y) FROM xy);` works in CRDB, but fails in Postgres with:
`ERROR: subquery has too few columns`.

**Describe the solution you'd like**
Add support for any level of nested tuples in IN subqueries, eg.
```
CREATE TABLE xy (x INT, y INT);
CREATE TABLE ab (a INT, b INT);

SELECT * FROM ab WHERE ROW(ROW(a, b)) IN (SELECT (x, y) FROM xy);
SELECT * FROM ab WHERE ROW(ROW(ROW(a, b))) IN (SELECT ROW(ROW(x, y)) FROM xy);
SELECT * FROM ab WHERE ROW(ROW(ROW(ROW(a, b)))) IN (SELECT ROW(ROW(ROW(x, y))) FROM xy);
etc...
```

**Describe alternatives you've considered**
Also, consider whether we want to match Postgres behavior for other subquery forms.
For example, should we error out the following as Postgres does:
`SELECT * FROM ab WHERE (a,b) IN (SELECT (x, y) FROM xy);`

**Additional context**
This issue is related to https://github.com/cockroachdb/cockroach/pull/101975

Jira issue: CRDB-27220

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the three nested-tuple IN queries in the issue against CockroachDB and PostgreSQL, then inspect the type-checking path for IN subqueries. Done means arbitrary tuple nesting works consistently, with an explicit decision and coverage for whether the simpler `(a,b) IN (SELECT (x,y) ...)` form should match PostgreSQL's error.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, sql
Domain
databases
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.