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 with zero microseconds encoding bug #138

Merged
merged 4 commits into from
Dec 21, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- fix query encoding for datetimes with zeroed microseconds `~U[****-**-** **:**:**.000000]` https://github.com/plausible/ch/pull/138

## 0.2.1 (2023-08-22)

- fix array casts with `Ch` subtype https://github.com/plausible/ch/pull/118
Expand Down
18 changes: 12 additions & 6 deletions lib/ch/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,21 @@ defimpl DBConnection.Query, for: Ch.Query do
defp encode_param(%NaiveDateTime{} = naive), do: NaiveDateTime.to_iso8601(naive)

defp encode_param(%DateTime{time_zone: "Etc/UTC", microsecond: microsecond} = dt) do
seconds = DateTime.to_unix(dt, :second)

case microsecond do
{val, size} when size > 0 ->
size = round(:math.pow(10, size))
Float.to_string((seconds * size + val) / size)
{val, precision} when val > 0 and precision > 0 ->
size = round(:math.pow(10, precision))
unix = DateTime.to_unix(dt, size)
seconds = div(unix, size)
fractional = rem(unix, size)

IO.iodata_to_binary([
Integer.to_string(seconds),
?.,
String.pad_leading(Integer.to_string(fractional), precision)
])

_ ->
Integer.to_string(seconds)
dt |> DateTime.to_unix(:second) |> Integer.to_string()
end
end

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 @@ -142,6 +142,16 @@ defmodule Ch.ConnectionTest do
[[msk, "2021-01-01 15:00:00.123456"]]
end

test "utc datetime64 zero microseconds query param encoding", %{conn: conn} do
# this test case guards against a previous bug where DateTimes with a microsecond value of 0 and precision > 0 would
# get encoded as a val like "1.6095024e9" which ClickHouse would be unable to parse to a DateTime.
utc = ~U[2021-01-01 12:00:00.000000Z]
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[2021-01-01 12:00:00.000000], 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