Skip to content

fix(clickhouse): time filters use server timezone for toDateTime literals#41579

Merged
rusackas merged 4 commits into
apache:masterfrom
innovark37:innovark/fix/clickhouse-time-filter-utc
Jul 8, 2026
Merged

fix(clickhouse): time filters use server timezone for toDateTime literals#41579
rusackas merged 4 commits into
apache:masterfrom
innovark37:innovark/fix/clickhouse-time-filter-utc

Conversation

@innovark37

Copy link
Copy Markdown
Contributor

SUMMARY

Fixes ClickHouse temporal filter SQL generation for deployments where the ClickHouse server/session timezone is not UTC.

Superset computes time filter bounds in UTC, but ClickHouse toDateTime('YYYY-MM-DD HH:mm:ss') interprets the string in the server/session timezone when no timezone argument is provided. This can shift the effective filter bounds and cause expected rows to be excluded.

This change renders ClickHouse DateTime literals with an explicit UTC timezone:

toDateTime('2026-06-30 09:20:00', 'UTC')

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

Setup

  • ClickHouse configured with a non-UTC timezone (Europe/Moscow)
{652E6CF9-B379-425E-8590-734A41D4C9F1}
  • Test data
{B612A6CE-CC74-45E4-81B0-1A05B402DDB6}

Before:

  • The chart returns an empty result even when the selected UTC time range contains matching rows.
{B2A9B64E-7D32-4A02-ADC5-3980B9464976}
  • The generated SQL uses toDateTime('...', ) without an explicit timezone:
toDateTime('2026-06-30 09:20:00')
{22E34CC4-39D6-4611-8928-31B66DB7F07A}

After:

  • The same chart returns the expected row.
{C1BCA014-512C-42C8-9424-AD584E6C1CB8}
  • The generated SQL includes the UTC timezone explicitly:
toDateTime('2026-06-30 09:20:00', 'UTC')
{EE1C53FD-01A7-4876-BD9F-BFC5B22B6FDB}

TESTING INSTRUCTIONS

  1. Configure ClickHouse with a non-UTC timezone, for example Europe/Moscow.
  2. Verify timezone behavior in SQL Lab:
SELECT
  timezone() AS clickhouse_tz,
  toUnixTimestamp(toDateTime('2026-06-30 09:00:00')) AS without_tz,
  toUnixTimestamp(toDateTime('2026-06-30 09:00:00', 'UTC')) AS with_utc,
  without_tz - with_utc AS diff_seconds;
  1. Create test data:
CREATE DATABASE IF NOT EXISTS tz_bug_test;

DROP TABLE IF EXISTS tz_bug_test.events;

CREATE TABLE tz_bug_test.events
(
  id UInt8,
  name String,
  createdAt DateTime('UTC')
)
ENGINE = MergeTree
ORDER BY createdAt;

INSERT INTO tz_bug_test.events VALUES
  (1, 'event 12:00 Moscow', toDateTime('2026-06-30 09:00:00', 'UTC')),
  (2, 'event 12:30 Moscow', toDateTime('2026-06-30 09:30:00', 'UTC')),
  (3, 'event 13:00 Moscow', toDateTime('2026-06-30 10:00:00', 'UTC'));
  1. Create a dataset for tz_bug_test.events.
  2. Ensure createdAt is marked as temporal.
  3. Create a Table chart with id, name, and createdAt.
  4. Use createdAt as the time column.
  5. Apply this custom time range:
2026-06-30 09:20:00 : 2026-06-30 09:40:00
  1. Confirm that event 12:30 Moscow is returned.
  2. Open the generated SQL and confirm the filter uses:
toDateTime('2026-06-30 09:20:00', 'UTC')

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@dosubot dosubot Bot added data:connect:clickhouse Related to Clickhouse explore:filter Related to filters in Explore global:timezone Related to timezones labels Jun 30, 2026
@bito-code-review

bito-code-review Bot commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Code Review Agent Run #7aff25

Actionable Suggestions - 0
Filtered by Review Rules

Bito filtered these suggestions based on rules created automatically for your feedback. Manage rules.

  • tests/unit_tests/db_engine_specs/test_clickhouse.py - 1
Review Details
  • Files reviewed - 2 · Commit Range: b2bfdbf..b2bfdbf
    • superset/db_engine_specs/clickhouse.py
    • tests/unit_tests/db_engine_specs/test_clickhouse.py
  • Files skipped - 0
  • Tools
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

Comment thread superset/db_engine_specs/clickhouse.py Outdated
Comment thread tests/unit_tests/db_engine_specs/test_clickhouse.py Outdated
@codecov

codecov Bot commented Jun 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 20.00000% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 64.41%. Comparing base (7cc7e9f) to head (36275c0).

Files with missing lines Patch % Lines
superset/db_engine_specs/clickhouse.py 20.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #41579      +/-   ##
==========================================
- Coverage   64.42%   64.41%   -0.01%     
==========================================
  Files        2668     2668              
  Lines      147183   147186       +3     
  Branches    33947    33948       +1     
==========================================
- Hits        94822    94817       -5     
- Misses      50646    50652       +6     
- Partials     1715     1717       +2     
Flag Coverage Δ
hive 39.10% <20.00%> (-0.01%) ⬇️
mysql 57.65% <20.00%> (-0.01%) ⬇️
postgres 57.71% <20.00%> (-0.01%) ⬇️
presto 40.65% <20.00%> (-0.01%) ⬇️
python 59.12% <20.00%> (-0.01%) ⬇️
sqlite 57.35% <20.00%> (-0.01%) ⬇️
unit 100.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread superset/db_engine_specs/clickhouse.py Outdated
@bito-code-review

bito-code-review Bot commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Code Review Agent Run #683152

Actionable Suggestions - 0
Review Details
  • Files reviewed - 2 · Commit Range: b2bfdbf..21a6992
    • superset/db_engine_specs/clickhouse.py
    • tests/unit_tests/db_engine_specs/test_clickhouse.py
  • Files skipped - 0
  • Tools
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

@rusackas rusackas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @innovark37, pinning the toDateTime literal to 'UTC' is the right call given our filter datetimes are UTC-naive, and normalizing tz-aware inputs first is a nice touch. Your rebuttal on the Top-N/prequery thread reads right to me too... that path already leans on the same UTC-naive convention. LGTM!

@rusackas rusackas merged commit f6d03e4 into apache:master Jul 8, 2026
59 checks passed
@innovark37 innovark37 deleted the innovark/fix/clickhouse-time-filter-utc branch July 13, 2026 05:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data:connect:clickhouse Related to Clickhouse explore:filter Related to filters in Explore global:timezone Related to timezones size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants