Define dump command for all temporal types (#225)

This commit is contained in:
Kostas Kyrimis 2021-09-07 16:42:03 +03:00 committed by Antonio Andelic
parent d744711e5e
commit b45ae403e6
2 changed files with 78 additions and 4 deletions

View File

@ -8,6 +8,8 @@
#include <utility>
#include <vector>
#include "fmt/format.h"
#include "query/db_accessor.hpp"
#include "query/exceptions.hpp"
#include "query/stream.hpp"
@ -17,6 +19,7 @@
#include "utils/algorithm.hpp"
#include "utils/logging.hpp"
#include "utils/string.hpp"
#include "utils/temporal.hpp"
namespace query {
@ -55,6 +58,49 @@ void DumpPreciseDouble(std::ostream *os, double value) {
*os << temp_oss.str();
}
namespace {
void DumpDate(std::ostream &os, const storage::TemporalData &value) {
utils::Date date(value.microseconds);
os << fmt::format("DATE(\"{}\")", date);
}
void DumpLocalTime(std::ostream &os, const storage::TemporalData &value) {
utils::LocalTime lt(value.microseconds);
os << fmt::format("LOCALTIME(\"{}\")", lt);
}
void DumpLocalDateTime(std::ostream &os, const storage::TemporalData &value) {
utils::LocalDateTime ldt(value.microseconds);
os << fmt::format("LOCALDATETIME(\"{}\")", ldt);
}
void DumpDuration(std::ostream &os, const storage::TemporalData &value) {
utils::Duration dur(value.microseconds);
os << fmt::format("DURATION(\"{}\")", dur);
}
void DumpTemporalData(std::ostream &os, const storage::TemporalData &value) {
switch (value.type) {
case storage::TemporalType::Date: {
DumpDate(os, value);
return;
}
case storage::TemporalType::LocalTime: {
DumpLocalTime(os, value);
return;
}
case storage::TemporalType::LocalDateTime: {
DumpLocalDateTime(os, value);
return;
}
case storage::TemporalType::Duration: {
DumpDuration(os, value);
return;
}
}
}
} // namespace
void DumpPropertyValue(std::ostream *os, const storage::PropertyValue &value) {
switch (value.type()) {
case storage::PropertyValue::Type::Null:
@ -90,7 +136,7 @@ void DumpPropertyValue(std::ostream *os, const storage::PropertyValue &value) {
return;
}
case storage::PropertyValue::Type::TemporalData: {
// TODO(antonio2368): Define dump command for temporal data
DumpTemporalData(*os, value.ValueTemporalData());
return;
}
}

View File

@ -11,6 +11,8 @@
#include "query/interpreter.hpp"
#include "query/typed_value.hpp"
#include "storage/v2/storage.hpp"
#include "storage/v2/temporal.hpp"
#include "utils/temporal.hpp"
const char *kPropertyId = "property_id";
@ -387,7 +389,16 @@ TEST(DumpTest, PropertyValue) {
auto double_value = storage::PropertyValue(-1.2);
auto str_value = storage::PropertyValue("hello 'world'");
auto map_value = storage::PropertyValue({{"prop 1", int_value}, {"prop`2`", bool_value}});
auto list_value = storage::PropertyValue({map_value, null_value, double_value});
auto dt = storage::PropertyValue(
storage::TemporalData(storage::TemporalType::Date, utils::Date({1994, 12, 7}).MicrosecondsSinceEpoch()));
auto lt = storage::PropertyValue(storage::TemporalData(
storage::TemporalType::LocalTime, utils::LocalTime({14, 10, 44, 99, 99}).MicrosecondsSinceEpoch()));
auto ldt = storage::PropertyValue(
storage::TemporalData(storage::TemporalType::LocalDateTime,
utils::LocalDateTime({1994, 12, 7}, {14, 10, 44, 99, 99}).MicrosecondsSinceEpoch()));
auto dur = storage::PropertyValue(storage::TemporalData(storage::TemporalType::Duration,
utils::Duration({1, 2, 3, 4, 5, 6, 10, 11}).microseconds));
auto list_value = storage::PropertyValue({map_value, null_value, double_value, dt, lt, ldt, dur});
CreateVertex(&dba, {}, {{"p1", list_value}, {"p2", str_value}}, false);
ASSERT_FALSE(dba.Commit().HasError());
}
@ -402,11 +413,13 @@ TEST(DumpTest, PropertyValue) {
}
VerifyQueries(stream.GetResults(), kCreateInternalIndex,
"CREATE (:__mg_vertex__ {__mg_id__: 0, `p1`: [{`prop 1`: 13, "
"`prop``2```: true}, Null, -1.2], `p2`: \"hello \\'world\\'\"});",
"`prop``2```: true}, Null, -1.2, DATE(\"1994-12-07\"), "
"LOCALTIME(\"14:10:44.099099\"), LOCALDATETIME(\"1994-12-07T14:10:44.099099\"), "
"DURATION(\"P0001-02-03T04:05:06.010011\")"
"], `p2`: \"hello \\'world\\'\"});",
kDropInternalIndex, kRemoveInternalLabelProperty);
}
}
// NOLINTNEXTLINE(hicpp-special-member-functions)
TEST(DumpTest, SingleEdge) {
storage::Storage db;
@ -634,6 +647,21 @@ TEST(DumpTest, CheckStateSimpleGraph) {
CreateEdge(&dba, &z, &u, "Knows", {});
CreateEdge(&dba, &w, &z, "Knows", {{"how", storage::PropertyValue("school")}});
CreateEdge(&dba, &w, &z, "Likes", {{"how", storage::PropertyValue("very much")}});
CreateEdge(&dba, &w, &z, "Date",
{{"time", storage::PropertyValue(storage::TemporalData(
storage::TemporalType::Date, utils::Date({1994, 12, 7}).MicrosecondsSinceEpoch()))}});
CreateEdge(&dba, &w, &z, "LocalTime",
{{"time", storage::PropertyValue(
storage::TemporalData(storage::TemporalType::LocalTime,
utils::LocalTime({14, 10, 44, 99, 99}).MicrosecondsSinceEpoch()))}});
CreateEdge(&dba, &w, &z, "LocalDateTime",
{{"time", storage::PropertyValue(storage::TemporalData(
storage::TemporalType::LocalDateTime,
utils::LocalDateTime({1994, 12, 7}, {14, 10, 44, 99, 99}).MicrosecondsSinceEpoch()))}});
CreateEdge(
&dba, &w, &z, "Duration",
{{"time", storage::PropertyValue(storage::TemporalData(
storage::TemporalType::Duration, utils::Duration({1, 2, 3, 4, 5, 6, 10, 11}).microseconds))}});
ASSERT_FALSE(dba.Commit().HasError());
}
{