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

Open
#176,203 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go, sql
Domain
backend, databases

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

C-bug O-community T-sql-queries X-blathers-triaged X-move-internal

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.

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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cockroachdb/cockroach

All issues in cockroachdb/cockroach

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.