def test_filter_iterator_interrupt(): expression = "?p = <http://schema.org/eligibleRegion>" iterator, card = hdtDoc.search_triples(triple['subject'], triple['predicate'], triple['object']) scan = ProjectionIterator(ScanIterator(iterator, triple, card)) iterator = FilterIterator(scan, expression) (results, saved, done) = engine.execute(iterator, 10e-7) assert len(results) <= 4 for b in results: assert b['?p'] == 'http://schema.org/eligibleRegion' assert b['?o'] in [ 'http://db.uwaterloo.ca/~galuc/wsdbm/Country0', 'http://db.uwaterloo.ca/~galuc/wsdbm/Country1', 'http://db.uwaterloo.ca/~galuc/wsdbm/Country4', 'http://db.uwaterloo.ca/~galuc/wsdbm/Country9' ] tmp = len(results) reloaded = load(saved.SerializeToString(), DummyDataset(hdtDoc, 'watdiv100')) (results, saved, done) = engine.execute(reloaded, 10e7) assert len(results) + tmp == 4 for b in results: assert b['?p'] == 'http://schema.org/eligibleRegion' assert b['?o'] in [ 'http://db.uwaterloo.ca/~galuc/wsdbm/Country0', 'http://db.uwaterloo.ca/~galuc/wsdbm/Country1', 'http://db.uwaterloo.ca/~galuc/wsdbm/Country4', 'http://db.uwaterloo.ca/~galuc/wsdbm/Country9' ] assert done
def build_query_plan(query, db_connector, saved_plan=None, projection=None): cardinalities = [] if saved_plan is not None: return load(saved_plan, db_connector), [] # optional = query['optional'] if 'optional' in query and len(query['optional']) > 0 else None root = None if query['type'] == 'union': root, cardinalities = build_union_plan(query['union'], db_connector, projection) elif query['type'] == 'bgp': root, cardinalities = build_join_plan(query['bgp'], db_connector, projection=projection) else: raise Exception('Unkown query type found during query optimization') # apply filter clause(s) if 'filters' in query and len(query['filters']) > 0: # reduce all filters in a conjunctive expression expression = reduce(lambda x, y: "({}) && ({})".format(x, y), query['filters']) root = FilterIterator(root, expression) return root, cardinalities
def execute_query(query, default_graph_uri, next_link, dataset, mimetype, url): """Execute a query using the SageEngine and returns the appropriate HTTP response""" graph_name = format_graph_uri(default_graph_uri, url) if not dataset.has_graph(graph_name): return sage_http_error( "No RDF graph matching the default URI provided was found.") graph = dataset.get_graph(graph_name) # decode next_link or build query execution plan cardinalities = dict() start = time() if next_link is not None: plan = load(decode_saved_plan(next_link), dataset) else: plan, cardinalities = parse_query(query, dataset, graph_name, url) loading_time = (time() - start) * 1000 # execute query engine = SageEngine() quota = graph.quota / 1000 max_results = graph.max_results bindings, saved_plan, is_done = engine.execute(plan, quota, max_results) # compute controls for the next page start = time() next_page = None if not is_done: next_page = encode_saved_plan(saved_plan) exportTime = (time() - start) * 1000 stats = { "cardinalities": cardinalities, "import": loading_time, "export": exportTime } # send response if mimetype == "application/sparql-results+json": return Response(responses.w3c_json_streaming(bindings, next_page, stats, url), content_type='application/json') if mimetype == "application/xml" or mimetype == "application/sparql-results+xml": return Response(responses.w3c_xml(bindings, next_page, stats), content_type="application/xml") if mimetype == "application/json": return Response(responses.raw_json_streaming(bindings, next_page, stats, url), content_type='application/json') # otherwise, return the HTML version return render_template("sage_page.html", query=query, default_graph_uri=default_graph_uri, bindings=bindings, next_page=next_page, stats=stats)
def build_query_plan(query, dataset, default_graph, saved_plan=None): """Build a pipeline of iterators used to evaluate a query""" cardinalities = [] if saved_plan is not None: return load(saved_plan, dataset), [] root = None if query['type'] == 'union': root, cardinalities = build_union_plan(query['union'], dataset, default_graph) elif query['type'] == 'bgp': root, cardinalities = build_join_plan(query['bgp'], dataset, default_graph) else: raise Exception('Unkown query type found during query optimization') # apply (possible) filter clause(s) if 'filters' in query and len(query['filters']) > 0: # exclude empty strings filters = list(filter(lambda x: len(x) > 0, query['filters'])) if len(filters) > 0: # reduce all filters in a conjunctive expression expression = reduce(lambda x, y: "({}) && ({})".format(x, y), filters) root = FilterIterator(root, expression) return root, cardinalities