Repository navigation
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #824
{{ dbt.date(year, month, day) }}is one of dbt's cross-database macros (dbt 1.8+). dbt-clickhouse had noclickhouse__date, so dispatch fell back todefault__date, which renders the PostgreSQLto_date('2023-09-07', 'YYYY-MM-DD'). ClickHouse has noto_date, so every model calling the macro failed withUNKNOWN_FUNCTION.This adds
clickhouse__datenext to the other cross-database macros inutils.sql. It renderstoDate('2023-09-07'), which is the sameDateascast('2023-09-07' as date).Dateonly 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')returns1970-01-01). Since the macro gets a literal year, month and day, it can tell when that would happen, and renderstoDate32(...)for those dates instead, sodbt.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 theDaterange (including both bounds) and outside it.tests/integration/adapter/utils/test_date.py: runs dbt-tests-adapter'sBaseDate(adate_spinebetween twodbt.date()values) and a model that checks dates before 1970 and after 2149 come back unclamped.On
mainall 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) andpytest tests/integration/adapter/utils(42 passed) against ClickHouse 26.10 in Docker, andmake lintis clean.Checklist
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__datemacro so{{ dbt.date(year, month, day) }}renders valid SQL instead of PostgreSQL-styleto_date(...), which ClickHouse does not support.For dates within the native
Daterange (1970-01-01 through 2149-06-06) it emitstoDate('YYYY-MM-DD'); outside that range it usestoDate32(...)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
BaseDatesuite 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.