Skip to content

Commit a4138ec

Browse files
committed
resolve review comments
1 parent 900e106 commit a4138ec

File tree

8 files changed

+225
-348
lines changed

8 files changed

+225
-348
lines changed

src/iceberg/test/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ add_iceberg_test(util_test
105105
endian_test.cc
106106
formatter_test.cc
107107
string_util_test.cc
108-
temporal_util_test.cc
109108
truncate_util_test.cc
110109
uuid_test.cc
111110
visit_type_test.cc)

src/iceberg/test/bucket_util_test.cc

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
#include <gtest/gtest.h>
2525

2626
#include "iceberg/util/decimal.h"
27-
#include "iceberg/util/temporal_util.h"
2827
#include "iceberg/util/uuid.h"
28+
#include "temporal_test_helper.h"
2929

3030
namespace iceberg {
3131

@@ -43,52 +43,53 @@ TEST(BucketUtilsTest, HashHelper) {
4343

4444
// date hash
4545
EXPECT_EQ(BucketUtils::HashInt(
46-
TemporalUtils::CreateDate({.year = 2017, .month = 11, .day = 16})),
46+
TemporalTestHelper::CreateDate({.year = 2017, .month = 11, .day = 16})),
4747
-653330422);
4848

4949
// time
5050
EXPECT_EQ(BucketUtils::HashLong(
51-
TemporalUtils::CreateTime({.hour = 22, .minute = 31, .second = 8})),
51+
TemporalTestHelper::CreateTime({.hour = 22, .minute = 31, .second = 8})),
5252
-662762989);
5353

5454
// timestamp
5555
// 2017-11-16T22:31:08 in microseconds
5656
EXPECT_EQ(
57-
BucketUtils::HashLong(TemporalUtils::CreateTimestamp(
57+
BucketUtils::HashLong(TemporalTestHelper::CreateTimestamp(
5858
{.year = 2017, .month = 11, .day = 16, .hour = 22, .minute = 31, .second = 8})),
5959
-2047944441);
6060

6161
// 2017-11-16T22:31:08.000001 in microseconds
62-
EXPECT_EQ(BucketUtils::HashLong(TemporalUtils::CreateTimestamp({.year = 2017,
63-
.month = 11,
64-
.day = 16,
65-
.hour = 22,
66-
.minute = 31,
67-
.second = 8,
68-
.microsecond = 1})),
69-
-1207196810);
62+
EXPECT_EQ(
63+
BucketUtils::HashLong(TemporalTestHelper::CreateTimestamp({.year = 2017,
64+
.month = 11,
65+
.day = 16,
66+
.hour = 22,
67+
.minute = 31,
68+
.second = 8,
69+
.microsecond = 1})),
70+
-1207196810);
7071

7172
// 2017-11-16T14:31:08-08:00 in microseconds
7273
EXPECT_EQ(BucketUtils::HashLong(
73-
TemporalUtils::CreateTimestampTz({.year = 2017,
74-
.month = 11,
75-
.day = 16,
76-
.hour = 14,
77-
.minute = 31,
78-
.second = 8,
79-
.tz_offset_minutes = -480})),
74+
TemporalTestHelper::CreateTimestampTz({.year = 2017,
75+
.month = 11,
76+
.day = 16,
77+
.hour = 14,
78+
.minute = 31,
79+
.second = 8,
80+
.tz_offset_minutes = -480})),
8081
-2047944441);
8182

8283
// 2017-11-16T14:31:08.000001-08:00 in microseconds
8384
EXPECT_EQ(BucketUtils::HashLong(
84-
TemporalUtils::CreateTimestampTz({.year = 2017,
85-
.month = 11,
86-
.day = 16,
87-
.hour = 14,
88-
.minute = 31,
89-
.second = 8,
90-
.microsecond = 1,
91-
.tz_offset_minutes = -480})),
85+
TemporalTestHelper::CreateTimestampTz({.year = 2017,
86+
.month = 11,
87+
.day = 16,
88+
.hour = 14,
89+
.minute = 31,
90+
.second = 8,
91+
.microsecond = 1,
92+
.tz_offset_minutes = -480})),
9293
-1207196810);
9394

9495
// string

