should be it

This commit is contained in:
2025-10-24 19:21:19 -05:00
parent a4b23fc57c
commit f09560c7b1
14047 changed files with 3161551 additions and 1 deletions

View File

@@ -0,0 +1,39 @@
//===----------------------------------------------------------------------===//
// DuckDB
//
// icu-datefunc.hpp
//
//
//===----------------------------------------------------------------------===//
#pragma once
#include "icu-datefunc.hpp"
namespace duckdb {
struct ICUMakeDate : public ICUDateFunc {
static date_t Operation(icu::Calendar *calendar, timestamp_t instant);
static bool CastToDate(Vector &source, Vector &result, idx_t count, CastParameters &parameters);
static BoundCastInfo BindCastToDate(BindCastInput &input, const LogicalType &source, const LogicalType &target);
static void AddCasts(ExtensionLoader &loader);
static date_t ToDate(ClientContext &context, timestamp_t instant);
};
struct ICUToTimeTZ : public ICUDateFunc {
static dtime_tz_t Operation(icu::Calendar *calendar, dtime_tz_t timetz);
static bool ToTimeTZ(icu::Calendar *calendar, timestamp_t instant, dtime_tz_t &result);
static bool CastToTimeTZ(Vector &source, Vector &result, idx_t count, CastParameters &parameters);
static BoundCastInfo BindCastToTimeTZ(BindCastInput &input, const LogicalType &source, const LogicalType &target);
static void AddCasts(ExtensionLoader &loader);
};
} // namespace duckdb

View File

@@ -0,0 +1,17 @@
//===----------------------------------------------------------------------===//
// DuckDB
//
// icu-current.hpp
//
//
//===----------------------------------------------------------------------===//
#pragma once
namespace duckdb {
class ExtensionLoader;
void RegisterICUCurrentFunctions(ExtensionLoader &loader);
} // namespace duckdb

View File

@@ -0,0 +1,17 @@
//===----------------------------------------------------------------------===//
// DuckDB
//
// icu-dateadd.hpp
//
//
//===----------------------------------------------------------------------===//
#pragma once
namespace duckdb {
class ExtensionLoader;
void RegisterICUDateAddFunctions(ExtensionLoader &loader);
} // namespace duckdb

View File

