sql: row-by-row engine shifts TIMESTAMP operands by the session time zone before comparing, so `=` disagrees with the vectorized engine on a DST transition day

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

Nessuno ha ancora preso questa issue.

Valutazione

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

Direzione di ricerca

Start with pkg/sql/sem/tree/datum.go at compareTimestamps and trace how TimeFromDatumForComparison handles DTimestamp operands. Compare that path with the vectorized comparison in pkg/sql/colexec/execgen/cmd/execgen/overloads_cmp.go; also inspect pkg/sql/colexec/colexecsel/select_in_tmpl.go for the related IN behavior. Done when timestamp equality, ordering, and the reported IN cases agree across engines and ignore the session time zone.

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

With a session time zone that has a daylight-saving transition on the day in question (here America/New_York on 2024-03-10), comparing two values of type TIMESTAMP (without time zone) gives different results in the two engines. For a = '2024-03-10 06:30:00' and b = '2024-03-10 07:30:00', SELECT a = b FROM t returns true in the row-by-row engine and false in the vectorized engine. The row-by-row engine is affected for ordering comparisons as well (it also returns true for 06:50:00 > 07:10:00 on that day), and the same query returns false once the session time zone is set to UTC. The vectorized result is the correct one: TIMESTAMP does not use the session time zone, so two different wall-clock values must not compare equal and their order must not depend on the session time zone. The row-by-row 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 (a TIMESTAMP, b TIMESTAMP);
INSERT INTO t VALUES ('2024-03-10 06:30:00', '2024-03-10 07:30:00');

SET TIME ZONE 'America/New_York';
-- row-by-row engine
SET vectorize = off;
EXPLAIN (VERBOSE) SELECT a = b FROM t;
SELECT a = b FROM t;
-- vectorized engine
SET vectorize = on;
EXPLAIN (VERBOSE) SELECT a = b FROM t;
SELECT a = b FROM t;

Expected behavior

SELECT a = b FROM t should return false: the two TIMESTAMP values are different wall-clock values. The CockroachDB documentation (timestamp.md, v26.2) says that TIMESTAMP does not depend on the session time zone: "TIMESTAMP presents all TIMESTAMP values in UTC." and "The difference between these two variants is that TIMESTAMPTZ uses the client's session time zone, while the other simply does not." The PostgreSQL manual (datatype.sgml) says the same for timestamp without time zone: "In a value that has been determined to be timestamp without time zone, PostgreSQL will silently ignore any time zone indication. That is, the resulting value is derived from the date/time fields in the input string, and is not adjusted for time zone." PostgreSQL 18.6 also returns false for this query.

PostgreSQL 18.6 (Debian 18.6-1.pgdg12+2):

SELECT a = b FROM t;
 ?column? 
----------
 f
(1 row)

Additional data / screenshots

Row-by-row engine (vectorize = off): true. Vectorized engine (vectorize = on): false.

-- row-by-row engine
EXPLAIN (VERBOSE) SELECT a = b FROM t;
                       info                       
--------------------------------------------------
 distribution: local
 vectorized: false
 
 • render
 │ columns: ("?column?")
 │ render ?column?: a = b
 │
 └── • scan
       columns: (a, b)
       estimated row count: 1,000 (missing stats)
       table: t@t_pkey
       spans: FULL SCAN
(12 rows)

SELECT a = b FROM t;
 ?column? 
----------
 t
(1 row)

-- vectorized engine
EXPLAIN (VERBOSE) SELECT a = b FROM t;
                       info                       
--------------------------------------------------
 distribution: local
 vectorized: true
 
 • render
 │ columns: ("?column?")
 │ render ?column?: a = b
 │
 └── • scan
       columns: (a, b)
       estimated row count: 1,000 (missing stats)
       table: t@t_pkey
       spans: FULL SCAN
(12 rows)

SELECT a = b FROM t;
 ?column? 
----------
 f
(1 row)

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

The row-by-row comparison goes through compareTimestamps (pkg/sql/sem/tree/datum.go), which calls TimeFromDatumForComparison on each operand even when both operands are TIMESTAMP. For a DTimestamp, that function computes _, zoneOffset := t.Time.In(cmpCtx.GetLocation()).Zone() and ts := t.Time.In(cmpCtx.GetLocation()).Add(-time.Duration(zoneOffset) * time.Second), i.e. it shifts each wall-clock value by the session time zone's offset, where the offset is taken at the instant obtained by reading the wall clock as UTC. Applied to two TIMESTAMP operands this shift is wrong, and when the operands lie on different sides of a UTC offset change the two shifts differ: in America/New_York on 2024-03-10, 06:30 is shifted by the EST offset and 07:30 by the EDT offset, so the two different values end up at the same instant. The vectorized comparison (generated from timestampCustomizer.getCmpOpCompareFunc in pkg/sql/colexec/execgen/cmd/execgen/overloads_cmp.go, which inlines tree.compareTimestamps) compares the raw time.Time values and is correct.

A related effect is in the vectorized IN / NOT IN on TIMESTAMP: the list is sorted with the tree comparison (the shifted order) but binary-searched with the raw time.Time (pkg/sql/colexec/colexecsel/select_in_tmpl.go, lines 166-200), so it also differs from the row-by-row engine in the two IN cases observed (PostgreSQL agrees with the row-by-row engine there). Once the row-by-row comparison uses the raw value, the sort order and the search agree and those differences should disappear with it.

Found with AI assistance.

Jira issue: CRDB-70787

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.