Skip to content

Commit 62f09b5

Browse files
iSteforuslandoga
authored andcommitted
DecodeBinary: Correctly truncate NaiveDateTime resulting from DateTime64
1 parent 67404d6 commit 62f09b5

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

lib/ch/row_binary.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1462,6 +1462,7 @@ defmodule Ch.RowBinary do
14621462
case timezone do
14631463
nil ->
14641464
NaiveDateTime.add(@epoch_naive_datetime, s, time_unit)
1465+
|> truncate(time_unit)
14651466

14661467
"UTC" ->
14671468
DateTime.from_unix!(s, time_unit)
@@ -1537,12 +1538,17 @@ defmodule Ch.RowBinary do
15371538
end
15381539
end
15391540

1540-
@compile inline: [time_unit: 1]
1541+
@compile inline: [time_unit: 1, time_prec: 1]
15411542
for precision <- 0..9 do
15421543
time_unit = round(:math.pow(10, precision))
1544+
15431545
defp time_unit(unquote(precision)), do: unquote(time_unit)
1546+
defp time_prec(unquote(time_unit)), do: unquote(precision)
15441547
end
15451548

1549+
defp truncate(%{microsecond: {micros, _prec}} = date, time_unit),
1550+
do: %{date | microsecond: {micros, time_prec(time_unit)}}
1551+
15461552
@compile inline: [time_after_midnight: 2]
15471553
defp time_after_midnight(ticks, time_unit) do
15481554
if ticks >= 0 and ticks < 86400 * time_unit do

test/ch/row_binary_test.exs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -595,11 +595,17 @@ defmodule Ch.RowBinaryTest do
595595
end
596596

597597
test "datetime64" do
598-
types = ["DateTime64(3)", "DateTime64(6, 'UTC')", "DateTime64(9, 'Asia/Tokyo')"]
598+
types = [
599+
"DateTime64(0)",
600+
"DateTime64(3)",
601+
"DateTime64(6, 'UTC')",
602+
"DateTime64(9, 'Asia/Tokyo')"
603+
]
599604

600605
data = [
601606
[
602-
encode({:datetime64, 1000}, ~N[2022-01-01 12:00:00.123]),
607+
encode({:datetime64, 1}, ~N[2022-01-01 12:00:00]),
608+
encode({:datetime64, 1_000}, ~N[2022-01-01 12:00:00.123]),
603609
encode({:datetime64, 1_000_000}, ~U[2022-01-01 12:00:00.123456Z]),
604610
encode(
605611
{:datetime64, 1_000_000_000},
@@ -608,7 +614,8 @@ defmodule Ch.RowBinaryTest do
608614
)
609615
],
610616
[
611-
encode({:datetime64, 1000}, ~N[2042-12-31 23:59:59.987]),
617+
encode({:datetime64, 1}, ~N[2042-12-31 23:59:59]),
618+
encode({:datetime64, 1_000}, ~N[2042-12-31 23:59:59.987]),
612619
encode({:datetime64, 1_000_000}, ~U[2042-12-31 23:59:59.987654Z]),
613620
encode(
614621
{:datetime64, 1_000_000_000},
@@ -620,12 +627,14 @@ defmodule Ch.RowBinaryTest do
620627

621628
assert byte_by_byte(data, types) == [
622629
[
623-
~N[2022-01-01 12:00:00.123000],
630+
~N[2022-01-01 12:00:00],
631+
~N[2022-01-01 12:00:00.123],
624632
~U[2022-01-01 12:00:00.123456Z],
625633
DateTime.new!(~D[2022-01-01], ~T[12:00:00.123456], "Asia/Tokyo")
626634
],
627635
[
628-
~N[2042-12-31 23:59:59.987000],
636+
~N[2042-12-31 23:59:59],
637+
~N[2042-12-31 23:59:59.987],
629638
~U[2042-12-31 23:59:59.987654Z],
630639
DateTime.new!(~D[2042-12-31], ~T[23:59:59.987654], "Asia/Tokyo")
631640
]

0 commit comments

Comments
 (0)