Skip to content

Fix datetime parameter formating bug#184

Merged
alex-clickhouse merged 1 commit intomainfrom
datetime-parameter-format-fix
Jan 26, 2026
Merged

Fix datetime parameter formating bug#184
alex-clickhouse merged 1 commit intomainfrom
datetime-parameter-format-fix

Conversation

@alex-clickhouse
Copy link
Copy Markdown
Collaborator

@alex-clickhouse alex-clickhouse commented Jan 23, 2026

Previously, when we wanted to preserve an instant we would simply send the number of ticks in UTC. That would get interpreted as UTC no matter what the column type, thus making everything consistent. There's a problem with this, however:

This is fine:

curl "http://localhost:8123/?param_dt=1705320000" -d "INSERT INTO test.datetime_test VALUES ({dt:DateTime})"

But this throws:

curl "http://localhost:8123/?param_dt=0" -d "INSERT INTO test.datetime_test VALUES ({dt:DateTime})"

Code: 41. DB::Exception: Cannot parse DateTime: value 0 cannot be parsed as DateTime for query parameter 'dt': at row 0: While executing ValuesBlockInputFormat. (CANNOT_PARSE_DATETIME) (version 25.12.1.649 (official build))

This means that values near the epoch won't work, hence the bug reported here: #183

This PR changes the http parameter formatting approach to use strings instead, with the appropriate timezone adjustments based on the parameter type to maintain consistent behavior. Also adds a bunch more tests.


Note

Fixes DateTime HTTP parameter formatting

  • Switches HttpParameterFormatter to emit ISO strings for DateTime/DateTime64, converting UTC/Local values into the parameter’s target timezone (or UTC) while sending Unspecified as-is
  • Adds helpers FormatDateTimeInTargetTimezone and FormatDateTime64InTargetTimezone for timezone-aware ISO formatting

Extensive test coverage added

  • New tests for HTTP params and bulk copy covering Unspecified, Utc, Local, and DateTimeOffset across UTC, Europe/Amsterdam, and no-timezone columns
  • Validates instant preservation vs wall-clock semantics, with/without timezone hints in parameter types
  • Regression tests for Unix epoch (0) and min range, plus DateTime64 sub-second precision handling

Written by Cursor Bugbot for commit b9f1a48. This will update automatically on new commits. Configure here.

@codecov
Copy link
Copy Markdown

codecov bot commented Jan 23, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@alex-clickhouse alex-clickhouse marked this pull request as ready for review January 23, 2026 15:15
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug where DateTime parameters formatted as Unix timestamps near epoch (e.g., 0 for 1970-01-01 00:00:00) were rejected by ClickHouse's HTTP parameter parser. The fix changes HTTP parameter formatting to use ISO-formatted strings with appropriate timezone conversions instead of Unix timestamps.

Changes:

  • Modified HttpParameterFormatter.cs to format DateTime/DateTime64 parameters as ISO strings with timezone conversions instead of Unix timestamps
  • Added comprehensive test coverage for DateTime/DateTime64 parameter handling across different timezones, DateTimeKind values, and edge cases including Unix epoch

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
ClickHouse.Driver/Formats/HttpParameterFormatter.cs Replaced Unix timestamp formatting with ISO string formatting and timezone conversion logic for DateTime/DateTime64 parameters
ClickHouse.Driver.Tests/Types/TimezoneHandlingTests.cs Added extensive test coverage for DateTime/DateTime64 parameter handling, timezone conversions, and edge cases including Unix epoch

Comment thread ClickHouse.Driver/Formats/HttpParameterFormatter.cs
@alex-clickhouse alex-clickhouse merged commit f521125 into main Jan 26, 2026
18 checks passed
@alex-clickhouse alex-clickhouse deleted the datetime-parameter-format-fix branch January 26, 2026 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants