-
Notifications
You must be signed in to change notification settings - Fork 5
/
warehouse.py
899 lines (720 loc) · 32.4 KB
/
warehouse.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
"""
Provides SQL access to datasets in the library, allowing users to issue SQL queries, either as SQL
or via Sqlalchemy, to return datasets.
Example:
import ambry
l = ambry.get_library()
for row in l.warehouse.query('SELECT * FROM <partition id or vid> ... '):
print row
"""
import sqlparse
import six
import apsw
import psycopg2
from ambry_sources.med import sqlite as sqlite_med, postgresql as postgres_med
from ambry.orm.exc import NotFoundError
from ambry.identity import ObjectNumber, NotObjectNumberError, TableNumber
from ambry.util import get_logger, parse_url_to_dict
logger = get_logger(__name__)
# debug logging
#
# import logging
# logger = get_logger(__name__, level=logging.DEBUG, propagate=False)
class WarehouseError(Exception):
""" Base class for all warehouse errors. """
pass
class MissingTableError(WarehouseError):
""" Raises if warehouse does not have table for the partition. """
pass
class MissingViewError(WarehouseError):
""" Raises if warehouse does not have view associated with the table. """
pass
class Warehouse(object):
""" Provides SQL access to datasets in the library, allowing users to issue SQL queries, either as SQL
or via SQLAlchemy, to return datasets.
"""
def __init__(self, library):
# If keep_connection is true, do not close the connection until close method call.
self._library = library
warehouse_dsn = library.config.library.get('warehouse')
if not warehouse_dsn:
warehouse_dsn = library.config.library.database
if warehouse_dsn.startswith('sqlite:'):
logger.debug('Initializing sqlite warehouse.')
self._backend = SQLiteWrapper(library, warehouse_dsn)
elif warehouse_dsn.startswith('postgresql'):
logger.debug('Initializing postgres warehouse.')
self._backend = PostgreSQLWrapper(library, warehouse_dsn)
else:
raise Exception(
'Do not know how to handle {} dsn.'
.format(warehouse_dsn))
def query(self, query=''):
""" Creates virtual tables for all partitions found in the query and executes query.
Args:
query (str): sql query
"""
# FIXME: If query is empty, return a Sqlalchemy query or select object
logger.debug(
'Executing warehouse query using {} backend. \n query: {}'
.format(self._backend._dsn, query))
connection = self._backend._get_connection()
return self._backend.query(connection, query)
def install(self, ref):
""" Finds partition by reference and installs it to warehouse db.
Args:
ref (str): id, vid, name or versioned name of the partition.
"""
try:
obj_number = ObjectNumber.parse(ref)
if isinstance(obj_number, TableNumber):
table = self._library.table(ref)
connection = self._backend._get_connection()
self._backend.install_table(connection, table)
else:
# assume partition
raise NotObjectNumberError
except NotObjectNumberError:
# assume partition.
partition = self._library.partition(ref)
connection = self._backend._get_connection()
self._backend.install(connection, partition)
def materialize(self, ref):
""" Creates materialized table for given partition reference.
Args:
ref (str): id, vid, name or versioned name of the partition.
Returns:
str: name of the partition table in the database.
"""
logger.debug(
'Materializing warehouse partition.\n partition: {}'.format(ref))
partition = self._library.partition(ref)
connection = self._backend._get_connection()
return self._backend.install(connection, partition, materialize=True)
def index(self, ref, columns):
""" Create an index on the columns.
Args:
ref (str): id, vid, name or versioned name of the partition.
columns (list of str): names of the columns needed indexes.
"""
logger.debug(
'Creating index for partition.\n ref: {}, columns: {}'.format(ref, columns))
connection = self._backend._get_connection()
partition = self._library.partition(ref)
self._backend.index(connection, partition, columns)
def close(self):
""" Closes warehouse database. """
self._backend.close()
class DatabaseWrapper(object):
""" Base class for warehouse databases engines. """
def __init__(self, library, dsn):
self._library = library
self._dsn = dsn
def install(self, connection, partition, materialize=False):
""" Installs partition's mpr to the database to allow to execute sql queries over mpr.
Args:
connection:
partition (orm.Partition):
materialize (boolean): if True, create generic table. If False create MED over mpr.
Returns:
str: name of the created table.
"""
raise NotImplementedError
def install_table(self, connection, table):
""" Installs all partitons of the table and create view with union of all partitons.
Args:
connection: connection to database who stores warehouse data.
table (orm.Table):
"""
# first install all partitions of the table
queries = []
query_tmpl = 'SELECT * FROM {}'
for partition in table.partitions:
installed_name = self.install(connection, partition)
queries.append(query_tmpl.format(installed_name))
# now create view with union of all partitions.
query = 'CREATE VIEW {} AS {} '.format(
self.get_view_name(table), '\nUNION ALL\n'.join(queries))
logger.debug('Creating view for table.\n table: {}\n query: {}'.format(table.vid, query))
self._execute(connection, query, fetch=False)
def query(self, connection, query):
""" Creates virtual tables for all partitions found in the query and executes query.
Args:
query (str): sql query
"""
statements = sqlparse.parse(sqlparse.format(query, strip_comments=True))
# install all partitions and replace table names in the query.
#
logger.debug('Findind and installing all partitions from query. \n query: {}'.format(query))
new_query = []
for statement in statements:
logger.debug(
'Searching statement for partition ref.\n statement: {}'.format(statement.to_unicode()))
table_names = _get_table_names1(statement)
new_statement = statement.to_unicode()
if table_names:
for ref in table_names:
partition = None
table = None
try:
obj_number = ObjectNumber.parse(ref)
if isinstance(obj_number, TableNumber):
table = self._library.table(ref)
else:
# Do not care about other object numbers. Assume partition.
raise NotObjectNumberError
except NotObjectNumberError:
# assume partition
partition = self._library.partition(ref)
if partition:
logger.debug(
'Searching partition table in the warehouse database. \n partition: {}'
.format(partition.name))
try:
# try to use existing fdw or materialized view.
warehouse_table = self._get_warehouse_table(connection, partition)
logger.debug(
'Partition already installed. \n partition: {}'.format(partition.vid))
except MissingTableError:
# FDW is not created, create.
logger.debug(
'Partition is not installed. Install now. \n partition: {}'
.format(partition.vid))
warehouse_table = self.install(connection, partition)
new_statement = new_statement.replace(ref, warehouse_table)
if table:
logger.debug(
'Searching table view in the warehouse database. \n table: {}'
.format(table.vid))
try:
# try to use existing fdw or materialized view.
warehouse_table = self._get_warehouse_view(connection, table)
logger.debug(
'Table view already exists. \n table: {}'.format(table.vid))
except MissingTableError:
# View is not created, create.
logger.debug(
'Table view does not exist. Create now. \n table: {}'.format(table.vid))
self.install_table(connection, table)
new_statement = new_statement.replace(table.vid, self.get_view_name(table))
new_query.append(new_statement)
new_query = '\n'.join(new_query)
logger.debug(
'Executing updated query after partition install.'
'\n query before update: {}\n query to execute (updated query): {}'
.format(query, new_query))
return self._execute(connection, new_query)
def index(self, connection, partition, columns):
""" Create an index on the columns.
Args:
connection:
partition (orm.Partition):
columns (list of str): names of the columns needed indexes.
"""
raise NotImplementedError
def close(self):
""" Closes connection to database. """
raise NotImplementedError
def _get_warehouse_table(self, connection, partition):
""" Finds and returns partition table in the db represented by given connection.
Args:
connection: connection to db where to look for partition table.
partition (orm.Partition):
Raises:
MissingTableError: if database does not have partition table.
Returns:
str: database table storing partition data.
"""
raise NotImplementedError
def _get_warehouse_view(self, connection, table):
""" Finds and returns table view name in the db represented by given connection.
Args:
connection: connection to db where to look for partition table.
table (orm.Table):
Raises:
MissingViewError: if database does not have partition table.
Returns:
str: database table storing partition data.
"""
raise NotImplementedError
def _execute(self, connection, query, fetch=True):
""" Executes sql query using given connection.
Args:
connection: connection to db
query (str): sql query.
fetch (boolean, optional): if True, fetch query result and return it. If False, do not fetch.
Returns:
iterable: result of the query.
"""
raise NotImplementedError
def _get_connection(self):
""" Returns connection to database. """
raise NotImplementedError
class PostgreSQLWrapper(DatabaseWrapper):
""" Warehouse wrapper over PostgreSQL database. """
def install(self, connection, partition, materialize=False):
""" Creates FDW or materialize view for given partition.
Args:
connection: connection to postgresql
partition (orm.Partition):
materialize (boolean): if True, create read-only table. If False create virtual table.
Returns:
str: name of the created table.
"""
# FIXME: connect to 'warehouse' schema.
self._add_partition(connection, partition)
fdw_table = postgres_med.table_name(partition.vid)
view_table = '{}_v'.format(fdw_table)
if materialize:
with connection.cursor() as cursor:
view_exists = self._relation_exists(connection, view_table)
if view_exists:
logger.debug(
'Materialized view of the partition already exists.\n partition: {}, view: {}'
.format(partition.name, view_table))
else:
query = 'CREATE MATERIALIZED VIEW {} AS SELECT * FROM {};'\
.format(view_table, fdw_table)
logger.debug(
'Creating new materialized view of the partition.'
'\n partition: {}, view: {}, query: {}'
.format(partition.name, view_table, query))
cursor.execute(query)
cursor.execute('COMMIT;')
return view_table if materialize else fdw_table
@staticmethod
def get_view_name(table):
""" Returns view name of the table.
Args:
table (orm.Table):
Returns:
str:
"""
return 'partitions.{}'.format(table.vid)
def index(self, connection, partition, columns):
""" Create an index on the columns.
Args:
connection:
partition (orm.Partition):
columns (list of str):
"""
query_tmpl = '''
CREATE INDEX {index_name} ON {table_name} ({column});
'''
table_name = '{}_v'.format(postgres_med.table_name(partition.vid))
for column in columns:
query = query_tmpl.format(
index_name='{}_{}_i'.format(partition.vid, column), table_name=table_name,
column=column)
logger.debug('Creating postgres index.\n column: {}, query: {}'.format(column, query))
with connection.cursor() as cursor:
cursor.execute(query)
cursor.execute('COMMIT;')
def close(self):
""" Closes connection to database. """
if getattr(self, '_connection', None):
logger.debug('Closing postgresql connection.')
self._connection.close()
self._connection = None
if getattr(self, '_engine', None):
self._engine.dispose()
def _get_warehouse_view(self, connection, table):
""" Finds and returns table view name in the db represented by given connection.
Args:
connection: connection to db where to look for partition table.
table (orm.Table):
Raises:
MissingViewError: if database does not have partition table.
Returns:
str: database table storing partition data.
"""
logger.debug(
'Looking for view of the table.\n table: {}'.format(table.vid))
view = self.get_view_name(table)
view_exists = self._relation_exists(connection, view)
if view_exists:
logger.debug(
'View of the table found.\n table: {}, view: {}'
.format(table.vid, view))
return view
raise MissingViewError('postgres database of the warehouse does not have view for {} table.'
.format(table.vid))
def _get_warehouse_table(self, connection, partition):
""" Returns name of the table who stores partition data.
Args:
connection: connection to postgres db who stores warehouse data.
partition (orm.Partition):
Returns:
str:
Raises:
MissingTableError: if partition table not found in the warehouse db.
"""
# FIXME: This is the first candidate for optimization. Add field to partition
# with table name and update it while table creation.
# Optimized version.
#
# return partition.warehouse_table or raise exception
# Not optimized version.
#
# first check either partition has materialized view.
logger.debug(
'Looking for materialized view of the partition.\n partition: {}'.format(partition.name))
foreign_table = postgres_med.table_name(partition.vid)
view_table = '{}_v'.format(foreign_table)
view_exists = self._relation_exists(connection, view_table)
if view_exists:
logger.debug(
'Materialized view of the partition found.\n partition: {}, view: {}'
.format(partition.name, view_table))
return view_table
# now check for fdw/virtual table
logger.debug(
'Looking for foreign table of the partition.\n partition: {}'.format(partition.name))
foreign_exists = self._relation_exists(connection, foreign_table)
if foreign_exists:
logger.debug(
'Foreign table of the partition found.\n partition: {}, foreign table: {}'
.format(partition.name, foreign_table))
return foreign_table
raise MissingTableError('warehouse postgres database does not have table for {} partition.'
.format(partition.vid))
def _add_partition(self, connection, partition):
""" Creates FDW for the partition.
Args:
connection:
partition (orm.Partition):
"""
logger.debug('Creating foreign table for partition.\n partition: {}'.format(partition.name))
with connection.cursor() as cursor:
postgres_med.add_partition(cursor, partition.datafile, partition.vid)
def _get_connection(self):
""" Returns connection to the postgres database.
Returns:
connection to postgres database who stores warehouse data.
"""
if not getattr(self, '_connection', None):
logger.debug(
'Creating new connection.\n dsn: {}'
.format(self._dsn))
d = parse_url_to_dict(self._dsn)
self._connection = psycopg2.connect(
database=d['path'].strip('/'), user=d['username'], password=d['password'],
port=d['port'], host=d['hostname'])
# It takes some time to find the way how to get raw connection from sqlalchemy. So,
# I leave the commented code.
#
# self._engine = create_engine(self._dsn)
# self._connection = self._engine.raw_connection()
#
return self._connection
def _execute(self, connection, query, fetch=True):
""" Executes given query and returns result.
Args:
connection: connection to postgres database who stores warehouse data.
query (str): sql query
fetch (boolean, optional): if True, fetch query result and return it. If False, do not fetch.
Returns:
iterable with query result or None if fetch is False.
"""
# execute query
with connection.cursor() as cursor:
cursor.execute(query)
if fetch:
return cursor.fetchall()
def _relation_exists(self, connection, relation):
""" Returns True if relation exists in the postgres db. Otherwise returns False.
Args:
connection: connection to postgres database who stores warehouse data.
relation (str): name of the table, view or materialized view.
Note:
relation means table, view or materialized view here.
Returns:
boolean: True if relation exists, False otherwise.
"""
schema_name, table_name = relation.split('.')
exists_query = '''
SELECT 1
FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = %s
AND c.relname = %s
AND (c.relkind = 'r' OR c.relkind = 'v' OR c.relkind = 'm')
-- r - table, v - view, m - materialized view.
'''
with connection.cursor() as cursor:
cursor.execute(exists_query, [schema_name, table_name])
result = cursor.fetchall()
return result == [(1,)]
class SQLiteWrapper(DatabaseWrapper):
""" Warehouse wrapper over SQLite database. """
def install(self, connection, partition, materialize=False):
""" Creates virtual table or read-only table for given partition.
Args:
ref (str): id, vid, name or versioned name of the partition.
materialize (boolean): if True, create read-only table. If False create virtual table.
Returns:
str: name of the created table.
"""
self._add_partition(connection, partition)
virtual_table = sqlite_med.table_name(partition.vid)
table = '{}_v'.format(virtual_table)
if materialize:
if self._relation_exists(connection, table):
logger.debug(
'Materialized table of the partition already exists.\n partition: {}, table: {}'
.format(partition.name, table))
else:
cursor = connection.cursor()
# create table
create_query = self.__class__._get_create_query(partition, table)
logger.debug(
'Creating new materialized view of the partition.'
'\n partition: {}, view: {}, query: {}'
.format(partition.name, table, create_query))
cursor.execute(create_query)
# populate just created table with data from virtual table.
copy_query = '''INSERT INTO {} SELECT * FROM {};'''.format(table, virtual_table)
logger.debug(
'Populating sqlite table of the partition.'
'\n partition: {}, view: {}, query: {}'
.format(partition.name, table, copy_query))
cursor.execute(copy_query)
cursor.close()
return table if materialize else virtual_table
def index(self, connection, partition, columns):
""" Create an index on the columns.
Args:
connection (apsw.Connection): connection to sqlite database who stores warehouse data.
partition (orm.Partition):
columns (list of str):
"""
query_tmpl = '''
CREATE INDEX IF NOT EXISTS {index_name} ON {table_name} ({column});
'''
table_name = '{}_v'.format(sqlite_med.table_name(partition.vid))
for column in columns:
query = query_tmpl.format(
index_name='{}_{}_i'.format(partition.vid, column), table_name=table_name,
column=column)
logger.debug('Creating sqlite index.\n column: {}, query: {}'.format(column, query))
cursor = connection.cursor()
cursor.execute(query)
def close(self):
""" Closes connection to sqlite database. """
if getattr(self, '_connection', None):
logger.debug('Closing sqlite connection.')
self._connection.close()
@staticmethod
def get_view_name(table):
return table.vid
def _get_warehouse_view(self, connection, table):
""" Finds and returns table view name in the db represented by given connection.
Args:
connection: connection to db where to look for partition table.
table (orm.Table):
Raises:
MissingViewError: if database does not have partition table.
Returns:
str: database table storing partition data.
"""
logger.debug(
'Looking for view of the table.\n table: {}'.format(table.vid))
view = self.get_view_name(table)
view_exists = self._relation_exists(connection, view)
if view_exists:
logger.debug(
'View of the table exists.\n table: {}, view: {}'
.format(table.vid, view))
return view
raise MissingViewError('postgres database of the warehouse does not have view for {} table.'
.format(table.vid))
def _get_warehouse_table(self, connection, partition):
""" Returns name of the sqlite table who stores partition data.
Args:
connection (apsw.Connection): connection to sqlite database who stores warehouse data.
partition (orm.Partition):
Returns:
str:
Raises:
MissingTableError: if partition table not found in the warehouse db.
"""
# FIXME: This is the first candidate for optimization. Add field to partition
# with table name and update it while table creation.
# Optimized version.
#
# return partition.warehouse_table or raise exception
# Not optimized version.
#
# first check either partition has readonly table.
virtual_table = sqlite_med.table_name(partition.vid)
table = '{}_v'.format(virtual_table)
logger.debug(
'Looking for materialized table of the partition.\n partition: {}'.format(partition.name))
table_exists = self._relation_exists(connection, virtual_table)
if table_exists:
logger.debug(
'Materialized table of the partition found.\n partition: {}, table: {}'
.format(partition.name, table))
return table
# now check for virtual table
logger.debug(
'Looking for a virtual table of the partition.\n partition: {}'.format(partition.name))
virtual_exists = self._relation_exists(connection, virtual_table)
if virtual_exists:
logger.debug(
'Virtual table of the partition found.\n partition: {}, table: {}'
.format(partition.name, table))
return virtual_table
raise MissingTableError('warehouse postgres database does not have table for {} partition.'
.format(partition.vid))
def _relation_exists(self, connection, relation):
""" Returns True if relation (table or view) exists in the sqlite db. Otherwise returns False.
Args:
connection (apsw.Connection): connection to sqlite database who stores warehouse data.
partition (orm.Partition):
Returns:
boolean: True if relation exists, False otherwise.
"""
query = 'SELECT 1 FROM sqlite_master WHERE (type=\'table\' OR type=\'view\') AND name=?;'
cursor = connection.cursor()
cursor.execute(query, [relation])
result = cursor.fetchall()
return result == [(1,)]
@staticmethod
def _get_create_query(partition, tablename, include=None):
""" Creates and returns `CREATE TABLE ...` sql statement for given mprows.
Args:
partition (orm.Partition):
tablename (str): name of the table in the return create query.
include (list of str, optional): list of columns to include to query.
Returns:
str: create table query.
"""
TYPE_MAP = {
'int': 'INTEGER',
'float': 'REAL',
six.binary_type.__name__: 'TEXT',
six.text_type.__name__: 'TEXT',
'date': 'DATE',
'datetime': 'TIMESTAMP WITHOUT TIME ZONE'
}
columns_types = []
if not include:
include = []
for column in sorted(partition.datafile.reader.columns, key=lambda x: x['pos']):
if include and column['name'] not in include:
continue
sqlite_type = TYPE_MAP.get(column['type'])
if not sqlite_type:
raise Exception('Do not know how to convert {} to sql column.'.format(column['type']))
columns_types.append(' {} {}'.format(column['name'], sqlite_type))
columns_types_str = ',\n'.join(columns_types)
query = 'CREATE TABLE IF NOT EXISTS {}(\n{})'.format(tablename, columns_types_str)
return query
def _get_connection(self):
""" Returns connection to warehouse sqlite db.
Returns:
connection to the sqlite db who stores warehouse data.
"""
if getattr(self, '_connection', None):
logger.debug('Connection to warehouse db already exists. Using existing one.')
else:
dsn = self._dsn
if dsn == 'sqlite://':
dsn = ':memory:'
else:
dsn = dsn.replace('sqlite:///', '')
logger.debug(
'Creating new apsw connection.\n dsn: {}, config_dsn: {}'
.format(dsn, self._dsn))
self._connection = apsw.Connection(dsn)
return self._connection
def _add_partition(self, connection, partition):
""" Creates sqlite virtual table for given partition.
Args:
connection: connection to the sqlite db who stores warehouse data.
partition (orm.Partition):
"""
logger.debug('Creating virtual table for partition.\n partition: {}'.format(partition.name))
sqlite_med.add_partition(connection, partition.datafile, partition.vid)
def _execute(self, connection, query, fetch=True):
""" Executes given query using given connection.
Args:
connection (apsw.Connection): connection to the sqlite db who stores warehouse data.
query (str): sql query
fetch (boolean, optional): if True, fetch query result and return it. If False, do not fetch.
Returns:
iterable with query result.
"""
cursor = connection.cursor()
cursor.execute(query)
if fetch:
return cursor.fetchall()
def _get_table_names(statement):
""" Returns table names found in the query.
Args:
statement (sqlparse.sql.Statement): parsed by sqlparse sql statement.
Returns:
list of str
"""
tables = []
collect_table = False
for token in statement.tokens:
if token.value.lower() == 'from' or token.value.lower().endswith(' join'):
collect_table = True
continue
if isinstance(token, sqlparse.sql.Identifier) and collect_table:
tables.append(token.get_real_name())
collect_table = False
logger.debug(
'List of table names found in the statement.\n statement: {}\n tables: {}\n'
.format(statement.to_unicode(), tables))
return tables
# bundle.sql processing implementation.
# FIXME: Move to the better place.
def execute_sql(bundle, asql):
""" Executes all sql statements from asql.
Args:
bundle (FIXME:):
asql (str): unified sql - see https://github.com/CivicKnowledge/ambry/issues/140 for details.
"""
def convert_materialized_view(library, statement):
""" Finds materialized view and converts it to sqlite format.
Note:
Assume virtual tables for all partitions already created.
Args:
FIXME:
Returns:
"""
statement_str = statement.to_unicode()
if 'materialized view' in statement_str.lower():
# FIXME: Implement
# create table.
# populate table with data from select query.
pass
return statement_str
backend = SQLiteWrapper(bundle.library, bundle.library.database.dsn)
connection = backend._get_connection()
try:
statements = sqlparse.parse(sqlparse.format(asql, strip_comments=True))
for statement in statements:
statement_str = convert_materialized_view(bundle.library, statement)
if statement_str.strip():
backend.query(connection, statement_str)
finally:
backend.close()
def _convert_query(database, asql):
""" Converts simple query to database dialect.
Args:
connection:
asql: unified sql. See https://github.com/CivicKnowledge/ambry/issues/140 for details.
"""
return asql
def _get_table_names1(statement):
# Simplified version of more appropriate for ambry queryes
parts = statement.to_unicode().split()
tables = set()
for i, token in enumerate(parts):
if token.lower() == 'from' or token.lower().endswith('join'):
tables.add(parts[i + 1])
return list(tables)