| 1 |
from rdflib.namespace import RDF |
| 2 |
from six import text_type |
| 3 |
from sqlalchemy.sql import expression, functions |
| 4 |
|
| 5 |
from rdflib_sqlalchemy.constants import ( |
| 6 |
ASSERTED_TYPE_PARTITION, |
| 7 |
ASSERTED_NON_TYPE_PARTITION, |
| 8 |
ASSERTED_LITERAL_PARTITION, |
| 9 |
QUOTED_PARTITION, |
| 10 |
CONTEXT_SELECT, |
| 11 |
COUNT_SELECT, |
| 12 |
FULL_TRIPLE_PARTITIONS, |
| 13 |
TRIPLE_SELECT, |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
def query_analysis(query, store, connection): |
| 18 |
""" |
| 19 |
Helper function. |
| 20 |
|
| 21 |
For executing EXPLAIN on all dispatched SQL statements - |
| 22 |
for the pupose of analyzing index usage. |
| 23 |
|
| 24 |
""" |
| 25 |
res = connection.execute("explain " + query) |
| 26 |
rt = res.fetchall()[0] |
| 27 |
table, joinType, posKeys, _key, key_len, \ |
| 28 |
comparedCol, rowsExamined, extra = rt |
| 29 |
if not _key: |
| 30 |
assert joinType == "ALL" |
| 31 |
if not hasattr(store, "queryOptMarks"): |
| 32 |
store.queryOptMarks = {} |
| 33 |
hits = store.queryOptMarks.get(("FULL SCAN", table), 0) |
| 34 |
store.queryOptMarks[("FULL SCAN", table)] = hits + 1 |
| 35 |
|
| 36 |
if not hasattr(store, "queryOptMarks"): |
| 37 |
store.queryOptMarks = {} |
| 38 |
hits = store.queryOptMarks.get((_key, table), 0) |
| 39 |
store.queryOptMarks[(_key, table)] = hits + 1 |
| 40 |
|
| 41 |
|
| 42 |
def union_select(select_components, distinct=False, select_type=TRIPLE_SELECT): |
| 43 |
""" |
| 44 |
Helper function for building union all select statement. |
| 45 |
|
| 46 |
Args: |
| 47 |
select_components (iterable of tuples): Indicates the table and table type |
| 48 |
(table_name, where_clause_string, table_type) |
| 49 |
distinct (bool): Whether to eliminate duplicate results |
| 50 |
select_type (int): From `rdflib_sqlalchemy.constants`. Either `.COUNT_SELECT`, |
| 51 |
`.CONTEXT_SELECT`, `.TRIPLE_SELECT` |
| 52 |
|
| 53 |
""" |
| 54 |
selects = [] |
| 55 |
for table, whereClause, tableType in select_components: |
| 56 |
|
| 57 |
if select_type == COUNT_SELECT: |
| 58 |
c = table.c |
| 59 |
if tableType == ASSERTED_TYPE_PARTITION: |
| 60 |
cols = [c.member, c.klass] |
| 61 |
elif tableType in (ASSERTED_LITERAL_PARTITION, ASSERTED_NON_TYPE_PARTITION, QUOTED_PARTITION): |
| 62 |
cols = [c.subject, c.predicate, c.object] |
| 63 |
else: |
| 64 |
raise ValueError('Unrecognized table type {}'.format(tableType)) |
| 65 |
select_clause = expression.select(*[functions.count().label('aCount')]).select_from( |
| 66 |
expression.select(*cols).where(whereClause).distinct().select_from(table)) |
| 67 |
elif select_type == CONTEXT_SELECT: |
| 68 |
select_clause = expression.select(table.c.context) |
| 69 |
if whereClause is not None: |
| 70 |
select_clause = expression.select(table.c.context).where(whereClause) |
| 71 |
elif tableType in FULL_TRIPLE_PARTITIONS: |
| 72 |
select_clause = table.select().where(whereClause) |
| 73 |
elif tableType == ASSERTED_TYPE_PARTITION: |
| 74 |
select_clause = expression.select( |
| 75 |
*[table.c.id.label("id"), |
| 76 |
table.c.member.label("subject"), |
| 77 |
expression.literal(text_type(RDF.type)).label("predicate"), |
| 78 |
table.c.klass.label("object"), |
| 79 |
table.c.context.label("context"), |
| 80 |
table.c.termComb.label("termcomb"), |
| 81 |
expression.literal_column("NULL").label("objlanguage"), |
| 82 |
expression.literal_column("NULL").label("objdatatype")]).where( |
| 83 |
whereClause) |
| 84 |
elif tableType == ASSERTED_NON_TYPE_PARTITION: |
| 85 |
all_table_columns = [c for c in table.columns] + \ |
| 86 |
[expression.literal_column("NULL").label("objlanguage"), |
| 87 |
expression.literal_column("NULL").label("objdatatype")] |
| 88 |
if whereClause is not None: |
| 89 |
select_clause = expression.select(*all_table_columns).select_from(table).where(whereClause) |
| 90 |
else: |
| 91 |
select_clause = expression.select(*all_table_columns).select_from(table) |
| 92 |
selects.append(select_clause) |
| 93 |
|
| 94 |
order_statement = [] |
| 95 |
if select_type == TRIPLE_SELECT: |
| 96 |
order_statement = [ |
| 97 |
expression.literal_column("subject"), |
| 98 |
expression.literal_column("predicate"), |
| 99 |
expression.literal_column("object"), |
| 100 |
] |
| 101 |
if distinct and select_type != COUNT_SELECT: |
| 102 |
return expression.union(*selects).order_by(*order_statement) |
| 103 |
else: |
| 104 |
return expression.union_all(*selects).order_by(*order_statement) |