Skip to content

Respect use_client_time_zone for string datetime literals parsed on the server - #109051

Merged
yariks5s merged 3 commits into
ClickHouse:masterfrom
groeneai:fix-async-insert-use-client-time-zone-108038
Jul 6, 2026
Merged

yariks5s merged 3 commits into
ClickHouse:masterfrom
groeneai:fix-async-insert-use-client-time-zone-108038

Conversation

@groeneai

@groeneai groeneai commented Jul 1, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #108038

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed use_client_time_zone being ignored for DateTime/DateTime64 string literals interpreted on the server (asynchronous INSERT, SELECT literals). The client now propagates its local time zone as session_timezone when use_client_time_zone is enabled, so server-side parsing matches the synchronous INSERT path.

Description

Report: #108038

With --use_client_time_zone=1, a DateTime string literal in INSERT ... VALUES was parsed using the server time zone when async_insert was enabled (the default since 26.2), but correctly using the client time zone when async_insert=0:

# server tz Europe/Moscow, client tz Asia/Novosibirsk
env TZ=Asia/Novosibirsk clickhouse-client --use_client_time_zone=1 -q \
  "INSERT INTO dt VALUES ('2017-07-14 05:40:00')"                                  -- async: 1500000000 (server tz, WRONG)
env TZ=Asia/Novosibirsk clickhouse-client --use_client_time_zone=1 -q \
  "INSERT INTO dt SETTINGS async_insert=0 VALUES ('2017-07-14 05:40:00')"          -- sync:  1499985600 (client tz, correct)

Root cause is client-side. The synchronous path parses the VALUES block on the client, using the client's local time zone. The asynchronous path sends the raw query to the server, which parses the literal later using the session_timezone setting. With use_client_time_zone=1 the client left session_timezone empty, so the server fell back to its own default time zone. The server-side parse already honors session_timezone correctly; the client simply never told it the local time zone. The same gap affects any server-side interpretation of a string literal, for example SELECT toDateTime('2017-07-14 05:40:00').

Fix: in Client::connect(), when use_client_time_zone is enabled and the user has not set session_timezone explicitly, set session_timezone to the client's local time zone. An explicit --session_timezone still takes priority. The default (use_client_time_zone=0) path is unchanged.

Added a stateless regression test that fails before the change (async row stored in server tz) and passes after it (both async and sync rows stored in client tz).

Version info

  • Merged into: 26.7.1.548 (included in 26.7 and later)

…one is set

With --use_client_time_zone=1 the client parses DateTime string literals in its
local time zone, but literals interpreted server-side (asynchronous INSERT, SELECT
literals) use the session_timezone setting, which the client left empty. The server
then fell back to its own default time zone, so an async INSERT of a string literal
stored a different instant than the equivalent synchronous INSERT.

Set session_timezone to the client local time zone in Client::connect() when
use_client_time_zone is enabled and the user has not set session_timezone explicitly,
so server-side interpretation matches the client-side (synchronous) path. An explicit
--session_timezone still takes priority.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

