-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathpython_objects.cpp
More file actions
740 lines (678 loc) · 24.5 KB
/
Copy pathpython_objects.cpp
File metadata and controls
740 lines (678 loc) · 24.5 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
#include "duckdb_python/python_objects.hpp"
#include "duckdb/common/types.hpp"
#include "duckdb/common/types/uuid.hpp"
#include "duckdb/common/types/value.hpp"
#include "duckdb/common/types/decimal.hpp"
#include "duckdb/common/types/bit.hpp"
#include "duckdb/common/operator/cast_operators.hpp"
#include "duckdb_python/pyconnection/pyconnection.hpp"
#include "duckdb/common/operator/add.hpp"
#include "duckdb/common/types/bignum.hpp"
#include "duckdb/function/to_interval.hpp"
#include "datetime.h" // Python datetime initialize #1
#include <duckdb/common/types/variant.hpp>
#include <duckdb/function/scalar/variant_utils.hpp>
namespace duckdb {
PyDictionary::PyDictionary(nb::object dict) {
keys = nb::list(dict.attr("keys")());
values = nb::list(dict.attr("values")());
len = nb::len(keys);
this->dict = std::move(dict);
}
PyTimeDelta::PyTimeDelta(nb::handle &obj) {
days = PyTimeDelta::GetDays(obj);
seconds = PyTimeDelta::GetSeconds(obj);
microseconds = PyTimeDelta::GetMicros(obj);
}
interval_t PyTimeDelta::ToInterval() {
interval_t result;
auto micros_interval = Interval::FromMicro(microseconds);
auto days_interval = interval_t {/*months = */ 0,
/*days = */ days,
/*micros = */ 0};
auto seconds_interval = ToSecondsOperator::Operation<int64_t, interval_t>(seconds);
result = AddOperator::Operation<interval_t, interval_t, interval_t>(micros_interval, days_interval);
result = AddOperator::Operation<interval_t, interval_t, interval_t>(result, seconds_interval);
return result;
}
int64_t PyTimeDelta::GetDays(nb::handle &obj) {
// nb::object wrap: nb::int_() of a bare .attr() accessor is an ambiguous overload on MSVC.
return nb::cast<int64_t>(nb::int_(nb::object(obj.attr("days"))));
}
int64_t PyTimeDelta::GetSeconds(nb::handle &obj) {
return nb::cast<int64_t>(nb::int_(nb::object(obj.attr("seconds"))));
}
int64_t PyTimeDelta::GetMicros(nb::handle &obj) {
return nb::cast<int64_t>(nb::int_(nb::object(obj.attr("microseconds"))));
}
PyDecimal::PyDecimal(nb::handle &obj) : obj(obj) {
auto as_tuple = obj.attr("as_tuple")();
nb::object exponent = as_tuple.attr("exponent");
SetExponent(exponent);
auto sign = nb::cast<int8_t>(as_tuple.attr("sign"));
signed_value = sign != 0;
nb::object decimal_digits = as_tuple.attr("digits");
auto width = nb::len(decimal_digits);
digits.reserve(width);
for (auto digit : decimal_digits) {
digits.push_back(nb::cast<uint8_t>(digit));
}
}
bool PyDecimal::TryGetType(LogicalType &type) {
int32_t width = digits.size();
switch (exponent_type) {
case PyDecimalExponentType::EXPONENT_SCALE: {
auto scale = exponent_value;
if (scale > width) {
// The value starts with 1 or more zeros, which are optimized out of the 'digits' array
// 0.001; width=1, exponent=-3
width = scale + 1; // DECIMAL(4,3) - add 1 for the non-decimal values
}
if (width > Decimal::MAX_WIDTH_INT128) {
type = LogicalType::DOUBLE;
return true;
}
type = LogicalType::DECIMAL(width, scale);
return true;
}
case PyDecimalExponentType::EXPONENT_POWER: {
// Positive exponent: integer with extra trailing zeros, scale 0.
if (exponent_value > Decimal::MAX_WIDTH_INT128 || width > Decimal::MAX_WIDTH_INT128 - exponent_value) {
type = LogicalType::DOUBLE;
return true;
}
width += exponent_value;
type = LogicalType::DECIMAL(width, 0);
return true;
}
case PyDecimalExponentType::EXPONENT_INFINITY: {
type = LogicalType::FLOAT;
return true;
}
case PyDecimalExponentType::EXPONENT_NAN: {
type = LogicalType::FLOAT;
return true;
}
default: // LCOV_EXCL_START
throw NotImplementedException("case not implemented for type PyDecimalExponentType");
} // LCOV_EXCL_STOP
}
// LCOV_EXCL_START
static void ExponentNotRecognized() {
throw NotImplementedException("Failed to convert decimal.Decimal value, exponent type is unknown");
}
// LCOV_EXCL_STOP
void PyDecimal::SetExponent(nb::handle &exponent) {
if (nb::isinstance<nb::int_>(exponent)) {
this->exponent_value = nb::cast<int32_t>(exponent);
if (this->exponent_value >= 0) {
exponent_type = PyDecimalExponentType::EXPONENT_POWER;
return;
}
exponent_value *= -1;
exponent_type = PyDecimalExponentType::EXPONENT_SCALE;
return;
}
if (nb::isinstance<nb::str>(exponent)) {
string exponent_string = nb::cast<std::string>(nb::str(exponent));
if (exponent_string == "n") {
exponent_type = PyDecimalExponentType::EXPONENT_NAN;
return;
}
if (exponent_string == "F") {
exponent_type = PyDecimalExponentType::EXPONENT_INFINITY;
return;
}
}
// LCOV_EXCL_START
ExponentNotRecognized();
// LCOV_EXCL_STOP
}
static bool WidthFitsInDecimal(int32_t width) {
return width >= 0 && width <= Decimal::MAX_WIDTH_DECIMAL;
}
template <class OP>
Value PyDecimalCastSwitch(PyDecimal &decimal, uint8_t width, uint8_t scale) {
if (width > DecimalWidth<int64_t>::max) {
return OP::template Operation<hugeint_t>(decimal.signed_value, decimal.digits, width, scale);
}
if (width > DecimalWidth<int32_t>::max) {
return OP::template Operation<int64_t>(decimal.signed_value, decimal.digits, width, scale);
}
if (width > DecimalWidth<int16_t>::max) {
return OP::template Operation<int32_t>(decimal.signed_value, decimal.digits, width, scale);
}
return OP::template Operation<int16_t>(decimal.signed_value, decimal.digits, width, scale);
}
// Wont fit in a DECIMAL, fall back to DOUBLE
static Value CastToDouble(nb::handle &obj) {
string converted = nb::cast<std::string>(nb::str(obj));
string_t decimal_string(converted);
double double_val;
bool try_cast = TryCast::Operation<string_t, double>(decimal_string, double_val, true);
(void)try_cast;
D_ASSERT(try_cast);
return Value::DOUBLE(double_val);
}
Value PyDecimal::ToDuckValue() {
int32_t width = digits.size();
if (!WidthFitsInDecimal(width)) {
return CastToDouble(obj);
}
switch (exponent_type) {
case PyDecimalExponentType::EXPONENT_SCALE: {
uint8_t scale = exponent_value;
D_ASSERT(WidthFitsInDecimal(width));
if (scale > width) {
// Values like '0.001'
width = scale + 1; // leave 1 room for the non-decimal value
}
if (!WidthFitsInDecimal(width)) {
return CastToDouble(obj);
}
return PyDecimalCastSwitch<PyDecimalScaleConverter>(*this, width, scale);
}
case PyDecimalExponentType::EXPONENT_POWER: {
// Fold 10^exponent into the mantissa and store scale 0. Using the exponent as a
// DECIMAL scale cancelled the 10^n multiply and stored only the mantissa (1E+2 -> 1).
if (exponent_value > Decimal::MAX_WIDTH_DECIMAL || width > Decimal::MAX_WIDTH_DECIMAL - exponent_value) {
return CastToDouble(obj);
}
digits.insert(digits.end(), static_cast<size_t>(exponent_value), static_cast<uint8_t>(0));
width += exponent_value;
D_ASSERT(WidthFitsInDecimal(width));
return PyDecimalCastSwitch<PyDecimalScaleConverter>(*this, width, 0);
}
case PyDecimalExponentType::EXPONENT_NAN: {
return Value::FLOAT(NAN);
}
case PyDecimalExponentType::EXPONENT_INFINITY: {
return Value::FLOAT(INFINITY);
}
// LCOV_EXCL_START
default: {
throw NotImplementedException("case not implemented for type PyDecimalExponentType");
} // LCOV_EXCL_STOP
}
}
PyTime::PyTime(nb::handle &obj) : obj(obj) {
hour = PyTime::GetHours(obj); // NOLINT
minute = PyTime::GetMinutes(obj); // NOLINT
second = PyTime::GetSeconds(obj); // NOLINT
microsecond = PyTime::GetMicros(obj); // NOLINT
timezone_obj = PyTime::GetTZInfo(obj); // NOLINT
}
dtime_t PyTime::ToDuckTime() {
return Time::FromTime(hour, minute, second, microsecond);
}
Value PyTime::ToDuckValue() {
auto duckdb_time = this->ToDuckTime();
if (!nb::none().is(this->timezone_obj)) {
auto seconds = PyTimezone::GetUTCOffsetSeconds(this->timezone_obj);
return Value::TIMETZ(dtime_tz_t(duckdb_time, seconds));
}
return Value::TIME(duckdb_time);
}
int32_t PyTime::GetHours(nb::handle &obj) {
return PyDateTime_TIME_GET_HOUR(obj.ptr()); // NOLINT
}
int32_t PyTime::GetMinutes(nb::handle &obj) {
return PyDateTime_TIME_GET_MINUTE(obj.ptr()); // NOLINT
}
int32_t PyTime::GetSeconds(nb::handle &obj) {
return PyDateTime_TIME_GET_SECOND(obj.ptr()); // NOLINT
}
int32_t PyTime::GetMicros(nb::handle &obj) {
return PyDateTime_TIME_GET_MICROSECOND(obj.ptr()); // NOLINT
}
nb::object PyTime::GetTZInfo(nb::handle &obj) {
// The object returned is borrowed, there is no reference to steal
return nb::borrow<nb::object>(PyDateTime_TIME_GET_TZINFO(obj.ptr())); // NOLINT
}
interval_t PyTimezone::GetUTCOffset(nb::handle &datetime, nb::handle &tzone_obj) {
// The datetime object is provided because the utcoffset could be ambiguous
auto res = tzone_obj.attr("utcoffset")(datetime);
auto timedelta = PyTimeDelta(res);
return timedelta.ToInterval();
}
int32_t PyTimezone::GetUTCOffsetSeconds(nb::handle &tzone_obj) {
// We should be able to use None here, the tzone_obj of a datetime.time should never be ambiguous
auto res = tzone_obj.attr("utcoffset")(nb::none());
auto timedelta = PyTimeDelta(res);
if (timedelta.days != 0) {
throw InvalidInputException(
"Failed to convert 'tzinfo' object, utcoffset returned an invalid timedelta (days)");
}
if (timedelta.microseconds != 0) {
throw InvalidInputException(
"Failed to convert 'tzinfo' object, utcoffset returned an invalid timedelta (microseconds)");
}
return timedelta.seconds;
}
PyDateTime::PyDateTime(nb::handle &obj) : obj(obj) {
year = PyDateTime::GetYears(obj);
month = PyDateTime::GetMonths(obj);
day = PyDateTime::GetDays(obj);
hour = PyDateTime::GetHours(obj);
minute = PyDateTime::GetMinutes(obj);
second = PyDateTime::GetSeconds(obj);
micros = PyDateTime::GetMicros(obj);
tzone_obj = PyDateTime::GetTZInfo(obj);
}
timestamp_t PyDateTime::ToTimestamp() {
auto date = ToDate();
auto time = ToDuckTime();
return Timestamp::FromDatetime(date, time);
}
Value PyDateTime::ToDuckValue(const LogicalType &target_type) {
auto timestamp = ToTimestamp();
if (!nb::none().is(tzone_obj)) {
auto utc_offset = PyTimezone::GetUTCOffset(obj, tzone_obj);
// Need to subtract the UTC offset, so we invert the interval
utc_offset = Interval::Invert(utc_offset);
timestamp = Interval::Add(timestamp, utc_offset);
return Value::TIMESTAMPTZ(timestamp_tz_t(timestamp));
}
switch (target_type.id()) {
case LogicalTypeId::UNKNOWN:
case LogicalTypeId::TIMESTAMP: {
return Value::TIMESTAMP(timestamp);
}
case LogicalTypeId::TIMESTAMP_SEC:
case LogicalTypeId::TIMESTAMP_MS:
case LogicalTypeId::TIMESTAMP_NS:
// Because the 'Time::FromTime' method constructs a regular (usecond) timestamp, this is not compatible with
// creating sec/ms/ns timestamps
throw NotImplementedException("Conversion from 'datetime' to type %s is not implemented yet",
target_type.ToString());
default:
throw ConversionException("Could not convert 'datetime' to type %s", target_type.ToString());
}
}
date_t PyDateTime::ToDate() {
return Date::FromDate(year, month, day);
}
dtime_t PyDateTime::ToDuckTime() {
return Time::FromTime(hour, minute, second, micros);
}
int32_t PyDateTime::GetYears(nb::handle &obj) {
return PyDateTime_GET_YEAR(obj.ptr()); // NOLINT
}
int32_t PyDateTime::GetMonths(nb::handle &obj) {
return PyDateTime_GET_MONTH(obj.ptr()); // NOLINT
}
int32_t PyDateTime::GetDays(nb::handle &obj) {
return PyDateTime_GET_DAY(obj.ptr()); // NOLINT
}
int32_t PyDateTime::GetHours(nb::handle &obj) {
return PyDateTime_DATE_GET_HOUR(obj.ptr()); // NOLINT
}
int32_t PyDateTime::GetMinutes(nb::handle &obj) {
return PyDateTime_DATE_GET_MINUTE(obj.ptr()); // NOLINT
}
int32_t PyDateTime::GetSeconds(nb::handle &obj) {
return PyDateTime_DATE_GET_SECOND(obj.ptr()); // NOLINT
}
int32_t PyDateTime::GetMicros(nb::handle &obj) {
return PyDateTime_DATE_GET_MICROSECOND(obj.ptr()); // NOLINT
}
nb::object PyDateTime::GetTZInfo(nb::handle &obj) {
// The object returned is borrowed, there is no reference to steal
return nb::borrow<nb::object>(PyDateTime_DATE_GET_TZINFO(obj.ptr())); // NOLINT
}
PyDate::PyDate(nb::handle &ele) {
year = PyDateTime::GetYears(ele);
month = PyDateTime::GetMonths(ele);
day = PyDateTime::GetDays(ele);
}
date_t PyDate::ToDate() {
return Date::FromDate(year, month, day);
}
Value PyDate::ToDuckValue() {
auto value = Value::DATE(year, month, day);
return value;
}
void PythonObject::Initialize() {
PyDateTime_IMPORT; // NOLINT: Python datetime initialize #2
}
enum class InfinityType : uint8_t { NONE, POSITIVE, NEGATIVE };
InfinityType GetTimestampInfinityType(timestamp_t ×tamp) {
if (timestamp == timestamp_t::infinity()) {
return InfinityType::POSITIVE;
}
if (timestamp == timestamp_t::ninfinity()) {
return InfinityType::NEGATIVE;
}
return InfinityType::NONE;
}
nb::object PythonObject::FromStruct(const Value &val, const LogicalType &type,
const ClientProperties &client_properties) {
auto &struct_values = StructValue::GetChildren(val);
auto &child_types = StructType::GetChildTypes(type);
if (StructType::IsUnnamed(type)) {
duckdb::PyUtil::TupleBuilder py_tuple(struct_values.size());
for (idx_t i = 0; i < struct_values.size(); i++) {
auto &child_entry = child_types[i];
D_ASSERT(child_entry.first.empty());
auto &child_type = child_entry.second;
py_tuple.append(FromValue(struct_values[i], child_type, client_properties));
}
return py_tuple.take();
} else {
nb::dict py_struct;
for (idx_t i = 0; i < struct_values.size(); i++) {
auto &child_entry = child_types[i];
auto &child_name = child_entry.first;
auto &child_type = child_entry.second;
py_struct[child_name.c_str()] = FromValue(struct_values[i], child_type, client_properties);
}
return std::move(py_struct);
}
}
static bool KeyIsHashable(const LogicalType &type) {
switch (type.id()) {
case LogicalTypeId::BOOLEAN:
case LogicalTypeId::TINYINT:
case LogicalTypeId::SMALLINT:
case LogicalTypeId::INTEGER:
case LogicalTypeId::BIGINT:
case LogicalTypeId::UTINYINT:
case LogicalTypeId::USMALLINT:
case LogicalTypeId::UINTEGER:
case LogicalTypeId::UBIGINT:
case LogicalTypeId::HUGEINT:
case LogicalTypeId::UHUGEINT:
case LogicalTypeId::FLOAT:
case LogicalTypeId::DOUBLE:
case LogicalTypeId::DECIMAL:
case LogicalTypeId::ENUM:
case LogicalTypeId::VARCHAR:
case LogicalTypeId::BLOB:
case LogicalTypeId::BIT:
case LogicalTypeId::TIMESTAMP:
case LogicalTypeId::TIMESTAMP_MS:
case LogicalTypeId::TIMESTAMP_NS:
case LogicalTypeId::TIMESTAMP_SEC:
case LogicalTypeId::TIMESTAMP_TZ:
case LogicalTypeId::TIMESTAMP_TZ_NS:
case LogicalTypeId::TIME_TZ:
case LogicalTypeId::TIME:
case LogicalTypeId::DATE:
case LogicalTypeId::UUID:
case LogicalTypeId::INTERVAL:
case LogicalTypeId::GEOMETRY:
return true;
case LogicalTypeId::LIST:
case LogicalTypeId::ARRAY:
case LogicalTypeId::MAP:
case LogicalTypeId::VARIANT:
return false;
case LogicalTypeId::UNION: {
idx_t count = UnionType::GetMemberCount(type);
for (idx_t i = 0; i < count; i++) {
if (!KeyIsHashable(UnionType::GetMemberType(type, i))) {
return false;
}
}
// Only if all the member types are hashable do we say the entire UNION is hashable
return true;
}
case LogicalTypeId::STRUCT:
case LogicalTypeId::TUPLE:
return false;
case LogicalTypeId::SQLNULL:
// A SQLNULL key is always NULL, and Python's None is hashable.
return true;
default:
throw NotImplementedException("Unsupported type: \"%s\"", type.ToString());
}
}
nb::object PythonObject::FromValue(const Value &val, const LogicalType &type,
const ClientProperties &client_properties) {
auto &import_cache = *DuckDBPyConnection::ImportCache();
if (val.IsNull()) {
return nb::none();
}
switch (type.id()) {
case LogicalTypeId::BOOLEAN:
return nb::cast(val.GetValue<bool>());
case LogicalTypeId::TINYINT:
return nb::cast(val.GetValue<int8_t>());
case LogicalTypeId::SMALLINT:
return nb::cast(val.GetValue<int16_t>());
case LogicalTypeId::INTEGER:
return nb::cast(val.GetValue<int32_t>());
case LogicalTypeId::BIGINT:
return nb::cast(val.GetValue<int64_t>());
case LogicalTypeId::UTINYINT:
return nb::cast(val.GetValue<uint8_t>());
case LogicalTypeId::USMALLINT:
return nb::cast(val.GetValue<uint16_t>());
case LogicalTypeId::UINTEGER:
return nb::cast(val.GetValue<uint32_t>());
case LogicalTypeId::UBIGINT:
return nb::cast(val.GetValue<uint64_t>());
case LogicalTypeId::HUGEINT:
return nb::steal<nb::object>(PyLong_FromString(val.GetValue<string>().c_str(), nullptr, 10));
case LogicalTypeId::UHUGEINT:
return nb::steal<nb::object>(PyLong_FromString(val.GetValue<string>().c_str(), nullptr, 10));
case LogicalTypeId::FLOAT:
return nb::cast(val.GetValue<float>());
case LogicalTypeId::DOUBLE:
return nb::cast(val.GetValue<double>());
case LogicalTypeId::DECIMAL: {
return import_cache.decimal.Decimal()(val.ToString());
}
case LogicalTypeId::ENUM:
return nb::cast(EnumType::GetValue(val));
case LogicalTypeId::UNION: {
return PythonObject::FromValue(UnionValue::GetValue(val), UnionValue::GetType(val), client_properties);
}
case LogicalTypeId::VARCHAR:
return nb::cast(StringValue::Get(val));
case LogicalTypeId::BLOB:
case LogicalTypeId::GEOMETRY: {
auto &blob = StringValue::Get(val);
return nb::bytes(blob.data(), blob.size());
}
case LogicalTypeId::BIT:
return nb::cast(Bit::ToString(StringValue::Get(val)));
case LogicalTypeId::TIMESTAMP:
case LogicalTypeId::TIMESTAMP_MS:
case LogicalTypeId::TIMESTAMP_NS:
case LogicalTypeId::TIMESTAMP_SEC:
case LogicalTypeId::TIMESTAMP_TZ:
case LogicalTypeId::TIMESTAMP_TZ_NS: {
D_ASSERT(type.InternalType() == PhysicalType::INT64);
auto timestamp = val.GetValueUnsafe<timestamp_t>();
InfinityType infinity = GetTimestampInfinityType(timestamp);
if (infinity == InfinityType::POSITIVE) {
return nb::borrow<nb::object>(import_cache.datetime.datetime.max());
}
if (infinity == InfinityType::NEGATIVE) {
return nb::borrow<nb::object>(import_cache.datetime.datetime.min());
}
if (type.id() == LogicalTypeId::TIMESTAMP_MS) {
timestamp = Timestamp::FromEpochMs(timestamp.value);
} else if (type.id() == LogicalTypeId::TIMESTAMP_NS || type.id() == LogicalTypeId::TIMESTAMP_TZ_NS) {
timestamp = Timestamp::FromEpochNanoSeconds(timestamp.value);
} else if (type.id() == LogicalTypeId::TIMESTAMP_SEC) {
timestamp = Timestamp::FromEpochSeconds(timestamp.value);
}
int32_t year, month, day, hour, min, sec, micros;
date_t date;
dtime_t time;
Timestamp::Convert(timestamp, date, time);
Date::Convert(date, year, month, day);
Time::Convert(time, hour, min, sec, micros);
nb::object py_timestamp;
try {
auto python_conversion = PyDateTime_FromDateAndTime(year, month, day, hour, min, sec, micros);
if (!python_conversion) {
throw nb::python_error();
}
py_timestamp = nb::steal<nb::object>(python_conversion);
} catch (nb::python_error &e) {
// Failed to convert, fall back to str
auto fallback_str = val.ToString();
return nb::str(fallback_str.c_str(), fallback_str.size());
}
if (type.id() == LogicalTypeId::TIMESTAMP_TZ || type.id() == LogicalTypeId::TIMESTAMP_TZ_NS) {
// We have to add the timezone info
auto tz_utc = import_cache.pytz.timezone()("UTC");
auto timestamp_utc = tz_utc.attr("localize")(py_timestamp);
auto tz_info = import_cache.pytz.timezone()(client_properties.time_zone);
return timestamp_utc.attr("astimezone")(tz_info);
}
return py_timestamp;
}
case LogicalTypeId::TIME_TZ: {
D_ASSERT(type.InternalType() == PhysicalType::INT64);
int32_t hour, min, sec, microsec;
auto time_tz = val.GetValueUnsafe<dtime_tz_t>();
auto time = time_tz.time();
auto offset = time_tz.offset();
duckdb::Time::Convert(time, hour, min, sec, microsec);
nb::object py_time;
try {
auto python_conversion = PyTime_FromTime(hour, min, sec, microsec);
if (!python_conversion) {
throw nb::python_error();
}
py_time = nb::steal<nb::object>(python_conversion);
} catch (nb::python_error &e) {
// Failed to convert, fall back to str
auto fallback_str = val.ToString();
return nb::str(fallback_str.c_str(), fallback_str.size());
}
// We have to add the timezone info
auto timedelta = import_cache.datetime.timedelta()(nb::arg("seconds") = offset);
auto timezone_offset = import_cache.datetime.timezone()(timedelta);
auto tmp_datetime = import_cache.datetime.datetime.min();
auto tmp_datetime_with_tz = import_cache.datetime.datetime.combine()(tmp_datetime, py_time, timezone_offset);
return tmp_datetime_with_tz.attr("timetz")();
}
case LogicalTypeId::TIME:
case LogicalTypeId::TIME_NS: {
D_ASSERT(type.InternalType() == PhysicalType::INT64);
int32_t hour, min, sec, usec;
dtime_t time;
if (type.id() == LogicalTypeId::TIME) {
time = val.GetValueUnsafe<dtime_t>();
} else {
// Python's datetime doesn't support nanoseconds, we convert to micros.
time = dtime_t(val.GetValueUnsafe<dtime_ns_t>().value / 1000);
}
duckdb::Time::Convert(time, hour, min, sec, usec);
try {
auto pytime = PyTime_FromTime(hour, min, sec, usec);
if (!pytime) {
throw nb::python_error();
}
return nb::steal<nb::object>(pytime);
} catch (nb::python_error &e) {
{
auto fallback = val.ToString();
return nb::str(fallback.c_str(), fallback.size());
}
}
}
case LogicalTypeId::DATE: {
D_ASSERT(type.InternalType() == PhysicalType::INT32);
auto date = val.GetValueUnsafe<date_t>();
int32_t year, month, day;
if (!Value::IsFinite(date)) {
if (date == date_t::infinity()) {
return nb::borrow<nb::object>(import_cache.datetime.date.max());
}
return nb::borrow<nb::object>(import_cache.datetime.date.min());
}
duckdb::Date::Convert(date, year, month, day);
try {
auto pydate = PyDate_FromDate(year, month, day);
if (!pydate) {
throw nb::python_error();
}
return nb::steal<nb::object>(pydate);
} catch (nb::python_error &e) {
{
auto fallback = val.ToString();
return nb::str(fallback.c_str(), fallback.size());
}
}
}
case LogicalTypeId::LIST: {
auto &list_values = ListValue::GetChildren(val);
nb::list list;
for (auto &list_elem : list_values) {
list.append(FromValue(list_elem, ListType::GetChildType(type), client_properties));
}
return std::move(list);
}
case LogicalTypeId::ARRAY: {
auto &array_values = ArrayValue::GetChildren(val);
auto array_size = ArrayType::GetSize(type);
auto &child_type = ArrayType::GetChildType(type);
duckdb::PyUtil::TupleBuilder arr(array_size);
for (idx_t elem_idx = 0; elem_idx < array_size; elem_idx++) {
arr.append(FromValue(array_values[elem_idx], child_type, client_properties));
}
return arr.take();
}
case LogicalTypeId::MAP: {
auto &list_values = ListValue::GetChildren(val);
auto &key_type = MapType::KeyType(type);
auto &val_type = MapType::ValueType(type);
nb::dict py_struct;
if (KeyIsHashable(key_type)) {
for (auto &list_elem : list_values) {
auto &struct_children = StructValue::GetChildren(list_elem);
auto key = PythonObject::FromValue(struct_children[0], key_type, client_properties);
auto value = PythonObject::FromValue(struct_children[1], val_type, client_properties);
py_struct[std::move(key)] = std::move(value);
}
} else {
nb::list keys;
nb::list values;
for (auto &list_elem : list_values) {
auto &struct_children = StructValue::GetChildren(list_elem);
keys.append(PythonObject::FromValue(struct_children[0], key_type, client_properties));
values.append(PythonObject::FromValue(struct_children[1], val_type, client_properties));
}
py_struct["key"] = std::move(keys);
py_struct["value"] = std::move(values);
}
return std::move(py_struct);
}
case LogicalTypeId::STRUCT:
case LogicalTypeId::TUPLE: {
return FromStruct(val, type, client_properties);
}
case LogicalTypeId::UUID: {
auto uuid_value = val.GetValueUnsafe<hugeint_t>();
return import_cache.uuid.UUID()(UUID::ToString(uuid_value));
}
case LogicalTypeId::BIGNUM: {
auto bignum_value = val.GetValueUnsafe<bignum_t>();
auto bignum_str = Bignum::BignumToVarchar(bignum_value);
return nb::str(bignum_str.c_str(), bignum_str.size());
}
case LogicalTypeId::INTERVAL: {
auto interval_value = val.GetValueUnsafe<interval_t>();
int64_t days = duckdb::Interval::DAYS_PER_MONTH * interval_value.months + interval_value.days;
return import_cache.datetime.timedelta()(nb::arg("days") = days,
nb::arg("microseconds") = interval_value.micros);
}
case LogicalTypeId::VARIANT: {
Vector tmp(val, count_t(1));
RecursiveUnifiedVectorFormat format;
Vector::RecursiveToUnifiedFormat(tmp, format);
UnifiedVariantVectorData vector_data(format);
auto variant_val = VariantUtils::ConvertVariantToValue(vector_data, 0, 0);
return FromValue(variant_val, variant_val.type(), client_properties);
}
default:
throw NotImplementedException("Unsupported type: \"%s\"", type.ToString());
}
}
} // namespace duckdb