-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathduckdb_python.cpp
More file actions
1160 lines (1130 loc) · 49.5 KB
/
duckdb_python.cpp
File metadata and controls
1160 lines (1130 loc) · 49.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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "duckdb_python/pybind11/pybind_wrapper.hpp"
#include "duckdb/common/atomic.hpp"
#include "duckdb/common/vector.hpp"
#include "duckdb/parser/parser.hpp"
#include "duckdb_python/python_objects.hpp"
#include "duckdb_python/pyconnection/pyconnection.hpp"
#include "duckdb_python/pystatement.hpp"
#include "duckdb_python/pyrelation.hpp"
#include "duckdb_python/expression/pyexpression.hpp"
#include "duckdb_python/pyresult.hpp"
#include "duckdb_python/pybind11/exceptions.hpp"
#include "duckdb_python/typing.hpp"
#include "duckdb_python/functional.hpp"
#include "duckdb_python/pybind11/conversions/pyconnection_default.hpp"
#include "duckdb/common/box_renderer.hpp"
#include "duckdb/function/function.hpp"
#include "duckdb_python/pybind11/conversions/exception_handling_enum.hpp"
#include "duckdb_python/pybind11/conversions/python_udf_type_enum.hpp"
#include "duckdb_python/pybind11/conversions/python_csv_line_terminator_enum.hpp"
#include "duckdb/common/enums/statement_type.hpp"
#include "duckdb/common/adbc/adbc-init.hpp"
#include "duckdb.hpp"
#ifndef DUCKDB_PYTHON_LIB_NAME
#define DUCKDB_PYTHON_LIB_NAME _duckdb
#endif
namespace py = pybind11;
namespace duckdb {
enum PySQLTokenType : uint8_t {
PY_SQL_TOKEN_IDENTIFIER = 0,
PY_SQL_TOKEN_NUMERIC_CONSTANT,
PY_SQL_TOKEN_STRING_CONSTANT,
PY_SQL_TOKEN_OPERATOR,
PY_SQL_TOKEN_KEYWORD,
PY_SQL_TOKEN_COMMENT
};
static py::list PyTokenize(const string &query) {
auto tokens = Parser::Tokenize(query);
py::list result;
for (auto &token : tokens) {
auto tuple = py::tuple(2);
tuple[0] = token.start;
switch (token.type) {
case SimplifiedTokenType::SIMPLIFIED_TOKEN_IDENTIFIER:
tuple[1] = PY_SQL_TOKEN_IDENTIFIER;
break;
case SimplifiedTokenType::SIMPLIFIED_TOKEN_NUMERIC_CONSTANT:
tuple[1] = PY_SQL_TOKEN_NUMERIC_CONSTANT;
break;
case SimplifiedTokenType::SIMPLIFIED_TOKEN_STRING_CONSTANT:
tuple[1] = PY_SQL_TOKEN_STRING_CONSTANT;
break;
case SimplifiedTokenType::SIMPLIFIED_TOKEN_OPERATOR:
tuple[1] = PY_SQL_TOKEN_OPERATOR;
break;
case SimplifiedTokenType::SIMPLIFIED_TOKEN_KEYWORD:
tuple[1] = PY_SQL_TOKEN_KEYWORD;
break;
case SimplifiedTokenType::SIMPLIFIED_TOKEN_COMMENT:
tuple[1] = PY_SQL_TOKEN_COMMENT;
break;
default:
break;
}
result.append(tuple);
}
return result;
}
static void InitializeConnectionMethods(py::module_ &m) {
// START_OF_CONNECTION_METHODS
m.def(
"cursor",
[](shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->Cursor();
},
"Create a duplicate of the current connection", py::kw_only(), py::arg("connection") = py::none());
m.def(
"register_filesystem",
[](AbstractFileSystem filesystem, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
conn->RegisterFilesystem(filesystem);
},
"Register a fsspec compliant filesystem", py::arg("filesystem"), py::kw_only(),
py::arg("connection") = py::none());
m.def(
"unregister_filesystem",
[](const py::str &name, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
conn->UnregisterFilesystem(name);
},
"Unregister a filesystem", py::arg("name"), py::kw_only(), py::arg("connection") = py::none());
m.def(
"list_filesystems",
[](shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->ListFilesystems();
},
"List registered filesystems, including builtin ones", py::kw_only(), py::arg("connection") = py::none());
m.def(
"filesystem_is_registered",
[](const string &name, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FileSystemIsRegistered(name);
},
"Check if a filesystem with the provided name is currently registered", py::arg("name"), py::kw_only(),
py::arg("connection") = py::none());
m.def(
"get_profiling_information",
[](const py::str &format, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->GetProfilingInformation(format);
},
"Get profiling information from a query", py::kw_only(), py::arg("format") = "json",
py::arg("connection") = py::none());
m.def(
"enable_profiling",
[](shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->EnableProfiling();
},
"Enable profiling for the current connection", py::kw_only(), py::arg("connection") = py::none());
m.def(
"disable_profiling",
[](shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->DisableProfiling();
},
"Disable profiling for the current connection", py::kw_only(), py::arg("connection") = py::none());
m.def(
"create_function",
[](const string &name, const py::function &udf, const py::object &arguments = py::none(),
const shared_ptr<DuckDBPyType> &return_type = nullptr, PythonUDFType type = PythonUDFType::NATIVE,
FunctionNullHandling null_handling = FunctionNullHandling::DEFAULT_NULL_HANDLING,
PythonExceptionHandling exception_handling = PythonExceptionHandling::FORWARD_ERROR,
bool side_effects = false, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->RegisterScalarUDF(name, udf, arguments, return_type, type, null_handling, exception_handling,
side_effects);
},
"Create a DuckDB function out of the passing in Python function so it can be used in queries", py::arg("name"),
py::arg("function"), py::arg("parameters") = py::none(), py::arg("return_type") = py::none(), py::kw_only(),
py::arg("type") = PythonUDFType::NATIVE, py::arg("null_handling") = FunctionNullHandling::DEFAULT_NULL_HANDLING,
py::arg("exception_handling") = PythonExceptionHandling::FORWARD_ERROR, py::arg("side_effects") = false,
py::arg("connection") = py::none());
m.def(
"remove_function",
[](const string &name, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->UnregisterUDF(name);
},
"Remove a previously created function", py::arg("name"), py::kw_only(), py::arg("connection") = py::none());
m.def(
"sqltype",
[](const string &type_str, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->Type(type_str);
},
"Create a type object by parsing the 'type_str' string", py::arg("type_str"), py::kw_only(),
py::arg("connection") = py::none());
m.def(
"dtype",
[](const string &type_str, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->Type(type_str);
},
"Create a type object by parsing the 'type_str' string", py::arg("type_str"), py::kw_only(),
py::arg("connection") = py::none());
m.def(
"type",
[](const string &type_str, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->Type(type_str);
},
"Create a type object by parsing the 'type_str' string", py::arg("type_str"), py::kw_only(),
py::arg("connection") = py::none());
m.def(
"array_type",
[](const shared_ptr<DuckDBPyType> &type, idx_t size, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->ArrayType(type, size);
},
"Create an array type object of 'type'", py::arg("type").none(false), py::arg("size"), py::kw_only(),
py::arg("connection") = py::none());
m.def(
"list_type",
[](const shared_ptr<DuckDBPyType> &type, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->ListType(type);
},
"Create a list type object of 'type'", py::arg("type").none(false), py::kw_only(),
py::arg("connection") = py::none());
m.def(
"union_type",
[](const py::object &members, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->UnionType(members);
},
"Create a union type object from 'members'", py::arg("members").none(false), py::kw_only(),
py::arg("connection") = py::none());
m.def(
"string_type",
[](const string &collation = string(), shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->StringType(collation);
},
"Create a string type with an optional collation", py::arg("collation") = "", py::kw_only(),
py::arg("connection") = py::none());
m.def(
"enum_type",
[](const string &name, const shared_ptr<DuckDBPyType> &type, const py::list &values_p,
shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->EnumType(name, type, values_p);
},
"Create an enum type of underlying 'type', consisting of the list of 'values'", py::arg("name"),
py::arg("type"), py::arg("values"), py::kw_only(), py::arg("connection") = py::none());
m.def(
"decimal_type",
[](int width, int scale, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->DecimalType(width, scale);
},
"Create a decimal type with 'width' and 'scale'", py::arg("width"), py::arg("scale"), py::kw_only(),
py::arg("connection") = py::none());
m.def(
"struct_type",
[](const py::object &fields, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->StructType(fields);
},
"Create a struct type object from 'fields'", py::arg("fields"), py::kw_only(),
py::arg("connection") = py::none());
m.def(
"row_type",
[](const py::object &fields, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->StructType(fields);
},
"Create a struct type object from 'fields'", py::arg("fields"), py::kw_only(),
py::arg("connection") = py::none());
m.def(
"map_type",
[](const shared_ptr<DuckDBPyType> &key_type, const shared_ptr<DuckDBPyType> &value_type,
shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->MapType(key_type, value_type);
},
"Create a map type object from 'key_type' and 'value_type'", py::arg("key").none(false),
py::arg("value").none(false), py::kw_only(), py::arg("connection") = py::none());
m.def(
"duplicate",
[](shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->Cursor();
},
"Create a duplicate of the current connection", py::kw_only(), py::arg("connection") = py::none());
m.def(
"execute",
[](const py::object &query, py::object params = py::list(), shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->Execute(query, params);
},
"Execute the given SQL query, optionally using prepared statements with parameters set", py::arg("query"),
py::arg("parameters") = py::none(), py::kw_only(), py::arg("connection") = py::none());
m.def(
"executemany",
[](const py::object &query, py::object params = py::list(), shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->ExecuteMany(query, params);
},
"Execute the given prepared statement multiple times using the list of parameter sets in parameters",
py::arg("query"), py::arg("parameters") = py::none(), py::kw_only(), py::arg("connection") = py::none());
m.def(
"close",
[](shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
conn->Close();
},
"Close the connection", py::kw_only(), py::arg("connection") = py::none());
m.def(
"interrupt",
[](shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
conn->Interrupt();
},
"Interrupt pending operations", py::kw_only(), py::arg("connection") = py::none());
m.def(
"query_progress",
[](shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->QueryProgress();
},
"Query progress of pending operation", py::kw_only(), py::arg("connection") = py::none());
m.def(
"fetchone",
[](shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FetchOne();
},
"Fetch a single row from a result following execute", py::kw_only(), py::arg("connection") = py::none());
m.def(
"fetchmany",
[](idx_t size, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FetchMany(size);
},
"Fetch the next set of rows from a result following execute", py::arg("size") = 1, py::kw_only(),
py::arg("connection") = py::none());
m.def(
"fetchall",
[](shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FetchAll();
},
"Fetch all rows from a result following execute", py::kw_only(), py::arg("connection") = py::none());
m.def(
"fetchnumpy",
[](shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FetchNumpy();
},
"Fetch a result as list of NumPy arrays following execute", py::kw_only(), py::arg("connection") = py::none());
m.def(
"fetchdf",
[](bool date_as_object, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FetchDF(date_as_object);
},
"Fetch a result as DataFrame following execute()", py::kw_only(), py::arg("date_as_object") = false,
py::arg("connection") = py::none());
m.def(
"fetch_df",
[](bool date_as_object, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FetchDF(date_as_object);
},
"Fetch a result as DataFrame following execute()", py::kw_only(), py::arg("date_as_object") = false,
py::arg("connection") = py::none());
m.def(
"df",
[](bool date_as_object, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FetchDF(date_as_object);
},
"Fetch a result as DataFrame following execute()", py::kw_only(), py::arg("date_as_object") = false,
py::arg("connection") = py::none());
m.def(
"fetch_df_chunk",
[](const idx_t vectors_per_chunk = 1, bool date_as_object = false,
shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FetchDFChunk(vectors_per_chunk, date_as_object);
},
"Fetch a chunk of the result as DataFrame following execute()", py::arg("vectors_per_chunk") = 1, py::kw_only(),
py::arg("date_as_object") = false, py::arg("connection") = py::none());
m.def(
"pl",
[](idx_t rows_per_batch, bool lazy, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FetchPolars(rows_per_batch, lazy);
},
"Fetch a result as Polars DataFrame following execute()", py::arg("rows_per_batch") = 1000000, py::kw_only(),
py::arg("lazy") = false, py::arg("connection") = py::none());
m.def(
"to_arrow_table",
[](idx_t batch_size, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FetchArrow(batch_size);
},
"Fetch a result as Arrow table following execute()", py::arg("batch_size") = 1000000, py::kw_only(),
py::arg("connection") = py::none());
m.def(
"to_arrow_reader",
[](idx_t batch_size, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FetchRecordBatchReader(batch_size);
},
"Fetch an Arrow RecordBatchReader following execute()", py::arg("batch_size") = 1000000, py::kw_only(),
py::arg("connection") = py::none());
m.def(
"fetch_arrow_table",
[](idx_t rows_per_batch, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
PyErr_WarnEx(PyExc_DeprecationWarning, "fetch_arrow_table() is deprecated, use to_arrow_table() instead.",
0);
return conn->FetchArrow(rows_per_batch);
},
"Fetch a result as Arrow table following execute()", py::arg("rows_per_batch") = 1000000, py::kw_only(),
py::arg("connection") = py::none());
m.def(
"fetch_record_batch",
[](const idx_t rows_per_batch, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
PyErr_WarnEx(PyExc_DeprecationWarning, "fetch_record_batch() is deprecated, use to_arrow_reader() instead.",
0);
return conn->FetchRecordBatchReader(rows_per_batch);
},
"Fetch an Arrow RecordBatchReader following execute()", py::arg("rows_per_batch") = 1000000, py::kw_only(),
py::arg("connection") = py::none());
m.def(
"torch",
[](shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FetchPyTorch();
},
"Fetch a result as dict of PyTorch Tensors following execute()", py::kw_only(),
py::arg("connection") = py::none());
m.def(
"tf",
[](shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FetchTF();
},
"Fetch a result as dict of TensorFlow Tensors following execute()", py::kw_only(),
py::arg("connection") = py::none());
m.def(
"begin",
[](shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->Begin();
},
"Start a new transaction", py::kw_only(), py::arg("connection") = py::none());
m.def(
"commit",
[](shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->Commit();
},
"Commit changes performed within a transaction", py::kw_only(), py::arg("connection") = py::none());
m.def(
"rollback",
[](shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->Rollback();
},
"Roll back changes performed within a transaction", py::kw_only(), py::arg("connection") = py::none());
m.def(
"checkpoint",
[](shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->Checkpoint();
},
"Synchronizes data in the write-ahead log (WAL) to the database data file (no-op for in-memory connections)",
py::kw_only(), py::arg("connection") = py::none());
m.def(
"append",
[](const string &name, const PandasDataFrame &value, bool by_name,
shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->Append(name, value, by_name);
},
"Append the passed DataFrame to the named table", py::arg("table_name"), py::arg("df"), py::kw_only(),
py::arg("by_name") = false, py::arg("connection") = py::none());
m.def(
"register",
[](const string &name, const py::object &python_object, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->RegisterPythonObject(name, python_object);
},
"Register the passed Python Object value for querying with a view", py::arg("view_name"),
py::arg("python_object"), py::kw_only(), py::arg("connection") = py::none());
m.def(
"unregister",
[](const string &name, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->UnregisterPythonObject(name);
},
"Unregister the view name", py::arg("view_name"), py::kw_only(), py::arg("connection") = py::none());
m.def(
"table",
[](const string &tname, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->Table(tname);
},
"Create a relation object for the named table", py::arg("table_name"), py::kw_only(),
py::arg("connection") = py::none());
m.def(
"view",
[](const string &vname, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->View(vname);
},
"Create a relation object for the named view", py::arg("view_name"), py::kw_only(),
py::arg("connection") = py::none());
m.def(
"values",
[](const py::args ¶ms, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->Values(params);
},
"Create a relation object from the passed values", py::kw_only(), py::arg("connection") = py::none());
m.def(
"table_function",
[](const string &fname, py::object params = py::list(), shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->TableFunction(fname, params);
},
"Create a relation object from the named table function with given parameters", py::arg("name"),
py::arg("parameters") = py::none(), py::kw_only(), py::arg("connection") = py::none());
m.def(
"read_json",
[](const py::object &name, const Optional<py::object> &columns = py::none(),
const Optional<py::object> &sample_size = py::none(), const Optional<py::object> &maximum_depth = py::none(),
const Optional<py::str> &records = py::none(), const Optional<py::str> &format = py::none(),
const Optional<py::object> &date_format = py::none(),
const Optional<py::object> ×tamp_format = py::none(),
const Optional<py::object> &compression = py::none(),
const Optional<py::object> &maximum_object_size = py::none(),
const Optional<py::object> &ignore_errors = py::none(),
const Optional<py::object> &convert_strings_to_integers = py::none(),
const Optional<py::object> &field_appearance_threshold = py::none(),
const Optional<py::object> &map_inference_threshold = py::none(),
const Optional<py::object> &maximum_sample_files = py::none(),
const Optional<py::object> &filename = py::none(),
const Optional<py::object> &hive_partitioning = py::none(),
const Optional<py::object> &union_by_name = py::none(), const Optional<py::object> &hive_types = py::none(),
const Optional<py::object> &hive_types_autocast = py::none(),
shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->ReadJSON(name, columns, sample_size, maximum_depth, records, format, date_format,
timestamp_format, compression, maximum_object_size, ignore_errors,
convert_strings_to_integers, field_appearance_threshold, map_inference_threshold,
maximum_sample_files, filename, hive_partitioning, union_by_name, hive_types,
hive_types_autocast);
},
"Create a relation object from the JSON file in 'name'", py::arg("path_or_buffer"), py::kw_only(),
py::arg("columns") = py::none(), py::arg("sample_size") = py::none(), py::arg("maximum_depth") = py::none(),
py::arg("records") = py::none(), py::arg("format") = py::none(), py::arg("date_format") = py::none(),
py::arg("timestamp_format") = py::none(), py::arg("compression") = py::none(),
py::arg("maximum_object_size") = py::none(), py::arg("ignore_errors") = py::none(),
py::arg("convert_strings_to_integers") = py::none(), py::arg("field_appearance_threshold") = py::none(),
py::arg("map_inference_threshold") = py::none(), py::arg("maximum_sample_files") = py::none(),
py::arg("filename") = py::none(), py::arg("hive_partitioning") = py::none(),
py::arg("union_by_name") = py::none(), py::arg("hive_types") = py::none(),
py::arg("hive_types_autocast") = py::none(), py::arg("connection") = py::none());
m.def(
"extract_statements",
[](const string &query, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->ExtractStatements(query);
},
"Parse the query string and extract the Statement object(s) produced", py::arg("query"), py::kw_only(),
py::arg("connection") = py::none());
m.def(
"sql",
[](const py::object &query, string alias = "", py::object params = py::list(),
shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->RunQuery(query, alias, params);
},
"Run a SQL query. If it is a SELECT statement, create a relation object from the given SQL query, otherwise "
"run the query as-is.",
py::arg("query"), py::kw_only(), py::arg("alias") = "", py::arg("params") = py::none(),
py::arg("connection") = py::none());
m.def(
"query",
[](const py::object &query, string alias = "", py::object params = py::list(),
shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->RunQuery(query, alias, params);
},
"Run a SQL query. If it is a SELECT statement, create a relation object from the given SQL query, otherwise "
"run the query as-is.",
py::arg("query"), py::kw_only(), py::arg("alias") = "", py::arg("params") = py::none(),
py::arg("connection") = py::none());
m.def(
"from_query",
[](const py::object &query, string alias = "", py::object params = py::list(),
shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->RunQuery(query, alias, params);
},
"Run a SQL query. If it is a SELECT statement, create a relation object from the given SQL query, otherwise "
"run the query as-is.",
py::arg("query"), py::kw_only(), py::arg("alias") = "", py::arg("params") = py::none(),
py::arg("connection") = py::none());
m.def(
"read_csv",
[](const py::object &name, py::kwargs &kwargs) {
auto connection_arg = kwargs.contains("conn") ? kwargs["conn"] : py::none();
auto conn = py::cast<shared_ptr<DuckDBPyConnection>>(connection_arg);
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->ReadCSV(name, kwargs);
},
"Create a relation object from the CSV file in 'name'", py::arg("path_or_buffer"), py::kw_only());
m.def(
"from_csv_auto",
[](const py::object &name, py::kwargs &kwargs) {
auto connection_arg = kwargs.contains("conn") ? kwargs["conn"] : py::none();
auto conn = py::cast<shared_ptr<DuckDBPyConnection>>(connection_arg);
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->ReadCSV(name, kwargs);
},
"Create a relation object from the CSV file in 'name'", py::arg("path_or_buffer"), py::kw_only());
m.def(
"from_df",
[](const PandasDataFrame &value, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FromDF(value);
},
"Create a relation object from the DataFrame in df", py::arg("df"), py::kw_only(),
py::arg("connection") = py::none());
m.def(
"from_arrow",
[](py::object &arrow_object, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FromArrow(arrow_object);
},
"Create a relation object from an Arrow object", py::arg("arrow_object"), py::kw_only(),
py::arg("connection") = py::none());
m.def(
"from_parquet",
[](const string &file_glob, bool binary_as_string, bool file_row_number, bool filename, bool hive_partitioning,
bool union_by_name, const py::object &compression = py::none(),
shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FromParquet(file_glob, binary_as_string, file_row_number, filename, hive_partitioning,
union_by_name, compression);
},
"Create a relation object from the Parquet files in file_glob", py::arg("file_glob"),
py::arg("binary_as_string") = false, py::kw_only(), py::arg("file_row_number") = false,
py::arg("filename") = false, py::arg("hive_partitioning") = false, py::arg("union_by_name") = false,
py::arg("compression") = py::none(), py::arg("connection") = py::none());
m.def(
"read_parquet",
[](const string &file_glob, bool binary_as_string, bool file_row_number, bool filename, bool hive_partitioning,
bool union_by_name, const py::object &compression = py::none(),
shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FromParquet(file_glob, binary_as_string, file_row_number, filename, hive_partitioning,
union_by_name, compression);
},
"Create a relation object from the Parquet files in file_glob", py::arg("file_glob"),
py::arg("binary_as_string") = false, py::kw_only(), py::arg("file_row_number") = false,
py::arg("filename") = false, py::arg("hive_partitioning") = false, py::arg("union_by_name") = false,
py::arg("compression") = py::none(), py::arg("connection") = py::none());
m.def(
"from_parquet",
[](const vector<string> &file_globs, bool binary_as_string, bool file_row_number, bool filename,
bool hive_partitioning, bool union_by_name, const py::object &compression = py::none(),
shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FromParquets(file_globs, binary_as_string, file_row_number, filename, hive_partitioning,
union_by_name, compression);
},
"Create a relation object from the Parquet files in file_globs", py::arg("file_globs"),
py::arg("binary_as_string") = false, py::kw_only(), py::arg("file_row_number") = false,
py::arg("filename") = false, py::arg("hive_partitioning") = false, py::arg("union_by_name") = false,
py::arg("compression") = py::none(), py::arg("connection") = py::none());
m.def(
"read_parquet",
[](const vector<string> &file_globs, bool binary_as_string, bool file_row_number, bool filename,
bool hive_partitioning, bool union_by_name, const py::object &compression = py::none(),
shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FromParquets(file_globs, binary_as_string, file_row_number, filename, hive_partitioning,
union_by_name, compression);
},
"Create a relation object from the Parquet files in file_globs", py::arg("file_globs"),
py::arg("binary_as_string") = false, py::kw_only(), py::arg("file_row_number") = false,
py::arg("filename") = false, py::arg("hive_partitioning") = false, py::arg("union_by_name") = false,
py::arg("compression") = py::none(), py::arg("connection") = py::none());
m.def(
"get_table_names",
[](const string &query, bool qualified, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->GetTableNames(query, qualified);
},
"Extract the required table names from a query", py::arg("query"), py::kw_only(), py::arg("qualified") = false,
py::arg("connection") = py::none());
m.def(
"install_extension",
[](const string &extension, bool force_install = false, const py::object &repository = py::none(),
const py::object &repository_url = py::none(), const py::object &version = py::none(),
shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
conn->InstallExtension(extension, force_install, repository, repository_url, version);
},
"Install an extension by name, with an optional version and/or repository to get the extension from",
py::arg("extension"), py::kw_only(), py::arg("force_install") = false, py::arg("repository") = py::none(),
py::arg("repository_url") = py::none(), py::arg("version") = py::none(), py::arg("connection") = py::none());
m.def(
"load_extension",
[](const string &extension, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
conn->LoadExtension(extension);
},
"Load an installed extension", py::arg("extension"), py::kw_only(), py::arg("connection") = py::none());
m.def(
"project",
[](const PandasDataFrame &df, const py::args &args, const string &groups = "",
shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FromDF(df)->Project(args, groups);
},
"Project the relation object by the projection in project_expr", py::arg("df"), py::kw_only(),
py::arg("groups") = "", py::arg("connection") = py::none());
m.def(
"distinct",
[](const PandasDataFrame &df, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FromDF(df)->Distinct();
},
"Retrieve distinct rows from this relation object", py::arg("df"), py::kw_only(),
py::arg("connection") = py::none());
m.def(
"write_csv",
[](const PandasDataFrame &df, const string &filename, const py::object &sep = py::none(),
const py::object &na_rep = py::none(), const py::object &header = py::none(),
const py::object "echar = py::none(), const py::object &escapechar = py::none(),
const py::object &date_format = py::none(), const py::object ×tamp_format = py::none(),
const py::object "ing = py::none(), const py::object &encoding = py::none(),
const py::object &compression = py::none(), const py::object &overwrite = py::none(),
const py::object &per_thread_output = py::none(), const py::object &use_tmp_file = py::none(),
const py::object &partition_by = py::none(), const py::object &write_partition_columns = py::none(),
shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
conn->FromDF(df)->ToCSV(filename, sep, na_rep, header, quotechar, escapechar, date_format, timestamp_format,
quoting, encoding, compression, overwrite, per_thread_output, use_tmp_file,
partition_by, write_partition_columns);
},
"Write the relation object to a CSV file in 'file_name'", py::arg("df"), py::arg("filename"), py::kw_only(),
py::arg("sep") = py::none(), py::arg("na_rep") = py::none(), py::arg("header") = py::none(),
py::arg("quotechar") = py::none(), py::arg("escapechar") = py::none(), py::arg("date_format") = py::none(),
py::arg("timestamp_format") = py::none(), py::arg("quoting") = py::none(), py::arg("encoding") = py::none(),
py::arg("compression") = py::none(), py::arg("overwrite") = py::none(),
py::arg("per_thread_output") = py::none(), py::arg("use_tmp_file") = py::none(),
py::arg("partition_by") = py::none(), py::arg("write_partition_columns") = py::none(),
py::arg("connection") = py::none());
m.def(
"aggregate",
[](const PandasDataFrame &df, const py::object &expr, const string &groups = "",
shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FromDF(df)->Aggregate(expr, groups);
},
"Compute the aggregate aggr_expr by the optional groups group_expr on the relation", py::arg("df"),
py::arg("aggr_expr"), py::arg("group_expr") = "", py::kw_only(), py::arg("connection") = py::none());
m.def(
"alias",
[](const PandasDataFrame &df, const string &expr, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FromDF(df)->SetAlias(expr);
},
"Rename the relation object to new alias", py::arg("df"), py::arg("alias"), py::kw_only(),
py::arg("connection") = py::none());
m.def(
"filter",
[](const PandasDataFrame &df, const py::object &expr, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FromDF(df)->Filter(expr);
},
"Filter the relation object by the filter in filter_expr", py::arg("df"), py::arg("filter_expr"), py::kw_only(),
py::arg("connection") = py::none());
m.def(
"limit",
[](const PandasDataFrame &df, int64_t n, int64_t offset = 0, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FromDF(df)->Limit(n, offset);
},
"Only retrieve the first n rows from this relation object, starting at offset", py::arg("df"), py::arg("n"),
py::arg("offset") = 0, py::kw_only(), py::arg("connection") = py::none());
m.def(
"order",
[](const PandasDataFrame &df, const string &expr, shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FromDF(df)->Order(expr);
},
"Reorder the relation object by order_expr", py::arg("df"), py::arg("order_expr"), py::kw_only(),
py::arg("connection") = py::none());
m.def(
"query_df",
[](const PandasDataFrame &df, const string &view_name, const string &sql_query,
shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FromDF(df)->Query(view_name, sql_query);
},
"Run the given SQL query in sql_query on the view named virtual_table_name that refers to the relation object",
py::arg("df"), py::arg("virtual_table_name"), py::arg("sql_query"), py::kw_only(),
py::arg("connection") = py::none());
m.def(
"description",
[](shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->GetDescription();
},
"Get result set attributes, mainly column names", py::kw_only(), py::arg("connection") = py::none());
m.def(
"rowcount",
[](shared_ptr<DuckDBPyConnection> conn = nullptr) {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->GetRowcount();
},
"Get result set row count", py::kw_only(), py::arg("connection") = py::none());
// END_OF_CONNECTION_METHODS
// We define these "wrapper" methods manually because they are overloaded
m.def(
"arrow",
[](idx_t rows_per_batch, shared_ptr<DuckDBPyConnection> conn) -> duckdb::pyarrow::RecordBatchReader {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FetchRecordBatchReader(rows_per_batch);
},
"Alias of to_arrow_reader(). We recommend using to_arrow_reader() instead.",
py::arg("rows_per_batch") = 1000000, py::kw_only(), py::arg("connection") = py::none());
m.def(
"arrow",
[](py::object &arrow_object, shared_ptr<DuckDBPyConnection> conn) -> unique_ptr<DuckDBPyRelation> {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FromArrow(arrow_object);
},
"Create a relation object from an Arrow object", py::arg("arrow_object"), py::kw_only(),
py::arg("connection") = py::none());
m.def(
"df",
[](bool date_as_object, shared_ptr<DuckDBPyConnection> conn) -> PandasDataFrame {
if (!conn) {
conn = DuckDBPyConnection::DefaultConnection();
}
return conn->FetchDF(date_as_object);
},
"Fetch a result as DataFrame following execute()", py::kw_only(), py::arg("date_as_object") = false,