use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class RdfCloudTripleStoreConnection method evaluateInternal.
@Override
protected CloseableIteration<? extends BindingSet, QueryEvaluationException> evaluateInternal(TupleExpr tupleExpr, final Dataset dataset, BindingSet bindings, final boolean flag) throws SailException {
verifyIsOpen();
logger.trace("Incoming query model:\n{}", tupleExpr.toString());
if (provenanceCollector != null) {
try {
provenanceCollector.recordQuery(tupleExpr.toString());
} catch (final ProvenanceCollectionException e) {
logger.trace("Provenance failed to record query.", e);
}
}
tupleExpr = tupleExpr.clone();
final C queryConf = (C) store.getConf().clone();
if (queryConf == null) {
// Should not happen, but this is better than a null dereference error.
throw new SailException("Cloning store.getConf() returned null, aborting.");
}
if (bindings != null) {
final Binding dispPlan = bindings.getBinding(RdfCloudTripleStoreConfiguration.CONF_QUERYPLAN_FLAG);
if (dispPlan != null) {
queryConf.setDisplayQueryPlan(Boolean.parseBoolean(dispPlan.getValue().stringValue()));
}
final Binding authBinding = bindings.getBinding(RdfCloudTripleStoreConfiguration.CONF_QUERY_AUTH);
if (authBinding != null) {
queryConf.setAuths(authBinding.getValue().stringValue().split(","));
}
final Binding ttlBinding = bindings.getBinding(RdfCloudTripleStoreConfiguration.CONF_TTL);
if (ttlBinding != null) {
queryConf.setTtl(Long.valueOf(ttlBinding.getValue().stringValue()));
}
final Binding startTimeBinding = bindings.getBinding(RdfCloudTripleStoreConfiguration.CONF_STARTTIME);
if (startTimeBinding != null) {
queryConf.setStartTime(Long.valueOf(startTimeBinding.getValue().stringValue()));
}
final Binding performantBinding = bindings.getBinding(RdfCloudTripleStoreConfiguration.CONF_PERFORMANT);
if (performantBinding != null) {
queryConf.setBoolean(RdfCloudTripleStoreConfiguration.CONF_PERFORMANT, Boolean.parseBoolean(performantBinding.getValue().stringValue()));
}
final Binding inferBinding = bindings.getBinding(RdfCloudTripleStoreConfiguration.CONF_INFER);
if (inferBinding != null) {
queryConf.setInfer(Boolean.parseBoolean(inferBinding.getValue().stringValue()));
}
final Binding useStatsBinding = bindings.getBinding(RdfCloudTripleStoreConfiguration.CONF_USE_STATS);
if (useStatsBinding != null) {
queryConf.setUseStats(Boolean.parseBoolean(useStatsBinding.getValue().stringValue()));
}
final Binding offsetBinding = bindings.getBinding(RdfCloudTripleStoreConfiguration.CONF_OFFSET);
if (offsetBinding != null) {
queryConf.setOffset(Long.parseLong(offsetBinding.getValue().stringValue()));
}
final Binding limitBinding = bindings.getBinding(RdfCloudTripleStoreConfiguration.CONF_LIMIT);
if (limitBinding != null) {
queryConf.setLimit(Long.parseLong(limitBinding.getValue().stringValue()));
}
} else {
bindings = new QueryBindingSet();
}
if (!(tupleExpr instanceof QueryRoot)) {
tupleExpr = new QueryRoot(tupleExpr);
}
try {
final List<Class<QueryOptimizer>> optimizers = queryConf.getOptimizers();
final Class<QueryOptimizer> pcjOptimizer = queryConf.getPcjOptimizer();
if (pcjOptimizer != null) {
QueryOptimizer opt = null;
try {
final Constructor<QueryOptimizer> construct = pcjOptimizer.getDeclaredConstructor(new Class[] {});
opt = construct.newInstance();
} catch (final Exception e) {
}
if (opt == null) {
throw new NoSuchMethodException("Could not find valid constructor for " + pcjOptimizer.getName());
}
if (opt instanceof Configurable) {
((Configurable) opt).setConf(conf);
}
opt.optimize(tupleExpr, dataset, bindings);
}
final ParallelEvaluationStrategyImpl strategy = new ParallelEvaluationStrategyImpl(new StoreTripleSource<C>(queryConf, ryaDAO), inferenceEngine, dataset, queryConf);
(new BindingAssigner()).optimize(tupleExpr, dataset, bindings);
(new ConstantOptimizer(strategy)).optimize(tupleExpr, dataset, bindings);
(new CompareOptimizer()).optimize(tupleExpr, dataset, bindings);
(new ConjunctiveConstraintSplitter()).optimize(tupleExpr, dataset, bindings);
(new DisjunctiveConstraintOptimizer()).optimize(tupleExpr, dataset, bindings);
(new SameTermFilterOptimizer()).optimize(tupleExpr, dataset, bindings);
(new QueryModelNormalizer()).optimize(tupleExpr, dataset, bindings);
(new IterativeEvaluationOptimizer()).optimize(tupleExpr, dataset, bindings);
if (!optimizers.isEmpty()) {
for (final Class<QueryOptimizer> optclz : optimizers) {
QueryOptimizer result = null;
try {
final Constructor<QueryOptimizer> meth = optclz.getDeclaredConstructor(new Class[] {});
result = meth.newInstance();
} catch (final Exception e) {
}
try {
final Constructor<QueryOptimizer> meth = optclz.getDeclaredConstructor(EvaluationStrategy.class);
result = meth.newInstance(strategy);
} catch (final Exception e) {
}
if (result == null) {
throw new NoSuchMethodException("Could not find valid constructor for " + optclz.getName());
}
if (result instanceof Configurable) {
((Configurable) result).setConf(conf);
}
result.optimize(tupleExpr, dataset, bindings);
}
}
(new FilterOptimizer()).optimize(tupleExpr, dataset, bindings);
(new OrderLimitOptimizer()).optimize(tupleExpr, dataset, bindings);
logger.trace("Optimized query model:\n{}", tupleExpr.toString());
if (queryConf.isInfer() && this.inferenceEngine != null) {
try {
tupleExpr.visit(new DomainRangeVisitor(queryConf, inferenceEngine));
tupleExpr.visit(new SomeValuesFromVisitor(queryConf, inferenceEngine));
tupleExpr.visit(new AllValuesFromVisitor(queryConf, inferenceEngine));
tupleExpr.visit(new HasValueVisitor(queryConf, inferenceEngine));
tupleExpr.visit(new IntersectionOfVisitor(queryConf, inferenceEngine));
tupleExpr.visit(new ReflexivePropertyVisitor(queryConf, inferenceEngine));
tupleExpr.visit(new PropertyChainVisitor(queryConf, inferenceEngine));
tupleExpr.visit(new TransitivePropertyVisitor(queryConf, inferenceEngine));
tupleExpr.visit(new SymmetricPropertyVisitor(queryConf, inferenceEngine));
tupleExpr.visit(new InverseOfVisitor(queryConf, inferenceEngine));
tupleExpr.visit(new SubPropertyOfVisitor(queryConf, inferenceEngine));
tupleExpr.visit(new SubClassOfVisitor(queryConf, inferenceEngine));
tupleExpr.visit(new SameAsVisitor(queryConf, inferenceEngine));
tupleExpr.visit(new OneOfVisitor(queryConf, inferenceEngine));
tupleExpr.visit(new HasSelfVisitor(queryConf, inferenceEngine));
} catch (final Exception e) {
logger.error("Error encountered while visiting query node.", e);
}
}
if (queryConf.isPerformant()) {
tupleExpr.visit(new SeparateFilterJoinsVisitor());
// tupleExpr.visit(new FilterTimeIndexVisitor(queryConf));
// tupleExpr.visit(new PartitionFilterTimeIndexVisitor(queryConf));
}
final FilterRangeVisitor rangeVisitor = new FilterRangeVisitor(queryConf);
tupleExpr.visit(rangeVisitor);
// this has to be done twice to get replace the statementpatterns with the right ranges
tupleExpr.visit(rangeVisitor);
EvaluationStatistics stats = null;
if (!queryConf.isUseStats() && queryConf.isPerformant() || rdfEvalStatsDAO == null) {
stats = new DefaultStatistics();
} else if (queryConf.isUseStats()) {
if (queryConf.isUseSelectivity()) {
stats = new RdfCloudTripleStoreSelectivityEvaluationStatistics<C>(queryConf, rdfEvalStatsDAO, selectEvalDAO);
} else {
stats = new RdfCloudTripleStoreEvaluationStatistics<C>(queryConf, rdfEvalStatsDAO);
}
}
if (stats != null) {
if (stats instanceof RdfCloudTripleStoreSelectivityEvaluationStatistics) {
final QueryJoinSelectOptimizer qjso = new QueryJoinSelectOptimizer(stats, selectEvalDAO);
qjso.optimize(tupleExpr, dataset, bindings);
} else {
final QueryJoinOptimizer qjo = new QueryJoinOptimizer(stats);
// TODO: Make pluggable
qjo.optimize(tupleExpr, dataset, bindings);
}
}
final CloseableIteration<BindingSet, QueryEvaluationException> iter = strategy.evaluate(tupleExpr, EmptyBindingSet.getInstance());
final CloseableIteration<BindingSet, QueryEvaluationException> iterWrap = new CloseableIteration<BindingSet, QueryEvaluationException>() {
@Override
public void remove() throws QueryEvaluationException {
iter.remove();
}
@Override
public BindingSet next() throws QueryEvaluationException {
return iter.next();
}
@Override
public boolean hasNext() throws QueryEvaluationException {
return iter.hasNext();
}
@Override
public void close() throws QueryEvaluationException {
iter.close();
strategy.shutdown();
}
};
return iterWrap;
} catch (final QueryEvaluationException e) {
throw new SailException(e);
} catch (final Exception e) {
throw new SailException(e);
}
}
use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class StatementPatternEvalTest method simpleQueryWithBindingSetSameContext.
@Test
public void simpleQueryWithBindingSetSameContext() throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
// query is used to build statement that will be evaluated
String query = "select ?x ?c where{ graph ?c {?x <uri:talksTo> <uri:Bob>. }}";
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq = parser.parseQuery(query, null);
List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
RyaStatement statement1 = new RyaStatement(new RyaURI("uri:Joe"), new RyaURI("uri:talksTo"), new RyaType("uri:Bob"), new RyaURI("uri:context1"), "", new StatementMetadata());
dao.add(statement1);
RyaStatement statement2 = new RyaStatement(new RyaURI("uri:Doug"), new RyaURI("uri:talksTo"), new RyaType("uri:Bob"), new RyaURI("uri:context2"), "", new StatementMetadata());
dao.add(statement2);
RyaStatement statement3 = new RyaStatement(new RyaURI("uri:Eric"), new RyaURI("uri:talksTo"), new RyaType("uri:Bob"), new RyaURI("uri:context3"), "", new StatementMetadata());
dao.add(statement3);
QueryBindingSet bsConstraint1 = new QueryBindingSet();
bsConstraint1.addBinding("c", new URIImpl("uri:context1"));
QueryBindingSet bsConstraint2 = new QueryBindingSet();
bsConstraint2.addBinding("c", new URIImpl("uri:context1"));
CloseableIteration<BindingSet, QueryEvaluationException> iteration = eval.evaluate(spList.get(0), Arrays.asList(bsConstraint1, bsConstraint2));
List<BindingSet> bsList = new ArrayList<>();
while (iteration.hasNext()) {
bsList.add(iteration.next());
}
Assert.assertEquals(1, bsList.size());
QueryBindingSet expected = new QueryBindingSet();
expected.addBinding("x", new URIImpl("uri:Joe"));
expected.addBinding("c", new URIImpl("uri:context1"));
Assert.assertEquals(expected, bsList.get(0));
dao.delete(Arrays.asList(statement1, statement2, statement3).iterator(), conf);
}
use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class StatementPatternEvalTest method simpleQueryNoBindingSetConstantContext.
@Test
public void simpleQueryNoBindingSetConstantContext() throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
// query is used to build statement that will be evaluated
String query = "select ?x ?c where{ graph <uri:context1> {?x <uri:talksTo> <uri:Bob>. }}";
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq = parser.parseQuery(query, null);
List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
RyaStatement statement1 = new RyaStatement(new RyaURI("uri:Joe"), new RyaURI("uri:talksTo"), new RyaType("uri:Bob"), new RyaURI("uri:context1"), "", new StatementMetadata());
dao.add(statement1);
RyaStatement statement2 = new RyaStatement(new RyaURI("uri:Doug"), new RyaURI("uri:talksTo"), new RyaType("uri:Bob"), new RyaURI("uri:context2"), "", new StatementMetadata());
dao.add(statement2);
RyaStatement statement3 = new RyaStatement(new RyaURI("uri:Eric"), new RyaURI("uri:talksTo"), new RyaType("uri:Bob"), new RyaURI("uri:context3"), "", new StatementMetadata());
dao.add(statement3);
QueryBindingSet bsConstraint1 = new QueryBindingSet();
CloseableIteration<BindingSet, QueryEvaluationException> iteration = eval.evaluate(spList.get(0), Arrays.asList(bsConstraint1));
List<BindingSet> bsList = new ArrayList<>();
while (iteration.hasNext()) {
bsList.add(iteration.next());
}
Assert.assertEquals(1, bsList.size());
QueryBindingSet expected = new QueryBindingSet();
expected.addBinding("x", new URIImpl("uri:Joe"));
Assert.assertEquals(expected, bsList.get(0));
dao.delete(Arrays.asList(statement1, statement2, statement3).iterator(), conf);
}
use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class StatementPatternEvalTest method simpleQueryWithoutBindingSets.
@Test
public void simpleQueryWithoutBindingSets() throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
// query is used to build statement that will be evaluated
String query = "select ?x ?c where{ graph ?c {?x <uri:talksTo> <uri:Bob>. }}";
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq = parser.parseQuery(query, null);
List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
RyaStatement statement1 = new RyaStatement(new RyaURI("uri:Joe"), new RyaURI("uri:talksTo"), new RyaType("uri:Bob"), new RyaURI("uri:context1"), "", new StatementMetadata());
dao.add(statement1);
RyaStatement statement2 = new RyaStatement(new RyaURI("uri:Doug"), new RyaURI("uri:talksTo"), new RyaType("uri:Bob"), new RyaURI("uri:context2"), "", new StatementMetadata());
dao.add(statement2);
RyaStatement statement3 = new RyaStatement(new RyaURI("uri:Eric"), new RyaURI("uri:talksTo"), new RyaType("uri:Bob"), new RyaURI("uri:context3"), "", new StatementMetadata());
dao.add(statement3);
QueryBindingSet bsConstraint1 = new QueryBindingSet();
CloseableIteration<BindingSet, QueryEvaluationException> iteration = eval.evaluate(spList.get(0), Arrays.asList(bsConstraint1));
List<BindingSet> bsList = new ArrayList<>();
while (iteration.hasNext()) {
bsList.add(iteration.next());
}
Assert.assertEquals(3, bsList.size());
QueryBindingSet expected1 = new QueryBindingSet();
expected1.addBinding("x", new URIImpl("uri:Joe"));
expected1.addBinding("c", new URIImpl("uri:context1"));
QueryBindingSet expected2 = new QueryBindingSet();
expected2.addBinding("x", new URIImpl("uri:Doug"));
expected2.addBinding("c", new URIImpl("uri:context2"));
QueryBindingSet expected3 = new QueryBindingSet();
expected3.addBinding("x", new URIImpl("uri:Eric"));
expected3.addBinding("c", new URIImpl("uri:context3"));
Set<BindingSet> expected = new HashSet<>(Arrays.asList(expected1, expected2, expected3));
Set<BindingSet> actual = new HashSet<>(bsList);
Assert.assertEquals(expected, actual);
dao.delete(Arrays.asList(statement1, statement2, statement3).iterator(), conf);
}
use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class EntityTupleSet method evaluate.
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(BindingSet bindings) throws QueryEvaluationException {
// into the remainder of the star query to be evaluated
if (minCard < 1000 && starQuery.size() > 2 && numberOfSpVars(minSp) == 1 && !starQuery.commonVarConstant()) {
try {
RdfCloudTripleStoreConnection conn = getRyaSailConnection();
CloseableIteration<BindingSet, QueryEvaluationException> sol = (CloseableIteration<BindingSet, QueryEvaluationException>) conn.evaluate(minSp, null, bindings, false);
Set<BindingSet> bSet = Sets.newHashSet();
while (sol.hasNext()) {
// TODO this is not optimal - should check if bindings variables intersect minSp variables
// creating the following QueryBindingSet is only necessary if no intersection occurs
QueryBindingSet bs = new QueryBindingSet();
bs.addAll(sol.next());
bs.addAll(bindings);
bSet.add(bs);
}
List<StatementPattern> spList = starQuery.getNodes();
spList.remove(minSp);
StarQuery sq = new StarQuery(spList);
conn.close();
return new EntityTupleSet(sq, conf, true).evaluate(bSet);
} catch (Exception e) {
throw new QueryEvaluationException(e);
}
} else {
this.evalOptUsed = true;
return this.evaluate(Collections.singleton(bindings));
}
}
Aggregations