From ed9c6616e9068e55d4d09574aa5e56905696b455 Mon Sep 17 00:00:00 2001 From: Niclas Hoyer Date: Tue, 9 Sep 2014 14:05:50 +0200 Subject: [PATCH 1/2] Added query optimization to GraphSailConnection --- .../oupls/sail/GraphSailConnection.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/blueprints-graph-sail/src/main/java/com/tinkerpop/blueprints/oupls/sail/GraphSailConnection.java b/blueprints-graph-sail/src/main/java/com/tinkerpop/blueprints/oupls/sail/GraphSailConnection.java index ba768ea3..fe96007b 100644 --- a/blueprints-graph-sail/src/main/java/com/tinkerpop/blueprints/oupls/sail/GraphSailConnection.java +++ b/blueprints-graph-sail/src/main/java/com/tinkerpop/blueprints/oupls/sail/GraphSailConnection.java @@ -17,9 +17,24 @@ import org.openrdf.query.BindingSet; import org.openrdf.query.Dataset; import org.openrdf.query.QueryEvaluationException; +import org.openrdf.query.algebra.QueryRoot; +import org.openrdf.query.algebra.StatementPattern; import org.openrdf.query.algebra.TupleExpr; +import org.openrdf.query.algebra.Var; import org.openrdf.query.algebra.evaluation.TripleSource; +import org.openrdf.query.algebra.evaluation.impl.BindingAssigner; +import org.openrdf.query.algebra.evaluation.impl.CompareOptimizer; +import org.openrdf.query.algebra.evaluation.impl.ConjunctiveConstraintSplitter; +import org.openrdf.query.algebra.evaluation.impl.ConstantOptimizer; +import org.openrdf.query.algebra.evaluation.impl.DisjunctiveConstraintOptimizer; +import org.openrdf.query.algebra.evaluation.impl.EvaluationStatistics; import org.openrdf.query.algebra.evaluation.impl.EvaluationStrategyImpl; +import org.openrdf.query.algebra.evaluation.impl.FilterOptimizer; +import org.openrdf.query.algebra.evaluation.impl.IterativeEvaluationOptimizer; +import org.openrdf.query.algebra.evaluation.impl.OrderLimitOptimizer; +import org.openrdf.query.algebra.evaluation.impl.QueryJoinOptimizer; +import org.openrdf.query.algebra.evaluation.impl.QueryModelNormalizer; +import org.openrdf.query.algebra.evaluation.impl.SameTermFilterOptimizer; import org.openrdf.query.algebra.evaluation.iterator.CollectionIteration; import org.openrdf.sail.SailException; import org.openrdf.sail.helpers.DefaultSailChangedEvent; @@ -100,9 +115,28 @@ public CloseableIteration evalua final Dataset dataset, final BindingSet bindings, final boolean includeInferred) throws SailException { + // allow for better optimization + tupleExpr = tupleExpr.clone(); + if (!(tupleExpr instanceof QueryRoot)) { + tupleExpr = new QueryRoot(tupleExpr); + } try { TripleSource tripleSource = new SailConnectionTripleSource(this, store.valueFactory, includeInferred); EvaluationStrategyImpl strategy = new EvaluationStrategyImpl(tripleSource, dataset); + + // query optimization + new BindingAssigner().optimize(tupleExpr, dataset, bindings); + new ConstantOptimizer(strategy).optimize(tupleExpr, dataset, bindings); + new CompareOptimizer().optimize(tupleExpr, dataset, bindings); + new SameTermFilterOptimizer().optimize(tupleExpr, dataset, bindings); + new QueryModelNormalizer().optimize(tupleExpr, dataset, bindings); + new ConjunctiveConstraintSplitter().optimize(tupleExpr, dataset, bindings); + new DisjunctiveConstraintOptimizer().optimize(tupleExpr, dataset, bindings); + new IterativeEvaluationOptimizer().optimize(tupleExpr, dataset, bindings); + new QueryJoinOptimizer(new GraphSailEvaluationStatistics()).optimize(tupleExpr, dataset, bindings); + new FilterOptimizer().optimize(tupleExpr, dataset, bindings); + new OrderLimitOptimizer().optimize(tupleExpr, dataset, bindings); + return strategy.evaluate(tupleExpr, bindings); } catch (QueryEvaluationException e) { throw new SailException(e); @@ -798,4 +832,24 @@ private Value toSesame(final String s) { throw new IllegalStateException(); } } + + protected static class GraphSailEvaluationStatistics extends EvaluationStatistics { + + public GraphSailEvaluationStatistics() { + } + + @Override + protected CardinalityCalculator createCardinalityCalculator() { + return new GraphSailCardinalityCalculator(); + } + + protected class GraphSailCardinalityCalculator extends CardinalityCalculator { + + @Override + protected double getCardinality(StatementPattern sp) { + return super.getCardinality(sp); + } + + } + } } From dc0ec88d7c78e353919dc3386a0eb0846c6a68a7 Mon Sep 17 00:00:00 2001 From: niclashoyer Date: Wed, 17 Sep 2014 11:41:32 +0200 Subject: [PATCH 2/2] Reordered optimizations --- .../blueprints/oupls/sail/GraphSailConnection.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/blueprints-graph-sail/src/main/java/com/tinkerpop/blueprints/oupls/sail/GraphSailConnection.java b/blueprints-graph-sail/src/main/java/com/tinkerpop/blueprints/oupls/sail/GraphSailConnection.java index fe96007b..bd037a38 100644 --- a/blueprints-graph-sail/src/main/java/com/tinkerpop/blueprints/oupls/sail/GraphSailConnection.java +++ b/blueprints-graph-sail/src/main/java/com/tinkerpop/blueprints/oupls/sail/GraphSailConnection.java @@ -128,12 +128,12 @@ public CloseableIteration evalua new BindingAssigner().optimize(tupleExpr, dataset, bindings); new ConstantOptimizer(strategy).optimize(tupleExpr, dataset, bindings); new CompareOptimizer().optimize(tupleExpr, dataset, bindings); - new SameTermFilterOptimizer().optimize(tupleExpr, dataset, bindings); - new QueryModelNormalizer().optimize(tupleExpr, dataset, bindings); new ConjunctiveConstraintSplitter().optimize(tupleExpr, dataset, bindings); new DisjunctiveConstraintOptimizer().optimize(tupleExpr, dataset, bindings); - new IterativeEvaluationOptimizer().optimize(tupleExpr, dataset, bindings); + new SameTermFilterOptimizer().optimize(tupleExpr, dataset, bindings); + new QueryModelNormalizer().optimize(tupleExpr, dataset, bindings); new QueryJoinOptimizer(new GraphSailEvaluationStatistics()).optimize(tupleExpr, dataset, bindings); + new IterativeEvaluationOptimizer().optimize(tupleExpr, dataset, bindings); new FilterOptimizer().optimize(tupleExpr, dataset, bindings); new OrderLimitOptimizer().optimize(tupleExpr, dataset, bindings);