Skip to content

Commit 40834dd

Browse files
authored
fix: YearTransform return years since 1970 (#495)
1 parent c3e6ff7 commit 40834dd

File tree

5 files changed

+26
-18
lines changed

5 files changed

+26
-18
lines changed

src/iceberg/test/eval_expr_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ TEST_F(BoundExpressionTest, YearTransform) {
161161
// Evaluate (2021)
162162
ICEBERG_UNWRAP_OR_FAIL(auto result, bound_transform->Evaluate(*struct_like));
163163
EXPECT_FALSE(result.IsNull());
164-
EXPECT_EQ(std::get<int32_t>(result.value()), 2021); // Year value
164+
EXPECT_EQ(std::get<int32_t>(result.value()), 2021 - 1970); // Year value
165165
}
166166

167167
TEST_F(BoundExpressionTest, MonthTransform) {

src/iceberg/test/transform_test.cc

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ INSTANTIATE_TEST_SUITE_P(
459459
.hour = 11,
460460
.minute = 43,
461461
.second = 20})),
462-
.expected = Literal::Int(2021)},
462+
.expected = Literal::Int(2021 - 1970)},
463463
TransformParam{
464464
.str = "TimestampTz",
465465
// 2021-01-01T07:43:20+08:00, which is 2020-12-31T23:43:20Z
@@ -472,12 +472,12 @@ INSTANTIATE_TEST_SUITE_P(
472472
.minute = 43,
473473
.second = 20,
474474
.tz_offset_minutes = 480})),
475-
.expected = Literal::Int(2020)},
475+
.expected = Literal::Int(2020 - 1970)},
476476
TransformParam{.str = "Date",
477477
.source_type = iceberg::date(),
478478
.source = Literal::Date(TemporalTestHelper::CreateDate(
479479
{.year = 2052, .month = 2, .day = 20})),
480-
.expected = Literal::Int(2052)}),
480+
.expected = Literal::Int(2052 - 1970)}),
481481
[](const ::testing::TestParamInfo<TransformParam>& info) { return info.param.str; });
482482

483483
class MonthTransformTest : public ::testing::TestWithParam<TransformParam> {};
@@ -2061,7 +2061,8 @@ TEST_F(TransformProjectStrictTest, YearStrictLessThan) {
20612061
std::move(projected));
20622062
EXPECT_EQ(unbound_projected->op(), Expression::Operation::kLt);
20632063
EXPECT_EQ(unbound_projected->literals().size(), 1);
2064-
EXPECT_EQ(std::get<int32_t>(unbound_projected->literals().front().value()), 2021);
2064+
EXPECT_EQ(std::get<int32_t>(unbound_projected->literals().front().value()),
2065+
2021 - 1970);
20652066
}
20662067

20672068
TEST_F(TransformProjectStrictTest, YearStrictGreaterThanOrEqual) {
@@ -2085,7 +2086,8 @@ TEST_F(TransformProjectStrictTest, YearStrictGreaterThanOrEqual) {
20852086
std::move(projected));
20862087
EXPECT_EQ(unbound_projected->op(), Expression::Operation::kGt);
20872088
EXPECT_EQ(unbound_projected->literals().size(), 1);
2088-
EXPECT_EQ(std::get<int32_t>(unbound_projected->literals().front().value()), 2020);
2089+
EXPECT_EQ(std::get<int32_t>(unbound_projected->literals().front().value()),
2090+
2020 - 1970);
20892091
}
20902092

20912093
TEST_F(TransformProjectStrictTest, YearStrictNotEqual) {
@@ -2109,7 +2111,8 @@ TEST_F(TransformProjectStrictTest, YearStrictNotEqual) {
21092111
std::move(projected));
21102112
EXPECT_EQ(unbound_projected->op(), Expression::Operation::kNotEq);
21112113
EXPECT_EQ(unbound_projected->literals().size(), 1);
2112-
EXPECT_EQ(std::get<int32_t>(unbound_projected->literals().front().value()), 2021);
2114+
EXPECT_EQ(std::get<int32_t>(unbound_projected->literals().front().value()),
2115+
2021 - 1970);
21132116
}
21142117

21152118
TEST_F(TransformProjectStrictTest, MonthStrictLessThan) {
@@ -2218,7 +2221,8 @@ TEST_F(TransformProjectStrictTest, YearStrictUpperBound) {
22182221
std::move(projected));
22192222
EXPECT_EQ(unbound_projected->op(), Expression::Operation::kLt);
22202223
EXPECT_EQ(unbound_projected->literals().size(), 1);
2221-
EXPECT_EQ(std::get<int32_t>(unbound_projected->literals().front().value()), 2018);
2224+
EXPECT_EQ(std::get<int32_t>(unbound_projected->literals().front().value()),
2225+
2018 - 1970);
22222226
}
22232227

