forked from duckdb/duckdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icu-timezone.cpp
358 lines (295 loc) · 12.7 KB
/
icu-timezone.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include "duckdb/common/types/date.hpp"
#include "duckdb/common/types/time.hpp"
#include "duckdb/common/types/timestamp.hpp"
#include "duckdb/function/cast/cast_function_set.hpp"
#include "duckdb/parser/parsed_data/create_scalar_function_info.hpp"
#include "duckdb/parser/parsed_data/create_table_function_info.hpp"
#include "include/icu-datefunc.hpp"
#include "duckdb/transaction/meta_transaction.hpp"
namespace duckdb {
struct ICUTimeZoneData : public GlobalTableFunctionState {
ICUTimeZoneData() : tzs(icu::TimeZone::createEnumeration()) {
UErrorCode status = U_ZERO_ERROR;
std::unique_ptr<icu::Calendar> calendar(icu::Calendar::createInstance(status));
now = calendar->getNow();
}
std::unique_ptr<icu::StringEnumeration> tzs;
UDate now;
};
static unique_ptr<FunctionData> ICUTimeZoneBind(ClientContext &context, TableFunctionBindInput &input,
vector<LogicalType> &return_types, vector<string> &names) {
names.emplace_back("name");
return_types.emplace_back(LogicalType::VARCHAR);
names.emplace_back("abbrev");
return_types.emplace_back(LogicalType::VARCHAR);
names.emplace_back("utc_offset");
return_types.emplace_back(LogicalType::INTERVAL);
names.emplace_back("is_dst");
return_types.emplace_back(LogicalType::BOOLEAN);
return nullptr;
}
static unique_ptr<GlobalTableFunctionState> ICUTimeZoneInit(ClientContext &context, TableFunctionInitInput &input) {
return make_unique<ICUTimeZoneData>();
}
static void ICUTimeZoneFunction(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) {
auto &data = (ICUTimeZoneData &)*data_p.global_state;
idx_t index = 0;
while (index < STANDARD_VECTOR_SIZE) {
UErrorCode status = U_ZERO_ERROR;
auto long_id = data.tzs->snext(status);
if (U_FAILURE(status) || !long_id) {
break;
}
// The LONG name is the one we looked up
std::string utf8;
long_id->toUTF8String(utf8);
output.SetValue(0, index, Value(utf8));
// We don't have the zone tree for determining abbreviated names,
// so the SHORT name is the shortest, lexicographically first equivalent TZ without a slash.
std::string short_id;
long_id->toUTF8String(short_id);
const auto nIDs = icu::TimeZone::countEquivalentIDs(*long_id);
for (int32_t idx = 0; idx < nIDs; ++idx) {
const auto eid = icu::TimeZone::getEquivalentID(*long_id, idx);
if (eid.indexOf(char16_t('/')) >= 0) {
continue;
}
utf8.clear();
eid.toUTF8String(utf8);
if (utf8.size() < short_id.size() || (utf8.size() == short_id.size() && utf8 < short_id)) {
short_id = utf8;
}
}
output.SetValue(1, index, Value(short_id));
std::unique_ptr<icu::TimeZone> tz(icu::TimeZone::createTimeZone(*long_id));
int32_t raw_offset_ms;
int32_t dst_offset_ms;
tz->getOffset(data.now, false, raw_offset_ms, dst_offset_ms, status);
if (U_FAILURE(status)) {
break;
}
output.SetValue(2, index, Value::INTERVAL(Interval::FromMicro(raw_offset_ms * Interval::MICROS_PER_MSEC)));
output.SetValue(3, index, Value(dst_offset_ms != 0));
++index;
}
output.SetCardinality(index);
}
struct ICUFromNaiveTimestamp : public ICUDateFunc {
static inline timestamp_t Operation(icu::Calendar *calendar, timestamp_t naive) {
if (!Timestamp::IsFinite(naive)) {
return naive;
}
// Extract the parts from the "instant"
date_t local_date;
dtime_t local_time;
Timestamp::Convert(naive, local_date, local_time);
int32_t year;
int32_t mm;
int32_t dd;
Date::Convert(local_date, year, mm, dd);
int32_t hr;
int32_t mn;
int32_t secs;
int32_t frac;
Time::Convert(local_time, hr, mn, secs, frac);
int32_t millis = frac / Interval::MICROS_PER_MSEC;
uint64_t micros = frac % Interval::MICROS_PER_MSEC;
// Use them to set the time in the time zone
calendar->set(UCAL_YEAR, year);
calendar->set(UCAL_MONTH, int32_t(mm - 1));
calendar->set(UCAL_DATE, dd);
calendar->set(UCAL_HOUR_OF_DAY, hr);
calendar->set(UCAL_MINUTE, mn);
calendar->set(UCAL_SECOND, secs);
calendar->set(UCAL_MILLISECOND, millis);
return GetTime(calendar, micros);
}
static bool CastFromNaive(Vector &source, Vector &result, idx_t count, CastParameters ¶meters) {
auto &cast_data = (CastData &)*parameters.cast_data;
auto info = (BindData *)cast_data.info.get();
CalendarPtr calendar(info->calendar->clone());
UnaryExecutor::Execute<timestamp_t, timestamp_t>(
source, result, count, [&](timestamp_t input) { return Operation(calendar.get(), input); });
return true;
}
static BoundCastInfo BindCastFromNaive(BindCastInput &input, const LogicalType &source, const LogicalType &target) {
if (!input.context) {
throw InternalException("Missing context for TIMESTAMP to TIMESTAMPTZ cast.");
}
auto cast_data = make_unique<CastData>(make_unique<BindData>(*input.context));
return BoundCastInfo(CastFromNaive, std::move(cast_data));
}
static void AddCasts(ClientContext &context) {
auto &config = DBConfig::GetConfig(context);
auto &casts = config.GetCastFunctions();
casts.RegisterCastFunction(LogicalType::TIMESTAMP, LogicalType::TIMESTAMP_TZ, BindCastFromNaive);
}
};
struct ICUToNaiveTimestamp : public ICUDateFunc {
static inline timestamp_t Operation(icu::Calendar *calendar, timestamp_t instant) {
if (!Timestamp::IsFinite(instant)) {
return instant;
}
// Extract the time zone parts
auto micros = SetTime(calendar, instant);
const auto era = ExtractField(calendar, UCAL_ERA);
const auto year = ExtractField(calendar, UCAL_YEAR);
const auto mm = ExtractField(calendar, UCAL_MONTH) + 1;
const auto dd = ExtractField(calendar, UCAL_DATE);
const auto yyyy = era ? year : (-year + 1);
date_t local_date;
if (!Date::TryFromDate(yyyy, mm, dd, local_date)) {
throw ConversionException("Unable to convert TIMESTAMPTZ to local date");
}
const auto hr = ExtractField(calendar, UCAL_HOUR_OF_DAY);
const auto mn = ExtractField(calendar, UCAL_MINUTE);
const auto secs = ExtractField(calendar, UCAL_SECOND);
const auto millis = ExtractField(calendar, UCAL_MILLISECOND);
micros += millis * Interval::MICROS_PER_MSEC;
dtime_t local_time = Time::FromTime(hr, mn, secs, micros);
timestamp_t naive;
if (!Timestamp::TryFromDatetime(local_date, local_time, naive)) {
throw ConversionException("Unable to convert TIMESTAMPTZ to local TIMESTAMP");
}
return naive;
}
static bool CastToNaive(Vector &source, Vector &result, idx_t count, CastParameters ¶meters) {
auto &cast_data = (CastData &)*parameters.cast_data;
auto info = (BindData *)cast_data.info.get();
CalendarPtr calendar(info->calendar->clone());
UnaryExecutor::Execute<timestamp_t, timestamp_t>(
source, result, count, [&](timestamp_t input) { return Operation(calendar.get(), input); });
return true;
}
static BoundCastInfo BindCastToNaive(BindCastInput &input, const LogicalType &source, const LogicalType &target) {
if (!input.context) {
throw InternalException("Missing context for TIMESTAMPTZ to TIMESTAMP cast.");
}
auto cast_data = make_unique<CastData>(make_unique<BindData>(*input.context));
return BoundCastInfo(CastToNaive, std::move(cast_data));
}
static void AddCasts(ClientContext &context) {
auto &config = DBConfig::GetConfig(context);
auto &casts = config.GetCastFunctions();
casts.RegisterCastFunction(LogicalType::TIMESTAMP_TZ, LogicalType::TIMESTAMP, BindCastToNaive);
}
};
struct ICULocalTimestampFunc : public ICUDateFunc {
struct BindDataNow : public BindData {
explicit BindDataNow(ClientContext &context) : BindData(context) {
now = MetaTransaction::Get(context).start_timestamp;
}
BindDataNow(const BindDataNow &other) : BindData(other), now(other.now) {
}
bool Equals(const FunctionData &other_p) const override {
auto &other = (const BindDataNow &)other_p;
if (now != other.now) {
return false;
}
return BindData::Equals(other_p);
}
unique_ptr<FunctionData> Copy() const override {
return make_unique<BindDataNow>(*this);
}
timestamp_t now;
};
static unique_ptr<FunctionData> BindNow(ClientContext &context, ScalarFunction &bound_function,
vector<unique_ptr<Expression>> &arguments) {
return make_unique<BindDataNow>(context);
}
static timestamp_t GetLocalTimestamp(ExpressionState &state) {
auto &func_expr = (BoundFunctionExpression &)state.expr;
auto &info = (BindDataNow &)*func_expr.bind_info;
CalendarPtr calendar_ptr(info.calendar->clone());
auto calendar = calendar_ptr.get();
const auto now = info.now;
return ICUToNaiveTimestamp::Operation(calendar, now);
}
static void Execute(DataChunk &input, ExpressionState &state, Vector &result) {
D_ASSERT(input.ColumnCount() == 0);
result.SetVectorType(VectorType::CONSTANT_VECTOR);
auto rdata = ConstantVector::GetData<timestamp_t>(result);
rdata[0] = GetLocalTimestamp(state);
}
static void AddFunction(const string &name, ClientContext &context) {
ScalarFunctionSet set(name);
set.AddFunction(ScalarFunction({}, LogicalType::TIMESTAMP, Execute, BindNow));
CreateScalarFunctionInfo func_info(set);
auto &catalog = Catalog::GetSystemCatalog(context);
catalog.AddFunction(context, &func_info);
}
};
struct ICULocalTimeFunc : public ICUDateFunc {
static void Execute(DataChunk &input, ExpressionState &state, Vector &result) {
D_ASSERT(input.ColumnCount() == 0);
result.SetVectorType(VectorType::CONSTANT_VECTOR);
auto rdata = ConstantVector::GetData<dtime_t>(result);
const auto local = ICULocalTimestampFunc::GetLocalTimestamp(state);
rdata[0] = Timestamp::GetTime(local);
}
static void AddFunction(const string &name, ClientContext &context) {
ScalarFunctionSet set(name);
set.AddFunction(ScalarFunction({}, LogicalType::TIME, Execute, ICULocalTimestampFunc::BindNow));
CreateScalarFunctionInfo func_info(set);
auto &catalog = Catalog::GetSystemCatalog(context);
catalog.AddFunction(context, &func_info);
}
};
struct ICUTimeZoneFunc : public ICUDateFunc {
template <typename OP>
static void Execute(DataChunk &input, ExpressionState &state, Vector &result) {
auto &func_expr = (BoundFunctionExpression &)state.expr;
auto &info = (BindData &)*func_expr.bind_info;
CalendarPtr calendar_ptr(info.calendar->clone());
auto calendar = calendar_ptr.get();
// Two cases: constant TZ, variable TZ
D_ASSERT(input.ColumnCount() == 2);
auto &tz_vec = input.data[0];
auto &ts_vec = input.data[1];
if (tz_vec.GetVectorType() == VectorType::CONSTANT_VECTOR) {
if (ConstantVector::IsNull(tz_vec)) {
result.SetVectorType(VectorType::CONSTANT_VECTOR);
ConstantVector::SetNull(result, true);
} else {
SetTimeZone(calendar, *ConstantVector::GetData<string_t>(tz_vec));
UnaryExecutor::Execute<timestamp_t, timestamp_t>(
ts_vec, result, input.size(), [&](timestamp_t ts) { return OP::Operation(calendar, ts); });
}
} else {
BinaryExecutor::Execute<string_t, timestamp_t, timestamp_t>(tz_vec, ts_vec, result, input.size(),
[&](string_t tz_id, timestamp_t ts) {
if (Timestamp::IsFinite(ts)) {
SetTimeZone(calendar, tz_id);
return OP::Operation(calendar, ts);
} else {
return ts;
}
});
}
}
static void AddFunction(const string &name, ClientContext &context) {
ScalarFunctionSet set(name);
set.AddFunction(ScalarFunction({LogicalType::VARCHAR, LogicalType::TIMESTAMP}, LogicalType::TIMESTAMP_TZ,
Execute<ICUFromNaiveTimestamp>, Bind));
set.AddFunction(ScalarFunction({LogicalType::VARCHAR, LogicalType::TIMESTAMP_TZ}, LogicalType::TIMESTAMP,
Execute<ICUToNaiveTimestamp>, Bind));
CreateScalarFunctionInfo func_info(set);
auto &catalog = Catalog::GetSystemCatalog(context);
catalog.AddFunction(context, &func_info);
}
};
void RegisterICUTimeZoneFunctions(ClientContext &context) {
// Table functions
auto &catalog = Catalog::GetSystemCatalog(context);
TableFunction tz_names("pg_timezone_names", {}, ICUTimeZoneFunction, ICUTimeZoneBind, ICUTimeZoneInit);
CreateTableFunctionInfo tz_names_info(std::move(tz_names));
catalog.CreateTableFunction(context, &tz_names_info);
// Scalar functions
ICUTimeZoneFunc::AddFunction("timezone", context);
ICULocalTimestampFunc::AddFunction("current_localtimestamp", context);
ICULocalTimeFunc::AddFunction("current_localtime", context);
// Casts
ICUFromNaiveTimestamp::AddCasts(context);
ICUToNaiveTimestamp::AddCasts(context);
}
} // namespace duckdb