sql: DISTINCT ON a computed key fails with ORDER BY ... NULLS LAST (v26.2 regression)
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 56/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Domain
- backend-api-design, databases
Research direction
Start in SQL optimization around buildOrderByPreProjection, identified in the issue as a likely cause, and trace how computed ORDER BY columns are projected when NULLS ordering is explicit. Compare that path with how the DISTINCT ON key’s projection is matched. Reproduce with the supplied SQL and verify it succeeds while the listed plain-column, default-order, and subquery cases continue to work.
Written by the indexing model from the issue text.
Description
Describe the problem
Since v26.2.0-alpha.2, DISTINCT ON over a computed key errors when a later ORDER BY column has non-default NULLS ordering. v26.1 and Postgres accept the query.
To Reproduce
CREATE TABLE t (a INT, b INT);
INSERT INTO t VALUES (1, NULL), (1, 2), (2, 3);
SELECT DISTINCT ON (k) a + 1 AS k, b FROM t ORDER BY k, b NULLS LAST;
ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions
SQLSTATE: 42P10
These still work: a plain-column key (DISTINCT ON (a) ... ORDER BY a, b NULLS LAST), default NULLS order (ORDER BY k, b), or computing the key in a subquery.
Expected behavior
v26.1.8 returns:
k | b
----+----
2 | 2
3 | 3
Additional data
| Version | Result |
|---|---|
| v25.4.16, v26.1.0, v26.1.8, v26.2.0-alpha.1 | OK |
| v26.2.0-alpha.2, v26.2.0, v26.2.11, v26.3.5, v26.4.0-rc.1 | 42P10 |
The likely cause is 35c5a88b3582 (sql/opt: support subqueries in ORDER BY with NULLS FIRST/LAST, #129956). With non-default NULLS ordering, buildOrderByPreProjection appears to move computed ORDER BY columns to new pre-projected IDs, so they no longer match the DISTINCT ON key's projection column.
Environment:
- CockroachDB v26.2.11, v26.3.5 and v26.4.0-rc.1 (official darwin arm64 binaries,
start-single-node) - Client:
cockroach sql
Jira issue: CRDB-71580
- 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
-
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
-
bug ready-for-triage ready-to-code
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
RedHatInsights/insights-ingress-go#946 · 4 comments ·
Maintainers usually reply within 1 day