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

Aperta
#176,202 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
58/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
go, sql
Ambito
backend, databases

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
Go
Stelle
32.6k
Fork
4.2k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di cockroachdb/cockroach

Tutte le issue di cockroachdb/cockroach

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.