Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 7 additions & 6 deletions src/iceberg/expression/json_serde.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "iceberg/util/json_util_internal.h"
#include "iceberg/util/macros.h"
#include "iceberg/util/string_util.h"
#include "iceberg/util/temporal_util.h"
#include "iceberg/util/transform_util.h"

namespace iceberg {
Expand Down Expand Up @@ -363,7 +364,7 @@ Result<Literal> LiteralFromJson(const nlohmann::json& json, const Type* type) {
return JsonParseError("Cannot parse {} as a date value", SafeDumpJson(json));
}
ICEBERG_ASSIGN_OR_RAISE(auto days,
TransformUtil::ParseDay(json.get<std::string>()));
TemporalUtils::ParseDay(json.get<std::string>()));
return Literal::Date(days);
}

Expand All @@ -372,7 +373,7 @@ Result<Literal> LiteralFromJson(const nlohmann::json& json, const Type* type) {
return JsonParseError("Cannot parse {} as a time value", SafeDumpJson(json));
}
ICEBERG_ASSIGN_OR_RAISE(auto micros,
TransformUtil::ParseTime(json.get<std::string>()));
TemporalUtils::ParseTime(json.get<std::string>()));
return Literal::Time(micros);
}

Expand All @@ -381,7 +382,7 @@ Result<Literal> LiteralFromJson(const nlohmann::json& json, const Type* type) {
return JsonParseError("Cannot parse {} as a timestamp value", SafeDumpJson(json));
}
ICEBERG_ASSIGN_OR_RAISE(auto micros,
TransformUtil::ParseTimestamp(json.get<std::string>()));
TemporalUtils::ParseTimestamp(json.get<std::string>()));
return Literal::Timestamp(micros);
}

Expand All @@ -391,7 +392,7 @@ Result<Literal> LiteralFromJson(const nlohmann::json& json, const Type* type) {
SafeDumpJson(json));
}
ICEBERG_ASSIGN_OR_RAISE(
auto micros, TransformUtil::ParseTimestampWithZone(json.get<std::string>()));
auto micros, TemporalUtils::ParseTimestampWithZone(json.get<std::string>()));
return Literal::TimestampTz(micros);
}

Expand All @@ -401,7 +402,7 @@ Result<Literal> LiteralFromJson(const nlohmann::json& json, const Type* type) {
SafeDumpJson(json));
}
ICEBERG_ASSIGN_OR_RAISE(auto nanos,
TransformUtil::ParseTimestampNs(json.get<std::string>()));
TemporalUtils::ParseTimestampNs(json.get<std::string>()));
return Literal::TimestampNs(nanos);
}

Expand All @@ -411,7 +412,7 @@ Result<Literal> LiteralFromJson(const nlohmann::json& json, const Type* type) {
SafeDumpJson(json));
}
ICEBERG_ASSIGN_OR_RAISE(
auto nanos, TransformUtil::ParseTimestampNsWithZone(json.get<std::string>()));
auto nanos, TemporalUtils::ParseTimestampNsWithZone(json.get<std::string>()));
return Literal::TimestampTzNs(nanos);
}

Expand Down
13 changes: 6 additions & 7 deletions src/iceberg/expression/literal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include "iceberg/util/macros.h"
#include "iceberg/util/string_util.h"
#include "iceberg/util/temporal_util.h"
#include "iceberg/util/transform_util.h"

