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
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 48/100
Research direction
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.
Written by the indexing model from the issue text.
Description
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
- 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