groeneai commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. env TZ=Asia/Novosibirsk clickhouse-client --use_client_time_zone=1 -q "INSERT INTO dt VALUES ('2017-07-14 05:40:00')" (async default) stores 1500000000 (server tz Europe/Moscow); SETTINGS async_insert=0 stores 1499985600 (client tz). Fails on demand.
b Root cause explained? Yes. The sync path parses VALUES on the client (client tz). The async path sends raw text to the server, which parses it later using session_timezone. With use_client_time_zone=1 the client left session_timezone empty, so the server used its own default tz. The client never propagated its local tz.
c Fix matches root cause? Yes. In Client::connect(), when use_client_time_zone=1 and session_timezone is not explicitly set, set session_timezone to the client local tz. That is exactly the missing propagation; the server-side parse already honors session_timezone correctly (verified).
d Test intent preserved / new tests added? Yes. New stateless test 04401_async_insert_use_client_time_zone.sh asserts async and sync INSERT store the same instant in the client tz. Existing 00387_use_client_time_zone behavior preserved (it uses explicit --session_timezone '', which the fix respects).
e Both directions demonstrated? Yes. Pre-fix binary: test prints async 1500000000 / sync 1500036000 -> FAILS. With fix: 10/10 runs async 1500036000 / sync 1500036000 -> PASSES.
f Fix is general across code paths? Yes. Fix is at the single client connect point, not a symptom guard. clickhouse-local is unaffected (in-process, client tz == server tz). No sibling client path needs the same change.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. Verified DateTime, DateTime64(3), and Nullable(DateTime) async literals all interpret in the client tz. session_timezone uniformly governs the whole DateTime family.
h Backward compatible? Yes. Only affects the opt-in use_client_time_zone=1 case; the default (=0) path is unchanged. Explicit --session_timezone still wins. No setting default, on-disk, or wire format change.
i Invariants and contracts preserved? Yes. Documented DataTypeDateTime semantics (no explicit tz -> session_timezone) now hold uniformly for client and server parsing. Multi-query interactive sessions do not oscillate: the server echoes the client-set session_timezone back in TimezoneUpdate, verified stable across queries.

Session id: cron:clickhouse-worker-slot-0:20260701-113900

@groeneai

groeneai commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator Author

cc @azat @yariks5s — could you review this? The client left session_timezone empty under --use_client_time_zone=1, so server-side literal parsing (async INSERT, SELECT literals) used the server tz instead of the client tz; the fix propagates the client local tz as session_timezone in Client::connect() (explicit --session_timezone still wins).

@clickhouse-gh

clickhouse-gh Bot commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6efe92f]

Summary: ✅


AI Review

Summary

This PR now seeds session_timezone transiently per query from a session_timezone-agnostic capture of the client-local zone, so server-side DateTime/DateTime64 literal parsing tracks use_client_time_zone across the flag, SET, SETTINGS, and override-reset cases. The current head addresses the earlier review issues, and I did not find any remaining correctness, compatibility, or regression-evidence gaps.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 1, 2026
Comment thread programs/client/Client.cpp Resolved
@yariks5s yariks5s self-assigned this Jul 1, 2026
…g changes

The previous change seeded session_timezone once in Client::connect(), so it
only reflected the use_client_time_zone value at connect time. On an already
open connection, SET use_client_time_zone / per-query SETTINGS change the
setting without reconnecting, leaving two holes:

  - turning use_client_time_zone on mid-session did not seed session_timezone,
    so server-side literal parsing kept using the server time zone;
  - turning it off left the auto-seeded session_timezone in place, so
    server-side parsing never switched back.

Capture the client local time zone once on the first connect (before the
branch that may switch the process default to the server time zone), and seed
session_timezone from it in processParsedSingleQuery for every query whose
effective use_client_time_zone is set and that does not set session_timezone
explicitly. The seed is applied inside the transient per-query settings scope,
so it is recomputed for each query and reverted afterwards, tracking
use_client_time_zone changes in both directions without leaking state.

Extend the regression test to cover the command line flag, a mid-session SET,
a per-query SETTINGS, and turning use_client_time_zone back off.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

groeneai commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator Author

