-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsql_interface.py
1025 lines (777 loc) · 31.6 KB
/
sql_interface.py
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
import operator
from pathlib import Path
import subprocess
import json
import dsnparse
from .utils import classify, dataclass
from .loggers import sql_log
from .context import context
from .core.sql import Sql, QueryBuilder, sqlite, postgres, mysql, duck, bigquery, quote_id, snowflake, redshift, oracle, presto
from .core.sql_import_result import sql_result_to_python, type_from_sql
from .core.pql_types import T, Type, Object, Id
from .core.exceptions import DatabaseQueryError, Signal
@dataclass
class Const(Object):
type: Type
value: object
class ConnectError(Exception):
pass
def log_sql(sql):
for i, s in enumerate(sql.split('\n')):
prefix = '/**/ ' if i else '/**/;; '
sql_log.debug(prefix+s)
class SqlInterface:
_conn: object
supports_foreign_key = True
requires_subquery_name = False
id_type_decl = 'INTEGER'
max_rows_per_query = 1024
offset_before_limit = False
def __init__(self, print_sql=False):
self._print_sql = print_sql
def query(self, sql, subqueries=None, qargs=(), quiet=False):
assert context.state
assert isinstance(sql, Sql), sql
sql_code = self.compile_sql(sql, subqueries, qargs)
if self._print_sql and not quiet:
log_sql(sql_code)
return self._execute_sql(sql.type, sql_code)
def compile_sql(self, sql, subqueries=None, qargs=()):
qb = QueryBuilder()
return sql.finalize_with_subqueries(qb, subqueries)
def commit(self):
self._conn.commit()
def rollback(self):
self._conn.rollback()
def close(self):
self._conn.close()
def list_namespaces(self):
return []
def import_table_types(self):
# Inefficient implementation but generic
tables = self.list_tables()
for table_name in tables:
table_type = self.import_table_type(table_name)
yield None, table_name, table_type
def qualified_name(self, name):
return name
def quote_name(self, name):
return f'"{name}"'
def set_active_dataset(self, name):
raise Signal.make(T.DbError, None, "set_active_dataset() is not implemented for %s" % type(self).__name__)
# from multiprocessing import Queue
import queue
import threading
from time import sleep
class TaskQueue:
def __init__(self):
self._queue = queue.Queue()
self._task_results = {}
self._closed = False
self._start_worker()
def _add_task(self, task, *args, **kwargs):
args = args or ()
kwargs = kwargs or {}
task_id = object()
self._queue.put((task_id, task, args, kwargs))
return task_id
def _start_worker(self):
self.worker = t = threading.Thread(target=self._worker)
t.daemon = True
t.start()
def _worker(self):
while not self._closed:
try:
task_id, item, args, kwargs = self._queue.get(timeout=0.1)
except queue.Empty:
continue
try:
res = item(*args, **kwargs)
except Exception as e:
res = e
self._task_results[task_id] = res
self._queue.task_done()
def _get_result(self, task_id):
while task_id not in self._task_results:
sleep(0.001)
res = self._task_results.pop(task_id)
if isinstance(res, Exception):
raise res
return res
def run_task(self, task, *args, **kwargs):
task_id = self._add_task(task, *args, **kwargs)
return self._get_result(task_id)
def close(self):
self._closed = True
class BaseConnection:
pass
class ThreadedConnection(BaseConnection):
def __init__(self, create_connection):
self._queue = TaskQueue()
self._conn = self._queue.run_task(create_connection)
def _backend_execute_sql(self, sql_code):
c = self._conn.cursor()
c.execute(sql_code)
return c
def _import_result(self, sql_type, c):
if sql_type is T.nulltype:
return None
try:
res = c.fetchall()
except Exception as e:
msg = "Exception when trying to fetch SQL result. Got error: %s"
raise DatabaseQueryError(msg%(e))
return sql_result_to_python(Const(sql_type, res))
def _execute_sql(self, state, sql_type, sql_code):
assert state
with context(state=state):
try:
c = self._backend_execute_sql(sql_code)
except Exception as e:
msg = "Exception when trying to execute SQL code:\n %s\n\nGot error: %s"
raise DatabaseQueryError(msg%(sql_code, e))
return self._import_result(sql_type, c)
def execute_sql(self, sql_type, sql_code):
return self._queue.run_task(self._execute_sql, context.state, sql_type, sql_code)
def commit(self):
self._queue.run_task(self._conn.commit)
def rollback(self):
self._queue.run_task(self._conn.rollback)
def close(self):
self._queue.run_task(self._conn.close)
self._queue.close()
class SqlInterfaceCursor(SqlInterface):
"An interface that uses the standard SQL cursor interface"
def __init__(self, *a, **kw):
super().__init__(*a, **kw)
self._conn = ThreadedConnection(self._create_connection)
def _execute_sql(self, sql_type, sql_code):
return self._conn.execute_sql(sql_type, sql_code)
def ping(self):
c = self._conn.cursor()
c.execute('select 1')
row ,= c.fetchall()
n ,= row
assert n == 1
class OracleInterface(SqlInterfaceCursor):
target = oracle
id_type_decl = "NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY"
def __init__(self, host, port, database, user, password, print_sql=False):
self._print_sql = print_sql
self.args = dict(dsn="%s/%s" % (host, database) if database else host, user=user, password=password)
super().__init__(print_sql)
def _create_connection(self):
import cx_Oracle
try:
# return cx_Oracle.connect(user="oracle", password="TzR2n6e4i7ULgSzN", dsn="127.0.0.1/ORACLE")
return cx_Oracle.connect(**self.args)
except Exception as e:
raise ConnectError(*e.args) from e
def list_tables(self):
sql_code = "SELECT table_name FROM all_tables WHERE tablespace_name = 'USERS' ORDER BY table_name"
names = self._execute_sql(T.list[T.string], sql_code)
return list(map(Id, names))
def import_table_type(self, name, columns_whitelist=None):
columns_t = T.table(dict(
name=T.string,
type=T.string,
nullable=T.string,
default=T.string,
))
columns_q = "SELECT column_name, data_type, nullable, data_default FROM USER_TAB_COLUMNS WHERE table_name = '%s'" % name.name.upper()
sql_columns = self._execute_sql(columns_t, columns_q)
wl = {}
if columns_whitelist:
wl = {c.upper(): c for c in columns_whitelist}
sql_columns = [c for c in sql_columns if c['name'] in wl]
cols = {wl.get(c['name'], c['name']): type_from_sql(c['type'], c['nullable']) for c in sql_columns}
return T.table(cols, name=name)
def quote_name(self, name):
return f'{name}'
def table_exists(self, name):
assert isinstance(name, Id)
tables = [t.lower() for t in self.list_tables()]
return name.lower() in tables
class MysqlInterface(SqlInterfaceCursor):
target = mysql
id_type_decl = "INTEGER NOT NULL AUTO_INCREMENT"
requires_subquery_name = True
def __init__(self, host, port, database, user, password, print_sql=False):
self._print_sql = print_sql
args = dict(host=host, port=port, database=database, user=user, password=password)
self._args = {k:v for k, v in args.items() if v is not None}
super().__init__(print_sql)
def _create_connection(self):
import mysql.connector
from mysql.connector import errorcode
try:
return mysql.connector.connect(charset='utf8', use_unicode=True, **self._args)
except mysql.connector.Error as e:
if e.errno == errorcode.ER_ACCESS_DENIED_ERROR:
raise ConnectError("Bad user name or password") from e
elif e.errno == errorcode.ER_BAD_DB_ERROR:
raise ConnectError("Database does not exist") from e
else:
raise ConnectError(*e.args) from e
def table_exists(self, name):
assert isinstance(name, Id)
tables = [t.lower() for t in self.list_tables()]
return name.lower() in tables
def list_tables(self):
sql_code = "SHOW TABLES"
names = self._execute_sql(T.list[T.string], sql_code)
return list(map(Id, names))
DESC_TABLE_TYPE = T.table(dict(
name=T.string,
type=T.string,
nullable=T.string,
key=T.string,
default=T.string,
extra=T.string,
))
def import_table_type(self, name, columns_whitelist=None):
assert isinstance(name, Id)
assert len(name.parts) == 1 # TODO !
columns_q = "desc %s" % name.name
sql_columns = self._execute_sql(self.DESC_TABLE_TYPE, columns_q)
if columns_whitelist:
wl = set(columns_whitelist)
sql_columns = [c for c in sql_columns if c['name'] in wl]
cols = {c['name']: type_from_sql(c['type'], c['nullable']) for c in sql_columns}
return T.table(cols, name=name)
def quote_name(self, name):
return f'`{name}`'
class PrestoInterface(SqlInterfaceCursor):
target = presto
offset_before_limit = True
DESC_TABLE_TYPE = T.table(dict(
name=T.string,
type=T.string,
unknown_empty1=T.string,
unknown_empty2=T.string,
))
def __init__(self, host, port, user, password, catalog, schema=None, print_sql=False):
self.args = dict(host=host, user=user, catalog=catalog, schema=schema)
super().__init__(print_sql)
def _create_connection(self):
import prestodb
return prestodb.dbapi.connect(**self.args)
def import_table_type(self, name, columns_whitelist=None):
assert isinstance(name, Id)
assert len(name.parts) == 1 # TODO !
columns_q = "desc %s" % name.name
sql_columns = self._execute_sql(self.DESC_TABLE_TYPE, columns_q)
if columns_whitelist:
wl = set(columns_whitelist)
sql_columns = [c for c in sql_columns if c['name'] in wl]
cols = {c['name']: type_from_sql(c['type'], False) for c in sql_columns}
return T.table(cols, name=name)
def quote_name(self, name):
return f'"{name}"'
list_tables = MysqlInterface.list_tables
class PostgresInterface(SqlInterfaceCursor):
target = postgres
id_type_decl = "SERIAL"
requires_subquery_name = True
def __init__(self, host, port, database, user, password, print_sql=False):
self.args = dict(host=host, port=port, database=database, user=user, password=password)
super().__init__(print_sql)
def _create_connection(self):
import psycopg2
import psycopg2.extras
psycopg2.extensions.set_wait_callback(psycopg2.extras.wait_select)
try:
return psycopg2.connect(**self.args)
except psycopg2.OperationalError as e:
raise ConnectError(*e.args) from e
def table_exists(self, table_id):
assert isinstance(table_id, Id)
if len(table_id.parts) == 1:
schema = 'public'
name ,= table_id.parts
elif len(table_id.parts) == 2:
schema, name = table_id.parts
else:
raise Signal.make(T.DbError, None, "Postgres doesn't support nested schemas")
sql_code = f"SELECT count(*) FROM information_schema.tables where table_name='{name}' and table_schema='{schema}'"
cnt = self._execute_sql(T.int, sql_code)
return cnt > 0
def list_tables(self):
# TODO import more schemas?
sql_code = "SELECT table_name FROM information_schema.tables where table_schema='public'"
names = self._execute_sql(T.list[T.string], sql_code)
return list(map(Id, names))
_schema_columns_t = T.table(dict(
schema=T.string,
table=T.string,
name=T.string,
pos=T.int,
nullable=T.bool,
type=T.string,
))
def import_table_type(self, table_id, columns_whitelist=None):
assert isinstance(table_id, Id)
if len(table_id.parts) == 1:
schema = 'public'
name ,= table_id.parts
elif len(table_id.parts) == 2:
schema, name = table_id.parts
else:
raise Signal.make(T.DbError, None, "Postgres doesn't support nested schemas")
columns_q = f"""SELECT table_schema, table_name, column_name, ordinal_position, is_nullable, data_type
FROM information_schema.columns
WHERE table_name = '{name}' AND table_schema = '{schema}'
"""
sql_columns = self._execute_sql(self._schema_columns_t, columns_q)
if columns_whitelist:
wl = set(columns_whitelist)
sql_columns = [c for c in sql_columns if c['name'] in wl]
cols = [(c['pos'], c['name'], type_from_sql(c['type'], c['nullable'])) for c in sql_columns]
cols.sort()
cols = dict(c[1:] for c in cols)
return T.table(cols, name=table_id)
def import_table_types(self):
columns_q = """SELECT table_schema, table_name, column_name, ordinal_position, is_nullable, data_type
FROM information_schema.columns
"""
sql_columns = self._execute_sql(self._schema_columns_t, columns_q)
columns_by_table = classify(sql_columns, lambda c: (c['schema'], c['table']))
for (schema, table_name), columns in columns_by_table.items():
cols = [(c['pos'], c['name'], type_from_sql(c['type'], c['nullable'])) for c in columns]
cols.sort()
cols = dict(c[1:] for c in cols)
# name = '%s.%s' % (schema, table_name)
yield schema, table_name, T.table(cols, name=Id(schema, table_name))
class RedshiftInterface(PostgresInterface):
target = redshift
id_type_decl = "INT IDENTITY(1,1)"
class SnowflakeInterface(SqlInterface):
target = snowflake
id_type_decl = "number autoincrement"
max_rows_per_query = 16384
def __init__(self, account, user, password, warehouse, schema, database, role=None, print_sql=False):
import logging
logging.getLogger('snowflake.connector').setLevel(logging.WARNING)
import snowflake.connector
self._client = self._conn = snowflake.connector.connect(
user=user,
password=password,
account=account,
role=role,
database=database,
warehouse=warehouse,
schema=schema,
)
self._schema = schema
self._print_sql = print_sql
def qualified_name(self, name):
"Ensure the name has a dataset"
assert isinstance(name, Id)
if len(name.parts) > 1:
# already has dataset
return name
return Id(self._schema, name.parts[-1])
def quote_name(self, name):
return f'"{name}"'
def commit(self):
self._client.commit()
def table_exists(self, name):
assert isinstance(name, Id)
tables = [t.lower() for t in self.list_tables()]
return name.lower() in tables
def list_tables(self):
sql_code = "SHOW TABLES"
table_type = T.table(dict(
created_on=T.datetime,
name=T.string,
database_name=T.string,
schema_name=T.string,
kind=T.string,
comment=T.string,
cluster_by=T.string,
rows=T.int,
bytes=T.int,
owner=T.string,
retention_time=T.string,
dropped_on=T.string,
automatic_clustering=T.string,
change_tracking=T.string,
search_optimization=T.string,
search_optimization_progress=T.string,
search_optimization_bytes=T.string,
# is_exteral=T.string,
))
tables = self._execute_sql(table_type, sql_code)
return [Id(table['name']) for table in tables]
def import_table_type(self, name, columns_whitelist=None):
assert isinstance(name, Id)
sql_code = f"DESC TABLE {quote_id(name)}"
table_type = T.table(dict(
name=T.string,
type=T.string,
kind=T.string,
null=T.bool,
default=T.string,
primary_key=T.bool,
unique_key=T.bool,
check=T.string,
expression=T.string,
comment=T.string,
policy=T.string,
))
fields = self._execute_sql(table_type, sql_code)
cols = {
f['name']: type_from_sql(f['type'], f['null'] == 'Y')
for f in fields
if columns_whitelist is None or f['name'] in columns_whitelist
}
return T.table(cols, name=name)
def _import_result(self, sql_type, c):
if sql_type is T.nulltype:
return None
try:
res = c.fetchall()
except Exception as e:
msg = "Exception when trying to fetch SQL result. Got error: %s"
raise DatabaseQueryError(msg%(e))
return sql_result_to_python(Const(sql_type, res))
def _execute_sql(self, sql_type, sql_code):
import snowflake.connector
cs = self._client.cursor()
try:
res = cs.execute(sql_code)
return self._import_result(sql_type, res)
except snowflake.connector.errors.DatabaseError as e:
msg = "Exception when trying to execute SQL code:\n %s\n\nGot error: %s"
raise DatabaseQueryError(msg%(sql_code, e))
finally:
cs.close()
def commit(self):
pass
def rollback(self):
pass
class BigQueryInterface(SqlInterface):
target = bigquery
DEFAULT_PREQL_DATASET = '_preql_d9e7334a9e028e8aa38509912dfc2aac'
supports_foreign_key = False
id_type_decl = 'STRING NOT NULL'
def __init__(self, project, dataset, print_sql=False):
from google.cloud import bigquery
# job_config = bigquery.job.QueryJobConfig(default_dataset=f'{project}._preql')
# self._client = bigquery.Client(project, default_query_job_config=job_config)
self._client = bigquery.Client(project)
self.project = project
self.dataset = dataset
self._active_dataset = dataset
self._print_sql = print_sql
self._dataset_ensured = False
def quote_name(self, name):
return f'`{name}`'
def ensure_dataset(self):
if self._dataset_ensured:
return
self._client.delete_dataset(self.DEFAULT_PREQL_DATASET, delete_contents=True, not_found_ok=True)
self._client.create_dataset(self.DEFAULT_PREQL_DATASET)
self._dataset_ensured = True
def _list_tables(self):
for ds in self._client.list_datasets():
for t in self._client.list_tables(ds.reference):
# yield t.full_table_id.replace(':', '.') # Hacky
yield Id(*t.full_table_id.replace(':', '.').split('.'))
def list_tables(self):
return list(self._list_tables())
def list_namespaces(self):
datasets = self._client.list_datasets()
return [x.reference.dataset_id for x in datasets]
def _execute_sql(self, sql_type, sql_code):
assert context.state
try:
res = list(self._client.query(sql_code))
except Exception as e:
msg = "Exception when trying to execute SQL code:\n %s\n\nGot error: %s"
raise DatabaseQueryError(msg%(sql_code, e))
if sql_type is not T.nulltype:
res = [list(i.values()) for i in res]
return sql_result_to_python(Const(sql_type, res))
_schema_columns_t = T.table(dict(
schema=T.string,
table=T.string,
name=T.string,
pos=T.int,
nullable=T.bool,
type=T.string,
))
def get_table(self, name):
from google.api_core.exceptions import NotFound, BadRequest
try:
return self._client.get_table('.'.join(name.parts))
except ValueError as e:
raise Signal.make(T.ValueError, None, f'BigQuery error: {e}')
except NotFound as e:
raise Signal.make(T.DbQueryError, None, str(e))
except BadRequest as e:
raise Signal.make(T.DbQueryError, None, str(e))
def table_exists(self, name):
try:
self.get_table(name)
except Signal:
return False
return True
def import_table_type(self, name, columns_whitelist=None):
assert isinstance(name, Id)
schema = self.get_table(name).schema
cols = {
f.name: type_from_sql(f.field_type, f.is_nullable)
for f in schema
if columns_whitelist is None or f.name in columns_whitelist
}
return T.table(cols, name=name)
def import_table_types(self):
# Inefficient implementation
tables = self.list_tables()
for table_id in tables:
table_type = self.import_table_type(table_id)
# XXX support nested schemas
schema = table_id.parts[:-1]
name = table_id.parts[-1]
yield schema[-1], name, table_type
def list_datasets(self):
return [ds.dataset_id for ds in self._client.list_datasets()]
def set_active_dataset(self, dataset):
self._client.create_dataset(dataset, exists_ok=True)
self._active_dataset = dataset
def get_default_dataset(self):
# if self._default_dataset is None:
# datasets = self.list_datasets()
# if not datasets:
# raise Signal.make(T.ValueError, None, "No dataset found.")
# self._default_dataset = datasets[0]
# return self._default_dataset
self.ensure_dataset()
return self.DEFAULT_PREQL_DATASET
def qualified_name(self, name):
"Ensure the name has a dataset"
assert isinstance(name, Id)
if len(name.parts) > 1:
# already has dataset
return name
return Id(self._active_dataset or self.get_default_dataset(), name.parts[-1])
def rollback(self):
# XXX No error? No warning?
pass
def commit(self):
# XXX No error? No warning?
pass
def close(self):
self._client.close()
class AbsSqliteInterface:
def table_exists(self, name):
assert isinstance(name, Id), name
if len(name.parts) > 1:
raise Signal.make(T.DbError, None, "Sqlite does not implement namespaces")
sql_code = "SELECT count(*) FROM sqlite_master where name='%s' and type='table'" % name.name
cnt = self._execute_sql(T.int, sql_code)
return cnt > 0
def list_tables(self):
sql_code = "SELECT name FROM sqlite_master where type='table'"
return [Id(x) for x in self._execute_sql(T.list[T.string], sql_code)]
table_schema_type = T.table(dict(
pos=T.int,
name=T.string,
type=T.string,
notnull=T.bool,
default_value=T.string,
pk=T.bool,
))
def import_table_type(self, name, columns_whitelist=None):
assert isinstance(name, Id), name
columns_q = """pragma table_info(%s)""" % quote_id(name)
sql_columns = self._execute_sql(self.table_schema_type, columns_q)
if columns_whitelist:
wl = set(columns_whitelist)
sql_columns = [c for c in sql_columns if c['name'] in wl]
cols = [(c['pos'], c['name'], type_from_sql(c['type'], not c['notnull'])) for c in sql_columns]
cols.sort()
cols = dict(c[1:] for c in cols)
pk = [[c['name']] for c in sql_columns if c['pk']]
return T.table(cols, name=name, pk=pk)
class _SqliteProduct:
def __init__(self):
self.product = 1
def step(self, value):
self.product *= value
def finalize(self):
return self.product
import math
class _SqliteStddev:
def __init__(self):
self.M = 0.0
self.S = 0.0
self.k = 1
def step(self, value):
if value is None:
return
tM = self.M
self.M += (value - tM) / self.k
self.S += (value - tM) * (value - self.M)
self.k += 1
def finalize(self):
if self.k < 3:
return None
return math.sqrt(self.S / (self.k-2))
class SqliteInterface(SqlInterfaceCursor, AbsSqliteInterface):
target = sqlite
def __init__(self, filename=None, print_sql=False):
self._filename = filename
super().__init__(print_sql)
def _create_connection(self):
import sqlite3
# sqlite3.enable_callback_tracebacks(True)
try:
conn = sqlite3.connect(self._filename or ':memory:')
except sqlite3.OperationalError as e:
raise ConnectError(*e.args) from e
def sqlite_throw(x):
raise Exception(x)
conn.create_function("power", 2, operator.pow)
conn.create_function("_pql_throw", 1, sqlite_throw)
conn.create_aggregate("_pql_product", 1, _SqliteProduct)
conn.create_aggregate("stddev", 1, _SqliteStddev)
return conn
def quote_name(self, name):
return f'[{name}]'
class DuckInterface(SqliteInterface):
target = duck
supports_foreign_key = False
requires_subquery_name = True
def _create_connection(self):
import duckdb
return duckdb.connect(self._filename or ':memory:')
def quote_name(self, name):
return f'"{name}"'
def rollback(self):
pass # XXX
class GitInterface(AbsSqliteInterface):
"Uses https://github.com/augmentable-dev/askgit"
target = sqlite
def __init__(self, path, print_sql):
self.path = path
self._print_sql = print_sql
table_schema_type = T.table(dict(
cid=T.int,
dflt_value=T.string,
name=T.string,
notnull=T.bool,
pk=T.bool,
type=T.string,
))
def _execute_sql(self, sql_type, sql_code):
assert context.state
try:
res = subprocess.check_output(['askgit', '--format', 'json', sql_code])
except FileNotFoundError:
msg = "Could not find executable 'askgit'. Make sure it's installed, and try again."
raise DatabaseQueryError(msg)
except subprocess.CalledProcessError as e:
msg = "Exception when trying to execute SQL code:\n %s\n\nGot error: %s"
raise DatabaseQueryError(msg%(sql_code, e))
return self._import_result(sql_type, res)
def _import_result(self, sql_type, c):
if sql_type is not T.nulltype:
if sql_type <= T.table:
lookup = dict(reversed(x) for x in enumerate(sql_type.elems))
rows = [json.loads(x) for x in c.split(b'\n') if x.strip()]
res = []
for row in rows:
# TODO refactor into a function
x = ["PLACEHOLDER"] * len(row)
for k, v in row.items():
x[lookup[k]] = v
assert "PLACEHOLDER" not in x, (x, row)
res.append(x)
else:
res = [list(json.loads(x).values()) for x in c.split(b'\n') if x.strip()]
return sql_result_to_python(Const(sql_type, res))
def import_table_type(self, name, columns_whitelist=None):
assert isinstance(name, Id)
assert len(name.parts) == 1 # TODO !
# TODO merge with superclass
columns_q = """pragma table_info('%s')""" % name
sql_columns = self._execute_sql(self.table_schema_type, columns_q)
if columns_whitelist:
wl = set(columns_whitelist)
sql_columns = [c for c in sql_columns if c['name'] in wl]
cols = [(c['cid'], c['name'], type_from_sql(c['type'], not c['notnull'])) for c in sql_columns]
cols.sort()
cols = dict(c[1:] for c in cols)
return T.table(cols, name=name)
def list_tables(self):
# TODO merge with superclass?
sql_code = "SELECT name FROM sqlite_master where type='table'"
res = self._execute_sql(T.table(dict(name=T.string)), sql_code)
return [Id(x['name']) for x in res]
def _drop_tables(state, *tables):
# XXX temporary. Used for testing
db = state.db
with context(state=state):
for t in tables:
t = quote_id(db.qualified_name(t))
db._execute_sql(T.nulltype, f"DROP TABLE {t};")
_SQLITE_SCHEME = 'sqlite://'
HELP_SNOWFLAKE_URI_FORMAT = 'snowflake://<user>:<pass>@<account>/<database>/<schema>?warehouse=<warehouse>'
HELP_PRESTO_URI_FORMAT = 'presto://<user>@<host>/<catalog>/<schema>'
def create_engine(db_uri, print_sql, auto_create):
if db_uri.startswith(_SQLITE_SCHEME):
# Parse sqlite:// ourselves, to allow for sqlite://c:/path/to/db
path = db_uri[len(_SQLITE_SCHEME):]
if not auto_create and path != ':memory:':
if not Path(path).exists():
raise ConnectError("File %r doesn't exist. To create it, set auto_create to True" % path)
return SqliteInterface(path, print_sql=print_sql)
dsn = dsnparse.parse(db_uri)
if len(dsn.schemes) > 1:
raise NotImplementedError("Preql doesn't support multiple schemes")
scheme ,= dsn.schemes
if scheme == 'snowflake':
if len(dsn.paths) == 1:
database, = dsn.paths
schema = dsn.query['schema']
elif len(dsn.paths) == 2:
database, schema = dsn.paths
else:
raise ValueError(f"Too many parts in path. Expected format: '{HELP_SNOWFLAKE_URI_FORMAT}'")
try:
warehouse = dsn.query["warehouse"]
except KeyError:
raise ValueError(f"Must provide warehouse. Expected format: '{HELP_SNOWFLAKE_URI_FORMAT}'")
return SnowflakeInterface(dsn.host, dsn.user, dsn.password, warehouse=warehouse, database=database, schema=schema, print_sql=print_sql)
elif scheme == "presto":
if len(dsn.paths) == 1:
catalog, = dsn.paths
schema = dsn.query.get('schema')
elif len(dsn.paths) == 2:
catalog, schema = dsn.paths
else:
raise ValueError(f"Too many parts in path. Expected format: '{HELP_PRESTO_URI_FORMAT}'")