@@ -0,0 +1,87 @@
//===----------------------------------------------------------------------===//
// DuckDB
//
// icu-datefunc.hpp
//
//
//===----------------------------------------------------------------------===//
#pragma once
#include "duckdb.hpp"
#include "duckdb/common/enums/date_part_specifier.hpp"
#include "duckdb/planner/expression/bound_function_expression.hpp"
#include "tz_calendar.hpp"
namespace duckdb {
struct ICUDateFunc {
struct BindData : public FunctionData {
explicit BindData(ClientContext &context);
BindData(const string &tz_setting, const string &cal_setting);
BindData(const BindData &other);
string tz_setting;
string cal_setting;
CalendarPtr calendar;
bool Equals(const FunctionData &other_p) const override;
duckdb::unique_ptr<FunctionData> Copy() const override;
void InitCalendar();
};
struct CastData : public BoundCastData {
explicit CastData(duckdb::unique_ptr<FunctionData> info_p) : info(std::move(info_p)) {
}
duckdb::unique_ptr<BoundCastData> Copy() const override {
return make_uniq<CastData>(info->Copy());
}
duckdb::unique_ptr<FunctionData> info;
};
//! Binds a default calendar object for use by the function
static duckdb::unique_ptr<FunctionData> Bind(ClientContext &context, ScalarFunction &bound_function,
vector<duckdb::unique_ptr<Expression>> &arguments);
//! Tries to set the time zone for the calendar and returns false if it is not valid.
static bool TrySetTimeZone(icu::Calendar *calendar, const string_t &tz_id);
//! Sets the time zone for the calendar. Throws if it is not valid
static void SetTimeZone(icu::Calendar *calendar, const string_t &tz_id, string *error_message = nullptr);
//! Gets the timestamp from the calendar, throwing if it is not in range.
static bool TryGetTime(icu::Calendar *calendar, uint64_t micros, timestamp_t &result);
//! Gets the timestamp from the calendar, throwing if it is not in range.
static timestamp_t GetTime(icu::Calendar *calendar, uint64_t micros = 0);
//! Gets the timestamp from the calendar, assuming it is in range.
static timestamp_t GetTimeUnsafe(icu::Calendar *calendar, uint64_t micros = 0);
//! Sets the calendar to the timestamp, returning the unused µs part
static uint64_t SetTime(icu::Calendar *calendar, timestamp_t date);
//! Extracts the field from the calendar
static int32_t ExtractField(icu::Calendar *calendar, UCalendarDateFields field);
//! Subtracts the field of the given date from the calendar
static int32_t SubtractField(icu::Calendar *calendar, UCalendarDateFields field, timestamp_t end_date);
//! Adds the timestamp and the interval using the calendar
static timestamp_t Add(TZCalendar &calendar, timestamp_t timestamp, interval_t interval);
//! Subtracts the interval from the timestamp using the calendar
static timestamp_t Sub(TZCalendar &calendar, timestamp_t timestamp, interval_t interval);
//! Subtracts the latter timestamp from the former timestamp using the calendar
static interval_t Sub(TZCalendar &calendar, timestamp_t end_date, timestamp_t start_date);
//! Pulls out the bin values from the timestamp assuming it is an instant,
//! constructs an ICU timestamp, and then converts that back to a DuckDB instant
//! Adding offset doesn't really work around DST because the bin values are ambiguous
static timestamp_t FromNaive(icu::Calendar *calendar, timestamp_t naive);
//! Truncates the calendar time to the given part precision
typedef void (*part_trunc_t)(icu::Calendar *calendar, uint64_t &micros);
static part_trunc_t TruncationFactory(DatePartSpecifier part);
static timestamp_t CurrentMidnight(icu::Calendar *calendar, ExpressionState &state);
//! Subtracts the two times at the given part precision
typedef int64_t (*part_sub_t)(icu::Calendar *calendar, timestamp_t start_date, timestamp_t end_date);
static part_sub_t SubtractFactory(DatePartSpecifier part);
};
} // namespace duckdb

View File

@@ -0,0 +1,17 @@
//===----------------------------------------------------------------------===//
// DuckDB
//
// icu-datepart.hpp
//
//
//===----------------------------------------------------------------------===//
#pragma once
namespace duckdb {
class ExtensionLoader;
void RegisterICUDatePartFunctions(ExtensionLoader &loader);
} // namespace duckdb

View File

@@ -0,0 +1,17 @@
//===----------------------------------------------------------------------===//
// DuckDB
//
// icu-datediff.hpp
//
//
//===----------------------------------------------------------------------===//
#pragma once
namespace duckdb {
class ExtensionLoader;
void RegisterICUDateSubFunctions(ExtensionLoader &loader);
} // namespace duckdb

View File

@@ -0,0 +1,17 @@
//===----------------------------------------------------------------------===//
// DuckDB
//
// icu-datetrunc.hpp
//
//
//===----------------------------------------------------------------------===//
#pragma once
namespace duckdb {
class ExtensionLoader;
void RegisterICUDateTruncFunctions(ExtensionLoader &loader);
} // namespace duckdb

View File

@@ -0,0 +1,28 @@
//===----------------------------------------------------------------------===//
// DuckDB
//
// icu-helpers.hpp
//
//
//===----------------------------------------------------------------------===//
#pragma once
#include "duckdb.hpp"
#include "unicode/timezone.h"
#include "duckdb/common/types/timestamp.hpp"
namespace duckdb {
struct ICUHelpers {
//! Tries to get a time zone - returns nullptr if the timezone is not found
static unique_ptr<icu::TimeZone> TryGetTimeZone(string &tz_str);
//! Gets a time zone - throws an error if the timezone is not found
static unique_ptr<icu::TimeZone> GetTimeZone(string &tz_str, string *error_message = nullptr);
static TimestampComponents GetComponents(timestamp_tz_t ts, icu::Calendar *calendar);
static timestamp_t ToTimestamp(TimestampComponents data);
};
} // namespace duckdb

