Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/iceberg/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ add_iceberg_test(util_test
endian_test.cc
formatter_test.cc
string_util_test.cc
temporal_util_test.cc
truncate_util_test.cc
uuid_test.cc
visit_type_test.cc)
Expand Down
58 changes: 43 additions & 15 deletions src/iceberg/test/bucket_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <gtest/gtest.h>

#include "iceberg/util/decimal.h"
#include "iceberg/util/temporal_util.h"
#include "iceberg/util/uuid.h"

namespace iceberg {
Expand All @@ -41,27 +42,54 @@ TEST(BucketUtilsTest, HashHelper) {
EXPECT_EQ(BucketUtils::HashBytes(decimal->ToBigEndian()), -500754589);

// date hash
std::chrono::sys_days sd = std::chrono::year{2017} / 11 / 16;
std::chrono::sys_days epoch{std::chrono::year{1970} / 1 / 1};
int32_t days = (sd - epoch).count();
EXPECT_EQ(BucketUtils::HashInt(days), -653330422);
EXPECT_EQ(BucketUtils::HashInt(
TemporalUtils::CreateDate({.year = 2017, .month = 11, .day = 16})),
-653330422);

// time
// 22:31:08 in microseconds
int64_t time_micros = (22 * 3600 + 31 * 60 + 8) * 1000000LL;
EXPECT_EQ(BucketUtils::HashLong(time_micros), -662762989);
EXPECT_EQ(BucketUtils::HashLong(
TemporalUtils::CreateTime({.hour = 22, .minute = 31, .second = 8})),
-662762989);

// timestamp
// 2017-11-16T22:31:08 in microseconds
std::chrono::system_clock::time_point tp =
std::chrono::sys_days{std::chrono::year{2017} / 11 / 16} + std::chrono::hours{22} +
std::chrono::minutes{31} + std::chrono::seconds{8};
int64_t timestamp_micros =
std::chrono::duration_cast<std::chrono::microseconds>(tp.time_since_epoch())
.count();
EXPECT_EQ(BucketUtils::HashLong(timestamp_micros), -2047944441);
EXPECT_EQ(
BucketUtils::HashLong(TemporalUtils::CreateTimestamp(
{.year = 2017, .month = 11, .day = 16, .hour = 22, .minute = 31, .second = 8})),
-2047944441);

// 2017-11-16T22:31:08.000001 in microseconds
EXPECT_EQ(BucketUtils::HashLong(timestamp_micros + 1), -1207196810);
EXPECT_EQ(BucketUtils::HashLong(TemporalUtils::CreateTimestamp({.year = 2017,
.month = 11,
.day = 16,
.hour = 22,
.minute = 31,
.second = 8,
.microsecond = 1})),
-1207196810);

// 2017-11-16T14:31:08-08:00 in microseconds
EXPECT_EQ(BucketUtils::HashLong(
TemporalUtils::CreateTimestampTz({.year = 2017,
.month = 11,
.day = 16,
.hour = 14,
.minute = 31,
.second = 8,
.tz_offset_minutes = -480})),
-2047944441);

// 2017-11-16T14:31:08.000001-08:00 in microseconds
EXPECT_EQ(BucketUtils::HashLong(
TemporalUtils::CreateTimestampTz({.year = 2017,
.month = 11,
.day = 16,
.hour = 14,
.minute = 31,
.second = 8,
.microsecond = 1,
.tz_offset_minutes = -480})),
-1207196810);

// string
std::string str = "iceberg";
Expand Down
1 change: 1 addition & 0 deletions src/iceberg/test/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ iceberg_tests = {
'endian_test.cc',
'formatter_test.cc',
'string_util_test.cc',
'temporal_util_test.cc',
'truncate_util_test.cc',
'uuid_test.cc',
'visit_type_test.cc',
Expand Down
171 changes: 171 additions & 0 deletions src/iceberg/test/temporal_util_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include "iceberg/util/temporal_util.h"

#include <gtest/gtest.h>

namespace iceberg {

TEST(TemporalUtilTest, CreateDate) {
EXPECT_EQ(TemporalUtils::CreateDate({.year = 1970, .month = 1, .day = 1}), 0);
EXPECT_EQ(TemporalUtils::CreateDate({.year = 1970, .month = 1, .day = 2}), 1);
EXPECT_EQ(TemporalUtils::CreateDate({.year = 1969, .month = 12, .day = 31}), -1);
EXPECT_EQ(TemporalUtils::CreateDate({.year = 2000, .month = 1, .day = 1}), 10957);
EXPECT_EQ(TemporalUtils::CreateDate({.year = 2017, .month = 11, .day = 16}), 17486);
EXPECT_EQ(TemporalUtils::CreateDate({.year = 2052, .month = 2, .day = 20}), 30000);
}

TEST(TemporalUtilTest, CreateTime) {
EXPECT_EQ(TemporalUtils::CreateTime({.hour = 0, .minute = 0, .second = 0}), 0);
EXPECT_EQ(TemporalUtils::CreateTime({.hour = 1, .minute = 0, .second = 0}),
3600000000LL);
EXPECT_EQ(TemporalUtils::CreateTime({.hour = 0, .minute = 1, .second = 0}), 60000000LL);
EXPECT_EQ(TemporalUtils::CreateTime({.hour = 0, .minute = 0, .second = 1}), 1000000LL);
EXPECT_EQ(TemporalUtils::CreateTime({.hour = 22, .minute = 31, .second = 8}),
81068000000LL);
EXPECT_EQ(TemporalUtils::CreateTime({.hour = 23, .minute = 59, .second = 59}),
86399000000LL);
EXPECT_EQ(
TemporalUtils::CreateTime({.hour = 0, .minute = 0, .second = 0, .microsecond = 1}),
1LL);
EXPECT_EQ(TemporalUtils::CreateTime(
{.hour = 0, .minute = 0, .second = 0, .microsecond = 999999}),
999999LL);
EXPECT_EQ(
TemporalUtils::CreateTime({.hour = 0, .minute = 0, .second = 1, .microsecond = 1}),
1000001LL);
EXPECT_EQ(TemporalUtils::CreateTime(
{.hour = 23, .minute = 59, .second = 59, .microsecond = 999999}),
86399999999LL);
}

TEST(TemporalUtilTest, CreateTimestamp) {
EXPECT_EQ(
TemporalUtils::CreateTimestamp(
{.year = 1970, .month = 1, .day = 1, .hour = 0, .minute = 0, .second = 0}),
0LL);
EXPECT_EQ(
TemporalUtils::CreateTimestamp(
{.year = 1970, .month = 1, .day = 2, .hour = 0, .minute = 0, .second = 0}),
86400000000LL);
EXPECT_EQ(
TemporalUtils::CreateTimestamp(
{.year = 1969, .month = 12, .day = 31, .hour = 23, .minute = 59, .second = 59}),
-1000000LL);
EXPECT_EQ(
TemporalUtils::CreateTimestamp(
{.year = 2000, .month = 1, .day = 1, .hour = 0, .minute = 0, .second = 0}),
946684800000000LL);
EXPECT_EQ(
TemporalUtils::CreateTimestamp(
{.year = 2017, .month = 11, .day = 16, .hour = 22, .minute = 31, .second = 8}),
1510871468000000LL);
EXPECT_EQ(TemporalUtils::CreateTimestamp({.year = 2017,
.month = 11,
.day = 16,
.hour = 22,
.minute = 31,
.second = 8,
.microsecond = 1}),
1510871468000001LL);
EXPECT_EQ(TemporalUtils::CreateTimestamp({.year = 2023,
.month = 10,
.day = 5,
.hour = 15,
.minute = 45,
.second = 30,
.microsecond = 123456}),
1696520730123456LL);
}

TEST(TemporalUtilTest, CreateTimestampTz) {
EXPECT_EQ(TemporalUtils::CreateTimestampTz({.year = 2017,
.month = 11,
.day = 16,
.hour = 14,
.minute = 31,
.second = 8,
.tz_offset_minutes = -480}),
1510871468000000LL);
EXPECT_EQ(TemporalUtils::CreateTimestampTz({.year = 2017,
.month = 11,
.day = 16,
.hour = 14,
.minute = 31,
.second = 8,
.microsecond = 1,
.tz_offset_minutes = -480}),
1510871468000001LL);
EXPECT_EQ(TemporalUtils::CreateTimestampTz({.year = 2023,
.month = 10,
.day = 5,
.hour = 15,
.minute = 45,
.second = 30,
.microsecond = 123456,
.tz_offset_minutes = 60}),
1696517130123456LL);
EXPECT_EQ(TemporalUtils::CreateTimestampTz({.year = 2023,
.month = 10,
.day = 5,
.hour = 15,
.minute = 45,
.second = 30,
.microsecond = 123456,
.tz_offset_minutes = -60}),
1696524330123456LL);
}

TEST(TemporalUtilTest, CreateTimestampNanos) {
EXPECT_EQ(
TemporalUtils::CreateTimestampNanos(
{.year = 2017, .month = 11, .day = 16, .hour = 22, .minute = 31, .second = 8}),
1510871468000000000LL);
EXPECT_EQ(TemporalUtils::CreateTimestampNanos({.year = 2017,
.month = 11,
.day = 16,
.hour = 22,
.minute = 31,
.second = 8,
.nanosecond = 1}),
1510871468000000001LL);
}

TEST(TemporalUtilTest, CreateTimestampTzNanos) {
EXPECT_EQ(TemporalUtils::CreateTimestampTzNanos({.year = 2017,
.month = 11,
.day = 16,
.hour = 14,
.minute = 31,
.second = 8,
.tz_offset_minutes = -480}),
1510871468000000000LL);
EXPECT_EQ(TemporalUtils::CreateTimestampTzNanos({.year = 2017,
.month = 11,
.day = 16,
.hour = 14,
.minute = 31,
.second = 8,
.nanosecond = 1,
.tz_offset_minutes = -480}),
1510871468000000001LL);
}

} // namespace iceberg
Loading
Loading