forked from duckdb/duckdb-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyresult.cpp
More file actions
540 lines (486 loc) · 17.8 KB
/
Copy pathpyresult.cpp
File metadata and controls
540 lines (486 loc) · 17.8 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
#include "duckdb_python/pyrelation.hpp"
#include "duckdb_python/pyconnection/pyconnection.hpp"
#include "duckdb_python/pyresult.hpp"
#include "duckdb_python/python_objects.hpp"
#include "duckdb_python/numpy/numpy_type.hpp"
#include "duckdb_python/arrow/arrow_array_stream.hpp"
#include "duckdb/common/arrow/arrow.hpp"
#include "duckdb/common/arrow/arrow_util.hpp"
#include "duckdb/common/arrow/arrow_converter.hpp"
#include "duckdb/common/arrow/arrow_wrapper.hpp"
#include "duckdb/common/arrow/result_arrow_wrapper.hpp"
#include "duckdb/common/types/date.hpp"
#include "duckdb/common/types/hugeint.hpp"
#include "duckdb/common/types/uhugeint.hpp"
#include "duckdb/common/types/time.hpp"
#include "duckdb/common/types/timestamp.hpp"
#include "duckdb/common/types/uuid.hpp"
#include "duckdb_python/numpy/array_wrapper.hpp"
#include "duckdb/common/exception.hpp"
#include "duckdb/common/enums/stream_execution_result.hpp"
#include "duckdb_python/arrow/arrow_export_utils.hpp"
#include "duckdb/main/chunk_scan_state/query_result.hpp"
#include "duckdb/common/arrow/arrow_query_result.hpp"
using namespace pybind11::literals;
namespace duckdb {
DuckDBPyResult::DuckDBPyResult(unique_ptr<QueryResult> result_p) : result(std::move(result_p)) {
if (!result) {
throw InternalException("PyResult created without a result object");
}
}
DuckDBPyResult::~DuckDBPyResult() {
try {
D_ASSERT(py::gil_check());
py::gil_scoped_release gil;
result.reset();
current_chunk.reset();
} catch (...) { // NOLINT
}
}
ClientProperties DuckDBPyResult::GetClientProperties() {
return result->client_properties;
}
const vector<string> &DuckDBPyResult::GetNames() {
if (!result) {
throw InternalException("Calling GetNames without a result object");
}
return result->names;
}
const vector<LogicalType> &DuckDBPyResult::GetTypes() {
if (!result) {
throw InternalException("Calling GetTypes without a result object");
}
return result->types;
}
unique_ptr<DataChunk> DuckDBPyResult::FetchChunk() {
if (!result) {
throw InternalException("FetchChunk called without a result object");
}
return FetchNext(*result);
}
unique_ptr<DataChunk> DuckDBPyResult::FetchNext(QueryResult &query_result) {
if (!result_closed && query_result.type == QueryResultType::STREAM_RESULT &&
!query_result.Cast<StreamQueryResult>().IsOpen()) {
result_closed = true;
return nullptr;
}
if (query_result.type == QueryResultType::STREAM_RESULT) {
auto &stream_result = query_result.Cast<StreamQueryResult>();
StreamExecutionResult execution_result;
while (!StreamQueryResult::IsChunkReady(execution_result = stream_result.ExecuteTask())) {
{
py::gil_scoped_acquire gil;
if (PyErr_CheckSignals() != 0) {
throw std::runtime_error("Query interrupted");
}
}
if (execution_result == StreamExecutionResult::BLOCKED) {
stream_result.WaitForTask();
}
}
if (execution_result == StreamExecutionResult::EXECUTION_CANCELLED) {
throw InvalidInputException("The execution of the query was cancelled before it could finish, likely "
"caused by executing a different query");
}
if (execution_result == StreamExecutionResult::EXECUTION_ERROR) {
stream_result.ThrowError();
}
}
auto chunk = query_result.Fetch();
if (query_result.HasError()) {
query_result.ThrowError();
}
return chunk;
}
unique_ptr<DataChunk> DuckDBPyResult::FetchNextRaw(QueryResult &query_result) {
if (!result_closed && query_result.type == QueryResultType::STREAM_RESULT &&
!query_result.Cast<StreamQueryResult>().IsOpen()) {
result_closed = true;
return nullptr;
}
auto chunk = query_result.FetchRaw();
if (query_result.HasError()) {
query_result.ThrowError();
}
return chunk;
}
Optional<py::tuple> DuckDBPyResult::Fetchone() {
{
D_ASSERT(py::gil_check());
py::gil_scoped_release release;
if (!result) {
throw InvalidInputException("result closed");
}
if (!current_chunk || chunk_offset >= current_chunk->size()) {
current_chunk = FetchNext(*result);
chunk_offset = 0;
}
}
if (!current_chunk || current_chunk->size() == 0) {
return py::none();
}
py::tuple res(result->types.size());
for (idx_t col_idx = 0; col_idx < result->types.size(); col_idx++) {
auto &mask = FlatVector::Validity(current_chunk->data[col_idx]);
if (!mask.RowIsValid(chunk_offset)) {
res[col_idx] = py::none();
continue;
}
auto val = current_chunk->data[col_idx].GetValue(chunk_offset);
res[col_idx] = PythonObject::FromValue(val, result->types[col_idx], result->client_properties);
}
chunk_offset++;
return res;
}
py::list DuckDBPyResult::Fetchmany(idx_t size) {
py::list res;
for (idx_t i = 0; i < size; i++) {
auto fres = Fetchone();
if (fres.is_none()) {
break;
}
res.append(fres);
}
return res;
}
py::list DuckDBPyResult::Fetchall() {
py::list res;
while (true) {
auto fres = Fetchone();
if (fres.is_none()) {
break;
}
res.append(fres);
}
return res;
}
py::dict DuckDBPyResult::FetchNumpy() {
return FetchNumpyInternal();
}
void DuckDBPyResult::FillNumpy(py::dict &res, idx_t col_idx, NumpyResultConversion &conversion, const char *name) {
if (result->types[col_idx].id() == LogicalTypeId::ENUM) {
auto &import_cache = *DuckDBPyConnection::ImportCache();
auto pandas_categorical = import_cache.pandas.Categorical();
auto categorical_dtype = import_cache.pandas.CategoricalDtype();
if (!pandas_categorical || !categorical_dtype) {
throw InvalidInputException("'pandas' is required for this operation but it was not installed");
}
// first we (might) need to create the categorical type
if (categories_type.find(col_idx) == categories_type.end()) {
// Equivalent to: pandas.CategoricalDtype(['a', 'b'], ordered=True)
categories_type[col_idx] = categorical_dtype(categories[col_idx], true);
}
// Equivalent to: pandas.Categorical.from_codes(codes=[0, 1, 0, 1], dtype=dtype)
res[name] = pandas_categorical.attr("from_codes")(conversion.ToArray(col_idx),
py::arg("dtype") = categories_type[col_idx]);
if (!conversion.ToPandas()) {
res[name] = res[name].attr("to_numpy")();
}
} else {
res[name] = conversion.ToArray(col_idx);
}
}
void InsertCategory(QueryResult &result, unordered_map<idx_t, py::list> &categories) {
for (idx_t col_idx = 0; col_idx < result.types.size(); col_idx++) {
auto &type = result.types[col_idx];
if (type.id() == LogicalTypeId::ENUM) {
// It's an ENUM type, in addition to converting the codes we must convert the categories
if (categories.find(col_idx) == categories.end()) {
auto &categories_list = EnumType::GetValuesInsertOrder(type);
auto categories_size = EnumType::GetSize(type);
for (idx_t i = 0; i < categories_size; i++) {
categories[col_idx].append(py::cast(categories_list.GetValue(i).ToString()));
}
}
}
}
}
unique_ptr<NumpyResultConversion> DuckDBPyResult::InitializeNumpyConversion(bool pandas) {
if (!result) {
throw InvalidInputException("result closed");
}
idx_t initial_capacity = STANDARD_VECTOR_SIZE * 2ULL;
if (result->type == QueryResultType::MATERIALIZED_RESULT) {
// materialized query result: we know exactly how much space we need
auto &materialized = result->Cast<MaterializedQueryResult>();
initial_capacity = materialized.RowCount();
}
auto conversion =
make_uniq<NumpyResultConversion>(result->types, initial_capacity, result->client_properties, pandas);
return conversion;
}
py::dict DuckDBPyResult::FetchNumpyInternal(bool stream, idx_t vectors_per_chunk,
unique_ptr<NumpyResultConversion> conversion_p) {
if (!result) {
throw InvalidInputException("result closed");
}
if (!conversion_p) {
conversion_p = InitializeNumpyConversion();
}
auto &conversion = *conversion_p;
if (result->type == QueryResultType::MATERIALIZED_RESULT) {
auto &materialized = result->Cast<MaterializedQueryResult>();
for (auto &chunk : materialized.Collection().Chunks()) {
conversion.Append(chunk);
}
InsertCategory(materialized, categories);
materialized.Collection().Reset();
} else {
D_ASSERT(result->type == QueryResultType::STREAM_RESULT);
if (!stream) {
vectors_per_chunk = NumericLimits<idx_t>::Maximum();
}
auto &stream_result = result->Cast<StreamQueryResult>();
for (idx_t count_vec = 0; count_vec < vectors_per_chunk; count_vec++) {
if (!stream_result.IsOpen()) {
break;
}
unique_ptr<DataChunk> chunk;
{
D_ASSERT(py::gil_check());
py::gil_scoped_release release;
chunk = FetchNextRaw(stream_result);
}
if (!chunk || chunk->size() == 0) {
//! finished
break;
}
conversion.Append(*chunk);
InsertCategory(stream_result, categories);
}
}
// now that we have materialized the result in contiguous arrays, construct the actual NumPy arrays or categorical
// types
py::dict res;
auto names = result->names;
QueryResult::DeduplicateColumns(names);
for (idx_t col_idx = 0; col_idx < result->names.size(); col_idx++) {
auto &name = names[col_idx];
FillNumpy(res, col_idx, conversion, name.c_str());
}
return res;
}
static void ReplaceDFColumn(PandasDataFrame &df, const char *col_name, idx_t idx, const py::handle &new_value) {
df.attr("drop")("columns"_a = col_name, "inplace"_a = true);
df.attr("insert")(idx, col_name, new_value, "allow_duplicates"_a = false);
}
// TODO: unify these with an enum/flag to indicate which conversions to do
void DuckDBPyResult::ConvertDateTimeTypes(PandasDataFrame &df, bool date_as_object) const {
auto names = df.attr("columns").cast<vector<string>>();
for (idx_t i = 0; i < result->ColumnCount(); i++) {
if (result->types[i] == LogicalType::TIMESTAMP_TZ) {
// first localize to UTC then convert to timezone_config
auto utc_local = df[names[i].c_str()].attr("dt").attr("tz_localize")("UTC");
auto new_value = utc_local.attr("dt").attr("tz_convert")(result->client_properties.time_zone);
// We need to create the column anew because the exact dt changed to a new timezone
ReplaceDFColumn(df, names[i].c_str(), i, new_value);
} else if (date_as_object && result->types[i] == LogicalType::DATE) {
auto new_value = df[names[i].c_str()].attr("dt").attr("date");
ReplaceDFColumn(df, names[i].c_str(), i, new_value);
}
}
}
static py::object ConvertNumpyDtype(py::handle numpy_array) {
D_ASSERT(py::gil_check());
auto &import_cache = *DuckDBPyConnection::ImportCache();
auto dtype = numpy_array.attr("dtype");
if (!py::isinstance(numpy_array, import_cache.numpy.ma.masked_array())) {
return dtype;
}
auto numpy_type = ConvertNumpyType(dtype);
switch (numpy_type.type) {
case NumpyNullableType::BOOL: {
return import_cache.pandas.BooleanDtype()();
}
case NumpyNullableType::UINT_8: {
return import_cache.pandas.UInt8Dtype()();
}
case NumpyNullableType::UINT_16: {
return import_cache.pandas.UInt16Dtype()();
}
case NumpyNullableType::UINT_32: {
return import_cache.pandas.UInt32Dtype()();
}
case NumpyNullableType::UINT_64: {
return import_cache.pandas.UInt64Dtype()();
}
case NumpyNullableType::INT_8: {
return import_cache.pandas.Int8Dtype()();
}
case NumpyNullableType::INT_16: {
return import_cache.pandas.Int16Dtype()();
}
case NumpyNullableType::INT_32: {
return import_cache.pandas.Int32Dtype()();
}
case NumpyNullableType::INT_64: {
return import_cache.pandas.Int64Dtype()();
}
case NumpyNullableType::FLOAT_32:
case NumpyNullableType::FLOAT_64:
case NumpyNullableType::FLOAT_16: // there is no pandas.Float16Dtype
default:
return dtype;
}
}
PandasDataFrame DuckDBPyResult::FrameFromNumpy(bool date_as_object, const py::handle &o) {
D_ASSERT(py::gil_check());
auto &import_cache = *DuckDBPyConnection::ImportCache();
auto pandas = import_cache.pandas();
if (!pandas) {
throw InvalidInputException("'pandas' is required for this operation but it was not installed");
}
py::object items = o.attr("items")();
for (const py::handle &item : items) {
// Each item is a tuple of (key, value)
auto key_value = py::cast<py::tuple>(item);
py::handle key = key_value[0]; // Access the first element (key)
py::handle value = key_value[1]; // Access the second element (value)
auto dtype = ConvertNumpyDtype(value);
if (py::isinstance(value, import_cache.numpy.ma.masked_array())) {
// o[key] = pd.Series(value.filled(pd.NA), dtype=dtype)
auto series = pandas.attr("Series")(value.attr("data"), py::arg("dtype") = dtype);
series.attr("__setitem__")(value.attr("mask"), import_cache.pandas.NA());
o.attr("__setitem__")(key, series);
}
}
PandasDataFrame df = py::cast<PandasDataFrame>(pandas.attr("DataFrame").attr("from_dict")(o));
// Convert TZ and (optionally) Date types
ConvertDateTimeTypes(df, date_as_object);
auto names = df.attr("columns").cast<vector<string>>();
D_ASSERT(result->ColumnCount() == names.size());
return df;
}
PandasDataFrame DuckDBPyResult::FetchDF(bool date_as_object) {
auto conversion = InitializeNumpyConversion(true);
return FrameFromNumpy(date_as_object, FetchNumpyInternal(false, 1, std::move(conversion)));
}
PandasDataFrame DuckDBPyResult::FetchDFChunk(idx_t num_of_vectors, bool date_as_object) {
auto conversion = InitializeNumpyConversion(true);
return FrameFromNumpy(date_as_object, FetchNumpyInternal(true, num_of_vectors, std::move(conversion)));
}
py::dict DuckDBPyResult::FetchPyTorch() {
auto result_dict = FetchNumpyInternal();
auto from_numpy = py::module::import("torch").attr("from_numpy");
for (auto &item : result_dict) {
result_dict[item.first] = from_numpy(item.second);
}
return result_dict;
}
py::dict DuckDBPyResult::FetchTF() {
auto result_dict = FetchNumpyInternal();
auto convert_to_tensor = py::module::import("tensorflow").attr("convert_to_tensor");
for (auto &item : result_dict) {
result_dict[item.first] = convert_to_tensor(item.second);
}
return result_dict;
}
duckdb::pyarrow::Table DuckDBPyResult::FetchArrowTable(idx_t rows_per_batch, bool to_polars) {
if (!result) {
throw InvalidInputException("There is no query result");
}
auto names = result->names;
if (to_polars) {
QueryResult::DeduplicateColumns(names);
}
if (!result) {
throw InvalidInputException("result closed");
}
auto pyarrow_lib_module = py::module::import("pyarrow").attr("lib");
py::list batches;
if (result->type == QueryResultType::ARROW_RESULT) {
auto &arrow_result = result->Cast<ArrowQueryResult>();
auto arrays = arrow_result.ConsumeArrays();
for (auto &array : arrays) {
ArrowSchema arrow_schema;
auto result_names = arrow_result.names;
if (to_polars) {
QueryResult::DeduplicateColumns(result_names);
}
ArrowArray data = array->arrow_array;
array->arrow_array.release = nullptr;
ArrowConverter::ToArrowSchema(&arrow_schema, arrow_result.types, result_names,
arrow_result.client_properties);
TransformDuckToArrowChunk(arrow_schema, data, batches);
}
} else {
QueryResultChunkScanState scan_state(*result.get());
while (true) {
ArrowArray data;
idx_t count;
auto &query_result = *result.get();
{
D_ASSERT(py::gil_check());
py::gil_scoped_release release;
count = ArrowUtil::FetchChunk(scan_state, query_result.client_properties, rows_per_batch, &data,
ArrowTypeExtensionData::GetExtensionTypes(
*query_result.client_properties.client_context, query_result.types));
}
if (count == 0) {
break;
}
ArrowSchema arrow_schema;
auto result_names = query_result.names;
if (to_polars) {
QueryResult::DeduplicateColumns(result_names);
}
ArrowConverter::ToArrowSchema(&arrow_schema, query_result.types, result_names,
query_result.client_properties);
TransformDuckToArrowChunk(arrow_schema, data, batches);
}
}
return pyarrow::ToArrowTable(result->types, names, std::move(batches), result->client_properties);
}
ArrowArrayStream DuckDBPyResult::FetchArrowArrayStream(idx_t rows_per_batch) {
if (!result) {
throw InvalidInputException("There is no query result");
}
ResultArrowArrayStreamWrapper *result_stream = new ResultArrowArrayStreamWrapper(std::move(result), rows_per_batch);
// The 'result_stream' is part of the 'private_data' of the ArrowArrayStream and its lifetime is bound to that of
// the ArrowArrayStream.
return result_stream->stream;
}
duckdb::pyarrow::RecordBatchReader DuckDBPyResult::FetchRecordBatchReader(idx_t rows_per_batch) {
if (!result) {
throw InvalidInputException("There is no query result");
}
py::gil_scoped_acquire acquire;
auto pyarrow_lib_module = py::module::import("pyarrow").attr("lib");
auto record_batch_reader_func = pyarrow_lib_module.attr("RecordBatchReader").attr("_import_from_c");
auto stream = FetchArrowArrayStream(rows_per_batch);
py::object record_batch_reader = record_batch_reader_func((uint64_t)&stream); // NOLINT
return py::cast<duckdb::pyarrow::RecordBatchReader>(record_batch_reader);
}
static void ArrowArrayStreamPyCapsuleDestructor(PyObject *object) {
auto data = PyCapsule_GetPointer(object, "arrow_array_stream");
if (!data) {
return;
}
auto stream = reinterpret_cast<ArrowArrayStream *>(data);
if (stream->release) {
stream->release(stream);
}
delete stream;
}
py::object DuckDBPyResult::FetchArrowCapsule(idx_t rows_per_batch) {
auto stream_p = FetchArrowArrayStream(rows_per_batch);
auto stream = new ArrowArrayStream();
*stream = stream_p;
return py::capsule(stream, "arrow_array_stream", ArrowArrayStreamPyCapsuleDestructor);
}
py::list DuckDBPyResult::GetDescription(const vector<string> &names, const vector<LogicalType> &types) {
py::list desc;
for (idx_t col_idx = 0; col_idx < names.size(); col_idx++) {
auto py_name = py::str(names[col_idx]);
auto py_type = DuckDBPyType(types[col_idx]);
desc.append(py::make_tuple(py_name, py_type, py::none(), py::none(), py::none(), py::none(), py::none()));
}
return desc;
}
void DuckDBPyResult::Close() {
result = nullptr;
}
bool DuckDBPyResult::IsClosed() const {
return result_closed;
}
} // namespace duckdb