Line data Source code
1 : #include "backend/engine/semantic/functions/datetime_funcs.h"
2 :
3 : #include <vector>
4 :
5 : #include "googlesql/public/value.h"
6 : #include "gtest/gtest.h"
7 :
8 : namespace bigquery_emulator {
9 : namespace backend {
10 : namespace engine {
11 : namespace semantic {
12 : namespace functions {
13 : namespace {
14 :
15 : using ::googlesql::Value;
16 :
17 1 : TEST(DatetimeFuncsTest, ParseDateBasic) {
18 1 : Value fmt = Value::String("%Y%m%d");
19 1 : Value date_str = Value::String("20081225");
20 1 : std::vector<Value> args = {fmt, date_str};
21 1 : auto got = ParseDate(args);
22 1 : ASSERT_TRUE(got.ok());
23 1 : EXPECT_EQ(got->date_value(), 14238);
24 1 : }
25 :
26 1 : TEST(DatetimeFuncsTest, DateAddDay) {
27 1 : Value fmt = Value::String("%Y-%m-%d");
28 1 : Value base_str = Value::String("2020-09-22");
29 1 : auto base = ParseDate({fmt, base_str});
30 1 : ASSERT_TRUE(base.ok());
31 1 : std::vector<Value> args = {
32 1 : *base,
33 1 : Value::Int64(1),
34 1 : Value::Int64(static_cast<int64_t>(::googlesql::functions::DAY))};
35 1 : auto got = DateAddSubDiffTrunc("date_add", args);
36 1 : ASSERT_TRUE(got.ok());
37 1 : Value next_str = Value::String("2020-09-23");
38 1 : auto next = ParseDate({fmt, next_str});
39 1 : ASSERT_TRUE(next.ok());
40 1 : EXPECT_EQ(got->date_value(), next->date_value());
41 1 : }
42 :
43 1 : TEST(DatetimeFuncsTest, DateAddMonthEndClampsToLastDay) {
44 1 : Value fmt = Value::String("%Y-%m-%d");
45 1 : Value base_str = Value::String("2024-01-31");
46 1 : auto base = ParseDate({fmt, base_str});
47 1 : ASSERT_TRUE(base.ok());
48 1 : std::vector<Value> args = {
49 1 : *base,
50 1 : Value::Int64(1),
51 1 : Value::Int64(static_cast<int64_t>(::googlesql::functions::MONTH))};
52 1 : auto got = DateAddSubDiffTrunc("date_add", args);
53 2 : ASSERT_TRUE(got.ok()) << got.status();
54 1 : Value expected_str = Value::String("2024-02-29");
55 1 : auto expected = ParseDate({fmt, expected_str});
56 1 : ASSERT_TRUE(expected.ok());
57 1 : EXPECT_EQ(got->date_value(), expected->date_value());
58 1 : }
59 :
60 1 : TEST(DatetimeFuncsTest, DateDiffMonthBoundaryWithinSameMonth) {
61 1 : Value fmt = Value::String("%Y-%m-%d");
62 1 : Value start_str = Value::String("2024-01-31");
63 1 : Value end_str = Value::String("2024-01-01");
64 1 : auto start = ParseDate({fmt, start_str});
65 1 : auto end = ParseDate({fmt, end_str});
66 1 : ASSERT_TRUE(start.ok());
67 1 : ASSERT_TRUE(end.ok());
68 1 : std::vector<Value> args = {
69 1 : *end,
70 1 : *start,
71 1 : Value::Int64(static_cast<int64_t>(::googlesql::functions::MONTH))};
72 1 : auto got = DateAddSubDiffTrunc("date_diff", args);
73 2 : ASSERT_TRUE(got.ok()) << got.status();
74 1 : EXPECT_EQ(got->int64_value(), 0);
75 1 : }
76 :
77 1 : TEST(DatetimeFuncsTest, FormatDatePercentF) {
78 1 : Value fmt = Value::String("%F");
79 1 : Value date_val = Value::Date(14238);
80 1 : std::vector<Value> args = {fmt, date_val};
81 1 : auto got = FormatDate(args);
82 1 : ASSERT_TRUE(got.ok());
83 1 : EXPECT_EQ(got->string_value(), "2008-12-25");
84 1 : }
85 :
86 1 : TEST(DatetimeFuncsTest, DateConstructorFromIsoString) {
87 1 : Value iso = Value::String("1800-01-01");
88 1 : auto got = DateConstructor({iso});
89 2 : ASSERT_TRUE(got.ok()) << got.status();
90 1 : Value fmt = Value::String("%Y-%m-%d");
91 1 : auto roundtrip = ParseDate({fmt, iso});
92 1 : ASSERT_TRUE(roundtrip.ok());
93 1 : EXPECT_EQ(got->date_value(), roundtrip->date_value());
94 1 : }
95 :
96 1 : TEST(DatetimeFuncsTest, DateConstructorFromYmdInts) {
97 1 : Value year = Value::Int64(2020);
98 1 : Value month = Value::Int64(9);
99 1 : Value day = Value::Int64(22);
100 1 : auto got = DateConstructor({year, month, day});
101 2 : ASSERT_TRUE(got.ok()) << got.status();
102 1 : Value fmt = Value::String("%Y-%m-%d");
103 1 : Value date_str = Value::String("2020-09-22");
104 1 : auto roundtrip = ParseDate({fmt, date_str});
105 1 : ASSERT_TRUE(roundtrip.ok());
106 1 : EXPECT_EQ(got->date_value(), roundtrip->date_value());
107 1 : }
108 :
109 : } // namespace
110 : } // namespace functions
111 : } // namespace semantic
112 : } // namespace engine
113 : } // namespace backend
114 : } // namespace bigquery_emulator
|