Follow-up commit 4061501 addresses the review finding (one-shot connect() seed missing mid-session use_client_time_zone transitions). Updated pre-PR validation gate:

Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. Server tz Europe/Moscow, client tz America/Hermosillo, '2017-07-14 05:40:00' = 1500036000 (client) / 1500000000 (server). Three deterministic repros: (1) SET use_client_time_zone=1 mid-session then async/sync INSERT; (2) --use_client_time_zone=1 then SET use_client_time_zone=0; (3) SETTINGS use_client_time_zone=1 per query without the CLI flag.
b Root cause explained? Yes. session_timezone was seeded once in Client::connect(), so it only reflected use_client_time_zone at connect time. SET / per-query SETTINGS change the setting on an already-open connection without reconnecting, so the seed never fired on a mid-session 0->1 flip and was never cleared on a 1->0 flip. session_timezone (in client_context) is the single lever that drives both the client-side sync literal parse and the server-side async/SELECT literal parse via DateLUT::instance().
c Fix matches root cause? Yes. Capture the client local tz once on the first connect() (before the branch that may switch the process default to the server tz), then seed session_timezone from it in processParsedSingleQuery for every query whose effective use_client_time_zone is set and that does not set session_timezone explicitly. Recomputed per query, reverted with the transient per-query settings scope.
d Test intent preserved / new tests added? Yes. 04401_async_insert_use_client_time_zone extended to cover the CLI flag, a mid-session SET, per-query SETTINGS, and turning use_client_time_zone back off (asserting the stored instant matches a plain server-parsed insert, so it is server-tz-independent).
e Both directions demonstrated? Yes. On the previous (one-shot) binary the extended test fails: set_async/set_sync/settings_async = 1500000000 and reset_async matches server tz = 0. On the fixed binary all six assertions pass.
f Fix is general across code paths? Yes. The seed sits at the single client-side point where every query's effective settings are applied before being sent (processParsedSingleQuery), so it covers async INSERT, sync INSERT, and SELECT literals uniformly regardless of how use_client_time_zone was set (CLI, SET, per-query SETTINGS). No sibling client path parses literals separately.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. Verified via async with DateTime, DateTime64(3), and Nullable(DateTime) (all 1500036000 / 1500036000500). The fix operates on session_timezone, which the server applies to all DateTime literal interpretation, so it is type-agnostic.
h Backward compatible? Yes. The seed only fires when use_client_time_zone is enabled (default off) and session_timezone is not explicitly set. Default behavior (use_client_time_zone=0) is unchanged (both paths use server tz); explicit --session_timezone / per-query SETTINGS session_timezone (including empty '') still wins via the isChanged guard. No setting default, wire, or on-disk format change.
i Invariants and contracts preserved? Yes. The seed is written inside the existing transient settings scope (old_settings + SCOPE_EXIT setSettings), so it is reverted per query exactly like other per-query settings and never leaks persistent state. client_local_timezone is captured once before any setDefaultTimezone call, respecting the existing rule that the client DateLUT default is bound at connect time. clickhouse-local is unaffected (its connect() does not set client_local_timezone, so the seed is skipped; client and server share one tz there).

Session id: cron:clickhouse-worker-slot-1:20260701-161500

Comment thread programs/client/Client.cpp Resolved
The per-query session_timezone seed reads client_local_timezone, captured
once in Client::connect(). It was captured via DateLUT::instance(), which
folds in session_timezone: starting the client with an explicit
--session_timezone (on a host whose real zone differs) cached that override
value instead of the machine-local zone. A later SET session_timezone =
DEFAULT then reseeded future queries from the cached override, so server-side
literal parsing (async INSERT, SELECT) never returned to true
use_client_time_zone semantics.

Use DateLUT::serverTimezoneInstance().getTimeZone() instead, which reads the
process default directly and ignores session_timezone. On the client with
use_client_time_zone=1 setDefaultTimezone() is never called, so the process
default stays the machine-local zone captured at DateLUT construction.

Extends 04401_async_insert_use_client_time_zone to cover the
--session_timezone=UTC start followed by SET session_timezone = DEFAULT.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

groeneai commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator Author

