From 3e9f25b8e47923af1873de6b41b4ed00e2ece406 Mon Sep 17 00:00:00 2001 From: Andi Date: Fri, 3 Nov 2023 10:54:01 +0100 Subject: [PATCH] Support creating date and localtime from localdatetime (#1381) --- .../interpret/awesome_memgraph_functions.cpp | 14 ++++++++++-- .../tests/memgraph_V1/features/match.feature | 16 +++++++++++++- .../features/match.feature | 16 +++++++++++++- tests/unit/utils_temporal.cpp | 22 ++++++++++++++++++- 4 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/query/interpret/awesome_memgraph_functions.cpp b/src/query/interpret/awesome_memgraph_functions.cpp index 93de7b7d2..2cfd11f8c 100644 --- a/src/query/interpret/awesome_memgraph_functions.cpp +++ b/src/query/interpret/awesome_memgraph_functions.cpp @@ -1156,11 +1156,16 @@ void MapNumericParameters(auto ¶meter_mappings, const auto &input_parameters } TypedValue Date(const TypedValue *args, int64_t nargs, const FunctionContext &ctx) { - FType>>("date", args, nargs); + FType>>("date", args, nargs); if (nargs == 0) { return TypedValue(utils::LocalDateTime(ctx.timestamp).date, ctx.memory); } + if (args[0].IsLocalDateTime()) { + utils::Date date{args[0].ValueLocalDateTime().date}; + return TypedValue(date, ctx.memory); + } + if (args[0].IsString()) { const auto &[date_parameters, is_extended] = utils::ParseDateParameters(args[0].ValueString()); return TypedValue(utils::Date(date_parameters), ctx.memory); @@ -1178,12 +1183,17 @@ TypedValue Date(const TypedValue *args, int64_t nargs, const FunctionContext &ct } TypedValue LocalTime(const TypedValue *args, int64_t nargs, const FunctionContext &ctx) { - FType>>("localtime", args, nargs); + FType>>("localtime", args, nargs); if (nargs == 0) { return TypedValue(utils::LocalDateTime(ctx.timestamp).local_time, ctx.memory); } + if (args[0].IsLocalDateTime()) { + utils::LocalTime local_time{args[0].ValueLocalDateTime().local_time}; + return TypedValue(local_time, ctx.memory); + } + if (args[0].IsString()) { const auto &[local_time_parameters, is_extended] = utils::ParseLocalTimeParameters(args[0].ValueString()); return TypedValue(utils::LocalTime(local_time_parameters), ctx.memory); diff --git a/tests/gql_behave/tests/memgraph_V1/features/match.feature b/tests/gql_behave/tests/memgraph_V1/features/match.feature index 830e1b9f5..cf41c20f2 100644 --- a/tests/gql_behave/tests/memgraph_V1/features/match.feature +++ b/tests/gql_behave/tests/memgraph_V1/features/match.feature @@ -684,4 +684,18 @@ Feature: Match """ MATCH path = (pers:Person {id: 1})-[:KNOWS*2..]->(pers) RETURN path """ - Then the result should be empty + Then the result should be empty + + Scenario: Match with temporal property + Given an empty graph + And having executed: + """ + CREATE (n:User {time: localDateTime("2021-10-05T14:15:00")}); + """ + When executing query: + """ + MATCH (n) RETURN date(n.time); + """ + Then the result should be + | date(n.time) | + | 2021-10-05 | diff --git a/tests/gql_behave/tests/memgraph_V1_on_disk/features/match.feature b/tests/gql_behave/tests/memgraph_V1_on_disk/features/match.feature index 830e1b9f5..cf41c20f2 100644 --- a/tests/gql_behave/tests/memgraph_V1_on_disk/features/match.feature +++ b/tests/gql_behave/tests/memgraph_V1_on_disk/features/match.feature @@ -684,4 +684,18 @@ Feature: Match """ MATCH path = (pers:Person {id: 1})-[:KNOWS*2..]->(pers) RETURN path """ - Then the result should be empty + Then the result should be empty + + Scenario: Match with temporal property + Given an empty graph + And having executed: + """ + CREATE (n:User {time: localDateTime("2021-10-05T14:15:00")}); + """ + When executing query: + """ + MATCH (n) RETURN date(n.time); + """ + Then the result should be + | date(n.time) | + | 2021-10-05 | diff --git a/tests/unit/utils_temporal.cpp b/tests/unit/utils_temporal.cpp index 5736a2d50..cbdf5ab12 100644 --- a/tests/unit/utils_temporal.cpp +++ b/tests/unit/utils_temporal.cpp @@ -1,4 +1,4 @@ -// Copyright 2022 Memgraph Ltd. +// Copyright 2023 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 @@ -194,6 +194,26 @@ TEST(TemporalTest, DurationConversion) { }; } +TEST(TemporalTest, LocalDateTimeToDate) { + memgraph::utils::LocalDateTime local_date_time{memgraph::utils::DateParameters{2020, 11, 22}, + memgraph::utils::LocalTimeParameters{13, 21, 40, 123, 456}}; + memgraph::utils::Date date{local_date_time.date}; + ASSERT_EQ(date.year, 2020); + ASSERT_EQ(date.month, 11); + ASSERT_EQ(date.day, 22); +} + +TEST(TemporalTest, LocalDateTimeToLocalTime) { + memgraph::utils::LocalDateTime local_date_time{memgraph::utils::DateParameters{2020, 11, 22}, + memgraph::utils::LocalTimeParameters{13, 21, 40, 123, 456}}; + memgraph::utils::LocalTime local_time{local_date_time.local_time}; + ASSERT_EQ(local_time.hour, 13); + ASSERT_EQ(local_time.minute, 21); + ASSERT_EQ(local_time.second, 40); + ASSERT_EQ(local_time.millisecond, 123); + ASSERT_EQ(local_time.microsecond, 456); +} + namespace { using namespace std::literals; inline constexpr std::array parsing_test_dates_extended{