forked from singlestore-labs/singlestoredb-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accel.c
4419 lines (3865 loc) · 148 KB
/
accel.c
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 <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <Python.h>
#ifndef Py_LIMITED_API
#include <datetime.h>
#endif
#ifndef PyBUF_WRITE
#define PyBUF_WRITE 0x200
#endif
#define ACCEL_OUT_TUPLES 0
#define ACCEL_OUT_STRUCTSEQUENCES 1
#define ACCEL_OUT_DICTS 2
#define ACCEL_OUT_NAMEDTUPLES 3
#define NUMPY_BOOL 1
#define NUMPY_INT8 2
#define NUMPY_INT16 3
#define NUMPY_INT32 4
#define NUMPY_INT64 5
#define NUMPY_UINT8 6
#define NUMPY_UINT16 7
#define NUMPY_UINT32 8
#define NUMPY_UINT64 9
#define NUMPY_FLOAT32 10
#define NUMPY_FLOAT64 11
#define NUMPY_TIMEDELTA 12
#define NUMPY_DATETIME 13
#define NUMPY_OBJECT 14
#define MYSQL_FLAG_NOT_NULL 1
#define MYSQL_FLAG_PRI_KEY 2
#define MYSQL_FLAG_UNIQUE_KEY 4
#define MYSQL_FLAG_MULTIPLE_KEY 8
#define MYSQL_FLAG_BLOB 16
#define MYSQL_FLAG_UNSIGNED 32
#define MYSQL_FLAG_ZEROFILL 64
#define MYSQL_FLAG_BINARY 128
#define MYSQL_FLAG_ENUM 256
#define MYSQL_FLAG_AUTO_INCREMENT 512
#define MYSQL_FLAG_TIMESTAMP 1024
#define MYSQL_FLAG_SET 2048
#define MYSQL_FLAG_PART_KEY 16384
#define MYSQL_FLAG_GROUP 32767
#define MYSQL_FLAG_UNIQUE 65536
#define MYSQL_TYPE_DECIMAL 0
#define MYSQL_TYPE_TINY 1
#define MYSQL_TYPE_SHORT 2
#define MYSQL_TYPE_LONG 3
#define MYSQL_TYPE_FLOAT 4
#define MYSQL_TYPE_DOUBLE 5
#define MYSQL_TYPE_NULL 6
#define MYSQL_TYPE_TIMESTAMP 7
#define MYSQL_TYPE_LONGLONG 8
#define MYSQL_TYPE_INT24 9
#define MYSQL_TYPE_DATE 10
#define MYSQL_TYPE_TIME 11
#define MYSQL_TYPE_DATETIME 12
#define MYSQL_TYPE_YEAR 13
#define MYSQL_TYPE_NEWDATE 14
#define MYSQL_TYPE_VARCHAR 15
#define MYSQL_TYPE_BIT 16
#define MYSQL_TYPE_JSON 245
#define MYSQL_TYPE_NEWDECIMAL 246
#define MYSQL_TYPE_ENUM 247
#define MYSQL_TYPE_SET 248
#define MYSQL_TYPE_TINY_BLOB 249
#define MYSQL_TYPE_MEDIUM_BLOB 250
#define MYSQL_TYPE_LONG_BLOB 251
#define MYSQL_TYPE_BLOB 252
#define MYSQL_TYPE_VAR_STRING 253
#define MYSQL_TYPE_STRING 254
#define MYSQL_TYPE_GEOMETRY 255
#define MYSQL_TYPE_CHAR MYSQL_TYPE_TINY
#define MYSQL_TYPE_INTERVAL MYSQL_TYPE_ENUM
#define MYSQL_COLUMN_NULL 251
#define MYSQL_COLUMN_UNSIGNED_CHAR 251
#define MYSQL_COLUMN_UNSIGNED_SHORT 252
#define MYSQL_COLUMN_UNSIGNED_INT24 253
#define MYSQL_COLUMN_UNSIGNED_INT64 254
#define MYSQL_SERVER_MORE_RESULTS_EXISTS 8
// 2**24 - 1
#define MYSQL_MAX_PACKET_LEN 16777215
#define ACCEL_OPTION_TIME_TYPE_TIMEDELTA 0
#define ACCEL_OPTION_TIME_TYPE_TIME 1
#define ACCEL_OPTION_JSON_TYPE_STRING 0
#define ACCEL_OPTION_JSON_TYPE_OBJ 1
#define ACCEL_OPTION_BIT_TYPE_BYTES 0
#define ACCEL_OPTION_BIT_TYPE_INT 1
#define CHR2INT1(x) ((x)[1] - '0')
#define CHR2INT2(x) ((((x)[0] - '0') * 10) + ((x)[1] - '0'))
#define CHR2INT3(x) ((((x)[0] - '0') * 1e2) + (((x)[1] - '0') * 10) + ((x)[2] - '0'))
#define CHR2INT4(x) ((((x)[0] - '0') * 1e3) + (((x)[1] - '0') * 1e2) + (((x)[2] - '0') * 10) + ((x)[3] - '0'))
#define CHR2INT6(x) ((((x)[0] - '0') * 1e5) + (((x)[1] - '0') * 1e4) + (((x)[2] - '0') * 1e3) + (((x)[3] - '0') * 1e2) + (((x)[4] - '0') * 10) + (((x)[5] - '0')))
#define CHECK_DATE_STR(s, s_l) \
((s_l) == 10 && \
(s)[0] >= '0' && (s)[0] <= '9' && \
(s)[1] >= '0' && (s)[1] <= '9' && \
(s)[2] >= '0' && (s)[2] <= '9' && \
(s)[3] >= '0' && (s)[3] <= '9' && \
(s)[4] == '-' && \
(((s)[5] == '1' && ((s)[6] >= '0' && (s)[6] <= '2')) || \
((s)[5] == '0' && ((s)[6] >= '1' && (s)[6] <= '9'))) && \
(s)[7] == '-' && \
((((s)[8] >= '0' && (s)[8] <= '2') && ((s)[9] >= '0' && (s)[9] <= '9')) || \
((s)[8] == '3' && ((s)[9] >= '0' && (s)[9] <= '1'))) && \
!((s)[0] == '0' && (s)[1] == '0' && (s)[2] == '0' && (s)[3] == '0') && \
!((s)[5] == '0' && (s)[6] == '0') && \
!((s)[8] == '0' && (s)[9] == '0'))
#define CHECK_TIME_STR(s, s_l) \
((s_l) == 8 && \
((((s)[0] >= '0' && (s)[0] <= '1') && ((s)[1] >= '0' && (s)[1] <= '9')) || \
((s)[0] == '2' && ((s)[1] >= '0' && (s)[1] <= '3'))) && \
(s)[2] == ':' && \
(((s)[3] >= '0' && (s)[3] <= '5') && ((s)[4] >= '0' && (s)[4] <= '9')) && \
(s)[5] == ':' && \
(((s)[6] >= '0' && (s)[6] <= '5') && ((s)[7] >= '0' && (s)[7] <= '9')))
#define CHECK_MICROSECONDS_STR(s, s_l) \
((s_l) == 7 && \
(s)[0] == '.' && \
(s)[1] >= '0' && (s)[1] <= '9' && \
(s)[2] >= '0' && (s)[2] <= '9' && \
(s)[3] >= '0' && (s)[3] <= '9' && \
(s)[4] >= '0' && (s)[4] <= '9' && \
(s)[5] >= '0' && (s)[5] <= '9' && \
(s)[6] >= '0' && (s)[6] <= '9')
#define CHECK_MILLISECONDS_STR(s, s_l) \
((s_l) == 4 && \
(s)[0] == '.' && \
(s)[1] >= '0' && (s)[1] <= '9' && \
(s)[2] >= '0' && (s)[2] <= '9' && \
(s)[3] >= '0' && (s)[3] <= '9')
#define CHECK_MICRO_TIME_STR(s, s_l) \
((s_l) == 15 && CHECK_TIME_STR(s, 8) && CHECK_MICROSECONDS_STR((s)+8, 7))
#define CHECK_MILLI_TIME_STR(s, s_l) \
((s_l) == 12 && CHECK_TIME_STR(s, 8) && CHECK_MILLISECONDS_STR((s)+8, 4))
#define CHECK_DATETIME_STR(s, s_l) \
((s_l) == 19 && \
CHECK_DATE_STR(s, 10) && \
((s)[10] == ' ' || (s)[10] == 'T') && \
CHECK_TIME_STR((s)+11, 8))
#define CHECK_MICRO_DATETIME_STR(s, s_l) \
((s_l) == 26 && \
CHECK_DATE_STR(s, 10) && \
((s)[10] == ' ' || (s)[10] == 'T') && \
CHECK_MICRO_TIME_STR((s)+11, 15))
#define CHECK_MILLI_DATETIME_STR(s, s_l) \
((s_l) == 23 && \
CHECK_DATE_STR(s, 10) && \
((s)[10] == ' ' || (s)[10] == 'T') && \
CHECK_MICRO_TIME_STR((s)+11, 12))
#define CHECK_ANY_DATETIME_STR(s, s_l) \
(((s_l) == 19 && CHECK_DATETIME_STR(s, s_l)) || \
((s_l) == 23 && CHECK_MILLI_DATETIME_STR(s, s_l)) || \
((s_l) == 26 && CHECK_MICRO_DATETIME_STR(s, s_l)))
#define DATETIME_SIZE (19)
#define DATETIME_MILLI_SIZE (23)
#define DATETIME_MICRO_SIZE (26)
#define IS_DATETIME_MILLI(s, s_l) ((s_l) == 23)
#define IS_DATETIME_MICRO(s, s_l) ((s_l) == 26)
#define CHECK_ANY_TIME_STR(s, s_l) \
(((s_l) == 8 && CHECK_TIME_STR(s, s_l)) || \
((s_l) == 12 && CHECK_MILLI_TIME_STR(s, s_l)) || \
((s_l) == 15 && CHECK_MICRO_TIME_STR(s, s_l)))
#define TIME_SIZE (8)
#define TIME_MILLI_SIZE (12)
#define TIME_MICRO_SIZE (15)
#define IS_TIME_MILLI(s, s_l) ((s_l) == 12)
#define IS_TIME_MICRO(s, s_l) ((s_l) == 15)
// 0000-00-00 00:00:00
// 0000-00-00 00:00:00.000
// 0000-00-00 00:00:00.000000
#define CHECK_ANY_ZERO_DATETIME_STR(s, s_l) \
(((s_l) == 19 && CHECK_ZERO_DATETIME_STR(s, s_l)) || \
((s_l) == 23 && CHECK_ZERO_MILLI_DATETIME_STR(s, s_l)) || \
((s_l) == 26 && CHECK_ZERO_MICRO_DATETIME_STR(s, s_l)))
#define CHECK_ZERO_DATETIME_STR(s, s_l) \
(s_l == 19 && \
CHECK_ZERO_DATE_STR(s, 10) && \
((s)[10] == ' ' || (s)[10] == 'T') && \
CHECK_ZERO_TIME_STR((s)+11, 8))
#define CHECK_ZERO_MILLI_DATETIME_STR(s, s_l) \
(s_l == 23 && \
CHECK_ZERO_DATE_STR(s, 10) && \
((s)[10] == ' ' || (s)[10] == 'T') && \
CHECK_ZERO_MILLI_TIME_STR((s)+11, 12))
#define CHECK_ZERO_MICRO_DATETIME_STR(s, s_l) \
(s_l == 26 && \
CHECK_ZERO_DATE_STR(s, 10) && \
((s)[10] == ' ' || (s)[10] == 'T') && \
CHECK_ZERO_MICRO_TIME_STR((s)+11, 15))
#define CHECK_ZERO_DATE_STR(s, s_l) \
(s_l == 10 && ((s)[0] == '0' && (s)[1] == '0' && (s)[2] == '0' && (s)[3] == '0' && \
(s)[4] == '-' && (s)[5] == '0' && (s)[6] == '0' && (s)[7] == '-' && \
(s)[8] == '0' && (s)[9] == '0'))
#define CHECK_ZERO_TIME_STR(s, s_l) \
(s_l == 8 && ((s)[0] == '0' && (s)[1] == '0' && (s)[2] == ':' && \
(s)[3] == '0' && (s)[4] == '0' && (s)[5] == ':' && \
(s)[6] == '0' && (s)[7] == '0'))
#define CHECK_ZERO_MILLI_TIME_STR(s, s_l) \
(s_l == 12 && CHECK_ZERO_TIME_STR(s, 8) && \
(s)[8] == '.' && (s)[9] == '0' && (s)[10] == '0' && (s)[11] == '0')
#define CHECK_ZERO_MICRO_TIME_STR(s, s_l) \
(s_l == 15 && CHECK_ZERO_TIME_STR(s, 8) && \
(s)[8] == '.' && (s)[9] == '0' && (s)[10] == '0' && (s)[11] == '0' && \
(s)[12] == '0' && (s)[13] == '0' && (s)[14] == '0')
#define CHECK_TIMEDELTA1_STR(s, s_l) \
((s_l) == 7 && \
(s)[0] >= '0' && (s)[0] <= '9' && \
(s)[1] == ':' && \
(s)[2] >= '0' && (s)[2] <= '5' && \
(s)[3] >= '0' && (s)[3] <= '9' && \
(s)[4] == ':' && \
(s)[5] >= '0' && (s)[5] <= '5' && \
(s)[6] >= '0' && (s)[6] <= '9')
#define CHECK_TIMEDELTA1_MILLI_STR(s, s_l) \
((s_l) == 11 && CHECK_TIMEDELTA1_STR(s, 7) && CHECK_MILLISECONDS_STR((s)+7, 4))
#define CHECK_TIMEDELTA1_MICRO_STR(s, s_l) \
((s_l) == 14 && CHECK_TIMEDELTA1_STR(s, 7) && CHECK_MICROSECONDS_STR((s)+7, 7))
#define CHECK_TIMEDELTA2_STR(s, s_l) \
((s_l) == 8 && \
(s)[0] >= '0' && (s)[0] <= '9' && \
CHECK_TIMEDELTA1_STR((s)+1, 7))
#define CHECK_TIMEDELTA2_MILLI_STR(s, s_l) \
((s_l) == 12 && CHECK_TIMEDELTA2_STR(s, 8) && CHECK_MILLISECONDS_STR((s)+8, 4))
#define CHECK_TIMEDELTA2_MICRO_STR(s, s_l) \
((s_l) == 15 && CHECK_TIMEDELTA2_STR(s, 8) && CHECK_MICROSECONDS_STR((s)+8, 7))
#define CHECK_TIMEDELTA3_STR(s, s_l) \
((s_l) == 9 && \
(s)[0] >= '0' && (s)[0] <= '9' && \
(s)[1] >= '0' && (s)[1] <= '9' && \
CHECK_TIMEDELTA1_STR((s)+2, 7))
#define CHECK_TIMEDELTA3_MILLI_STR(s, s_l) \
((s_l) == 13 && CHECK_TIMEDELTA3_STR(s, 9) && CHECK_MILLISECONDS_STR((s)+9, 4))
#define CHECK_TIMEDELTA3_MICRO_STR(s, s_l) \
((s_l) == 16 && CHECK_TIMEDELTA3_STR(s, 9) && CHECK_MICROSECONDS_STR((s)+9, 7))
//
// 0:00:00 / 0:00:00.000 / 0:00:00.000000
// 00:00:00 / 00:00:00.000 / 00:00:00.000000
// 000:00:00 / 000:00:00.000 / 000:00:00.000000
//
#define CHECK_ANY_TIMEDELTA_STR(s, s_l) \
(((s_l) > 0 && (s)[0] == '-') ? \
(-1 * (_CHECK_ANY_TIMEDELTA_STR((s)+1, (s_l)-1))) : \
(_CHECK_ANY_TIMEDELTA_STR((s), (s_l))))
#define _CHECK_ANY_TIMEDELTA_STR(s, s_l) \
(CHECK_TIMEDELTA1_STR(s, s_l) || \
CHECK_TIMEDELTA2_STR(s, s_l) || \
CHECK_TIMEDELTA3_STR(s, s_l) || \
CHECK_TIMEDELTA1_MILLI_STR(s, s_l) || \
CHECK_TIMEDELTA2_MILLI_STR(s, s_l) || \
CHECK_TIMEDELTA3_MILLI_STR(s, s_l) || \
CHECK_TIMEDELTA1_MICRO_STR(s, s_l) || \
CHECK_TIMEDELTA2_MICRO_STR(s, s_l) || \
CHECK_TIMEDELTA3_MICRO_STR(s, s_l))
#define TIMEDELTA1_SIZE (7)
#define TIMEDELTA2_SIZE (8)
#define TIMEDELTA3_SIZE (9)
#define TIMEDELTA1_MILLI_SIZE (11)
#define TIMEDELTA2_MILLI_SIZE (12)
#define TIMEDELTA3_MILLI_SIZE (13)
#define TIMEDELTA1_MICRO_SIZE (14)
#define TIMEDELTA2_MICRO_SIZE (15)
#define TIMEDELTA3_MICRO_SIZE (16)
#define IS_TIMEDELTA1(s, s_l) ((s_l) == 7 || (s_l) == 11 || (s_l) == 14)
#define IS_TIMEDELTA2(s, s_l) ((s_l) == 8 || (s_l) == 12 || (s_l) == 15)
#define IS_TIMEDELTA3(s, s_l) ((s_l) == 9 || (s_l) == 13 || (s_l) == 16)
#define IS_TIMEDELTA_MILLI(s, s_l) ((s_l) == 11 || (s_l) == 12 || (s_l) == 13)
#define IS_TIMEDELTA_MICRO(s, s_l) ((s_l) == 14 || (s_l) == 15 || (s_l) == 16)
#define CHECKRC(x) if ((x) < 0) goto error;
typedef struct {
int results_type;
int parse_json;
PyObject *invalid_values;
} MySQLAccelOptions;
inline int IMAX(int a, int b) { return((a) > (b) ? a : b); }
inline int IMIN(int a, int b) { return((a) < (b) ? a : b); }
char *_PyUnicode_AsUTF8(PyObject *unicode) {
PyObject *bytes = PyUnicode_AsEncodedString(unicode, "utf-8", "strict");
if (!bytes) return NULL;
char *str = NULL;
Py_ssize_t str_l = 0;
if (PyBytes_AsStringAndSize(bytes, &str, &str_l) < 0) {
return NULL;
}
char *out = calloc(str_l + 1, 1);
memcpy(out, str, str_l);
return out;
}
//
// Cached int values for date/time components
//
static PyObject *PyInts[62] = {0};
//
// Cached string values
//
typedef struct {
PyObject *unbuffered_active;
PyObject *active_idx;
PyObject *_state;
PyObject *affected_rows;
PyObject *warning_count;
PyObject *connection;
PyObject *has_next;
PyObject *options;
PyObject *Decimal;
PyObject *date;
PyObject *timedelta;
PyObject *time;
PyObject *datetime;
PyObject *loads;
PyObject *field_count;
PyObject *converters;
PyObject *fields;
PyObject *flags;
PyObject *scale;
PyObject *type_code;
PyObject *name;
PyObject *table_name;
PyObject *_sock;
PyObject *settimeout;
PyObject *_rfile;
PyObject *read;
PyObject *x_errno;
PyObject *_result;
PyObject *_read_timeout;
PyObject *_next_seq_id;
PyObject *rows;
PyObject *namedtuple;
PyObject *Row;
PyObject *Series;
PyObject *array;
PyObject *vectorize;
} PyStrings;
static PyStrings PyStr = {0};
//
// Cached Python functions
//
typedef struct {
PyObject *json_loads;
PyObject *decimal_Decimal;
PyObject *datetime_date;
PyObject *datetime_time;
PyObject *datetime_timedelta;
PyObject *datetime_datetime;
PyObject *collections_namedtuple;
PyObject *numpy_array;
PyObject *numpy_vectorize;
} PyFunctions;
static PyFunctions PyFunc = {0};
//
// Cached Python objects
//
typedef struct {
PyObject *namedtuple_kwargs;
PyObject *create_numpy_array_args;
PyObject *create_numpy_array_kwargs;
} PyObjects;
static PyObjects PyObj = {0};
//
// State
//
static PyTypeObject *StateType = NULL;
typedef struct {
PyObject_HEAD
PyObject *py_conn; // Database connection
PyObject *py_fields; // List of table fields
PyObject *py_rows; // Output object
PyObject *py_rfile; // Socket file I/O
PyObject *py_read; // File I/O read method
PyObject *py_sock; // Socket
PyObject *py_read_timeout; // Socket read timeout value
PyObject *py_settimeout; // Socket settimeout method
PyObject **py_converters; // List of converter functions
PyObject **py_names; // Column names
PyObject *py_names_list; // Python list of column names
PyObject *py_default_converters; // Dict of default converters
PyObject *py_namedtuple; // Generated namedtuple type
PyObject *py_namedtuple_args; // Pre-allocated tuple for namedtuple args
PyTypeObject *structsequence; // StructSequence type (like C namedtuple)
PyStructSequence_Desc structsequence_desc;
PyObject **py_encodings; // Encoding for each column as Python string
PyObject **py_invalid_values; // Values to use when invalid data exists in a cell
const char **encodings; // Encoding for each column
unsigned long long n_cols; // Total number of columns
unsigned long long n_rows; // Total number of rows read
unsigned long long n_rows_in_batch; // Number of rows in current batch (fetchmany size)
unsigned long *type_codes; // Type code for each column
unsigned long *flags; // Column flags
unsigned long *scales; // Column scales
unsigned long *offsets; // Column offsets in buffer
unsigned long long next_seq_id; // MySQL packet sequence number
MySQLAccelOptions options; // Packet reader options
int unbuffered; // Are we running in unbuffered mode?
int is_eof; // Have we hit the eof packet yet?
struct {
PyObject *_next_seq_id;
PyObject *rows;
} py_str;
char *encoding_errors;
} StateObject;
static void read_options(MySQLAccelOptions *options, PyObject *dict);
#define DESTROY(x) do { if (x) { free((void*)x); (x) = NULL; } } while (0)
static void State_clear_fields(StateObject *self) {
if (!self) return;
DESTROY(self->offsets);
DESTROY(self->scales);
DESTROY(self->flags);
DESTROY(self->type_codes);
DESTROY(self->encodings);
DESTROY(self->structsequence_desc.fields);
DESTROY(self->encoding_errors);
if (self->py_converters) {
for (unsigned long i = 0; i < self->n_cols; i++) {
Py_CLEAR(self->py_converters[i]);
}
DESTROY(self->py_converters);
}
if (self->py_names) {
for (unsigned long i = 0; i < self->n_cols; i++) {
Py_CLEAR(self->py_names[i]);
}
DESTROY(self->py_names);
}
if (self->py_encodings) {
for (unsigned long i = 0; i < self->n_cols; i++) {
Py_CLEAR(self->py_encodings[i]);
}
DESTROY(self->py_encodings);
}
if (self->py_invalid_values) {
for (unsigned long i = 0; i < self->n_cols; i++) {
Py_CLEAR(self->py_invalid_values[i]);
}
DESTROY(self->py_invalid_values);
}
Py_CLEAR(self->structsequence);
Py_CLEAR(self->py_namedtuple);
Py_CLEAR(self->py_namedtuple_args);
Py_CLEAR(self->py_names_list);
Py_CLEAR(self->py_default_converters);
Py_CLEAR(self->py_settimeout);
Py_CLEAR(self->py_read_timeout);
Py_CLEAR(self->py_sock);
Py_CLEAR(self->py_read);
Py_CLEAR(self->py_rfile);
Py_CLEAR(self->py_rows);
Py_CLEAR(self->py_fields);
Py_CLEAR(self->py_conn);
}
static void State_dealloc(StateObject *self) {
State_clear_fields(self);
PyObject_Del(self);
}
static int State_init(StateObject *self, PyObject *args, PyObject *kwds) {
int rc = 0;
PyObject *py_res = NULL;
PyObject *py_converters = NULL;
PyObject *py_options = NULL;
PyObject *py_args = NULL;
unsigned long long requested_n_rows = 0;
if (!PyArg_ParseTuple(args, "OK", &py_res, &requested_n_rows)) {
return -1;
}
py_options = PyObject_GetAttr(py_res, PyStr.options);
if (!py_options) {
Py_INCREF(Py_None);
py_options = Py_None;
}
if (PyDict_Check(py_options)) {
self->py_default_converters = PyDict_GetItemString(py_options, "default_converters");
if (self->py_default_converters && !PyDict_Check(self->py_default_converters)) {
self->py_default_converters = NULL;
}
Py_XINCREF(self->py_default_converters);
PyObject *py_unbuffered = PyDict_GetItemString(py_options, "unbuffered");
if (py_unbuffered && PyObject_IsTrue(py_unbuffered)) {
self->unbuffered = 1;
}
PyObject *py_encoding_errors = PyDict_GetItemString(py_options, "encoding_errors");
if (py_encoding_errors) {
self->encoding_errors = _PyUnicode_AsUTF8(py_encoding_errors);
if (!self->encoding_errors) goto error;
}
}
if (!self->encoding_errors) {
self->encoding_errors = calloc(7, 1);
if (!self->encoding_errors) goto error;
memcpy(self->encoding_errors, "strict", 6);
}
if (self->unbuffered) {
PyObject *unbuffered_active = PyObject_GetAttr(py_res, PyStr.unbuffered_active);
if (!unbuffered_active || !PyObject_IsTrue(unbuffered_active)) {
Py_XDECREF(unbuffered_active);
goto error;
}
Py_XDECREF(unbuffered_active);
}
// Retrieve type codes for each column.
PyObject *py_field_count = PyObject_GetAttr(py_res, PyStr.field_count);
if (!py_field_count) goto error;
self->n_cols = PyLong_AsUnsignedLong(py_field_count);
Py_XDECREF(py_field_count);
py_converters = PyObject_GetAttr(py_res, PyStr.converters);
if (!py_converters) goto error;
self->py_converters = calloc(self->n_cols, sizeof(PyObject*));
if (!self->py_converters) goto error;
self->type_codes = calloc(self->n_cols, sizeof(unsigned long));
if (!self->type_codes) goto error;
self->flags = calloc(self->n_cols, sizeof(unsigned long));
if (!self->flags) goto error;
self->scales = calloc(self->n_cols, sizeof(unsigned long));
if (!self->scales) goto error;
self->encodings = calloc(self->n_cols, sizeof(char*));
if (!self->encodings) goto error;
self->py_encodings = calloc(self->n_cols, sizeof(char*));
if (!self->py_encodings) goto error;
self->py_invalid_values = calloc(self->n_cols, sizeof(char*));
if (!self->py_invalid_values) goto error;
self->py_names = calloc(self->n_cols, sizeof(PyObject*));
if (!self->py_names) goto error;
self->py_fields = PyObject_GetAttr(py_res, PyStr.fields);
if (!self->py_fields) goto error;
self->py_names_list = PyList_New(self->n_cols);
if (!self->py_names_list) goto error;
for (unsigned long i = 0; i < self->n_cols; i++) {
// Get type codes.
PyObject *py_field = PyList_GetItem(self->py_fields, i);
if (!py_field) goto error;
PyObject *py_flags = PyObject_GetAttr(py_field, PyStr.flags);
if (!py_flags) goto error;
self->flags[i] = PyLong_AsUnsignedLong(py_flags);
Py_XDECREF(py_flags);
PyObject *py_scale = PyObject_GetAttr(py_field, PyStr.scale);
if (!py_scale) goto error;
self->scales[i] = PyLong_AsUnsignedLong(py_scale);
Py_XDECREF(py_scale);
PyObject *py_field_type = PyObject_GetAttr(py_field, PyStr.type_code);
if (!py_field_type) goto error;
self->type_codes[i] = PyLong_AsUnsignedLong(py_field_type);
PyObject *py_default_converter = (self->py_default_converters) ?
PyDict_GetItem(self->py_default_converters, py_field_type) : NULL;
PyObject *py_invalid_value = (self->options.invalid_values) ?
PyDict_GetItem(self->options.invalid_values, py_field_type) : NULL;
Py_XDECREF(py_field_type);
// Get field name.
PyObject *py_field_name = PyObject_GetAttr(py_field, PyStr.name);
if (!py_field_name) goto error;
// Make sure field name is not a duplicate.
int dup_found = 0;
for (unsigned long j = 0; j < i; j++) {
if (PyUnicode_Compare(self->py_names[j], py_field_name) == 0) {
dup_found = 1;
break;
}
}
if (dup_found) {
PyObject *py_table_name = PyObject_GetAttr(py_field, PyStr.table_name);
self->py_names[i] = PyUnicode_FromFormat("%U.%U", py_table_name, py_field_name);
Py_XDECREF(py_table_name);
if (!self->py_names[i]) goto error;
} else {
self->py_names[i] = py_field_name;
}
Py_INCREF(self->py_names[i]); // Extra ref since SetItem steals one
rc = PyList_SetItem(self->py_names_list, i, self->py_names[i]);
if (rc) goto error;
// Get field encodings (NULL means binary) and default converters.
PyObject *py_tmp = PyList_GetItem(py_converters, i);
if (!py_tmp) goto error;
PyObject *py_encoding = PyTuple_GetItem(py_tmp, 0);
if (!py_encoding) goto error;
PyObject *py_converter = PyTuple_GetItem(py_tmp, 1);
if (!py_converter) goto error;
self->py_encodings[i] = (py_encoding == Py_None) ? NULL : py_encoding;
Py_XINCREF(self->py_encodings[i]);
self->encodings[i] = (!py_encoding || py_encoding == Py_None) ?
NULL : _PyUnicode_AsUTF8(py_encoding);
self->py_invalid_values[i] = (!py_invalid_value || py_invalid_value == Py_None) ?
NULL : py_converter;
Py_XINCREF(self->py_invalid_values[i]);
self->py_converters[i] = (!py_converter
|| py_converter == Py_None
|| py_converter == py_default_converter) ?
NULL : py_converter;
Py_XINCREF(self->py_converters[i]);
}
// Loop over all data packets.
self->py_conn = PyObject_GetAttr(py_res, PyStr.connection);
if (!self->py_conn) goto error;
// Cache socket timeout and read methods.
self->py_sock = PyObject_GetAttr(self->py_conn, PyStr._sock);
if (!self->py_sock) goto error;
self->py_settimeout = PyObject_GetAttr(self->py_sock, PyStr.settimeout);
if (!self->py_settimeout) goto error;
self->py_read_timeout = PyObject_GetAttr(self->py_conn, PyStr._read_timeout);
if (!self->py_read_timeout) goto error;
self->py_rfile = PyObject_GetAttr(self->py_conn, PyStr._rfile);
if (!self->py_rfile) goto error;
self->py_read = PyObject_GetAttr(self->py_rfile, PyStr.read);
if (!self->py_read) goto error;
PyObject *py_next_seq_id = PyObject_GetAttr(self->py_conn, PyStr._next_seq_id);
if (!py_next_seq_id) goto error;
self->next_seq_id = PyLong_AsUnsignedLongLong(py_next_seq_id);
Py_XDECREF(py_next_seq_id);
if (py_options && PyDict_Check(py_options)) {
read_options(&self->options, py_options);
}
switch (self->options.results_type) {
case ACCEL_OUT_NAMEDTUPLES:
case ACCEL_OUT_STRUCTSEQUENCES:
if (self->options.results_type == ACCEL_OUT_NAMEDTUPLES)
{
py_args = PyTuple_New(2);
if (!py_args) goto error;
rc = PyTuple_SetItem(py_args, 0, PyStr.Row);
if (rc) goto error;
Py_INCREF(PyStr.Row);
rc = PyTuple_SetItem(py_args, 1, self->py_names_list);
if (rc) goto error;
Py_INCREF(self->py_names_list);
self->py_namedtuple = PyObject_Call(
PyFunc.collections_namedtuple,
py_args, PyObj.namedtuple_kwargs);
if (!self->py_namedtuple) goto error;
self->py_namedtuple_args = PyTuple_New(self->n_cols);
if (!self->py_namedtuple_args) goto error;
}
else
{
self->structsequence_desc.name = "singlestoredb.Row";
self->structsequence_desc.doc = "Row of data values";
self->structsequence_desc.n_in_sequence = (int)self->n_cols;
self->structsequence_desc.fields = calloc(self->n_cols + 1, sizeof(PyStructSequence_Field));
if (!self->structsequence_desc.fields) goto error;
for (unsigned long i = 0; i < self->n_cols; i++) {
self->structsequence_desc.fields[i].name = _PyUnicode_AsUTF8(self->py_names[i]);
self->structsequence_desc.fields[i].doc = NULL;
}
self->structsequence = PyStructSequence_NewType(&self->structsequence_desc);
if (!self->structsequence) goto error;
}
// Fall through
default:
// For fetchone, reuse the same list every time.
//if (requested_n_rows == 1) {
// self->py_rows = PyList_New(1);
// PyList_SetItem(self->py_rows, 0, Py_None);
//} else {
self->py_rows = PyList_New(0);
//}
if (!self->py_rows) goto error;
PyObject_SetAttr(py_res, PyStr.rows, self->py_rows);
}
exit:
Py_XDECREF(py_args);
Py_XDECREF(py_converters);
Py_XDECREF(py_options);
if (PyErr_Occurred()) {
PyErr_Print();
}
return rc;
error:
State_clear_fields(self);
rc = -1;
goto exit;
}
static int State_reset_batch(
StateObject *self,
PyObject *py_res,
unsigned long long requested_n_rows
) {
int rc = 0;
PyObject *py_tmp = NULL;
self->n_rows_in_batch = 0;
//if (requested_n_rows != 1) {
py_tmp = self->py_rows;
self->py_rows = PyList_New(0);
Py_XDECREF(py_tmp);
if (!self->py_rows) { rc = -1; goto error; }
rc = PyObject_SetAttr(py_res, PyStr.rows, self->py_rows);
//}
exit:
return rc;
error:
goto exit;
}
static PyType_Slot StateType_slots[] = {
{Py_tp_init, (initproc)State_init},
{Py_tp_dealloc, (destructor)State_dealloc},
{Py_tp_doc, "PyMySQL accelerator"},
{0, NULL},
};
static PyType_Spec StateType_spec = {
.name = "_singlestoredb_accel.State",
.basicsize = sizeof(StateObject),
.itemsize = 0,
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.slots = StateType_slots,
};
//
// End State
//
static void read_options(MySQLAccelOptions *options, PyObject *dict) {
if (!options || !dict) return;
PyObject *key = NULL;
PyObject *value = NULL;
Py_ssize_t pos = 0;
while (PyDict_Next(dict, &pos, &key, &value)) {
if (PyUnicode_CompareWithASCIIString(key, "results_type") == 0) {
if (PyUnicode_CompareWithASCIIString(value, "dict") == 0 ||
PyUnicode_CompareWithASCIIString(value, "dicts") == 0 ) {
options->results_type = ACCEL_OUT_DICTS;
}
else if (PyUnicode_CompareWithASCIIString(value, "namedtuple") == 0 ||
PyUnicode_CompareWithASCIIString(value, "namedtuples") == 0) {
options->results_type = ACCEL_OUT_NAMEDTUPLES;
}
else if (PyUnicode_CompareWithASCIIString(value, "structsequence") == 0 ||
PyUnicode_CompareWithASCIIString(value, "structsequences") == 0) {
options->results_type = ACCEL_OUT_STRUCTSEQUENCES;
}
else {
options->results_type = ACCEL_OUT_TUPLES;
}
} else if (PyUnicode_CompareWithASCIIString(key, "parse_json") == 0) {
options->parse_json = PyObject_IsTrue(value);
} else if (PyUnicode_CompareWithASCIIString(key, "invalid_values") == 0) {
if (PyDict_Check(value)) {
options->invalid_values = value;
}
}
}
}
static void raise_exception(
PyObject *self,
char *err_type,
unsigned long long err_code,
char *err_str
) {
PyObject *py_exc = NULL;
PyObject *py_val = NULL;
py_exc = PyObject_GetAttrString(self, err_type);
if (!py_exc) goto error;
py_val = Py_BuildValue("(Ks)", err_code, err_str);
if (!py_val) goto error;
PyErr_SetObject(py_exc, py_val);
exit:
if (py_exc) { Py_DECREF(py_exc); }
if (py_val) { Py_DECREF(py_val); }
return;
error:
goto exit;
}
static int is_error_packet(char *buff_bytes) {
return buff_bytes && *(uint8_t*)buff_bytes == 0xFF;
}
static void force_close(PyObject *py_conn) {
PyObject *py_sock = NULL;
py_sock = PyObject_GetAttr(py_conn, PyStr._sock);
if (!py_sock) goto error;
Py_XDECREF(PyObject_CallMethod(py_sock, "close", NULL));
PyErr_Clear();
PyObject_SetAttr(py_conn, PyStr._sock, Py_None);
PyObject_SetAttr(py_conn, PyStr._rfile, Py_None);
exit:
Py_XDECREF(py_sock);
return;
error:
goto exit;
}
static PyObject *read_bytes(StateObject *py_state, unsigned long long num_bytes) {
PyObject *py_num_bytes = NULL;
PyObject *py_data = NULL;
PyObject *py_exc = NULL;
if (py_state->py_read_timeout && py_state->py_read_timeout != Py_None) {
Py_XDECREF(PyObject_CallFunctionObjArgs(py_state->py_settimeout,
py_state->py_read_timeout, NULL));
if (PyErr_Occurred()) goto error;
}
py_num_bytes = PyLong_FromUnsignedLongLong(num_bytes);
if (!py_num_bytes) goto error;
while (1) {
py_data = PyObject_CallFunctionObjArgs(py_state->py_read, py_num_bytes, NULL);
if ((py_exc = PyErr_Occurred())) {
if (PyErr_ExceptionMatches(PyExc_IOError) || PyErr_ExceptionMatches(PyExc_OSError)) {
PyObject *py_errno = PyObject_GetAttr(py_exc, PyStr.x_errno);
if (!py_errno) goto error;
unsigned long long err = PyLong_AsUnsignedLongLong(py_errno);
Py_DECREF(py_errno);
if (err == 4 /* errno.EINTER */) {
continue;
}
force_close(py_state->py_conn);
raise_exception(py_state->py_conn, "OperationalError", 0,
"Lost connection to SingleStoreDB server during query");
goto error;
}
else if (PyErr_ExceptionMatches(PyExc_BaseException)) {
// Don't convert unknown exception to MySQLError.
force_close(py_state->py_conn);
goto error;
}
}
if (py_data) {
break;
}
}
if (PyBytes_Size(py_data) < (long int)num_bytes) {
force_close(py_state->py_conn);
raise_exception(py_state->py_conn, "OperationalError", 0,
"Lost connection to SingleStoreDB server during query");
goto error;
}
exit:
Py_XDECREF(py_num_bytes);
return py_data;
error:
Py_CLEAR(py_data);
goto exit;
}
static PyObject *read_packet(StateObject *py_state) {
PyObject *py_buff = NULL;
PyObject *py_new_buff = NULL;
PyObject *py_packet_header = NULL;
PyObject *py_bytes_to_read = NULL;
PyObject *py_recv_data = NULL;
unsigned long long bytes_to_read = 0;
char *buff = NULL;
uint64_t btrl = 0;
uint8_t btrh = 0;
uint8_t packet_number = 0;
py_buff = PyByteArray_FromStringAndSize(NULL, 0);
if (!py_buff) goto error;
while (1) {
py_packet_header = read_bytes(py_state, 4);
if (!py_packet_header) goto error;
buff = PyBytes_AsString(py_packet_header);
btrl = *(uint16_t*)buff;
btrh = *(uint8_t*)(buff+2);
packet_number = *(uint8_t*)(buff+3);
bytes_to_read = btrl + (btrh << 16);
Py_CLEAR(py_packet_header);