Updated pre-PR validation gate after 6efe92f (session_timezone-agnostic capture).

Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. --use_client_time_zone=1 --session_timezone=UTC, then SET session_timezone = DEFAULT, then async INSERT of '2017-07-14 05:40:00' (client host tz America/Hermosillo) stored 1500010800 (UTC) every time before the fix.
b Root cause explained? Yes. The per-query session_timezone seed reads client_local_timezone, captured once in connect() via DateLUT::instance(). instance() (DateLUT.cpp:160-185) folds in session_timezone (query then global context), so an explicit --session_timezone=UTC cached "UTC" as the "local" zone. A later SET session_timezone = DEFAULT cleared the explicit override, and the seed reseeded from the cached "UTC" so server-side literal parsing never returned to true client-tz semantics.
c Fix matches root cause? Yes. Capture via DateLUT::serverTimezoneInstance().getTimeZone(), which reads the process default directly and ignores session_timezone -- exactly the contaminating layer. On the client with use_client_time_zone=1, setDefaultTimezone() is never called, so the process default is the machine-local zone.
d Test intent preserved / new tests added? Yes. 04401_async_insert_use_client_time_zone extended with the --session_timezone=UTC start + SET session_timezone = DEFAULT reset case; existing rows unchanged.
e Both directions demonstrated? Yes. Pre-fix binary (08a150ba, instance()): the new row is 1500010800 -> FAILS. Fixed binary (7f3126fd, serverTimezoneInstance()): 1500036000 -> PASSES, 5/5 runs.
f Fix is general across code paths? Yes. The single capture point feeds both the async-INSERT and SELECT server-side literal paths; setDefaultTimezone has only two callers (Client connect for use_client_time_zone=0, and BaseDaemon), neither reached before this capture in the =1 path.
g Fix generalizes across inputs? Yes. Independent of literal datatype (verified earlier for DateTime, DateTime64, Nullable(DateTime)); the change is purely which zone string is cached. Explicit --session_timezone (without reset) still wins; use_client_time_zone=0 still adopts server tz; clickhouse-local unaffected (empty capture).
h Backward compatible? Yes. Only the opt-in use_client_time_zone=1 path is affected, and only to make it match its documented semantics. No setting default, format, or protocol change.
i Invariants and contracts preserved? Yes. serverTimezoneInstance() returns the process-default DateLUTImpl regardless of session_timezone; capture still runs before the connect() server-tz switch and only once (.empty() guard), so reconnect behavior is unchanged. Explicit override precedence preserved via the isChanged("session_timezone") guard in the per-query seed.

Session id: cron:clickhouse-worker-slot-0:20260701-182600

@clickhouse-gh

clickhouse-gh Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.40% 85.40% +0.00%
Functions 92.60% 92.60% +0.00%
Branches 77.60% 77.60% +0.00%

Changed lines: Changed C/C++ lines covered: 15/15 (100.00%) · Uncovered code

Full report · Diff report

@yariks5s
yariks5s added this pull request to the merge queue Jul 6, 2026
Merged via the queue into ClickHouse:master with commit e6381e3 Jul 6, 2026
177 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 6, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment.

TL;DR

Respect use_client_time_zone for string datetime literals parsed on the server

ClickGap verified: reproduced it with the script below on 26.5; tested 6 version(s) — 5 affected, 1 clean (see Affects); long-standing (not a recent regression); introducing PR not yet pinned.

Reproduction

# server tz = Europe/Moscow, client tz = America/Hermosillo (UTC-7, no DST)
# With --use_client_time_zone=1 the client must make the server parse the
# DateTime string literal in the CLIENT time zone.
env TZ=America/Hermosillo clickhouse-client --use_client_time_zone=1 -q \
  "SELECT toUnixTimestamp(toDateTime('2017-07-14 05:40:00'))"
# Observed: 1500000000 (literal parsed in SERVER tz Europe/Moscow) -- WRONG
# Expected: 1500036000 (literal parsed in CLIENT tz America/Hermosillo)
# Same wrong value via async INSERT:
#   INSERT INTO dt SETTINGS async_insert=1, wait_for_async_insert=1 VALUES ('2017-07-14 05:40:00')

Bisect

  • Reproduces on every release line tested down to v22.3.20.29 (the oldest with a downloadable binary for this repro). The introducing change predates v22.3.20.29, so it can't be pinned from release binaries — this is a long-standing issue, not a recent regression. A maintainer with full git history can git blame the relevant file/line to find the exact change.

