Support creating date and localtime from localdatetime (#1381)

This commit is contained in:
Andi 2023-11-03 10:54:01 +01:00 committed by GitHub
parent c94201621a
commit 3e9f25b8e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 5 deletions

View File

@ -1156,11 +1156,16 @@ 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);
FType<Optional<Or<String, Map, LocalDateTime>>>("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<Optional<Or<String, Map>>>("localtime", args, nargs);
FType<Optional<Or<String, Map, LocalDateTime>>>("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);

View File

@ -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 |

View File

@ -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 |

View File

@ -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{