Skip to content

Cherry pick #100647 to 26.3: Fix session_timezone ignored during server-side DateTime parsing - #101351

Merged
CheSema merged 16 commits into
backport/26.3/100647from
cherrypick/26.3/100647
Mar 31, 2026
Merged

CheSema merged 16 commits into
backport/26.3/100647from
cherrypick/26.3/100647

Conversation

@robot-clickhouse

Copy link
Copy Markdown
Member

Original pull-request #100647

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

CheSema and others added 15 commits March 24, 2026 18:32
… the bug

The bug affects all server-side parsing paths, not just async insert.
HTTP sync inserts are also broken because `DataTypeDateTime` in table
metadata bakes in the timezone at CREATE TABLE time. Compare unix
timestamps to avoid the display-masking effect where wrong timezone on
INSERT is cancelled out by the same wrong timezone on SELECT.

Reference reflects current buggy behavior (3 of 4 cases wrong).

Co-Authored-By: Claude Opus 4.6 <[email protected]>
…-side parsing

When a `DateTime` column has no explicit timezone, `SerializationDateTime`
used the timezone baked into table metadata (captured at CREATE TABLE time).
This caused `session_timezone` to be ignored for all server-side parsing
paths: async inserts over TCP, and both sync and async inserts over HTTP.

Add `getEffectiveTimeZone` that dynamically resolves the timezone via
`DateLUT::instance()` (which respects `session_timezone` from the current
thread's query context) when the column has no explicit timezone, and
returns the baked timezone when it does (e.g. `DateTime('UTC')`).

#100614

Co-Authored-By: Claude Opus 4.6 <[email protected]>
When session_timezone is active it should override the column's timezone
for text parsing, even for columns with an explicit timezone like
`DateTime('America/New_York')`. Updated `getEffectiveTimeZone` to check
whether `DateLUT::instance()` differs from the server default.

Extended the test to cover both `DateTime` (no explicit timezone) and
`DateTime('America/New_York')` (explicit timezone) cases.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Apply `session_timezone` fix to `SerializationDateTime64` (same pattern
as `SerializationDateTime`). Split `getEffectiveTimeZone` into
`getInputTimeZone` and `getOutputTimeZone`:

- Both respect explicit column timezone (e.g. `DateTime('UTC')`) — it
  takes precedence over `session_timezone`.
- For columns without explicit timezone, both use `DateLUT::instance()`
  which dynamically resolves `session_timezone`.

Extended the test to cover `DateTime64`, `DateTime64` with explicit
timezone, and `DateTime` with explicit timezone. Updated test
`02737_session_timezone` with section labels for easier debugging.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
…etEffectiveTimeZone`

Both methods had identical logic, so a single method is sufficient.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Co-authored-by: clickhouse-gh[bot] <173705893+clickhouse-gh[bot]@users.noreply.github.com>
The pooled serialization cache keys did not include the
`has_explicit_time_zone` flag. When two `DataType`s resolve to the same
timezone name but differ in whether the timezone was explicitly specified
(e.g. `DateTime('UTC')` vs plain `DateTime` on a UTC-default server),
they shared the same pooled `SerializationDateTime` object. With the
`getEffectiveTimeZone` change, `has_explicit_time_zone` now controls
whether the baked timezone or the dynamic `DateLUT::instance()` is used,
so the flag must be part of the cache key to avoid cross-contamination.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
…House/ClickHouse into test-async-insert-session-timezone
Co-authored-by: clickhouse-gh[bot] <173705893+clickhouse-gh[bot]@users.noreply.github.com>
…plicit timezone

PR #100647 makes `SerializationDateTime` respect `session_timezone` via
`DateLUT::instance()`, which is thread-context-dependent. Mutation background
threads lack the original session context, so string literals like
'2000-01-01 02:00:00' resolve to different Unix timestamps than intended.

This adds an AST visitor that runs at ALTER time (alongside
`replaceNonDeterministicToScalars`) and wraps DateTime string literals with
explicit `toDateTime`/`toDateTime64` calls including the session timezone,
making interpretation deterministic regardless of execution context.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
…r-row

The previous approach called `DateLUT::instance` in `getEffectiveTimeZone`
on every row during serialization. Under parallel formatting with ASan,
this caused extreme mutex contention in `DateLUT::getImplementation` (1M+
lock/unlock cycles per column), leading to timeouts in tests like
`00159_parallel_formatting_http`.

Move timezone resolution to `DataTypeDateTime::doGetSerialization` and
`DataTypeDateTime64::doGetSerialization`, which are called once per column
per chunk. The serialization object pool deduplicates by timezone hash,
so only one instance exists per distinct timezone. The serialization
objects remain immutable and safe for concurrent use.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
…iter

`getDateTimeColumnType` used `typeid_cast` directly on the column type,
which missed wrapped variants like `Nullable(DateTime)` and
`LowCardinality(DateTime)`. Unwrap via `removeNullable`/`removeLowCardinality`
before the cast so mutation predicates on these columns are also rewritten
with the session timezone.

#100647 (comment)

Co-Authored-By: Claude Opus 4.6 <[email protected]>
…timezone

Fix session_timezone ignored during server-side DateTime parsing
@robot-clickhouse robot-clickhouse added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Mar 31, 2026
@CheSema
CheSema merged this pull request into backport/26.3/100647 Mar 31, 2026
@CheSema
CheSema deleted the cherrypick/26.3/100647 branch March 31, 2026 12:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants