COALESCE correlated scalar subquery duplicates result rows
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 22/100
Research direction
Start by reproducing both queries with the schema and data in the issue, then compare EXPLAIN output for the control and the correlated-subquery version to find where the extra rows appear. The problem is in how the optimizer handles a correlated scalar subquery under GROUP BY, which needs real familiarity with the SQL planner. Done means both queries return blue and red once each, with a regression test covering this shape.
Written by the indexing model from the issue text.
Description
Describe the problem
A correlated scalar subquery containing COALESCE duplicates rows when evaluated in a GROUP BY query.
The direct expression returns blue and red once each. Moving the same expression into a scalar subquery correlated by a primary key causes each value to be returned twice.
To Reproduce
The issue reproduces with the following sequence:
- Start a CockroachDB v26.3.0 cluster and connect with
cockroach sql. - Create the two tables and insert the rows shown in Additional data and screenshots.
- Run the control query and observe that it returns two rows.
- Run the correlated-subquery query and observe four rows:
blueandredare each returned twice.
Expected behavior
Both queries should return blue and red once each.
The inner query is correlated using the primary-key column coalesce_data.id and therefore produces exactly one scalar value for every outer row. Moving the expression into the correlated scalar subquery should not change the result cardinality.
Additional data and screenshots
The complete schema, test data, control query, affected query, and observed output are included below. No screenshot is required because the behavior is visible in the cockroach sql output.
Schema and test data
DROP TABLE IF EXISTS coalesce_guard;
DROP TABLE IF EXISTS coalesce_data;
CREATE TABLE coalesce_guard (
marker STRING
);
CREATE TABLE coalesce_data (
id INT PRIMARY KEY,
payload STRING
);
INSERT INTO coalesce_guard VALUES ('blocked');
INSERT INTO coalesce_data VALUES (1, 'red'), (2, 'blue');
Control query
SELECT NULLIF(
COALESCE(d.payload, 'missing'),
g.marker
) AS result
FROM coalesce_data AS d
CROSS JOIN coalesce_guard AS g
GROUP BY d.id, d.payload, g.marker
ORDER BY result;
Control output
result
------
blue
red
(2 rows)
Affected query
SELECT NULLIF(
(
SELECT COALESCE(x.payload, 'missing')
FROM coalesce_data AS x
WHERE x.id = d.id
),
g.marker
) AS result
FROM coalesce_data AS d
CROSS JOIN coalesce_guard AS g
GROUP BY d.id, d.payload, g.marker
ORDER BY result;
Actual output
result
------
blue
blue
red
red
(4 rows)
Environment:
- CockroachDB version: CockroachDB CCL v26.3.0, built 2026-07-28
- Server OS: macOS on Apple Silicon
- CockroachDB build target:
aarch64-apple-darwin21.2 - Client application:
cockroach sql - Connection: local single-node test cluster at
127.0.0.1:26257
Additional context
This is a silent wrong-result issue. The additional rows change the query cardinality without producing an error, making the problem difficult for an application to detect.
Applications using the result for grouping, deduplication, counting, or downstream processing may silently process each value more than once.
Jira issue: CRDB-71124
- Dominant language
- Go
- Stars
- 32.6k
- Forks
- 4.2k
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from cockroachdb/cockroach
-
C-bug O-community X-blathers-untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
cockroachdb/cockroach#176245 · 1 comment ·
-
sql: pg_database.dattablespace is 0 but pg_tablespace.oid is 1663, so they no longer joinPossibly taken @1AbdullahButt claimed this 14 days ago. OpenC-bug O-community X-blathers-triaged X-move-internal
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
cockroachdb/cockroach#176228 · 1 comment ·
-
PG compat: overflow in integer power calculation triggers incompatible SQLSTATEMay be free again @darshpatel1052 claimed this 18 days ago, and no pull request is open. OpenC-bug O-community X-blathers-untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
cockroachdb/cockroach#176223 · 2 comments ·
-
util/log: bufferedSinkCloser.Close leaks a goroutine after timeoutPossibly taken @dajiaohuang claimed this 14 days ago. OpenC-bug O-community X-blathers-untriaged
Difficulty 1/5 Under an hour Newbie friendliness 84/100
cockroachdb/cockroach#176210 · 1 comment ·
-
C-bug O-community T-sql-queries X-blathers-triaged X-move-internal
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
cockroachdb/cockroach#176150 · 1 comment ·
All issues in cockroachdb/cockroach
Similar issues
-
☢️ Bug v3
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
area/import-export area/projects concern/ux
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
go-vikunja/vikunja#4455 · 1 comment ·
Maintainers usually reply within 2 days
-
Dependabot PRs no longer merge automaticallyPossibly taken @dwisiswant0 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
projectdiscovery/urlfinder#160 ·
Maintainers usually reply within 1 day
-
database
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
chnm/legal-modernism#450 ·
Maintainers usually reply within 1 day
-
tech-debt
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day