sql: vectorized engine fails with `integer out of range` on a row after a satisfied LIMIT in a WHERE filter, row-by-row engine returns the result
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 48/100
Direzione di ricerca
Start with the vectorized projection path in pkg/sql/colexec/colexecproj/proj_non_const_ops_tmpl.go and its generated proj_non_const_ops.eg.go, then compare how pkg/sql/rowexec/filterer.go stops after LIMIT is satisfied. Reproduce with the supplied SQL using vectorize = on and vectorize = off; trace how projection, selection, and limit process a batch. Done means the vectorized query returns 1025 without raising an overflow from the later row.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Describe the problem
Table t(id, x, y) has 1026 rows; row 1025 has x + y = 0 and row 1026 has x = 9223372036854775807, y = 1. SELECT id FROM t WHERE (x + y) <= 0 ORDER BY id LIMIT 1 returns 1025 in the row-by-row engine, but fails with SQLSTATE 22003 integer out of range in the vectorized engine. The row-by-row engine evaluates the filter row by row, finds the first match at row 1025, and stops because LIMIT 1 is satisfied, so row 1026 is never evaluated. The vectorized engine computes x + y over the next whole batch (rows 1025 and 1026), the addition overflows on row 1026, and the error is raised before the selection operator and the limit can truncate the batch. The correct result is 1025: LIMIT 1 is already satisfied by row 1025 and later rows should not affect the result. The vectorized engine is the one that needs to change.
To Reproduce
Deployment: a single-node CockroachDB cluster started with cockroach start-single-node --insecure (tested: v26.2.7, 1 node, default settings except sql.stats.automatic_collection.enabled = false). Run the script in an empty database. The script runs the query twice in one session: first with vectorize = off (row-by-row engine), then with vectorize = on (vectorized engine).
SET TIME ZONE 'UTC';
CREATE TABLE t (id INT PRIMARY KEY, x INT NOT NULL, y INT NOT NULL);
INSERT INTO t SELECT g, CASE WHEN g = 1025 THEN -1 WHEN g = 1026 THEN 9223372036854775807 ELSE 1 END, 1 FROM generate_series(1, 1026) AS g;
-- row-by-row engine
SET vectorize = off;
EXPLAIN (VERBOSE) SELECT id FROM t WHERE (x + y) <= 0 ORDER BY id LIMIT 1;
SELECT id FROM t WHERE (x + y) <= 0 ORDER BY id LIMIT 1;
-- vectorized engine
SET vectorize = on;
EXPLAIN (VERBOSE) SELECT id FROM t WHERE (x + y) <= 0 ORDER BY id LIMIT 1;
SELECT id FROM t WHERE (x + y) <= 0 ORDER BY id LIMIT 1;
Expected behavior
The query should return 1025, since LIMIT 1 is already satisfied by row 1025 and a row after it should not change the result. PostgreSQL 18.6 returns 1025 for this query. We did not find a statement on this in the CockroachDB documentation (limit-offset.md, scalar-expressions.md, vectorized-execution.md) or in the PostgreSQL manual (syntax-express-eval, queries-limit): none of them says whether an error in a WHERE expression on rows after the LIMIT is already satisfied has to be raised.
PostgreSQL 18.6 (Debian 18.6-1.pgdg12+2):
SELECT id FROM t WHERE (x + y) <= 0 ORDER BY id LIMIT 1;
id
----
(0 rows)
Additional data / screenshots
Row-by-row engine (vectorize = off): one row, 1025. Vectorized engine (vectorize = on): error, SQLSTATE 22003, integer out of range.
-- row-by-row engine
EXPLAIN (VERBOSE) SELECT id FROM t WHERE (x + y) <= 0 ORDER BY id LIMIT 1;
info
----------------------------------------------------------
distribution: local
vectorized: false
• project
│ columns: (id)
│
└── • limit
│ columns: (id, x, y)
│ count: 1
│
└── • filter
│ columns: (id, x, y)
│ ordering: +id
│ estimated row count: 333 (missing stats)
│ filter: (x + y) <= 0
│
└── • scan
columns: (id, x, y)
ordering: +id
estimated row count: 1,000 (missing stats)
table: t@t_pkey
spans: FULL SCAN (SOFT LIMIT)
(22 rows)
SELECT id FROM t WHERE (x + y) <= 0 ORDER BY id LIMIT 1;
id
------
1025
(1 row)
-- vectorized engine
EXPLAIN (VERBOSE) SELECT id FROM t WHERE (x + y) <= 0 ORDER BY id LIMIT 1;
info
----------------------------------------------------------
distribution: local
vectorized: true
• project
│ columns: (id)
│
└── • limit
│ columns: (id, x, y)
│ count: 1
│
└── • filter
│ columns: (id, x, y)
│ ordering: +id
│ estimated row count: 333 (missing stats)
│ filter: (x + y) <= 0
│
└── • scan
columns: (id, x, y)
ordering: +id
estimated row count: 1,000 (missing stats)
table: t@t_pkey
spans: FULL SCAN (SOFT LIMIT)
(22 rows)
SELECT id FROM t WHERE (x + y) <= 0 ORDER BY id LIMIT 1;
psql:<stdin>:12: ERROR: 22003: integer out of range
LOCATION: init, eval.go:32
Environment:
- CockroachDB version:
CockroachDB CCL v26.2.7-dev-8652ac22d9f90501e48ae4f24efd7faab33cf7c5 (x86_64-pc-linux-gnu, built 2026/10/23 21:35:41, go1.25.13)
- Server OS: Linux 6.8.0-138-generic x86_64, AMD EPYC 7J13 64-Core Processor
- Client app:
psql
Additional context
Row-by-row engine: filtererProcessor.Next (pkg/sql/rowexec/filterer.go) evaluates the filter one row at a time (f.filter.EvalFilter(f.Ctx(), row)); after a row passes it calls f.ProcessRowHelper(row), and once the LIMIT is satisfied it returns without fetching the next row.
Vectorized engine: the generated projPlusInt64Int64Op.Next (pkg/sql/colexec/colexecproj/proj_non_const_ops.eg.go, from the _SET_PROJECTION template in proj_non_const_ops_tmpl.go) computes the sum for every element of the batch, including rows after the point where the LIMIT is satisfied, and on overflow calls colexecerror.ExpectedError(tree.ErrIntOutOfRange): result := int64(arg1) + int64(arg2); if (result < int64(arg1)) != (int64(arg2) < 0) { ... }. The projection is evaluated for the whole batch before the selection and limit operators run, so an error on a row that would never have been returned fails the whole statement.
- CockroachDB: pkg/sql/rowexec/filterer.go#L99 —
filtererProcessor.Next - CockroachDB: pkg/sql/colexec/colexecproj/proj_non_const_ops.eg.go#L4646 —
projPlusInt64Int64Op.Next
Found with AI assistance.
Jira issue: CRDB-70798
- Lingua principale
- Go
- Stelle
- 32.6k
- Fork
- 4.2k
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di cockroachdb/cockroach
-
C-bug O-community X-blathers-untriaged
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
cockroachdb/cockroach#176245 · 1 commento ·
-
sql: pg_database.dattablespace is 0 but pg_tablespace.oid is 1663, so they no longer joinForse già presa @1AbdullahButt l’ha presa 14 giorni fa. ApertaC-bug O-community X-blathers-triaged X-move-internal
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
cockroachdb/cockroach#176228 · 1 commento ·
-
PG compat: overflow in integer power calculation triggers incompatible SQLSTATEForse di nuovo libera @darshpatel1052 l’ha presa 18 giorni fa e non c’è nessuna pull request aperta. ApertaC-bug O-community X-blathers-untriaged
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
cockroachdb/cockroach#176223 · 2 commenti ·
-
util/log: bufferedSinkCloser.Close leaks a goroutine after timeoutForse già presa @dajiaohuang l’ha presa 14 giorni fa. ApertaC-bug O-community X-blathers-untriaged
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 84/100
cockroachdb/cockroach#176210 · 1 commento ·
-
C-bug O-community T-sql-queries X-blathers-triaged X-move-internal
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
cockroachdb/cockroach#176150 · 1 commento ·
Tutte le issue di cockroachdb/cockroach
Issue simili
-
☢️ Bug v3
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
I maintainer di solito rispondono entro 1 giorno
-
area/import-export area/projects concern/ux
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
go-vikunja/vikunja#4455 · 1 commento ·
I maintainer di solito rispondono entro 2 giorni
-
Dependabot PRs no longer merge automaticallyForse già presa @dwisiswant0 l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
projectdiscovery/urlfinder#160 ·
I maintainer di solito rispondono entro 1 giorno
-
database
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
chnm/legal-modernism#450 ·
I maintainer di solito rispondono entro 1 giorno
-
tech-debt
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno