Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: DateTime incorrectly encoded when microsecond precision > # digits #175

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- fix query encoding for datetimes where the microseconds value starts with zeroes `~U[****-**-** **:**:**.0*****]` https://github.com/plausible/ch/pull/175

## 0.2.5 (2024-03-05)

- add `:data` in `%Ch.Result{}` https://github.com/plausible/ch/pull/159
Expand All @@ -10,7 +14,7 @@
## 0.2.4 (2024-01-29)

- use `ch-#{version}` as user-agent https://github.com/plausible/ch/pull/154
- fix query string escaping for `\t`, `\\`, and `\n` https://github.com/plausible/ch/pull/155
- fix query string escaping for `\t`, `\\`, and `\n` https://github.com/plausible/ch/pull/155
Copy link
Contributor Author

Choose a reason for hiding this comment

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

my formatter did this, lemme know if you'd rather this line not be touched


## 0.2.3 (2024-01-29)

Expand Down
2 changes: 1 addition & 1 deletion lib/ch/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ defimpl DBConnection.Query, for: Ch.Query do
IO.iodata_to_binary([
Integer.to_string(seconds),
?.,
String.pad_leading(Integer.to_string(fractional), precision)
String.pad_leading(Integer.to_string(fractional), precision, "0")
])

_ ->
Expand Down
10 changes: 10 additions & 0 deletions test/ch/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ defmodule Ch.ConnectionTest do
[[~N[2021-01-01 12:00:00.000000], to_string(naive)]]
end

test "utc datetime64 microseconds with more precision than digits", %{conn: conn} do
# this test case guards against a previous bug where DateTimes with a microsecond value of with N digits
# and a precision > N would be encoded with a space like `234235234. 234123`
utc = ~U[2024-05-26 20:00:46.099856Z]
naive = utc |> DateTime.shift_zone!(Ch.Test.clickhouse_tz(conn)) |> DateTime.to_naive()

assert Ch.query!(conn, "select {$0:DateTime64(6)} as d, toString(d)", [utc]).rows ==
[[~N[2024-05-26 20:00:46.099856Z], to_string(naive)]]
end

test "select with options", %{conn: conn} do
assert {:ok, %{num_rows: 1, rows: [["async_insert", "Bool", "1"]]}} =
Ch.query(conn, "show settings like 'async_insert'", [], settings: [async_insert: 1])
Expand Down