Skip to content

fix: render dbt.date() with toDate instead of the missing to_date - #825

Open
DG47 wants to merge 3 commits into
ClickHouse:mainfrom
DG47:fix/date-macro
Open

DG47 wants to merge 3 commits into
ClickHouse:mainfrom
DG47:fix/date-macro

Conversation

@DG47

@DG47 DG47 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #824

{{ dbt.date(year, month, day) }} is one of dbt's cross-database macros (dbt 1.8+). dbt-clickhouse had no clickhouse__date, so dispatch fell back to default__date, which renders the PostgreSQL to_date('2023-09-07', 'YYYY-MM-DD'). ClickHouse has no to_date, so every model calling the macro failed with UNKNOWN_FUNCTION.

This adds clickhouse__date next to the other cross-database macros in utils.sql. It renders toDate('2023-09-07'), which is the same Date as cast('2023-09-07' as date).

Date only covers 1970-01-01 to 2149-06-06, and ClickHouse clamps a value outside that range to the nearest bound without an error (toDate('1960-01-01') returns 1970-01-01). Since the macro gets a literal year, month and day, it can tell when that would happen, and renders toDate32(...) for those dates instead, so dbt.date(1960, 1, 1) is 1960-01-01 rather than a silently wrong 1970-01-01. The docs section on cross-database macros notes this.

Tests

  • tests/unit/test_date_macro.py: renders the macro through the macro harness, inside the Date range (including both bounds) and outside it.
  • tests/integration/adapter/utils/test_date.py: runs dbt-tests-adapter's BaseDate (a date_spine between two dbt.date() values) and a model that checks dates before 1970 and after 2149 come back unclamped.

On main all 9 new tests fail (7 unit tests: macro not defined; 2 integration tests: Function with name 'to_date' does not exist). With the change they pass. pytest tests/unit (159 passed) and pytest tests/integration/adapter/utils (42 passed) against ClickHouse 26.10 in Docker, and make lint is clean.

Checklist

  • Unit and integration tests covering the common scenarios were added
  • A changelog entry was added following Updating the Changelog (user-facing, concise, PR link)

Note

Low Risk
Narrow change to a new cross-database macro with tests; no auth, persistence, or incremental logic affected.

Overview
Adds a ClickHouse-specific clickhouse__date macro so {{ dbt.date(year, month, day) }} renders valid SQL instead of PostgreSQL-style to_date(...), which ClickHouse does not support.

For dates within the native Date range (1970-01-01 through 2149-06-06) it emits toDate('YYYY-MM-DD'); outside that range it uses toDate32(...) so values are not silently clamped. Cross-database macro docs are updated to describe this behavior.

Unit tests assert macro rendering at range boundaries and beyond; integration tests reuse dbt's BaseDate suite and verify out-of-range dates are not clamped at query time.

Reviewed by Cursor Bugbot for commit a940321. Bugbot is set up for automated code reviews on this repo. Configure here.

@Flipmonster Flipmonster reopened this Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dbt.date(year, month, day) fails: renders to_date(...), which ClickHouse does not have

3 participants