This is an automated email from the ASF dual-hosted git repository.
wgtmac pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-cpp.git
The following commit(s) were added to refs/heads/main by this push:
new 3219693a fix(transform): compute year and month across the full date
range (#1158)
3219693a is described below
commit 3219693a74d7041b24d8648088643d8493acb301
Author: Junwang Zhao <[email protected]>
AuthorDate: Mon Nov 23 17:54:52 2026 +0800
fix(transform): compute year and month across the full date range (#1158)
Iceberg's year transform counts years since 1970. For 32768-01-01,
the expected result is 32768 - 1970 = 30798. Converting the date to
std::chrono::year can wrap 32768 to -32768, producing -34738 after
subtracting 1970. With only this upper bound, year(d) IN (0, 1) can
then skip a file containing rows from 1970. The month transform has
the same calendar range limit.
Split epoch days into 400-year Gregorian cycles using floor division.
Convert the day within a cycle to a calendar date in 1970 through
2369, then add the cycle offset using 64-bit arithmetic. Both
transforms now handle dates and timestamps on either side of the epoch.
Test the change from 32767 to 32768, both ends of the date and
microsecond timestamp ranges, and nanosecond timestamps just before
the epoch. The regression fails with the previous implementation.
All 431 utility tests and 578 schema tests pass.
---
src/iceberg/test/temporal_util_test.cc | 19 ++++
src/iceberg/util/temporal_util.cc | 165 ++++++++-------------------------
2 files changed, 59 insertions(+), 125 deletions(-)
diff --git a/src/iceberg/test/temporal_util_test.cc
b/src/iceberg/test/temporal_util_test.cc
index 791d02c5..4d9e2d73 100644
--- a/src/iceberg/test/temporal_util_test.cc
+++ b/src/iceberg/test/temporal_util_test.cc
@@ -24,10 +24,29 @@
#include <gtest/gtest.h>
+#include "iceberg/expression/literal.h"
#include "iceberg/test/matchers.h"
namespace iceberg {
+TEST(TemporalUtilTest, YearAndMonthAcrossCalendarRange) {
+ auto check = [](const Literal& value, int year, int month) {
+ ICEBERG_UNWRAP_OR_FAIL(auto years, TemporalUtils::ExtractYear(value));
+ EXPECT_EQ(std::get<int32_t>(years.value()), year - 1970);
+ ICEBERG_UNWRAP_OR_FAIL(auto months, TemporalUtils::ExtractMonth(value));
+ EXPECT_EQ(std::get<int32_t>(months.value()), (year - 1970) * 12 + month -
1);
+ };
+
+ check(Literal::Date(11248737), 32767, 12);
+ check(Literal::Date(11248738), 32768, 1);
+ check(Literal::Date(std::numeric_limits<int32_t>::min()), -5877641, 6);
+ check(Literal::Date(std::numeric_limits<int32_t>::max()), 5881580, 7);
+ check(Literal::Timestamp(std::numeric_limits<int64_t>::min()), -290308, 12);
+ check(Literal::TimestampTz(std::numeric_limits<int64_t>::max()), 294247, 1);
+ check(Literal::TimestampNs(-1), 1969, 12);
+ check(Literal::TimestampTzNs(-1), 1969, 12);
+}
+
TEST(TemporalUtilTest, ParseTimestampNs) {
ICEBERG_UNWRAP_OR_FAIL(
auto nanos,
TemporalUtils::ParseTimestampNs("2026-01-01T00:00:01.000001001"));
diff --git a/src/iceberg/util/temporal_util.cc
b/src/iceberg/util/temporal_util.cc
index 9e6a93cf..7f0f81ce 100644
--- a/src/iceberg/util/temporal_util.cc
+++ b/src/iceberg/util/temporal_util.cc
@@ -164,18 +164,6 @@ Result<int64_t> ParseTimeWithFraction(std::string_view
str, int64_t units_per_se
seconds * units_per_second + frac_units;
}
-inline constexpr year_month_day DateToYmd(int32_t days_since_epoch) {
- return {internal::kEpochDays + days{days_since_epoch}};
-}
-
-inline constexpr year_month_day TimestampToYmd(int64_t micros_since_epoch) {
- return
{floor<days>(sys_time<microseconds>(microseconds{micros_since_epoch}))};
-}
-
-inline constexpr year_month_day TimestampNsToYmd(int64_t nanos_since_epoch) {
- return {floor<days>(sys_time<nanoseconds>(nanoseconds{nanos_since_epoch}))};
-}
-
template <typename Duration>
requires std::is_same_v<Duration, days> || std::is_same_v<Duration, hours>
inline constexpr int32_t TimestampToDuration(int64_t micros_since_epoch) {
@@ -194,84 +182,45 @@ inline constexpr int32_t TimestampNsToDuration(int64_t
nanos_since_epoch) {
.count());
}
-inline constexpr int32_t MonthsSinceEpoch(const year_month_day& ymd) {
- auto delta = ymd.year() - internal::kEpochYmd.year();
- // Calculate the month as months from 1970-01
- // Note: January is month 1, so we subtract 1 to get zero-based month count.
- return static_cast<int32_t>(delta.count() * 12 +
static_cast<unsigned>(ymd.month()) -
- 1);
-}
-
-template <TypeId type_id>
-Result<Literal> ExtractYearImpl(const Literal& literal) {
- std::unreachable();
-}
-
-template <>
-Result<Literal> ExtractYearImpl<TypeId::kDate>(const Literal& literal) {
- auto value = std::get<int32_t>(literal.value());
- auto ymd = DateToYmd(value);
- return Literal::Int((ymd.year() - internal::kEpochYmd.year()).count());
-}
-
-template <>
-Result<Literal> ExtractYearImpl<TypeId::kTimestamp>(const Literal& literal) {
- auto value = std::get<int64_t>(literal.value());
- auto ymd = TimestampToYmd(value);
- return Literal::Int((ymd.year() - internal::kEpochYmd.year()).count());
-}
-
-template <>
-Result<Literal> ExtractYearImpl<TypeId::kTimestampNs>(const Literal& literal) {
- auto value = std::get<int64_t>(literal.value());
- auto ymd = TimestampNsToYmd(value);
- return Literal::Int((ymd.year() - internal::kEpochYmd.year()).count());
-}
-
-template <>
-Result<Literal> ExtractYearImpl<TypeId::kTimestampTz>(const Literal& literal) {
- return ExtractYearImpl<TypeId::kTimestamp>(literal);
-}
-
-template <>
-Result<Literal> ExtractYearImpl<TypeId::kTimestampTzNs>(const Literal&
literal) {
- return ExtractYearImpl<TypeId::kTimestampNs>(literal);
-}
-
-template <TypeId type_id>
-Result<Literal> ExtractMonthImpl(const Literal& literal) {
- std::unreachable();
-}
-
-template <>
-Result<Literal> ExtractMonthImpl<TypeId::kDate>(const Literal& literal) {
- auto value = std::get<int32_t>(literal.value());
- auto ymd = DateToYmd(value);
- return Literal::Int(MonthsSinceEpoch(ymd));
-}
-
-template <>
-Result<Literal> ExtractMonthImpl<TypeId::kTimestamp>(const Literal& literal) {
- auto value = std::get<int64_t>(literal.value());
- auto ymd = TimestampToYmd(value);
- return Literal::Int(MonthsSinceEpoch(ymd));
-}
+struct YearMonth {
+ int32_t years_since_epoch;
+ int32_t month_index;
+};
-template <>
-Result<Literal> ExtractMonthImpl<TypeId::kTimestampNs>(const Literal& literal)
{
- auto value = std::get<int64_t>(literal.value());
- auto ymd = TimestampNsToYmd(value);
- return Literal::Int(MonthsSinceEpoch(ymd));
-}
+Result<YearMonth> GetYearMonth(const Literal& literal, std::string_view unit) {
+ if (literal.IsAboveMax() || literal.IsBelowMin()) [[unlikely]] {
+ return NotSupported("Cannot extract {} from {}", unit, literal.ToString());
+ }
-template <>
-Result<Literal> ExtractMonthImpl<TypeId::kTimestampTz>(const Literal& literal)
{
- return ExtractMonthImpl<TypeId::kTimestamp>(literal);
-}
+ int64_t epoch_days;
+ switch (literal.type()->type_id()) {
+ case TypeId::kDate:
+ epoch_days = std::get<int32_t>(literal.value());
+ break;
+ case TypeId::kTimestamp:
+ case TypeId::kTimestampTz:
+ epoch_days = FloorDiv(std::get<int64_t>(literal.value()),
internal::kMicrosPerDay);
+ break;
+ case TypeId::kTimestampNs:
+ case TypeId::kTimestampTzNs:
+ epoch_days = FloorDiv(std::get<int64_t>(literal.value()),
internal::kNanosPerDay);
+ break;
+ default:
+ return NotSupported("Extract {} from type {} is not supported", unit,
+ literal.type()->ToString());
+ }
-template <>
-Result<Literal> ExtractMonthImpl<TypeId::kTimestampTzNs>(const Literal&
literal) {
- return ExtractMonthImpl<TypeId::kTimestampNs>(literal);
+ // Gregorian dates repeat every 400 years. Only pass days in the first
+ // cycle from 1970 to chrono so the calendar year always fits in
chrono::year.
+ constexpr int64_t kDaysPerCycle = 146097;
+ const auto cycles = FloorDiv(epoch_days, kDaysPerCycle);
+ const auto day_in_cycle = epoch_days - cycles * kDaysPerCycle;
+ const year_month_day ymd{internal::kEpochDays +
+ days{static_cast<int32_t>(day_in_cycle)}};
+ const auto years_in_cycle = (ymd.year() -
internal::kEpochYmd.year()).count();
+ return YearMonth{
+ .years_since_epoch = static_cast<int32_t>(cycles * 400 + years_in_cycle),
+ .month_index = static_cast<int32_t>(static_cast<unsigned>(ymd.month()))
- 1};
}
template <TypeId type_id>
@@ -449,54 +398,20 @@ Result<bool> TemporalUtils::IsUtcOffset(std::string_view
str) {
return timestamp_with_offset.second == 0;
}
-#define DISPATCH_EXTRACT_YEAR(type_id) \
- case type_id: \
- return ExtractYearImpl<type_id>(literal);
-
Result<Literal> TemporalUtils::ExtractYear(const Literal& literal) {
if (literal.IsNull()) [[unlikely]] {
return Literal::Null(int32());
}
-
- if (literal.IsAboveMax() || literal.IsBelowMin()) [[unlikely]] {
- return NotSupported("Cannot extract year from {}", literal.ToString());
- }
-
- switch (literal.type()->type_id()) {
- DISPATCH_EXTRACT_YEAR(TypeId::kDate)
- DISPATCH_EXTRACT_YEAR(TypeId::kTimestamp)
- DISPATCH_EXTRACT_YEAR(TypeId::kTimestampTz)
- DISPATCH_EXTRACT_YEAR(TypeId::kTimestampNs)
- DISPATCH_EXTRACT_YEAR(TypeId::kTimestampTzNs)
- default:
- return NotSupported("Extract year from type {} is not supported",
- literal.type()->ToString());
- }
+ ICEBERG_ASSIGN_OR_RAISE(auto ym, GetYearMonth(literal, "year"));
+ return Literal::Int(ym.years_since_epoch);
}
-#define DISPATCH_EXTRACT_MONTH(type_id) \
- case type_id: \
- return ExtractMonthImpl<type_id>(literal);
-
Result<Literal> TemporalUtils::ExtractMonth(const Literal& literal) {
if (literal.IsNull()) [[unlikely]] {
return Literal::Null(int32());
}
-
- if (literal.IsAboveMax() || literal.IsBelowMin()) [[unlikely]] {
- return NotSupported("Cannot extract month from {}", literal.ToString());
- }
-
- switch (literal.type()->type_id()) {
- DISPATCH_EXTRACT_MONTH(TypeId::kDate)
- DISPATCH_EXTRACT_MONTH(TypeId::kTimestamp)
- DISPATCH_EXTRACT_MONTH(TypeId::kTimestampTz)
- DISPATCH_EXTRACT_MONTH(TypeId::kTimestampNs)
- DISPATCH_EXTRACT_MONTH(TypeId::kTimestampTzNs)
- default:
- return NotSupported("Extract month from type {} is not supported",
- literal.type()->ToString());
- }
+ ICEBERG_ASSIGN_OR_RAISE(auto ym, GetYearMonth(literal, "month"));
+ return Literal::Int(ym.years_since_epoch * 12 + ym.month_index);
}
#define DISPATCH_EXTRACT_DAY(type_id) \