Affects

  • ✓ Fix is on master — this PR's change fixes the bug on master. Branches below still have the bug and need the backport.
  • Reproduces on: 26.5, 26.4, 26.3, 26.2, 26.1
  • Does NOT reproduce on: master
  • Backport needed: 26.5, 26.4, 26.3, 26.2, 26.1

Suggested next step

The fix has landed on master; the affected release branches above still need the backport.

Component: comp-cli
Severity: P2

cc fix PR author: @groeneai
cc fix PR reviewer: @yariks5s


ClickGap · Finding: phase_d_pr109051

Triage metadata

zvonand added a commit to Altinity/ClickHouse that referenced this pull request Jul 19, 2026
…r-side DateTime parsing

Test 04401_async_insert_use_client_time_zone (from ClickHouse#109051) requires the
server to respect session_timezone when parsing DateTime text into columns
without an explicit timezone. Upstream has this since PR ClickHouse#100647 (26.4,
backported to 26.3/26.2) but stable-25.8 does not, so async inserts (TCP)
and all inserts over HTTP were parsed in the server timezone.

- DataTypeDateTime/DataTypeDateTime64: resolve the effective timezone
  (respects session_timezone via DateLUT::instance()) when creating the
  default serialization. Adapted from upstream doGetSerialization() to
  25.8's doGetDefaultSerialization() API (no serialization object pool).
- MutationsDateTimeLiteralVisitor: wrap DateTime string literals in
  mutation predicates/assignments with explicit toDateTime(..., tz) at
  ALTER time, since background mutation threads lack the session context.
  Adapted make_intrusive -> std::make_shared for 25.8 AST pointers.
- Tests from the upstream PR: 02737 update, 04056, 04057.

Co-Authored-By: Claude Fable 5 <[email protected]>
zvonand added a commit to Altinity/ClickHouse that referenced this pull request Jul 19, 2026
…r-side DateTime parsing

Test 04401_async_insert_use_client_time_zone (from ClickHouse#109051) requires the
server to respect session_timezone when parsing DateTime text into columns
without an explicit timezone. Upstream has this since PR ClickHouse#100647 (26.4,
backported to 26.3/26.2) but stable-25.8 does not, so async inserts (TCP)
and all inserts over HTTP were parsed in the server timezone.

- DataTypeDateTime/DataTypeDateTime64: resolve the effective timezone
  (respects session_timezone via DateLUT::instance()) when creating the
  default serialization. Adapted from upstream doGetSerialization() to
  25.8's doGetDefaultSerialization() API (no serialization object pool).
- MutationsDateTimeLiteralVisitor: wrap DateTime string literals in
  mutation predicates/assignments with explicit toDateTime(..., tz) at
  ALTER time, since background mutation threads lack the session context.
  Adapted make_intrusive -> std::make_shared for 25.8 AST pointers.
- Tests from the upstream PR: 02737 update, 04056, 04057.

Co-Authored-By: Claude Fable 5 <[email protected]>
mkmkme added a commit to Altinity/ClickHouse that referenced this pull request Aug 17, 2026
Antalya 26.6 Backport of ClickHouse#109051 - Respect use_client_time_zone for string datetime literals parsed on the server
zvonand added a commit to Altinity/ClickHouse that referenced this pull request Aug 19, 2026
26.3 Stable backport of ClickHouse#109051: Respect use_client_time_zone for string datetime literals parsed on the server
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…se-client-time-zone-108038

Respect use_client_time_zone for string datetime literals parsed on the server
mkmkme added a commit to Altinity/ClickHouse that referenced this pull request Oct 22, 2026
Antalya 26.3 Backport of ClickHouse#109051 - Respect use_client_time_zone for string datetime literals parsed on the server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

use_client_time_zone ignored for string datetime literals in INSERT when async_insert is enabled

5 participants