feat: add clickhouse__convert_timezone for dbt_date compatibility#646
feat: add clickhouse__convert_timezone for dbt_date compatibility#646dataders wants to merge 1 commit into
Conversation
|
|
|
Thanks for the contribution 🙇 I see the
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 |
|
Referencing here #375 as we may be able to officially solve it now. |
There was a problem hiding this comment.
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_timemacro terminator.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| toTimeZone( | ||
| toDateTime({{ column }}, '{{ source_tz }}'), | ||
| '{{ target_tz }}' |
| {%- macro clickhouse__convert_timezone(column, target_tz, source_tz="UTC") -%} | ||
| toTimeZone( | ||
| toDateTime({{ column }}, '{{ source_tz }}'), | ||
| '{{ target_tz }}' | ||
| ) | ||
| {%- endmacro -%} |
Summary
The
dbt_datepackage provides aconvert_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_timezonetoutils/timestamps.sqlalongside the existing timestamp utilities.Implementation
Uses ClickHouse's native
toDateTime(col, tz)to interpret the source timezone, thentoTimeZone(dt, tz)to convert — the idiomatic ClickHouse approach.This implementation was already independently discovered in the
ClickHouse/jaffle-shop-clickhouseproject, where it lives as a project-level workaround. Moving it into the adapter itself means all dbt-clickhouse users getdbt_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_spinemodel (which usesdbt_date.convert_timezone) compiles and runs correctly with this macro in place.