22242228
TEST_F(TransformProjectStrictTest, VoidStrictReturnsNull) {

src/iceberg/transform.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,25 +111,25 @@ class ICEBERG_EXPORT Transform : public util::Formattable {
111111

112112
/// \brief Creates a shared singleton instance of the Year transform.
113113
///
114-
/// Extracts the year portion from a date or timestamp.
114+
/// Extracts the number of years from a date or timestamp since the epoch.
115115
/// \return A shared pointer to the Year transform.
116116
static std::shared_ptr<Transform> Year();
117117

118118
/// \brief Creates a shared singleton instance of the Month transform.
119119
///
120-
/// Extracts the month portion from a date or timestamp.
120+
/// Extracts the number of months from a date or timestamp since the epoch.
121121
/// \return A shared pointer to the Month transform.
122122
static std::shared_ptr<Transform> Month();
123123

124124
/// \brief Creates a shared singleton instance of the Day transform.
125125
///
126-
/// Extracts the day portion from a date or timestamp.
126+
/// Extracts the number of days from a date or timestamp since the epoch.
127127
/// \return A shared pointer to the Day transform.
128128
static std::shared_ptr<Transform> Day();
129129

130130
/// \brief Creates a shared singleton instance of the Hour transform.
131131
///
132-
/// Extracts the hour portion from a timestamp.
132+
/// Extracts the number of hours from a timestamp since the epoch.
133133
/// \return A shared pointer to the Hour transform.
134134
static std::shared_ptr<Transform> Hour();
135135

src/iceberg/transform_function.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ class ICEBERG_EXPORT TruncateTransform : public TransformFunction {
100100
int32_t width_;
101101
};
102102

103-
/// \brief Year transform that extracts the year component from timestamp inputs.
103+
/// \brief Year transform that extracts the number of years from timestamp inputs since
104+
/// the epoch.
104105
class ICEBERG_EXPORT YearTransform : public TransformFunction {
105106
public:
106107
/// \param source_type Must be a timestamp type.
@@ -119,7 +120,8 @@ class ICEBERG_EXPORT YearTransform : public TransformFunction {
119120
std::shared_ptr<Type> const& source_type);
120121
};
121122

122-
/// \brief Month transform that extracts the month component from timestamp inputs.
123+
/// \brief Month transform that extracts the number of months from timestamp inputs since
124+
/// the epoch.
123125
class ICEBERG_EXPORT MonthTransform : public TransformFunction {
124126
public:
125127
/// \param source_type Must be a timestamp type.
@@ -138,7 +140,8 @@ class ICEBERG_EXPORT MonthTransform : public TransformFunction {
138140
std::shared_ptr<Type> const& source_type);
139141
};
140142

141-
/// \brief Day transform that extracts the day of the month from timestamp inputs.
143+
/// \brief Day transform that extracts the number of days from timestamp inputs since the
144+
/// epoch.
142145
class ICEBERG_EXPORT DayTransform : public TransformFunction {
143146
public:
144147
/// \param source_type Must be a timestamp type.
@@ -161,7 +164,8 @@ class ICEBERG_EXPORT DayTransform : public TransformFunction {
161164
std::shared_ptr<Type> const& source_type);
162165
};
163166

164-
/// \brief Hour transform that extracts the hour component from timestamp inputs.
167+
/// \brief Hour transform that extracts the number of hours from timestamp inputs since
168+
/// the epoch.
165169
class ICEBERG_EXPORT HourTransform : public TransformFunction {
166170
public:
167171
/// \param source_type Must be a timestamp type.

src/iceberg/util/temporal_util.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ template <>
6868
Result<Literal> ExtractYearImpl<TypeId::kDate>(const Literal& literal) {
6969
auto value = std::get<int32_t>(literal.value());
7070
auto ymd = DateToYmd(value);
71-
return Literal::Int(static_cast<int32_t>(ymd.year()));
71+
return Literal::Int((ymd.year() - kEpochYmd.year()).count());
7272
}
7373

7474
template <>
7575
Result<Literal> ExtractYearImpl<TypeId::kTimestamp>(const Literal& literal) {
7676
auto value = std::get<int64_t>(literal.value());
7777
auto ymd = TimestampToYmd(value);
78-
return Literal::Int(static_cast<int32_t>(ymd.year()));
78+
return Literal::Int((ymd.year() - kEpochYmd.year()).count());
7979
}
8080

8181
template <>

0 commit comments

Comments
 (0)