duckdb 0.6.2-dev908.0 → 0.6.2-dev912.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "duckdb",
3
3
  "main": "./lib/duckdb.js",
4
4
  "types": "./lib/duckdb.d.ts",
5
- "version": "0.6.2-dev908.0",
5
+ "version": "0.6.2-dev912.0",
6
6
  "description": "DuckDB node.js API",
7
7
  "gypfile": true,
8
8
  "dependencies": {
@@ -1,6 +1,7 @@
1
1
  #include "duckdb/common/types/date.hpp"
2
2
  #include "duckdb/common/types/time.hpp"
3
3
  #include "duckdb/common/types/timestamp.hpp"
4
+ #include "duckdb/function/cast/cast_function_set.hpp"
4
5
  #include "duckdb/parser/parsed_data/create_scalar_function_info.hpp"
5
6
  #include "duckdb/parser/parsed_data/create_table_function_info.hpp"
6
7
  #include "include/icu-datefunc.hpp"
@@ -85,12 +86,16 @@ static void ICUTimeZoneFunction(ClientContext &context, TableFunctionInput &data
85
86
  output.SetCardinality(index);
86
87
  }
87
88
 
88
- struct ICUFromLocalTimestamp : public ICUDateFunc {
89
- static inline timestamp_t Operation(icu::Calendar *calendar, timestamp_t local) {
89
+ struct ICUFromNaiveTimestamp : public ICUDateFunc {
90
+ static inline timestamp_t Operation(icu::Calendar *calendar, timestamp_t naive) {
91
+ if (!Timestamp::IsFinite(naive)) {
92
+ return naive;
93
+ }
94
+
90
95
  // Extract the parts from the "instant"
91
96
  date_t local_date;
92
97
  dtime_t local_time;
93
- Timestamp::Convert(local, local_date, local_time);
98
+ Timestamp::Convert(naive, local_date, local_time);
94
99
 
95
100
  int32_t year;
96
101
  int32_t mm;
@@ -116,19 +121,52 @@ struct ICUFromLocalTimestamp : public ICUDateFunc {
116
121
 
117
122
  return GetTime(calendar, micros);
118
123
  }
124
+
125
+ static bool CastFromNaive(Vector &source, Vector &result, idx_t count, CastParameters &parameters) {
126
+ auto &cast_data = (CastData &)*parameters.cast_data;
127
+ auto info = (BindData *)cast_data.info.get();
128
+ CalendarPtr calendar(info->calendar->clone());
129
+
130
+ UnaryExecutor::Execute<timestamp_t, timestamp_t>(
131
+ source, result, count, [&](timestamp_t input) { return Operation(calendar.get(), input); });
132
+ return true;
133
+ }
134
+
135
+ static BoundCastInfo BindCastFromNaive(BindCastInput &input, const LogicalType &source, const LogicalType &target) {
136
+ if (!input.context) {
137
+ throw InternalException("Missing context for TIMESTAMP to TIMESTAMPTZ cast.");
138
+ }
139
+
140
+ auto cast_data = make_unique<CastData>(make_unique<BindData>(*input.context));
141
+
142
+ return BoundCastInfo(CastFromNaive, move(cast_data));
143
+ }
144
+
145
+ static void AddCasts(ClientContext &context) {
146
+ auto &config = DBConfig::GetConfig(context);
147
+ auto &casts = config.GetCastFunctions();
148
+
149
+ casts.RegisterCastFunction(LogicalType::TIMESTAMP, LogicalType::TIMESTAMP_TZ, BindCastFromNaive);
150
+ }
119
151
  };
120
152
 
121
- struct ICUToLocalTimestamp : public ICUDateFunc {
153
+ struct ICUToNaiveTimestamp : public ICUDateFunc {
122
154
  static inline timestamp_t Operation(icu::Calendar *calendar, timestamp_t instant) {
155
+ if (!Timestamp::IsFinite(instant)) {
156
+ return instant;
157
+ }
158
+
123
159
  // Extract the time zone parts
124
160
  auto micros = SetTime(calendar, instant);
161
+ const auto era = ExtractField(calendar, UCAL_ERA);
125
162
  const auto year = ExtractField(calendar, UCAL_YEAR);
126
163
  const auto mm = ExtractField(calendar, UCAL_MONTH) + 1;
127
164
  const auto dd = ExtractField(calendar, UCAL_DATE);
128
165
 
166
+ const auto yyyy = era ? year : (-year + 1);
129
167
  date_t local_date;
130
- if (!Date::TryFromDate(year, mm, dd, local_date)) {
131
- throw ConversionException("Unable to create local date in TIMEZONE function");
168
+ if (!Date::TryFromDate(yyyy, mm, dd, local_date)) {
169
+ throw ConversionException("Unable to convert TIMESTAMPTZ to local date");
132
170
  }
133
171
 
134
172
  const auto hr = ExtractField(calendar, UCAL_HOUR_OF_DAY);
@@ -139,12 +177,39 @@ struct ICUToLocalTimestamp : public ICUDateFunc {
139
177
  micros += millis * Interval::MICROS_PER_MSEC;
140
178
  dtime_t local_time = Time::FromTime(hr, mn, secs, micros);
141
179
 
142
- timestamp_t result;
143
- if (!Timestamp::TryFromDatetime(local_date, local_time, result)) {
144
- throw ConversionException("Unable to create local timestamp in TIMEZONE function");
180
+ timestamp_t naive;
181
+ if (!Timestamp::TryFromDatetime(local_date, local_time, naive)) {
182
+ throw ConversionException("Unable to convert TIMESTAMPTZ to local TIMESTAMP");
145
183
  }
146
184
 
147
- return result;
185
+ return naive;
186
+ }
187
+
188
+ static bool CastToNaive(Vector &source, Vector &result, idx_t count, CastParameters &parameters) {
189
+ auto &cast_data = (CastData &)*parameters.cast_data;
190
+ auto info = (BindData *)cast_data.info.get();
191
+ CalendarPtr calendar(info->calendar->clone());
192
+
193
+ UnaryExecutor::Execute<timestamp_t, timestamp_t>(
194
+ source, result, count, [&](timestamp_t input) { return Operation(calendar.get(), input); });
195
+ return true;
196
+ }
197
+
198
+ static BoundCastInfo BindCastToNaive(BindCastInput &input, const LogicalType &source, const LogicalType &target) {
199
+ if (!input.context) {
200
+ throw InternalException("Missing context for TIMESTAMPTZ to TIMESTAMP cast.");
201
+ }
202
+
203
+ auto cast_data = make_unique<CastData>(make_unique<BindData>(*input.context));
204
+
205
+ return BoundCastInfo(CastToNaive, move(cast_data));
206
+ }
207
+
208
+ static void AddCasts(ClientContext &context) {
209
+ auto &config = DBConfig::GetConfig(context);
210
+ auto &casts = config.GetCastFunctions();
211
+
212
+ casts.RegisterCastFunction(LogicalType::TIMESTAMP_TZ, LogicalType::TIMESTAMP, BindCastToNaive);
148
213
  }
149
214
  };
150
215
 
@@ -186,7 +251,7 @@ struct ICULocalTimestampFunc : public ICUDateFunc {
186
251
  auto calendar = calendar_ptr.get();
187
252
 
188
253
  const auto now = info.now;
189
- return ICUToLocalTimestamp::Operation(calendar, now);
254
+ return ICUToNaiveTimestamp::Operation(calendar, now);
190
255
  }
191
256
 
192
257
  static void Execute(DataChunk &input, ExpressionState &state, Vector &result) {
@@ -244,13 +309,8 @@ struct ICUTimeZoneFunc : public ICUDateFunc {
244
309
  ConstantVector::SetNull(result, true);
245
310
  } else {
246
311
  SetTimeZone(calendar, *ConstantVector::GetData<string_t>(tz_vec));
247
- UnaryExecutor::Execute<timestamp_t, timestamp_t>(ts_vec, result, input.size(), [&](timestamp_t ts) {
248
- if (Timestamp::IsFinite(ts)) {
249
- return OP::Operation(calendar, ts);
250
- } else {
251
- return ts;
252
- }
253
- });
312
+ UnaryExecutor::Execute<timestamp_t, timestamp_t>(
313
+ ts_vec, result, input.size(), [&](timestamp_t ts) { return OP::Operation(calendar, ts); });
254
314
  }
255
315
  } else {
256
316
  BinaryExecutor::Execute<string_t, timestamp_t, timestamp_t>(tz_vec, ts_vec, result, input.size(),
@@ -268,9 +328,9 @@ struct ICUTimeZoneFunc : public ICUDateFunc {
268
328
  static void AddFunction(const string &name, ClientContext &context) {
269
329
  ScalarFunctionSet set(name);
270
330
  set.AddFunction(ScalarFunction({LogicalType::VARCHAR, LogicalType::TIMESTAMP}, LogicalType::TIMESTAMP_TZ,
271
- Execute<ICUFromLocalTimestamp>, Bind));
331
+ Execute<ICUFromNaiveTimestamp>, Bind));
272
332
  set.AddFunction(ScalarFunction({LogicalType::VARCHAR, LogicalType::TIMESTAMP_TZ}, LogicalType::TIMESTAMP,
273
- Execute<ICUToLocalTimestamp>, Bind));
333
+ Execute<ICUToNaiveTimestamp>, Bind));
274
334
 
275
335
  CreateScalarFunctionInfo func_info(set);
276
336
  auto &catalog = Catalog::GetSystemCatalog(context);
@@ -279,14 +339,20 @@ struct ICUTimeZoneFunc : public ICUDateFunc {
279
339
  };
280
340
 
281
341
  void RegisterICUTimeZoneFunctions(ClientContext &context) {
342
+ // Table functions
282
343
  auto &catalog = Catalog::GetSystemCatalog(context);
283
344
  TableFunction tz_names("pg_timezone_names", {}, ICUTimeZoneFunction, ICUTimeZoneBind, ICUTimeZoneInit);
284
345
  CreateTableFunctionInfo tz_names_info(move(tz_names));
285
346
  catalog.CreateTableFunction(context, &tz_names_info);
286
347
 
348
+ // Scalar functions
287
349
  ICUTimeZoneFunc::AddFunction("timezone", context);
288
350
  ICULocalTimestampFunc::AddFunction("current_localtimestamp", context);
289
351
  ICULocalTimeFunc::AddFunction("current_localtime", context);
352
+
353
+ // Casts
354
+ ICUFromNaiveTimestamp::AddCasts(context);
355
+ ICUToNaiveTimestamp::AddCasts(context);
290
356
  }
291
357
 
292
358
  } // namespace duckdb
@@ -1674,6 +1674,9 @@ vector<idx_t> ListVector::Search(Vector &list, const Value &key, idx_t row) {
1674
1674
  case PhysicalType::DOUBLE:
1675
1675
  TemplatedSearchInMap<double>(list, key, offsets, key.IsNull(), entry.offset, entry.length);
1676
1676
  break;
1677
+ case PhysicalType::INTERVAL:
1678
+ TemplatedSearchInMap<interval_t>(list, key, offsets, key.IsNull(), entry.offset, entry.length);
1679
+ break;
1677
1680
  case PhysicalType::VARCHAR:
1678
1681
  SearchStringInMap(list, StringValue::Get(key), offsets, key.IsNull(), entry.offset, entry.length);
1679
1682
  break;
@@ -1,8 +1,8 @@
1
1
  #ifndef DUCKDB_VERSION
2
- #define DUCKDB_VERSION "0.6.2-dev908"
2
+ #define DUCKDB_VERSION "0.6.2-dev912"
3
3
  #endif
4
4
  #ifndef DUCKDB_SOURCE_ID
5
- #define DUCKDB_SOURCE_ID "c3791b63d0"
5
+ #define DUCKDB_SOURCE_ID "5c2a4ce026"
6
6
  #endif
7
7
  #include "duckdb/function/table/system_functions.hpp"
8
8
  #include "duckdb/main/database.hpp"