Make temporal types now functions use the query context timestamp (#333)

This commit is contained in:
Kostas Kyrimis 2022-02-04 13:57:38 +02:00 committed by GitHub
parent 265b203b00
commit 4fd8bdce4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 29 additions and 50 deletions

View File

@ -1,4 +1,4 @@
// Copyright 2021 Memgraph Ltd.
// Copyright 2022 Memgraph Ltd.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
@ -1080,7 +1080,7 @@ void MapNumericParameters(auto &parameter_mappings, const auto &input_parameters
TypedValue Date(const TypedValue *args, int64_t nargs, const FunctionContext &ctx) {
FType<Optional<Or<String, Map>>>("date", args, nargs);
if (nargs == 0) {
return TypedValue(utils::UtcToday(), ctx.memory);
return TypedValue(utils::LocalDateTime(ctx.timestamp).date, ctx.memory);
}
if (args[0].IsString()) {
@ -1103,7 +1103,7 @@ TypedValue LocalTime(const TypedValue *args, int64_t nargs, const FunctionContex
FType<Optional<Or<String, Map>>>("localtime", args, nargs);
if (nargs == 0) {
return TypedValue(utils::UtcLocalTime(), ctx.memory);
return TypedValue(utils::LocalDateTime(ctx.timestamp).local_time, ctx.memory);
}
if (args[0].IsString()) {
@ -1130,7 +1130,7 @@ TypedValue LocalDateTime(const TypedValue *args, int64_t nargs, const FunctionCo
FType<Optional<Or<String, Map>>>("localdatetime", args, nargs);
if (nargs == 0) {
return TypedValue(utils::UtcLocalDateTime(), ctx.memory);
return TypedValue(utils::LocalDateTime(ctx.timestamp), ctx.memory);
}
if (args[0].IsString()) {

View File

@ -1,4 +1,4 @@
// Copyright 2021 Memgraph Ltd.
// Copyright 2022 Memgraph Ltd.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source

View File

@ -1,4 +1,4 @@
// Copyright 2021 Memgraph Ltd.
// Copyright 2022 Memgraph Ltd.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
@ -1178,7 +1178,7 @@ mgp_error mgp_date_timestamp(mgp_date *date, int64_t *timestamp) {
}
mgp_error mgp_date_now(mgp_memory *memory, mgp_date **date) {
return WrapExceptions([memory] { return NewRawMgpObject<mgp_date>(memory, utils::UtcToday()); }, date);
return WrapExceptions([memory] { return NewRawMgpObject<mgp_date>(memory, utils::CurrentDate()); }, date);
}
mgp_error mgp_date_add_duration(mgp_date *date, mgp_duration *dur, mgp_memory *memory, mgp_date **result) {
@ -1241,7 +1241,7 @@ mgp_error mgp_local_time_timestamp(mgp_local_time *local_time, int64_t *timestam
}
mgp_error mgp_local_time_now(mgp_memory *memory, mgp_local_time **local_time) {
return WrapExceptions([memory] { return NewRawMgpObject<mgp_local_time>(memory, utils::UtcLocalTime()); },
return WrapExceptions([memory] { return NewRawMgpObject<mgp_local_time>(memory, utils::CurrentLocalTime()); },
local_time);
}
@ -1334,8 +1334,9 @@ mgp_error mgp_local_date_time_timestamp(mgp_local_date_time *local_date_time, in
}
mgp_error mgp_local_date_time_now(mgp_memory *memory, mgp_local_date_time **local_date_time) {
return WrapExceptions([memory] { return NewRawMgpObject<mgp_local_date_time>(memory, utils::UtcLocalDateTime()); },
local_date_time);
return WrapExceptions(
[memory] { return NewRawMgpObject<mgp_local_date_time>(memory, utils::CurrentLocalDateTime()); },
local_date_time);
}
mgp_error mgp_local_date_time_add_duration(mgp_local_date_time *local_date_time, mgp_duration *dur, mgp_memory *memory,

View File

@ -1,4 +1,4 @@
// Copyright 2021 Memgraph Ltd.
// Copyright 2022 Memgraph Ltd.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source

View File

@ -1,4 +1,4 @@
// Copyright 2021 Memgraph Ltd.
// Copyright 2022 Memgraph Ltd.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
@ -79,36 +79,14 @@ Date::Date(const DateParameters &date_parameters) {
day = date_parameters.day;
}
namespace {
tm GetUtcFromSystemClockOrThrow() {
Date CurrentDate() { return CurrentLocalDateTime().date; }
LocalTime CurrentLocalTime() { return CurrentLocalDateTime().local_time; }
LocalDateTime CurrentLocalDateTime() {
namespace chrono = std::chrono;
const auto today = chrono::system_clock::to_time_t(chrono::system_clock::now());
tm utc_today;
if (!gmtime_r(&today, &utc_today)) {
throw temporal::InvalidArgumentException("Can't access clock's UTC time");
}
return utc_today;
}
int64_t TMYearToUtcYear(int year) { return year + 1900; }
int64_t TMMonthToUtcMonth(int month) { return month + 1; }
} // namespace
Date UtcToday() {
const auto utc_today = GetUtcFromSystemClockOrThrow();
return Date({TMYearToUtcYear(utc_today.tm_year), TMMonthToUtcMonth(utc_today.tm_mon), utc_today.tm_mday});
}
LocalTime UtcLocalTime() {
const auto utc_today = GetUtcFromSystemClockOrThrow();
return LocalTime({utc_today.tm_hour, utc_today.tm_min, utc_today.tm_sec});
}
LocalDateTime UtcLocalDateTime() {
const auto utc_today = GetUtcFromSystemClockOrThrow();
return LocalDateTime({TMYearToUtcYear(utc_today.tm_year), TMMonthToUtcMonth(utc_today.tm_mon), utc_today.tm_mday},
{utc_today.tm_hour, utc_today.tm_min, utc_today.tm_sec});
auto ts = chrono::time_point_cast<chrono::microseconds>(chrono::system_clock::now());
return LocalDateTime(ts.time_since_epoch().count());
}
namespace {

View File

@ -1,4 +1,4 @@
// Copyright 2021 Memgraph Ltd.
// Copyright 2022 Memgraph Ltd.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
@ -313,7 +313,7 @@ struct LocalDateTimeHash {
size_t operator()(const LocalDateTime &local_date_time) const;
};
Date UtcToday();
LocalTime UtcLocalTime();
LocalDateTime UtcLocalDateTime();
Date CurrentDate();
LocalTime CurrentLocalTime();
LocalDateTime CurrentLocalDateTime();
} // namespace utils

View File

@ -1,4 +1,4 @@
// Copyright 2021 Memgraph Ltd.
// Copyright 2022 Memgraph Ltd.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
@ -48,7 +48,7 @@ class ExpressionEvaluatorTest : public ::testing::Test {
AstStorage storage;
utils::MonotonicBufferResource mem{1024};
EvaluationContext ctx{&mem};
EvaluationContext ctx{.memory = &mem, .timestamp = query::QueryTimestamp()};
SymbolTable symbol_table;
Frame frame{128};
@ -1916,7 +1916,7 @@ TEST_F(FunctionTest, Date) {
const auto map_param = TypedValue(
std::map<std::string, TypedValue>{{"year", TypedValue(1970)}, {"month", TypedValue(1)}, {"day", TypedValue(1)}});
EXPECT_EQ(EvaluateFunction("DATE", map_param).ValueDate(), unix_epoch);
const auto today = utils::UtcToday();
const auto today = utils::CurrentDate();
EXPECT_EQ(EvaluateFunction("DATE").ValueDate(), today);
EXPECT_THROW(EvaluateFunction("DATE", "{}"), utils::BasicException);
@ -1938,7 +1938,7 @@ TEST_F(FunctionTest, LocalTime) {
{"millisecond", TypedValue(4)},
{"microsecond", TypedValue(5)}});
EXPECT_EQ(EvaluateFunction("LOCALTIME", map_param).ValueLocalTime(), utils::LocalTime({1, 2, 3, 4, 5}));
const auto today = utils::UtcLocalTime();
const auto today = utils::CurrentLocalTime();
EXPECT_NEAR(EvaluateFunction("LOCALTIME").ValueLocalTime().MicrosecondsSinceEpoch(), today.MicrosecondsSinceEpoch(),
one_sec_in_microseconds);
@ -1956,7 +1956,7 @@ TEST_F(FunctionTest, LocalTime) {
TEST_F(FunctionTest, LocalDateTime) {
const auto local_date_time = utils::LocalDateTime({1970, 1, 1}, {13, 3, 2, 0, 0});
EXPECT_EQ(EvaluateFunction("LOCALDATETIME", "1970-01-01T13:03:02").ValueLocalDateTime(), local_date_time);
const auto today = utils::UtcLocalDateTime();
const auto today = utils::CurrentLocalDateTime();
const auto one_sec_in_microseconds = 1000000;
const auto map_param = TypedValue(std::map<std::string, TypedValue>{{"year", TypedValue(1972)},
{"month", TypedValue(2)},