diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b5d62b..88260e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 ## 0.2.3 (2024-01-29) diff --git a/lib/ch/query.ex b/lib/ch/query.ex index 66f7c83..4aaf5bb 100644 --- a/lib/ch/query.ex +++ b/lib/ch/query.ex @@ -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") ]) _ -> diff --git a/test/ch/connection_test.exs b/test/ch/connection_test.exs index da5f39a..1578220 100644 --- a/test/ch/connection_test.exs +++ b/test/ch/connection_test.exs @@ -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])