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
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 48/100
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
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.
- CockroachDB: pkg/sql/sem/tree/datum.go#L2615 —
compareTimestamps - CockroachDB: pkg/sql/colexec/colexecproj/proj_non_const_ops.eg.go#L47338 —
projGTTimestampTimestampOp.Next
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
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di cockroachdb/cockroach
-
C-bug O-community X-blathers-untriaged
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
cockroachdb/cockroach#176245 · 1 commento ·
-
sql: pg_database.dattablespace is 0 but pg_tablespace.oid is 1663, so they no longer joinForse già presa @1AbdullahButt l’ha presa 14 giorni fa. ApertaC-bug O-community X-blathers-triaged X-move-internal
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
cockroachdb/cockroach#176228 · 1 commento ·
-
PG compat: overflow in integer power calculation triggers incompatible SQLSTATEForse di nuovo libera @darshpatel1052 l’ha presa 18 giorni fa e non c’è nessuna pull request aperta. ApertaC-bug O-community X-blathers-untriaged
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
cockroachdb/cockroach#176223 · 2 commenti ·
-
util/log: bufferedSinkCloser.Close leaks a goroutine after timeoutForse già presa @dajiaohuang l’ha presa 14 giorni fa. ApertaC-bug O-community X-blathers-untriaged
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 84/100
cockroachdb/cockroach#176210 · 1 commento ·
-
C-bug O-community T-sql-queries X-blathers-triaged X-move-internal
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
cockroachdb/cockroach#176150 · 1 commento ·
Tutte le issue di cockroachdb/cockroach
Issue simili
-
☢️ Bug v3
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
I maintainer di solito rispondono entro 1 giorno
-
area/import-export area/projects concern/ux
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
go-vikunja/vikunja#4455 · 1 commento ·
I maintainer di solito rispondono entro 2 giorni
-
Dependabot PRs no longer merge automaticallyForse già presa @dwisiswant0 l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
projectdiscovery/urlfinder#160 ·
I maintainer di solito rispondono entro 1 giorno
-
database
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
chnm/legal-modernism#450 ·
I maintainer di solito rispondono entro 1 giorno
-
tech-debt
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno