-
Notifications
You must be signed in to change notification settings - Fork 7
/
vtfunc.pyx
589 lines (485 loc) · 17.9 KB
/
vtfunc.pyx
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
from cpython.object cimport PyObject
from cpython.ref cimport Py_INCREF, Py_DECREF
from libc.float cimport DBL_MAX
from libc.stdlib cimport rand
from libc.string cimport memcpy
from libc.string cimport memset
import traceback
cdef struct sqlite3_index_constraint:
int iColumn
unsigned char op
unsigned char usable
int iTermOffset
cdef struct sqlite3_index_orderby:
int iColumn
unsigned char desc
cdef struct sqlite3_index_constraint_usage:
int argvIndex
unsigned char omit
cdef extern from "sqlite3.h":
ctypedef struct sqlite3:
int busyTimeout
ctypedef struct sqlite3_context
ctypedef struct sqlite3_value
ctypedef long long sqlite3_int64
# Virtual tables.
ctypedef struct sqlite3_module # Forward reference.
ctypedef struct sqlite3_vtab:
const sqlite3_module *pModule
int nRef
char *zErrMsg
ctypedef struct sqlite3_vtab_cursor:
sqlite3_vtab *pVtab
ctypedef struct sqlite3_index_info:
int nConstraint
sqlite3_index_constraint *aConstraint
int nOrderBy
sqlite3_index_orderby *aOrderBy
sqlite3_index_constraint_usage *aConstraintUsage
int idxNum
char *idxStr
int needToFreeIdxStr
int orderByConsumed
double estimatedCost
sqlite3_int64 estimatedRows
int idxFlags
ctypedef struct sqlite3_module:
int iVersion
int (*xCreate)(sqlite3*, void *pAux, int argc, char **argv,
sqlite3_vtab **ppVTab, char**)
int (*xConnect)(sqlite3*, void *pAux, int argc, char **argv,
sqlite3_vtab **ppVTab, char**)
int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*)
int (*xDisconnect)(sqlite3_vtab *pVTab)
int (*xDestroy)(sqlite3_vtab *pVTab)
int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor)
int (*xClose)(sqlite3_vtab_cursor*)
int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
int argc, sqlite3_value **argv)
int (*xNext)(sqlite3_vtab_cursor*)
int (*xEof)(sqlite3_vtab_cursor*)
int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context *, int)
int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid)
int (*xUpdate)(sqlite3_vtab *pVTab, int, sqlite3_value **,
sqlite3_int64 **)
int (*xBegin)(sqlite3_vtab *pVTab)
int (*xSync)(sqlite3_vtab *pVTab)
int (*xCommit)(sqlite3_vtab *pVTab)
int (*xRollback)(sqlite3_vtab *pVTab)
int (*xFindFunction)(sqlite3_vtab *pVTab, int nArg, const char *zName,
void (**pxFunc)(sqlite3_context *, int,
sqlite3_value **),
void **ppArg)
int (*xRename)(sqlite3_vtab *pVTab, const char *zNew)
int (*xSavepoint)(sqlite3_vtab *pVTab, int)
int (*xRelease)(sqlite3_vtab *pVTab, int)
int (*xRollbackTo)(sqlite3_vtab *pVTab, int)
cdef int sqlite3_declare_vtab(sqlite3 *db, const char *zSQL)
cdef int sqlite3_create_module(sqlite3 *db, const char *zName,
const sqlite3_module *p, void *pClientData)
# Encoding.
cdef int SQLITE_UTF8 = 1
# Return values.
cdef int SQLITE_OK = 0
cdef int SQLITE_ERROR = 1
cdef int SQLITE_NOMEM = 7
cdef int SQLITE_OK_LOAD_PERMANENTLY = 256 # SQLite >= 3.14.
# Types of filtering operations.
cdef int SQLITE_INDEX_CONSTRAINT_EQ = 2
cdef int SQLITE_INDEX_CONSTRAINT_GT = 4
cdef int SQLITE_INDEX_CONSTRAINT_LE = 8
cdef int SQLITE_INDEX_CONSTRAINT_LT = 16
cdef int SQLITE_INDEX_CONSTRAINT_GE = 32
cdef int SQLITE_INDEX_CONSTRAINT_MATCH = 64
# sqlite_value_type.
cdef int SQLITE_INTEGER = 1
cdef int SQLITE_FLOAT = 2
cdef int SQLITE3_TEXT = 3
cdef int SQLITE_TEXT = 3
cdef int SQLITE_BLOB = 4
cdef int SQLITE_NULL = 5
ctypedef void (*sqlite3_destructor_type)(void*)
cdef int sqlite3_create_function(
sqlite3 *db,
const char *zFunctionName,
int nArg,
int eTextRep, # SQLITE_UTF8
void *pApp, # App-specific data.
void (*xFunc)(sqlite3_context *, int, sqlite3_value **),
void (*xStep)(sqlite3_context*, int, sqlite3_value **),
void (*xFinal)(sqlite3_context*),
)
# Converting from Sqlite -> Python.
cdef const void *sqlite3_value_blob(sqlite3_value*);
cdef int sqlite3_value_bytes(sqlite3_value*);
cdef double sqlite3_value_double(sqlite3_value*);
cdef int sqlite3_value_int(sqlite3_value*);
cdef sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
cdef const unsigned char *sqlite3_value_text(sqlite3_value*);
cdef int sqlite3_value_type(sqlite3_value*);
cdef int sqlite3_value_numeric_type(sqlite3_value*);
# Converting from Python -> Sqlite.
cdef void sqlite3_result_double(sqlite3_context*, double)
cdef void sqlite3_result_error(sqlite3_context*, const char*, int)
cdef void sqlite3_result_error_toobig(sqlite3_context*)
cdef void sqlite3_result_error_nomem(sqlite3_context*)
cdef void sqlite3_result_error_code(sqlite3_context*, int)
cdef void sqlite3_result_int(sqlite3_context*, int)
cdef void sqlite3_result_int64(sqlite3_context*, sqlite3_int64)
cdef void sqlite3_result_null(sqlite3_context*)
cdef void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*))
cdef void sqlite3_result_value(sqlite3_context*, sqlite3_value*)
# Memory management.
cdef void* sqlite3_malloc(int)
cdef void sqlite3_free(void *)
# Misc.
cdef int sqlite3_busy_handler(sqlite3 *db, int(*)(void *, int), void *)
cdef int sqlite3_sleep(int ms)
cdef extern from "_pysqlite/connection.h":
ctypedef struct pysqlite_Connection:
sqlite3 *db
cdef inline bytes encode(key):
cdef bytes bkey
if isinstance(key, unicode):
bkey = <bytes>key.encode('utf-8')
else:
bkey = <bytes>key
return bkey
cdef inline unicode decode(key):
cdef unicode ukey
if isinstance(key, bytes):
ukey = <unicode>key.decode('utf-8')
else:
ukey = <unicode>key
return ukey
ctypedef struct peewee_vtab:
sqlite3_vtab base
void *table_func_cls # Pointer to the user-defined table function.
ctypedef struct peewee_cursor:
sqlite3_vtab_cursor base
long long idx
void *table_func # Pointer to the table function.
void *row_data # Pointer to the current row data.
bint stopped # Did we run out of results?
cdef void sqlite_error(sqlite3_context *ctx, err_msg):
sqlite3_result_error(ctx, encode(err_msg), -1)
cdef int pwConnect(sqlite3 *db, void *pAux, int argc, char **argv,
sqlite3_vtab **ppVtab, char **pzErr) with gil:
cdef:
int rc
object table_func_cls = <object>pAux
peewee_vtab *pNew
rc = sqlite3_declare_vtab(
db,
encode('CREATE TABLE x(%s);' %
table_func_cls.get_table_columns_declaration()))
if rc == SQLITE_OK:
pNew = <peewee_vtab *>sqlite3_malloc(sizeof(pNew[0]))
memset(<char *>pNew, 0, sizeof(pNew[0]))
ppVtab[0] = &(pNew.base) # Point ppVtab at our custom vtab struct.
pNew.table_func_cls = <void *>table_func_cls
Py_INCREF(table_func_cls)
return rc
cdef int pwDisconnect(sqlite3_vtab *pBase) with gil:
cdef:
peewee_vtab *pVtab = <peewee_vtab *>pBase
object table_func_cls = <object>(pVtab.table_func_cls)
Py_DECREF(table_func_cls)
sqlite3_free(pVtab)
return SQLITE_OK
cdef int pwOpen(sqlite3_vtab *pBase, sqlite3_vtab_cursor **ppCursor) with gil:
cdef:
peewee_vtab *pVtab = <peewee_vtab *>pBase
peewee_cursor *pCur
object table_func_cls = <object>pVtab.table_func_cls
pCur = <peewee_cursor *>sqlite3_malloc(sizeof(pCur[0]))
memset(<char *>pCur, 0, sizeof(pCur[0]))
ppCursor[0] = &(pCur.base)
pCur.idx = 0
try:
table_func = table_func_cls()
except:
if table_func_cls.print_tracebacks:
traceback.print_exc()
sqlite3_free(pCur)
return SQLITE_ERROR
Py_INCREF(table_func)
pCur.table_func = <void *>table_func
pCur.stopped = False
return SQLITE_OK
cdef int pwClose(sqlite3_vtab_cursor *pBase) with gil:
cdef:
peewee_cursor *pCur = <peewee_cursor *>pBase
object table_func = <object>pCur.table_func
Py_DECREF(table_func)
sqlite3_free(pCur)
return SQLITE_OK
cdef int pwNext(sqlite3_vtab_cursor *pBase) with gil:
cdef:
peewee_cursor *pCur = <peewee_cursor *>pBase
object table_func = <object>pCur.table_func
tuple result
if pCur.row_data:
Py_DECREF(<tuple>pCur.row_data)
pCur.row_data = NULL
try:
result = tuple(table_func.iterate(pCur.idx))
except StopIteration:
pCur.stopped = True
except:
if table_func.print_tracebacks:
traceback.print_exc()
return SQLITE_ERROR
else:
Py_INCREF(result)
pCur.row_data = <void *>result
pCur.idx += 1
pCur.stopped = False
return SQLITE_OK
cdef int pwColumn(sqlite3_vtab_cursor *pBase, sqlite3_context *ctx,
int iCol) with gil:
cdef:
bytes bval
peewee_cursor *pCur = <peewee_cursor *>pBase
sqlite3_int64 x = 0
tuple row_data
if iCol == -1:
sqlite3_result_int64(ctx, <sqlite3_int64>pCur.idx)
return SQLITE_OK
if not pCur.row_data:
sqlite_error(ctx, 'error: row data not available.')
return SQLITE_ERROR
row_data = <tuple>pCur.row_data
value = row_data[iCol]
if value is None:
sqlite3_result_null(ctx)
elif isinstance(value, (int, long)):
sqlite3_result_int64(ctx, <sqlite3_int64>value)
elif isinstance(value, float):
sqlite3_result_double(ctx, <double>value)
elif isinstance(value, basestring):
bval = encode(value)
sqlite3_result_text(
ctx,
<const char *>bval,
-1,
<sqlite3_destructor_type>-1)
elif isinstance(value, bool):
sqlite3_result_int(ctx, int(value))
else:
sqlite_error(ctx, 'Unsupported type %s' % type(value))
return SQLITE_ERROR
return SQLITE_OK
cdef int pwRowid(sqlite3_vtab_cursor *pBase, sqlite3_int64 *pRowid):
cdef:
peewee_cursor *pCur = <peewee_cursor *>pBase
pRowid[0] = <sqlite3_int64>pCur.idx
return SQLITE_OK
cdef int pwEof(sqlite3_vtab_cursor *pBase):
cdef:
peewee_cursor *pCur = <peewee_cursor *>pBase
if pCur.stopped:
return 1
return 0
cdef int pwFilter(sqlite3_vtab_cursor *pBase, int idxNum,
const char *idxStr, int argc, sqlite3_value **argv) with gil:
cdef:
peewee_cursor *pCur = <peewee_cursor *>pBase
object table_func = <object>pCur.table_func
dict query = {}
int idx
int value_type
tuple row_data
void *row_data_raw
if not idxStr or argc == 0 and len(table_func.params):
return SQLITE_ERROR
elif idxStr:
params = decode(idxStr).split(',')
else:
params = []
for idx, param in enumerate(params):
value = argv[idx]
if not value:
query[param] = None
continue
value_type = sqlite3_value_type(value)
if value_type == SQLITE_INTEGER:
query[param] = sqlite3_value_int(value)
elif value_type == SQLITE_FLOAT:
query[param] = sqlite3_value_double(value)
elif value_type == SQLITE_TEXT:
query[param] = decode(sqlite3_value_text(value))
elif value_type == SQLITE_BLOB:
query[param] = <bytes>sqlite3_value_blob(value)
elif value_type == SQLITE_NULL:
query[param] = None
else:
query[param] = None
try:
table_func.initialize(**query)
except:
if table_func.print_tracebacks:
traceback.print_exc()
return SQLITE_ERROR
pCur.stopped = False
try:
row_data = tuple(table_func.iterate(0))
except StopIteration:
pCur.stopped = True
except Exception as exc:
if table_func.print_tracebacks:
traceback.print_exc()
return SQLITE_ERROR
else:
Py_INCREF(row_data)
pCur.row_data = <void *>row_data
pCur.idx += 1
return SQLITE_OK
cdef int pwBestIndex(sqlite3_vtab *pBase, sqlite3_index_info *pIdxInfo) \
with gil:
cdef:
int i
int idxNum = 0, nArg = 0
peewee_vtab *pVtab = <peewee_vtab *>pBase
object table_func_cls = <object>pVtab.table_func_cls
sqlite3_index_constraint *pConstraint
list columns = []
char *idxStr
int nParams = len(table_func_cls.params)
pConstraint = <sqlite3_index_constraint*>0
for i in range(pIdxInfo.nConstraint):
pConstraint = &pIdxInfo.aConstraint[i]
if not pConstraint.usable:
continue
if pConstraint.op != SQLITE_INDEX_CONSTRAINT_EQ:
continue
columns.append(table_func_cls.params[pConstraint.iColumn -
table_func_cls._ncols])
nArg += 1
pIdxInfo.aConstraintUsage[i].argvIndex = nArg
pIdxInfo.aConstraintUsage[i].omit = 1
if nArg > 0:
if nArg == nParams:
# All parameters are present, this is ideal.
pIdxInfo.estimatedCost = <double>1
pIdxInfo.estimatedRows = 10
else:
# Penalize score based on number of missing params.
pIdxInfo.estimatedCost = <double>10000000000000 * <double>(nParams - nArg)
pIdxInfo.estimatedRows = 10 ** (nParams - nArg)
joinedCols = encode(','.join(columns))
idxStr = <char *>sqlite3_malloc((len(joinedCols) + 1) * sizeof(char))
memcpy(idxStr, <char *>joinedCols, len(joinedCols))
idxStr[len(joinedCols)] = '\x00'
pIdxInfo.idxStr = idxStr
pIdxInfo.needToFreeIdxStr = 0
else:
pIdxInfo.estimatedCost = DBL_MAX
pIdxInfo.estimatedRows = 100000
return SQLITE_OK
cdef class _TableFunctionImpl(object):
cdef:
sqlite3_module module
object table_function
def __cinit__(self, table_function):
self.table_function = table_function
cdef create_module(self, pysqlite_Connection* sqlite_conn):
cdef:
bytes name = encode(self.table_function.name)
sqlite3 *db = sqlite_conn.db
int rc
# Populate the SQLite module struct members.
self.module.iVersion = 0
self.module.xCreate = NULL # NULL indicates eponymous *only*.
self.module.xConnect = pwConnect
self.module.xBestIndex = pwBestIndex
self.module.xDisconnect = pwDisconnect
self.module.xDestroy = NULL
self.module.xOpen = pwOpen
self.module.xClose = pwClose
self.module.xFilter = pwFilter
self.module.xNext = pwNext
self.module.xEof = pwEof
self.module.xColumn = pwColumn
self.module.xRowid = pwRowid
self.module.xUpdate = NULL
self.module.xBegin = NULL
self.module.xSync = NULL
self.module.xCommit = NULL
self.module.xRollback = NULL
self.module.xFindFunction = NULL
self.module.xRename = NULL
rc = sqlite3_create_module(
db,
<const char *>name,
&self.module,
<void *>(self.table_function))
Py_INCREF(self)
return rc == SQLITE_OK
class TableFunction(object):
"""
Implements a table-valued function (eponymous-only virtual table) in
SQLite.
Subclasses must define the columns (return values) and params (input
values) to their function. These are defined as class attributes.
The subclass also must implement two functions:
* initialize(**query)
* iterate(idx)
The `initialize` function accepts the query parameters passed in from
the SQL query. The `iterate` function accepts the index of the current
iteration (zero-based) and must return a tuple of result values or raise
a `StopIteration` to signal no more results.
"""
columns = None
params = None
name = None
print_tracebacks = True
_ncols = None
@classmethod
def register(cls, conn):
cdef _TableFunctionImpl impl = _TableFunctionImpl(cls)
impl.create_module(<pysqlite_Connection *>conn)
cls._ncols = len(cls.columns)
def initialize(self, **filters):
raise NotImplementedError
def iterate(self, idx):
raise NotImplementedError
@classmethod
def get_table_columns_declaration(cls):
cdef list accum = []
for column in cls.columns:
if isinstance(column, tuple):
if len(column) != 2:
raise ValueError('Column must be either a string or a '
'2-tuple of name, type')
accum.append('%s %s' % column)
else:
accum.append(column)
for param in cls.params:
accum.append('%s HIDDEN' % param)
return ', '.join(accum)
def aggressive_busy_handler(sqlite_conn, timeout=5000):
cdef:
int n = timeout
sqlite3 *db = (<pysqlite_Connection *>sqlite_conn).db
sqlite3_busy_handler(db, _aggressive_busy_handler, <void *>n)
return True
cdef int _aggressive_busy_handler(void *ptr, int n):
cdef:
int busyTimeout = <int>ptr
int current, total
if n < 20:
current = 25 - (rand() % 10) # ~20ms
total = n * 20
elif n < 40:
current = 50 - (rand() % 20) # ~40ms
total = 400 + ((n - 20) * 40)
else:
current = 120 - (rand() % 40) # ~100ms
total = 1200 + ((n - 40) * 100)
if total + current > busyTimeout:
current = busyTimeout - total
if current > 0:
sqlite3_sleep(current)
return 1
return 0