namespace iceberg {

Expand Down Expand Up @@ -203,29 +202,29 @@ Result<Literal> LiteralCaster::CastFromString(
return Literal::UUID(uuid);
}
case TypeId::kDate: {
ICEBERG_ASSIGN_OR_RAISE(auto days, TransformUtil::ParseDay(str_val));
ICEBERG_ASSIGN_OR_RAISE(auto days, TemporalUtils::ParseDay(str_val));
return Literal::Date(days);
}
case TypeId::kTime: {
ICEBERG_ASSIGN_OR_RAISE(auto micros, TransformUtil::ParseTime(str_val));
ICEBERG_ASSIGN_OR_RAISE(auto micros, TemporalUtils::ParseTime(str_val));
return Literal::Time(micros);
}
case TypeId::kTimestamp: {
ICEBERG_ASSIGN_OR_RAISE(auto micros, TransformUtil::ParseTimestamp(str_val));
ICEBERG_ASSIGN_OR_RAISE(auto micros, TemporalUtils::ParseTimestamp(str_val));
return Literal::Timestamp(micros);
}
case TypeId::kTimestampTz: {
ICEBERG_ASSIGN_OR_RAISE(auto micros,
TransformUtil::ParseTimestampWithZone(str_val));
TemporalUtils::ParseTimestampWithZone(str_val));
return Literal::TimestampTz(micros);
}
case TypeId::kTimestampNs: {
ICEBERG_ASSIGN_OR_RAISE(auto nanos, TransformUtil::ParseTimestampNs(str_val));
ICEBERG_ASSIGN_OR_RAISE(auto nanos, TemporalUtils::ParseTimestampNs(str_val));
return Literal::TimestampNs(nanos);
}
case TypeId::kTimestampTzNs: {
ICEBERG_ASSIGN_OR_RAISE(auto nanos,
TransformUtil::ParseTimestampNsWithZone(str_val));
TemporalUtils::ParseTimestampNsWithZone(str_val));
return Literal::TimestampTzNs(nanos);
}
case TypeId::kBinary: {
Expand Down
2 changes: 2 additions & 0 deletions src/iceberg/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,13 @@ add_iceberg_test(util_test
formatter_test.cc
lazy_test.cc
location_util_test.cc
math_util_internal_test.cc
roaring_position_bitmap_test.cc
position_delete_index_test.cc
retry_util_test.cc
string_util_test.cc
struct_like_set_test.cc
temporal_util_test.cc
transform_util_test.cc
truncate_util_test.cc
url_encoder_test.cc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "iceberg/schema.h"
#include "iceberg/test/matchers.h"
#include "iceberg/type.h"
#include "iceberg/util/temporal_util.h"

namespace iceberg {

Expand All @@ -38,9 +39,8 @@ constexpr bool kRowsMightMatch = true;
constexpr bool kRowCannotMatch = false;
constexpr int64_t kIntMinValue = 30;
constexpr int64_t kIntMaxValue = 79;
constexpr int64_t kMicrosPerDay = 86'400'000'000LL;
constexpr int64_t kTsMinValue = 30 * kMicrosPerDay;
constexpr int64_t kTsMaxValue = 79 * kMicrosPerDay;
constexpr int64_t kTsMinValue = 30 * internal::kMicrosPerDay;
constexpr int64_t kTsMaxValue = 79 * internal::kMicrosPerDay;

std::shared_ptr<UnboundTerm<BoundTransform>> ToBoundTransform(
const std::shared_ptr<UnboundTransform>& transform) {
Expand Down
10 changes: 6 additions & 4 deletions src/iceberg/test/literal_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "iceberg/test/matchers.h"
#include "iceberg/test/temporal_test_helper.h"
#include "iceberg/type.h"
#include "iceberg/util/temporal_util.h"

namespace iceberg {

Expand Down Expand Up @@ -676,10 +677,11 @@ INSTANTIATE_TEST_SUITE_P(
.small_literal = Literal::Date(100),
.large_literal = Literal::Date(200),
.equal_literal = Literal::Date(100)},
ComparisonLiteralTestParam{.test_name = "Time",
.small_literal = Literal::Time(43200000000LL),
.large_literal = Literal::Time(86400000000LL),
.equal_literal = Literal::Time(43200000000LL)},
ComparisonLiteralTestParam{
.test_name = "Time",
.small_literal = Literal::Time(internal::kMicrosPerDay / 2),
.large_literal = Literal::Time(internal::kMicrosPerDay),
.equal_literal = Literal::Time(internal::kMicrosPerDay / 2)},
ComparisonLiteralTestParam{.test_name = "Timestamp",
.small_literal = Literal::Timestamp(1000000LL),
.large_literal = Literal::Timestamp(2000000LL),
Expand Down
56 changes: 56 additions & 0 deletions src/iceberg/test/math_util_internal_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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/math_util_internal.h"

#include <limits>

#include <gtest/gtest.h>

#include "iceberg/test/matchers.h"

namespace iceberg {

TEST(MathUtilInternalTest, FloorDiv) {
EXPECT_EQ(0, FloorDiv(0, 1000));
EXPECT_EQ(1, FloorDiv(1001, 1000));
EXPECT_EQ(-1, FloorDiv(-1, 1000));
EXPECT_EQ(-2, FloorDiv(-1001, 1000));
EXPECT_EQ(1, FloorDiv(-1001, -1000));
EXPECT_EQ(-2, FloorDiv(1001, -1000));
}

TEST(MathUtilInternalTest, MultiplyExact) {
ICEBERG_UNWRAP_OR_FAIL(auto positive, MultiplyExact(1000, 1000));
EXPECT_EQ(1000000, positive);

ICEBERG_UNWRAP_OR_FAIL(auto negative, MultiplyExact(-1000, 1000));
EXPECT_EQ(-1000000, negative);

ICEBERG_UNWRAP_OR_FAIL(auto min_value,
MultiplyExact(std::numeric_limits<int64_t>::min(), 1));
EXPECT_EQ(std::numeric_limits<int64_t>::min(), min_value);

EXPECT_THAT(MultiplyExact(std::numeric_limits<int64_t>::max(), 2),
IsError(ErrorKind::kInvalidArgument));
EXPECT_THAT(MultiplyExact(std::numeric_limits<int64_t>::min(), -1),
IsError(ErrorKind::kInvalidArgument));
}

} // namespace iceberg
2 changes: 2 additions & 0 deletions src/iceberg/test/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,13 @@ iceberg_tests = {
'formatter_test.cc',
'lazy_test.cc',
'location_util_test.cc',
'math_util_internal_test.cc',
'position_delete_index_test.cc',
'retry_util_test.cc',
'roaring_position_bitmap_test.cc',
'string_util_test.cc',
'struct_like_set_test.cc',
'temporal_util_test.cc',
'transform_util_test.cc',
'truncate_util_test.cc',
'url_encoder_test.cc',
Expand Down
7 changes: 4 additions & 3 deletions src/iceberg/test/temporal_test_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <chrono>
#include <cstdint>

#include "iceberg/util/temporal_util.h"

namespace iceberg {

using namespace std::chrono; // NOLINT
Expand Down Expand Up @@ -64,13 +66,12 @@ struct TimestampNanosParts {
};

class TemporalTestHelper {
static constexpr auto kEpochDays = sys_days(year{1970} / January / 1);

public:
/// \brief Construct a Calendar date without timezone or time
static int32_t CreateDate(const DateParts& parts) {
return static_cast<int32_t>(
(sys_days(year{parts.year} / month{parts.month} / day{parts.day}) - kEpochDays)
(sys_days(year{parts.year} / month{parts.month} / day{parts.day}) -
internal::kEpochDays)
.count());
}

Expand Down
Loading
Loading