Repository navigation
Conversation
ClickHouse's dateDiff rejects String arguments, so dbt.datediff with a string literal such as '2016-03-09' (dbt's documented usage) failed, and so did date_spine and get_intervals_between with string bounds. Read a string literal argument as a DateTime64(6); columns and other expressions are passed through unchanged. Run the BaseDateSpine and BaseGetIntervalsBetween adapter suites.
…iterals # Conflicts: # CHANGELOG.md
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 #826
dbt documents string literals as
datediffarguments, for example{{ dbt.datediff("column", "'2016-03-09'", "month") }}. dbt-clickhouse had noclickhouse__datediff, so dispatch fell back todefault__datediff, which rendersdatediff(month, column, '2016-03-09'). ClickHouse'sdateDiffonly takesDate,Date32,DateTimeandDateTime64arguments, so the query failed withILLEGAL_TYPE_OF_ARGUMENT ... Expected: Date[32] or DateTime[64], got: String.dbt.get_intervals_betweenanddbt.date_spinecalldatediff, so they failed the same way with string bounds (date_spine("day", "'2023-09-01'", "'2023-09-10'")).dbt.dateaddalready handles the same literals, because ClickHouse reads a String plus an interval as aDateTime64.This adds
clickhouse__datediffinmacros/utils/datediff.sql. An argument that is a single-quoted string literal is wrapped intoDateTime64(<literal>, 6), so both'2016-03-09'and'2016-03-09 10:11:12.123456'work, including formillisecondandmicrosecond. ClickHouse parses the literal the same way it does fordateadd. Columns and any other expression are rendered unchanged, so existing models produce the same SQL apart from the function name now being spelleddateDiff.Tests
tests/unit/test_datediff_macro.py: renders the macro through the macro harness for columns, string literals on either side, both sides, and a typed literal ('2023-09-01'::Date), which is passed through.tests/integration/adapter/utils/test_datediff.py: newTestDateDiffStringLiterals, with literals on both sides, a timestamp literal with a fractional second, and a column on either side of a literal.tests/integration/adapter/utils/test_date_spine.py: runs dbt-tests-adapter'sBaseDateSpineandBaseGetIntervalsBetween, which the adapter did not run yet. Both keep the upstream string bounds. Thedate_spinemodel casts both sides of its join toDate, as the upstream fixture already does for BigQuery and Redshift, because ClickHouse does not compare the spine'sDateTime64with the fixture'sStringcolumn. Theget_intervals_betweenmodel writes the upstream'09/01/2023'and'09/12/2023'in ISO format, because ClickHouse reads a slash date as DD/MM/YYYY.On
mainthe three integration tests fail with thedateDifferror above, and the unit tests fail because the macro is not defined. With the change,pytest tests/unit(158 passed) andpytest tests/integration/adapter/utils(43 passed) pass against ClickHouse 26.10 in Docker, over both HTTP and the native driver for the new integration tests, andmake lintis clean.Checklist
Note
Low Risk
Scoped macro-only change that aligns behavior with documented dbt APIs; non-literal arguments are unchanged aside from using native
dateDiff.Overview
Adds a
clickhouse__datediffadapter macro sodbt.datediffrenders ClickHousedateDiffinstead of falling back to the default (which passed string bounds and failed type checks).Single-quoted string literals on either side are wrapped with
toDateTime64(..., 6)so documented patterns like'2016-03-09'and timestamps with fractional seconds work; columns and other expressions are left unchanged.This unblocks
date_spineandget_intervals_betweenwhen bounds are string literals. Coverage includes unit tests for macro rendering and integration tests for datediff literals, date spine, and get_intervals_between (with ClickHouse-specific date formatting/casts where needed).Reviewed by Cursor Bugbot for commit d04f837. Bugbot is set up for automated code reviews on this repo. Configure here.