-
Notifications
You must be signed in to change notification settings - Fork 11
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
Possible timestamp issue #45
Comments
It's probably your non-UTC ClickHouse server timezone issue. It happens due to timestamps in params being encoded as text and in rows -- as ints. I think I have "fixed" it in Ch a few days ago: https://github.com/plausible/ch#select-rows But even with that change you'd still encounter issues with naive timestamp unless you switch to a UTC server. |
But in my test I am not using a native timestamp I am using a timestamp with an UTC offset. So if I insert that timestamp and reuse to filter for that row I should get that inserted row since So I kinda expect the tests to pass so I assume there is still something going wrong or am I misunderstanding something? |
For |
I updated my local chto to latest ch (commit from(
u in Product,
where: u.approved_at == ^time
)
|> TestRepo.exists?()
|> IO.inspect() ^ This still yields false from(
u in Product,
where: fragment("? = ?", u.approved_at, ^time)
)
|> TestRepo.exists?()
|> IO.inspect() ^ This now throws an error:
|
I see. Checking |
test "filtering datetimes" do
# iso8601 type
time = ~U[2023-04-20 17:00:25Z]
account =
%Account{}
|> Account.changeset(%{name: "Test"})
|> TestRepo.insert!()
%Product{}
|> Product.changeset(%{
account_id: account.id,
name: "Qux1",
approved_at: time
})
|> TestRepo.insert!()
assert TestRepo.exists?(
from u in Product,
where: u.approved_at == ^time
)
assert TestRepo.exists?(
from u in Product,
where: fragment("? = ?", u.approved_at, ^time)
)
# `refute` because `DateTime` is stored as seconds, for milliseconds precision use `DateTime64(3)`
refute TestRepo.exists?(
from u in Product,
where: fragment("? = ?", u.approved_at, ^DateTime.to_unix(time, :millisecond))
)
end |
Hmmm, if you run I get |
|
What if you change ur database to |
at least that is the only difference I can now spot between your env and my as why the test fails |
It seems to be more complicated than I thought. Simply encoding UTC datetimes to unix timestamps in query strings is not enough. |
#46 still fails on |
Seems like the timestamp inserted in RowBinary is also shifted by ClickHouse. |
That is kinda weird because technically according to their documentation that should do it (Dealing with time & timezones must be the most annoying domain problem I encounter :D ) |
Should the CI maybe also run with a different clickhouse timezone setting so that in the future we can catch future timezone issues? |
For reference, this is what Ch is doing now: plausible/ch@cea3726 |
Sure, but that'd would require reworking the tests a bit since naive datetimes would still fail. Probably a tag for each naive test like |
I'll return to this issue after finishing #43 |
Thanks my friend. I am currently slammed with work (postgres -> clickhouse migration :D ) but looking to do #37 over the weekend. |
I have taken another look, look at the queries: false
16:18:06.013 [debug] QUERY OK source="products" db=2.9ms idle=191.2ms
SELECT true FROM "products" AS p0 WHERE (p0."approved_at" = {$0:DateTime}) LIMIT 1 [~N[2023-04-20 17:00:25]]
true
16:18:06.019 [debug] QUERY OK source="products" db=2.8ms idle=195.7ms
SELECT true FROM "products" AS p0 WHERE (p0."approved_at" = {$0:DateTime}) LIMIT 1 [~U[2023-04-20 17:00:25Z]]
for some reason the DateTime gets casted to a NaiveDateTime. I ahve not been successful yet to locate where this happens and why but this is the issue. If it would use not cast it it would behave the same as the |
Nice find, Ecto schemas use naive datetimes by default. |
@Zarathustra2 I've added |
Given the following test for
timestamps_test.exs
All 3 cases will print false for me.
Is this expected and if so why?
The text was updated successfully, but these errors were encountered: