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

Open
#176,202 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
58/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go, sql
Domain
backend, databases

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

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

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.

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

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.