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
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 48/100
Research direction
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.
Written by the indexing model from the issue text.
Description
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
- 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