Skip to content

fix: accept string literal arguments in datediff - #827

Open
DG47 wants to merge 3 commits into
ClickHouse:mainfrom
DG47:fix/datediff-string-literals
Open

DG47 wants to merge 3 commits into
ClickHouse:mainfrom
DG47:fix/datediff-string-literals

Conversation

@DG47

@DG47 DG47 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #826

dbt documents string literals as datediff arguments, for example {{ dbt.datediff("column", "'2016-03-09'", "month") }}. dbt-clickhouse had no clickhouse__datediff, so dispatch fell back to default__datediff, which renders datediff(month, column, '2016-03-09'). ClickHouse's dateDiff only takes Date, Date32, DateTime and DateTime64 arguments, so the query failed with ILLEGAL_TYPE_OF_ARGUMENT ... Expected: Date[32] or DateTime[64], got: String.

dbt.get_intervals_between and dbt.date_spine call datediff, so they failed the same way with string bounds (date_spine("day", "'2023-09-01'", "'2023-09-10'")). dbt.dateadd already handles the same literals, because ClickHouse reads a String plus an interval as a DateTime64.

This adds clickhouse__datediff in macros/utils/datediff.sql. An argument that is a single-quoted string literal is wrapped in toDateTime64(<literal>, 6), so both '2016-03-09' and '2016-03-09 10:11:12.123456' work, including for millisecond and microsecond. ClickHouse parses the literal the same way it does for dateadd. Columns and any other expression are rendered unchanged, so existing models produce the same SQL apart from the function name now being spelled dateDiff.

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: new TestDateDiffStringLiterals, 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's BaseDateSpine and BaseGetIntervalsBetween, which the adapter did not run yet. Both keep the upstream string bounds. The date_spine model casts both sides of its join to Date, as the upstream fixture already does for BigQuery and Redshift, because ClickHouse does not compare the spine's DateTime64 with the fixture's String column. The get_intervals_between model writes the upstream '09/01/2023' and '09/12/2023' in ISO format, because ClickHouse reads a slash date as DD/MM/YYYY.

On main the three integration tests fail with the dateDiff error above, and the unit tests fail because the macro is not defined. With the change, pytest tests/unit (158 passed) and pytest 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, 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
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__datediff adapter macro so dbt.datediff renders ClickHouse dateDiff instead 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_spine and get_intervals_between when 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.

DG47 added 2 commits November 16, 2026 07:44
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.
@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.datediff fails with a string literal argument, so date_spine and get_intervals_between fail with string bounds

3 participants