Skip to content

Flaky test: QueryTests.testClientUseOwnTimeZone / testServerTimezone / testServerTimeZoneFromHeader compare two now() reads that can differ by 1 s #3364

Open

Description

@polyglotAI-bot

Description

Three integration tests in client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java fail intermittently. Each one selects now() as t and toDateTime(now(), '<tz>') in the same query, and asserts that both columns hold the same instant. The server does not guarantee this. At a second boundary, column t can be one second earlier than the other columns. This is a test defect: the client decodes the values correctly.

Affected tests (they also run in the subclass QueryServerContentLZ4CompressionTests):

Test Query line Failing assertion
testServerTimeZoneFromHeader 1997 2010
testClientUseOwnTimeZone 2026 2041
testServerTimezone 2335 2342

Line numbers are for the main commit in Environment.

Steps to reproduce

  1. Run this query on a ClickHouse server. The scalar subquery runs during analysis, between the two clock reads:
    SELECT now() AS t, (SELECT sleep(2)) AS s, toDateTime(now(), 'UTC') AS utc_time
    Result: 2026-11-17 16:16:56 0 2026-11-17 16:16:58. The values differ by 2 s.
  2. Run the same query in these forms. The values are equal in each one:
    • SELECT now(), (SELECT sleep(2)) AS s, toDateTime(now(), 'UTC') AS utc_time (no alias on now())
    • WITH now() AS n SELECT n AS t, (SELECT sleep(2)) AS s, toDateTime(n, 'UTC') AS utc_time
    • the step 1 query with SETTINGS enable_analyzer = 0
  3. Run the exact query of testClientUseOwnTimeZone with no sleep, in 6 concurrent clickhouse-client --queries-file streams of 20000 queries each, with date_time_output_format = 'unix_timestamp'. 1 of 120000 results had a mismatch: 1794932293 1794932294 1794932294 (column t 1 s earlier). The Java client was not used, so the client cannot be the cause.

Error Log or Exception StackTrace

CI, check Java client ( client-v2 ) + CH 26.3, on two unrelated PRs:

On the same head as #3345, the same test passed on the client-v2 legs for CH 25.8, 26.8 and latest. A failure needs a clock read on each side of a second boundary, so it is rare on every server version. Both failures are on the 26.3 leg, but other versions can fail too (see Root cause).

The failing assertion is always t vs utc_time, and t is always the earlier value. The comparison utc_time vs est_time in testServerTimezone cannot fail, because those two columns share one now() node.

Expected Behaviour

The tests pass on every run. They compare values that come from one clock read.

Root cause

  • The analyzer builds each now() call separately, and each build reads the clock: src/Functions/now.cpp:133 (clock_gettime(CLOCK_REALTIME, ...) in NowOverloadResolver::buildImpl).
  • The analyzer shares one built function between calls with equal query-tree hashes (functions_cache, src/Analyzer/Resolve/resolveFunction.cpp:1236-1252 and :1498-1504, tag v26.3.9.8-lts). The hash includes the alias (compare_aliases = true by default, src/Analyzer/IQueryTreeNode.h:139). So now() as t and the un-aliased now() inside toDateTime(now(), 'UTC') read the clock two times.

master (now.cpp, resolveFunction.cpp) has the same logic. The fix is in the three test queries only.

Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Code Example

// testClientUseOwnTimeZone, QueryTests.java:2026
client.query("SELECT now() as t, toDateTime(now(), 'UTC') as utc_time, " +
        "toDateTime(now(), 'Europe/Lisbon')" +
        "SETTINGS session_timezone = '" + requestTimeZone + "'")

Configuration

Environment

  • Cloud
  • Client version: main at 93e50fa (0.12.0-rc1-SNAPSHOT)
  • Language version: CI matrix (the failures were on the client-v2 legs)
  • OS: GitHub Actions ubuntu-latest; local reproduction in a Linux container

ClickHouse Server

  • ClickHouse Server version: 26.3 (CI); 26.3.66.4 (local reproduction)
  • ClickHouse Server non-default settings, if any: none
  • CREATE TABLE statements for tables involved: none

Found by our PR monitor across unrelated CI runs of our own PRs. Shortened on 2026-11-20: the edit history has the first version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions