sql: vectorized engine evaluates `CAST` in the SELECT list on rows skipped by OFFSET and fails with 22007, row-by-row engine returns the row
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 58/100
Research direction
Start with pkg/sql/colexec/colbuilder/execplan.go, especially planPostProcessSpec and the calls to planProjectionOperators and colexec.NewOffsetOp; compare their ordering with ProcOutputHelper.ProcessRow in pkg/sql/execinfra/processorsbase.go. Then inspect the vectorized execution tests for OFFSET and projections. Done means the vectorized query skips the invalid timestamp row before evaluating its SELECT expression and returns the same single row as the row-by-row engine.
Written by the indexing model from the issue text.
Description
Describe the problem
For a table t with the values 'not-a-timestamp' and '2000-01-01' in column s, SELECT CAST(s AS TIMESTAMP) FROM t OFFSET 1 LIMIT 1 returns 2000-01-01 00:00:00 in the row-by-row engine, but fails with SQLSTATE 22007 (parsing as type timestamp: could not parse "not-a-timestamp") in the vectorized engine. The row skipped by OFFSET is not part of the result, yet the vectorized engine evaluates the CAST on it. The expected result is a single row, as in the row-by-row engine: in the plan printed by both engines the render (projection) sits above limit (offset: 1), so skipped rows should not be projected. 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, s STRING);
INSERT INTO t VALUES (1, 'not-a-timestamp'), (2, '2000-01-01');
-- row-by-row engine
SET vectorize = off;
EXPLAIN (VERBOSE) SELECT CAST(s AS TIMESTAMP) FROM t OFFSET 1 LIMIT 1;
SELECT CAST(s AS TIMESTAMP) FROM t OFFSET 1 LIMIT 1;
-- vectorized engine
SET vectorize = on;
EXPLAIN (VERBOSE) SELECT CAST(s AS TIMESTAMP) FROM t OFFSET 1 LIMIT 1;
SELECT CAST(s AS TIMESTAMP) FROM t OFFSET 1 LIMIT 1;
Expected behavior
The query should return one row, 2000-01-01 00:00:00. EXPLAIN shows render -> limit (offset: 1) -> scan in both engines, and the optimizer rule PushOffsetIntoProject (pkg/sql/opt/norm/rules/limit.opt) states: "PushOffsetIntoProject pushes the Offset operator into its Project input. It is desirable to push the Offset operator as low in the query as possible, in order to minimize the number of rows that other operators need to process." So a row skipped by OFFSET is not projected. Neither the CockroachDB documentation (limit-offset.md, scalar-expressions.md) nor the PostgreSQL manual says explicitly whether an error in the SELECT list on a skipped row has to be raised: the manual (queries-limit) only says "The rows skipped by an OFFSET clause still have to be computed inside the server; therefore a large OFFSET might be inefficient." PostgreSQL 18.6 itself raises 22007 for this query, so this expectation rests on CockroachDB's own plan and on the row-by-row engine.
PostgreSQL 18.6 (Debian 18.6-1.pgdg12+2):
SELECT CAST(s AS TIMESTAMP) FROM t OFFSET 1 LIMIT 1;
psql:<stdin>:5: ERROR: 42P01: relation "t" does not exist
LINE 1: SELECT CAST(s AS TIMESTAMP) FROM t OFFSET 1 LIMIT 1;
^
LOCATION: parserOpenTable, parse_relation.c:1498
Additional data / screenshots
Row-by-row engine (vectorize = off): one row, 2000-01-01 00:00:00. Vectorized engine (vectorize = on): error, SQLSTATE 22007, parsing as type timestamp: could not parse "not-a-timestamp".
-- row-by-row engine
EXPLAIN (VERBOSE) SELECT CAST(s AS TIMESTAMP) FROM t OFFSET 1 LIMIT 1;
info
--------------------------------------------------
distribution: local
vectorized: false
• render
│ columns: (s)
│ render s: s::TIMESTAMP
│
└── • limit
│ columns: (s)
│ offset: 1
│
└── • scan
columns: (s)
estimated row count: 2 (missing stats)
table: t@t_pkey
spans: LIMITED SCAN
limit: 2
(17 rows)
SELECT CAST(s AS TIMESTAMP) FROM t OFFSET 1 LIMIT 1;
s
---------------------
2000-01-01 00:00:00
(1 row)
-- vectorized engine
EXPLAIN (VERBOSE) SELECT CAST(s AS TIMESTAMP) FROM t OFFSET 1 LIMIT 1;
info
--------------------------------------------------
distribution: local
vectorized: true
• render
│ columns: (s)
│ render s: s::TIMESTAMP
│
└── • limit
│ columns: (s)
│ offset: 1
│
└── • scan
columns: (s)
estimated row count: 2 (missing stats)
table: t@t_pkey
spans: LIMITED SCAN
limit: 2
(17 rows)
SELECT CAST(s AS TIMESTAMP) FROM t OFFSET 1 LIMIT 1;
psql:<stdin>:12: ERROR: 22007: parsing as type timestamp: could not parse "not-a-timestamp"
LOCATION: inputErrorf, parsing.go:300
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: ProcOutputHelper.ProcessRow (pkg/sql/execinfra/processorsbase.go) increments rowIdx and, while rowIdx <= offset, suppresses the row (// Suppress row.). This happens before the // Rendering. step, so render expressions are evaluated only on rows that survive the offset.
Vectorized engine: planPostProcessSpec (pkg/sql/colexec/colbuilder/execplan.go) first plans a projection operator for every render expression with planProjectionOperators and only afterwards wraps the result with colexec.NewOffsetOp(r.Op, post.Offset). The executed operator tree is therefore limitOp -> offsetOp -> castStringTimestampOp -> ColBatchScan: the cast runs over the whole input batch, including the rows that offsetOp is going to drop, and raises the error before the offset is applied. This is the reverse of the order in the logical plan.
- CockroachDB: pkg/sql/execinfra/processorsbase.go#L288 —
ProcOutputHelper.ProcessRow - CockroachDB: pkg/sql/colexec/colbuilder/execplan.go#L2091 —
postProcessResult.planPostProcessSpec
Found with AI assistance.
Jira issue: CRDB-70797
- 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