Skip to content

feat: add clickhouse__convert_timezone for dbt_date compatibility#646

Open
dataders wants to merge 1 commit into
ClickHouse:mainfrom
dataders:feat/clickhouse-convert-timezone
Open

feat: add clickhouse__convert_timezone for dbt_date compatibility#646
dataders wants to merge 1 commit into
ClickHouse:mainfrom
dataders:feat/clickhouse-convert-timezone

Conversation

@dataders
Copy link
Copy Markdown

Summary

The dbt_date package provides a convert_timezone() macro that dispatches to {adapter}__convert_timezone. ClickHouse is not currently supported, causing {{ dbt_date.convert_timezone(...) }} calls to fail with an "Unknown macro" or unsupported adapter error.

This PR adds clickhouse__convert_timezone to utils/timestamps.sql alongside the existing timestamp utilities.

Implementation

{%- macro clickhouse__convert_timezone(column, target_tz, source_tz="UTC") -%}
    toTimeZone(
        toDateTime({{ column }}, '{{ source_tz }}'),
        '{{ target_tz }}'
    )
{%- endmacro -%}

Uses ClickHouse's native toDateTime(col, tz) to interpret the source timezone, then toTimeZone(dt, tz) to convert — the idiomatic ClickHouse approach.

This implementation was already independently discovered in the ClickHouse/jaffle-shop-clickhouse project, where it lives as a project-level workaround. Moving it into the adapter itself means all dbt-clickhouse users get dbt_date.convert_timezone() support without needing a manual override.

Testing

Confirmed working against ClickHouse 24.3 via the dbt Fusion ClickHouse adapter e2e test suite. The metricflow_time_spine model (which uses dbt_date.convert_timezone) compiles and runs correctly with this macro in place.

@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@koletzilla
Copy link
Copy Markdown
Contributor

koletzilla commented May 15, 2026

Thanks for the contribution 🙇

I see the dbt_date package currently points to the godatadriven repo https://hub.getdbt.com/godatadriven/dbt_date/latest/. Inside it we can find many implementations from this macro for different adapters https://github.com/godatadriven/dbt-date/blob/2e4bddaafa60122ca5d608c13d2225a59ef439fd/macros/calendar_date/convert_timezone.sql#L4. I think instead of adding it here, we should try to add this new macro in that repo and use that package. Would yo move this new macro to that file? Thanks!

Confirmed working against ClickHouse 24.3 via the dbt-labs/dbt-fusion#84 e2e test suite. The metricflow_time_spine model (which uses dbt_date.convert_timezone) compiles and runs correctly with this macro in place.

Not related to this PR but wanted to comment it: we should not test anymore against a version that old. We should test with the supported versions https://github.com/clickhouse/clickhouse/security or if we want to pick just one, use the latest LTS, which currently is 26.3

@koletzilla
Copy link
Copy Markdown
Contributor

Referencing here #375 as we may be able to officially solve it now.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a ClickHouse implementation of convert_timezone() so projects using the dbt_date package can successfully dispatch dbt_date.convert_timezone(...) to the adapter (clickhouse__convert_timezone) instead of failing with an unknown/unsupported macro error.

Changes:

  • Added clickhouse__convert_timezone(column, target_tz, source_tz="UTC") to the ClickHouse timestamps utility macros.
  • Normalized formatting around the existing clickhouse__snapshot_string_as_time macro terminator.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +11 to +13
toTimeZone(
toDateTime({{ column }}, '{{ source_tz }}'),
'{{ target_tz }}'
Comment on lines +10 to +15
{%- macro clickhouse__convert_timezone(column, target_tz, source_tz="UTC") -%}
toTimeZone(
toDateTime({{ column }}, '{{ source_tz }}'),
'{{ target_tz }}'
)
{%- endmacro -%}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants