Strabon

changeset 1471:faad70631f8b temporals

there is no need to call the JoinOptimizer twice (for the spatial and then for the temporal). So, I merged
the optimizers in one, by taking into account both the spatial and the temporal functions.
author Stella Giannakopoulou <sgian@di.uoa.gr>
date Mon Feb 09 19:19:29 2015 +0200 (2015-02-09)
parents beac3abdaed1
children bd7c59599449
files evaluation/src/main/java/org/openrdf/query/algebra/evaluation/impl/SpatialJoinOptimizer.java evaluation/src/main/java/org/openrdf/query/algebra/evaluation/impl/SpatioTemporalJoinOptimizer.java evaluation/src/main/java/org/openrdf/query/algebra/evaluation/impl/TemporalJoinOptimizer.java generaldb/src/main/java/org/openrdf/sail/generaldb/optimizers/GeneralDBQueryOptimizer.java
line diff
     1.1 --- a/evaluation/src/main/java/org/openrdf/query/algebra/evaluation/impl/SpatialJoinOptimizer.java	Tue Jan 27 13:21:49 2015 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,100 +0,0 @@
     1.4 -/**
     1.5 - * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 - * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 - * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.8 - * 
     1.9 - * Copyright (C) 2010, 2011, 2012, Pyravlos Team
    1.10 - * 
    1.11 - * http://www.strabon.di.uoa.gr/
    1.12 - */
    1.13 -package org.openrdf.query.algebra.evaluation.impl;
    1.14 -
    1.15 -import java.util.ArrayList;
    1.16 -import java.util.HashMap;
    1.17 -import java.util.Iterator;
    1.18 -import java.util.LinkedHashSet;
    1.19 -import java.util.List;
    1.20 -import java.util.Map;
    1.21 -import java.util.Set;
    1.22 -
    1.23 -import org.openrdf.query.BindingSet;
    1.24 -import org.openrdf.query.Dataset;
    1.25 -import org.openrdf.query.algebra.Compare;
    1.26 -import org.openrdf.query.algebra.Extension;
    1.27 -import org.openrdf.query.algebra.Filter;
    1.28 -import org.openrdf.query.algebra.FunctionCall;
    1.29 -import org.openrdf.query.algebra.Join;
    1.30 -import org.openrdf.query.algebra.LeftJoin;
    1.31 -import org.openrdf.query.algebra.MathExpr;
    1.32 -import org.openrdf.query.algebra.Or;
    1.33 -import org.openrdf.query.algebra.QueryModelNode;
    1.34 -import org.openrdf.query.algebra.StatementPattern;
    1.35 -import org.openrdf.query.algebra.StatementPattern.Scope;
    1.36 -import org.openrdf.query.algebra.TupleExpr;
    1.37 -import org.openrdf.query.algebra.ValueExpr;
    1.38 -import org.openrdf.query.algebra.Var;
    1.39 -import org.openrdf.query.algebra.evaluation.function.Function;
    1.40 -import org.openrdf.query.algebra.evaluation.function.FunctionRegistry;
    1.41 -import org.openrdf.query.algebra.evaluation.function.spatial.SpatialConstructFunc;
    1.42 -import org.openrdf.query.algebra.evaluation.function.spatial.SpatialMetricFunc;
    1.43 -import org.openrdf.query.algebra.evaluation.function.spatial.SpatialPropertyFunc;
    1.44 -import org.openrdf.query.algebra.evaluation.function.spatial.SpatialRelationshipFunc;
    1.45 -import org.openrdf.query.algebra.helpers.QueryModelVisitorBase;
    1.46 -import org.openrdf.query.algebra.helpers.StatementPatternCollector;
    1.47 -
    1.48 -/**
    1.49 - * A query optimizer that re-orders nested Joins.
    1.50 - * 
    1.51 - * @author Manos Karpathiotakis <mk@di.uoa.gr>
    1.52 - * 
    1.53 - * @author Konstantina Bereta <Konstantina.Bereta@di.uoa.gr>
    1.54 - */
    1.55 -public class SpatialJoinOptimizer extends SpatioTemporalJoinOptimizer
    1.56 -//implements QueryOptimizer //Removed it consciously 
    1.57 -{
    1.58 -
    1.59 -
    1.60 -	//private Set<String> existingVars = new TreeSet<String>();
    1.61 -	/**
    1.62 -	 * Applies generally applicable optimizations: path expressions are sorted
    1.63 -	 * from more to less specific.
    1.64 -	 * 
    1.65 -	 * @param tupleExpr
    1.66 -	 */
    1.67 -	public void optimize(TupleExpr tupleExpr, Dataset dataset, BindingSet bindings, List<TupleExpr> spatialJoins) {
    1.68 -		tupleExpr.visit(new JoinVisitor(spatialJoins));
    1.69 -	}
    1.70 -
    1.71 -
    1.72 -	protected boolean isRelevantSTFunc(FunctionCall functionCall)  
    1.73 -	{
    1.74 -		Function function = FunctionRegistry.getInstance().get(functionCall.getURI());
    1.75 -		if(function instanceof SpatialConstructFunc)
    1.76 -		{
    1.77 -			//TODO may have to comment this part again
    1.78 -			//uncommented because I use this function in the case of metrics
    1.79 -			return true;
    1.80 -		}
    1.81 -		else if(function instanceof SpatialRelationshipFunc)
    1.82 -		{
    1.83 -			return true;
    1.84 -		}
    1.85 -		else if(function instanceof SpatialPropertyFunc) //1 argument
    1.86 -		{
    1.87 -			return true;
    1.88 -		}
    1.89 -		else if(function instanceof SpatialMetricFunc) //Arguments # depends on the function selected
    1.90 -		{
    1.91 -			return true;
    1.92 -		}
    1.93 -		return false;
    1.94 -	}
    1.95 -
    1.96 -
    1.97 -	@Override
    1.98 -	public String getClassName() {
    1.99 -		// TODO Auto-generated method stub
   1.100 -		return this.getClass().getCanonicalName();
   1.101 -	}
   1.102 -
   1.103 -}
     2.1 --- a/evaluation/src/main/java/org/openrdf/query/algebra/evaluation/impl/SpatioTemporalJoinOptimizer.java	Tue Jan 27 13:21:49 2015 +0200
     2.2 +++ b/evaluation/src/main/java/org/openrdf/query/algebra/evaluation/impl/SpatioTemporalJoinOptimizer.java	Mon Feb 09 19:19:29 2015 +0200
     2.3 @@ -35,10 +35,7 @@
     2.4  import org.openrdf.query.algebra.Var;
     2.5  import org.openrdf.query.algebra.evaluation.function.Function;
     2.6  import org.openrdf.query.algebra.evaluation.function.FunctionRegistry;
     2.7 -import org.openrdf.query.algebra.evaluation.function.spatial.SpatialConstructFunc;
     2.8 -import org.openrdf.query.algebra.evaluation.function.spatial.SpatialMetricFunc;
     2.9 -import org.openrdf.query.algebra.evaluation.function.spatial.SpatialPropertyFunc;
    2.10 -import org.openrdf.query.algebra.evaluation.function.spatial.SpatialRelationshipFunc;
    2.11 +import org.openrdf.query.algebra.evaluation.function.temporal.stsparql.construct.TemporalConstructFunc;
    2.12  import org.openrdf.query.algebra.evaluation.function.temporal.stsparql.relation.TemporalRelationFunc;
    2.13  import org.openrdf.query.algebra.helpers.QueryModelVisitorBase;
    2.14  import org.openrdf.query.algebra.helpers.StatementPatternCollector;
    2.15 @@ -68,26 +65,29 @@
    2.16  	
    2.17  	public  abstract String getClassName();
    2.18  
    2.19 -	public void optimize(TupleExpr tupleExpr, Dataset dataset, BindingSet bindings, List<TupleExpr> spatialJoins) {
    2.20 -		tupleExpr.visit(new JoinVisitor(spatialJoins));
    2.21 +	public void optimize(TupleExpr tupleExpr, Dataset dataset, BindingSet bindings, List<TupleExpr> spatialJoins, List<TupleExpr> temporalJoins) {
    2.22 +		tupleExpr.visit(new JoinVisitor(spatialJoins, temporalJoins));
    2.23  	}
    2.24  
    2.25  	protected class JoinVisitor extends QueryModelVisitorBase<RuntimeException> {
    2.26  		
    2.27  		
    2.28  		
    2.29 -		public JoinVisitor(List<TupleExpr> spatialJoins) {
    2.30 +		public JoinVisitor(List<TupleExpr> spatialJoins, List<TupleExpr> temporalJoins) {
    2.31  			super();
    2.32  			this.spatialJoins = spatialJoins;
    2.33 +			this.temporalJoins = temporalJoins;
    2.34  		}
    2.35  
    2.36  		
    2.37  		private List<TupleExpr> spatialJoins;
    2.38 +		private List<TupleExpr> temporalJoins;
    2.39  		//buffer with a var as a second argument
    2.40  		private boolean problematicBuffer = false;
    2.41  
    2.42  		//indicates whether a metric expression contains a spatial function call	
    2.43  		private boolean containsSpatial = false;
    2.44 +		private boolean containsTemporal = false;
    2.45  		private boolean optimizableMetricOrProperty = true;
    2.46  
    2.47  		private int thematicJoinsSize = 0;
    2.48 @@ -271,7 +271,6 @@
    2.49  									{
    2.50  										int reduced = finalList.get(fix) - 1;
    2.51  										finalList.set(fix, reduced);
    2.52 -
    2.53  									}
    2.54  								}
    2.55  								break;
    2.56 @@ -288,6 +287,7 @@
    2.57  					{
    2.58  						{
    2.59  							Map.Entry entry = (Map.Entry)it.next();
    2.60 +							
    2.61  							if(count == position - varsMapSize)
    2.62  							{
    2.63  								//If I keep record of this entry, I can use the info later to avoid duplicate filters
    2.64 @@ -369,8 +369,10 @@
    2.65  				{
    2.66  					if(node.getCondition() instanceof FunctionCall)
    2.67  					{
    2.68 +						FunctionCall functionCall = (FunctionCall) node.getCondition();
    2.69 +						Function function = FunctionRegistry.getInstance().get(functionCall.getURI());
    2.70  						//Only interested in spatial ones
    2.71 -						if(isRelevantSTFunc((FunctionCall) node.getCondition()))
    2.72 +						if(isRelevantSTFunc(functionCall))
    2.73  						{
    2.74  							//Have to retrieve all nested variables and other info
    2.75  
    2.76 @@ -402,8 +404,10 @@
    2.77  									 */
    2.78  
    2.79  									StatementPattern t = new StatementPattern();
    2.80 -									String className = getClassName();
    2.81 -									if(className.equals("org.openrdf.query.algebra.evaluation.impl.SpatialJoinOptimizer"))
    2.82 +									
    2.83 +									//String className = getClassName();
    2.84 +									if(!(function instanceof TemporalConstructFunc) && !(function instanceof TemporalRelationFunc))
    2.85 +									//if(className.equals("org.openrdf.query.algebra.evaluation.impl.SpatialJoinOptimizer"))
    2.86  									{
    2.87  										t.setSubjectVar(new Var("-dummy-"));
    2.88  										t.setPredicateVar(new Var("-dummy-"));
    2.89 @@ -435,8 +439,9 @@
    2.90  					else if(node.getCondition() instanceof Compare)
    2.91  					{
    2.92  						containsSpatial = false;
    2.93 +						containsTemporal = false;
    2.94  						List<Var> allVars = new ArrayList<Var>(getVarsInMetricOrProperty(node.getCondition()));
    2.95 -						if(containsSpatial&&optimizableMetricOrProperty)
    2.96 +						if((containsSpatial  || containsTemporal) && optimizableMetricOrProperty)
    2.97  						{
    2.98  							//if the arguments are not 2, I am essentially doing a selection!
    2.99  							//No reason to push into optimizer then
   2.100 @@ -448,10 +453,20 @@
   2.101  								Filter toEnter = node.clone();
   2.102  
   2.103  								StatementPattern t = new StatementPattern();
   2.104 -								t.setSubjectVar(new Var("-dummy-"));
   2.105 -								t.setPredicateVar(new Var("-dummy-"));
   2.106 -								t.setObjectVar(new Var("-dummy-"));
   2.107 +								if(containsSpatial)
   2.108 +								{	
   2.109 +									t.setSubjectVar(new Var("-dummy-"));
   2.110 +									t.setPredicateVar(new Var("-dummy-"));
   2.111 +									t.setObjectVar(new Var("-dummy-"));
   2.112 +								}
   2.113 +								else if(containsTemporal)
   2.114 +								{
   2.115 +									t.setSubjectVar(new Var("-dummy-temporal"));
   2.116 +									t.setPredicateVar(new Var("-dummy-temporal"));
   2.117 +									t.setObjectVar(new Var("-dummy-temporal"));
   2.118 +								}	
   2.119  								t.setScope(Scope.DEFAULT_CONTEXTS);
   2.120 +								
   2.121  								toEnter.setArg(t);
   2.122  
   2.123  								if(!allSFilters.containsKey(toEnter))
   2.124 @@ -460,6 +475,7 @@
   2.125  								}
   2.126  							}
   2.127  							containsSpatial = false;
   2.128 +							containsTemporal = false;
   2.129  						}
   2.130  						optimizableMetricOrProperty = true;
   2.131  					}
   2.132 @@ -556,9 +572,13 @@
   2.133  					/**
   2.134  					 * There is a point in continuing the search recursively ONLY 
   2.135  					 * if I reach this case. Otherwise, the function call
   2.136 -					 * may not refer to a spatial function
   2.137 +					 * may not refer to a spatial or temporal function
   2.138  					 */
   2.139 -					this.containsSpatial = true;
   2.140 +					Function function = FunctionRegistry.getInstance().get(((FunctionCall) expr).getURI());
   2.141 +					if(!(function instanceof TemporalRelationFunc) && !(function instanceof TemporalConstructFunc))
   2.142 +						this.containsSpatial = true;
   2.143 +					else
   2.144 +						this.containsTemporal = true;
   2.145  					allVars.addAll(getFunctionCallVars((FunctionCall) expr));
   2.146  
   2.147  				}
   2.148 @@ -648,7 +668,7 @@
   2.149  		private boolean createOrderedJoins(int table[][], int lineToScan,int columnToSkip, 
   2.150  				int pathLen, Set<Var> varsTillNow, List<Integer> tempList, List<Integer> pathList, List<Integer> finalList)
   2.151  		{
   2.152 -			boolean success = false;
   2.153 + 			boolean success = false;
   2.154  			int dims = table.length;
   2.155  
   2.156  			int j;
   2.157 @@ -660,7 +680,6 @@
   2.158  				{
   2.159  					continue;
   2.160  				}
   2.161 -
   2.162  				//A connection exists!
   2.163  				if(table[lineToScan][j]>0)
   2.164  				{
     3.1 --- a/evaluation/src/main/java/org/openrdf/query/algebra/evaluation/impl/TemporalJoinOptimizer.java	Tue Jan 27 13:21:49 2015 +0200
     3.2 +++ b/evaluation/src/main/java/org/openrdf/query/algebra/evaluation/impl/TemporalJoinOptimizer.java	Mon Feb 09 19:19:29 2015 +0200
     3.3 @@ -9,36 +9,20 @@
     3.4   */
     3.5  package org.openrdf.query.algebra.evaluation.impl;
     3.6  
     3.7 -import java.util.ArrayList;
     3.8 -import java.util.HashMap;
     3.9 -import java.util.Iterator;
    3.10 -import java.util.LinkedHashSet;
    3.11  import java.util.List;
    3.12 -import java.util.Map;
    3.13 -import java.util.Set;
    3.14  
    3.15  import org.openrdf.query.BindingSet;
    3.16  import org.openrdf.query.Dataset;
    3.17 -import org.openrdf.query.algebra.Compare;
    3.18 -import org.openrdf.query.algebra.Extension;
    3.19 -import org.openrdf.query.algebra.Filter;
    3.20  import org.openrdf.query.algebra.FunctionCall;
    3.21 -import org.openrdf.query.algebra.Join;
    3.22 -import org.openrdf.query.algebra.LeftJoin;
    3.23 -import org.openrdf.query.algebra.MathExpr;
    3.24 -import org.openrdf.query.algebra.Or;
    3.25 -import org.openrdf.query.algebra.QueryModelNode;
    3.26 -import org.openrdf.query.algebra.StatementPattern;
    3.27 -import org.openrdf.query.algebra.StatementPattern.Scope;
    3.28  import org.openrdf.query.algebra.TupleExpr;
    3.29 -import org.openrdf.query.algebra.ValueExpr;
    3.30 -import org.openrdf.query.algebra.Var;
    3.31  import org.openrdf.query.algebra.evaluation.function.Function;
    3.32  import org.openrdf.query.algebra.evaluation.function.FunctionRegistry;
    3.33 +import org.openrdf.query.algebra.evaluation.function.spatial.SpatialConstructFunc;
    3.34 +import org.openrdf.query.algebra.evaluation.function.spatial.SpatialMetricFunc;
    3.35 +import org.openrdf.query.algebra.evaluation.function.spatial.SpatialPropertyFunc;
    3.36 +import org.openrdf.query.algebra.evaluation.function.spatial.SpatialRelationshipFunc;
    3.37 +import org.openrdf.query.algebra.evaluation.function.temporal.stsparql.construct.TemporalConstructFunc;
    3.38  import org.openrdf.query.algebra.evaluation.function.temporal.stsparql.relation.TemporalRelationFunc;
    3.39 -import org.openrdf.query.algebra.helpers.QueryModelVisitorBase;
    3.40 -import org.openrdf.query.algebra.helpers.StatementPatternCollector;
    3.41 -
    3.42  /**
    3.43   * A query optimizer that re-orders nested Joins.
    3.44   * 
    3.45 @@ -59,8 +43,8 @@
    3.46  	 * 
    3.47  	 * @param tupleExpr
    3.48  	 */
    3.49 -	public void optimize(TupleExpr tupleExpr, Dataset dataset, BindingSet bindings, List<TupleExpr> temporalJoins) {
    3.50 -		tupleExpr.visit(new JoinVisitor(temporalJoins));
    3.51 +	public void optimize(TupleExpr tupleExpr, Dataset dataset, BindingSet bindings, List<TupleExpr> spatialJoins, List<TupleExpr> temporalJoins) {
    3.52 +		tupleExpr.visit(new JoinVisitor(spatialJoins, temporalJoins));
    3.53  	}
    3.54  
    3.55  
    3.56 @@ -68,7 +52,7 @@
    3.57  	protected boolean isRelevantSTFunc(FunctionCall functionCall)
    3.58  	{
    3.59  		Function function = FunctionRegistry.getInstance().get(functionCall.getURI());
    3.60 -		if(function instanceof org.openrdf.query.algebra.evaluation.function.temporal.stsparql.construct.TemporalConstructFunc )
    3.61 +		if(function instanceof TemporalConstructFunc)
    3.62  		{
    3.63  			//TODO may have to comment this part again
    3.64  			//uncommented because I use this function in the case of metrics
    3.65 @@ -78,6 +62,24 @@
    3.66  		{
    3.67  			return true;
    3.68  		}
    3.69 +		else if(function instanceof SpatialConstructFunc)
    3.70 +		{
    3.71 +			//TODO may have to comment this part again
    3.72 +			//uncommented because I use this function in the case of metrics
    3.73 +			return true;
    3.74 +		}
    3.75 +		else if(function instanceof SpatialRelationshipFunc)
    3.76 +		{
    3.77 +			return true;
    3.78 +		}
    3.79 +		else if(function instanceof SpatialPropertyFunc) //1 argument
    3.80 +		{
    3.81 +			return true;
    3.82 +		}
    3.83 +		else if(function instanceof SpatialMetricFunc) //Arguments # depends on the function selected
    3.84 +		{
    3.85 +			return true;
    3.86 +		}
    3.87  		return false;
    3.88  	}
    3.89  
     4.1 --- a/generaldb/src/main/java/org/openrdf/sail/generaldb/optimizers/GeneralDBQueryOptimizer.java	Tue Jan 27 13:21:49 2015 +0200
     4.2 +++ b/generaldb/src/main/java/org/openrdf/sail/generaldb/optimizers/GeneralDBQueryOptimizer.java	Mon Feb 09 19:19:29 2015 +0200
     4.3 @@ -18,7 +18,6 @@
     4.4  import org.openrdf.query.algebra.evaluation.impl.ConjunctiveConstraintSplitter;
     4.5  import org.openrdf.query.algebra.evaluation.impl.DisjunctiveConstraintOptimizer;
     4.6  import org.openrdf.query.algebra.evaluation.impl.SameTermFilterOptimizer;
     4.7 -import org.openrdf.query.algebra.evaluation.impl.SpatialJoinOptimizer;
     4.8  import org.openrdf.query.algebra.evaluation.impl.TemporalJoinOptimizer;
     4.9  import org.openrdf.query.algebra.evaluation.impl.stSPARQLConstantOptimizer;
    4.10  import org.openrdf.sail.generaldb.GeneralDBValueFactory;
    4.11 @@ -118,9 +117,9 @@
    4.12  
    4.13  		//XXX
    4.14  	
    4.15 -		new TemporalJoinOptimizer().optimize(expr, dataset, bindings, temporalJoins);
    4.16 +		new TemporalJoinOptimizer().optimize(expr, dataset, bindings, spatialJoins, temporalJoins);
    4.17  		
    4.18 -		new SpatialJoinOptimizer().optimize(expr, dataset, bindings,spatialJoins);
    4.19 +		//new SpatialJoinOptimizer().optimize(expr, dataset, bindings,spatialJoins);
    4.20  		
    4.21  	}
    4.22  
    4.23 @@ -139,5 +138,4 @@
    4.24  
    4.25  		new GeneralDBRegexFlagsInliner().optimize(expr, dataset, bindings);
    4.26  	}
    4.27 -
    4.28  }