View File

@@ -0,0 +1,17 @@
//===----------------------------------------------------------------------===//
// DuckDB
//
// icu-list-range.hpp
//
//
//===----------------------------------------------------------------------===//
#pragma once
namespace duckdb {
class ExtensionLoader;
void RegisterICUListRangeFunctions(ExtensionLoader &loader);
} // namespace duckdb

View File

@@ -0,0 +1,17 @@
//===----------------------------------------------------------------------===//
// DuckDB
//
// icu-makedate.hpp
//
//
//===----------------------------------------------------------------------===//
#pragma once
namespace duckdb {
class ExtensionLoader;
void RegisterICUMakeDateFunctions(ExtensionLoader &loader);
} // namespace duckdb

View File

@@ -0,0 +1,17 @@
//===----------------------------------------------------------------------===//
// DuckDB
//
// icu-strptime.hpp
//
//
//===----------------------------------------------------------------------===//
#pragma once
namespace duckdb {
class ExtensionLoader;
void RegisterICUStrptimeFunctions(ExtensionLoader &loader);
} // namespace duckdb

View File

@@ -0,0 +1,17 @@
//===----------------------------------------------------------------------===//
// DuckDB
//
// icu-table-range.hpp
//
//
//===----------------------------------------------------------------------===//
#pragma once
namespace duckdb {
class ExtensionLoader;
void RegisterICUTableRangeFunctions(ExtensionLoader &loader);
} // namespace duckdb

View File

@@ -0,0 +1,17 @@
//===----------------------------------------------------------------------===//
// DuckDB
//
// icu-timebucket.hpp
//
//
//===----------------------------------------------------------------------===//
#pragma once
namespace duckdb {
class ExtensionLoader;
void RegisterICUTimeBucketFunctions(ExtensionLoader &loader);
} // namespace duckdb

View File

@@ -0,0 +1,17 @@
//===----------------------------------------------------------------------===//
// DuckDB
//
// icu-timezone.hpp
//
//
//===----------------------------------------------------------------------===//
#pragma once
namespace duckdb {
class ExtensionLoader;
void RegisterICUTimeZoneFunctions(ExtensionLoader &loader);
} // namespace duckdb

View File

@@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
// DuckDB
//
// icu_extension.hpp
//
//
//===----------------------------------------------------------------------===//
#pragma once
#include "duckdb.hpp"
namespace duckdb {
class IcuExtension : public Extension {
public:
void Load(ExtensionLoader &loader) override;
std::string Name() override;
std::string Version() const override;
};
} // namespace duckdb

View File

@@ -0,0 +1,40 @@
//===----------------------------------------------------------------------===//
// DuckDB
//
// tz_calendar.hpp
//
//
//===----------------------------------------------------------------------===//
#pragma once
#include "unicode/calendar.h"
#include "duckdb/common/string_util.hpp"
namespace duckdb {
using CalendarPtr = duckdb::unique_ptr<icu::Calendar>;
struct TZCalendar {
TZCalendar(icu::Calendar &calendar_p, const string &cal_setting)
: calendar(CalendarPtr(calendar_p.clone())),
is_gregorian(cal_setting.empty() || StringUtil::CIEquals(cal_setting, "gregorian")),
supports_intervals(calendar->getMaximum(UCAL_MONTH) < 12) { // 0-based
}
icu::Calendar *GetICUCalendar() {
return calendar.get();
}
bool IsGregorian() const {
return is_gregorian;
}
bool SupportsIntervals() const {
return supports_intervals;
}
CalendarPtr calendar;
const bool is_gregorian;
const bool supports_intervals;
};
} // namespace duckdb