src/iceberg/test/meson.build

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ iceberg_tests = {
7474
'endian_test.cc',
7575
'formatter_test.cc',
7676
'string_util_test.cc',
77-
'temporal_util_test.cc',
7877
'truncate_util_test.cc',
7978
'uuid_test.cc',
8079
'visit_type_test.cc',
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#pragma once
21+
22+
#include <chrono>
23+
#include <cstdint>
24+
25+
namespace iceberg {
26+
27+
using namespace std::chrono; // NOLINT
28+
29+
struct DateParts {
30+
int32_t year{0};
31+
uint8_t month{0};
32+
uint8_t day{0};
33+
};
34+
35+
struct TimeParts {
36+
int32_t hour{0};
37+
int32_t minute{0};
38+
int32_t second{0};
39+
int32_t microsecond{0};
40+
};
41+
42+
struct TimestampParts {
43+
int32_t year{0};
44+
uint8_t month{0};
45+
uint8_t day{0};
46+
int32_t hour{0};
47+
int32_t minute{0};
48+
int32_t second{0};
49+
int32_t microsecond{0};
50+
// e.g. -480 for PST (UTC-8:00), +480 for Asia/Shanghai (UTC+8:00)
51+
int32_t tz_offset_minutes{0};
52+
};
53+
54+
struct TimestampNanosParts {
55+
int32_t year{0};
56+
uint8_t month{0};
57+
uint8_t day{0};
58+
int32_t hour{0};
59+
int32_t minute{0};
60+
int32_t second{0};
61+
int32_t nanosecond{0};
62+
// e.g. -480 for PST (UTC-8:00), +480 for Asia/Shanghai (UTC+8:00)
63+
int32_t tz_offset_minutes{0};
64+
};
65+
66+
class TemporalTestHelper {
67+
static constexpr auto kEpochDays = sys_days(year{1970} / January / 1);
68+
69+
public:
70+
/// \brief Construct a Calendar date without timezone or time
71+
static int32_t CreateDate(const DateParts& parts) {
72+
return static_cast<int32_t>(
73+
(sys_days(year{parts.year} / month{parts.month} / day{parts.day}) - kEpochDays)
74+
.count());
75+
}
76+
77+
/// \brief Construct a time-of-day, microsecond precision, without date, timezone
78+
static int64_t CreateTime(const TimeParts& parts) {
79+
return duration_cast<microseconds>(hours(parts.hour) + minutes(parts.minute) +
80+
seconds(parts.second) +
81+
microseconds(parts.microsecond))
82+
.count();
83+
}
84+
85+
/// \brief Construct a timestamp, microsecond precision, without timezone
86+
static int64_t CreateTimestamp(const TimestampParts& parts) {
87+
year_month_day ymd{year{parts.year}, month{parts.month}, day{parts.day}};
88+
auto tp = sys_time<microseconds>{(sys_days(ymd) + hours{parts.hour} +
89+
minutes{parts.minute} + seconds{parts.second} +
90+
microseconds{parts.microsecond})
91+
.time_since_epoch()};
92+
return tp.time_since_epoch().count();
93+
}
94+
95+
/// \brief Construct a timestamp, microsecond precision, with timezone
96+
static int64_t CreateTimestampTz(const TimestampParts& parts) {
97+
year_month_day ymd{year{parts.year}, month{parts.month}, day{parts.day}};
98+
auto tp = sys_time<microseconds>{(sys_days(ymd) + hours{parts.hour} +
99+
minutes{parts.minute} + seconds{parts.second} +
100+
microseconds{parts.microsecond} -
101+
minutes{parts.tz_offset_minutes})
102+
.time_since_epoch()};
103+
return tp.time_since_epoch().count();
104+
}
105+
106+
/// \brief Construct a timestamp, nanosecond precision, without timezone
107+
static int64_t CreateTimestampNanos(const TimestampNanosParts& parts) {
108+
year_month_day ymd{year{parts.year}, month{parts.month}, day{parts.day}};
109+
auto tp =
110+
sys_time<nanoseconds>{(sys_days(ymd) + hours{parts.hour} + minutes{parts.minute} +
111+
seconds{parts.second} + nanoseconds{parts.nanosecond})
112+
.time_since_epoch()};
113+
return tp.time_since_epoch().count();
114+
}
115+
116+
/// \brief Construct a timestamp, nanosecond precision, with timezone
117+
static int64_t CreateTimestampTzNanos(const TimestampNanosParts& parts) {
118+
year_month_day ymd{year{parts.year}, month{parts.month}, day{parts.day}};
119+
auto tp =
120+
sys_time<nanoseconds>{(sys_days(ymd) + hours{parts.hour} + minutes{parts.minute} +
121+
seconds{parts.second} + nanoseconds{parts.nanosecond} -
122+
minutes{parts.tz_offset_minutes})
123+
.time_since_epoch()};
124+
return tp.time_since_epoch().count();
125+
}
126+
};
127+
128+
} // namespace iceberg

0 commit comments

Comments
 (0)