Strabon

changeset 904:e0e85317a1c4

fixes ticket #22
author Stella Giannakopoulou <sgian@di.uoa.gr>
date Fri Mar 22 16:44:16 2013 +0200 (2013-03-22)
parents 47fe852f59f2
children ba5c6c56ac94
files generaldb/src/main/java/org/openrdf/sail/generaldb/optimizers/AggregateOptimizer.java generaldb/src/main/java/org/openrdf/sail/generaldb/optimizers/GeneralDBQueryOptimizer.java
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/generaldb/src/main/java/org/openrdf/sail/generaldb/optimizers/AggregateOptimizer.java	Fri Mar 22 16:44:16 2013 +0200
     1.3 @@ -0,0 +1,85 @@
     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.sail.generaldb.optimizers;
    1.14 +
    1.15 +import java.util.Iterator;
    1.16 +
    1.17 +import org.openrdf.query.algebra.Extension;
    1.18 +import org.openrdf.query.algebra.ExtensionElem;
    1.19 +import org.openrdf.query.algebra.FunctionCall;
    1.20 +import org.openrdf.query.algebra.Group;
    1.21 +import org.openrdf.query.algebra.TupleExpr;
    1.22 +import org.openrdf.query.algebra.ValueExpr;
    1.23 +import org.openrdf.query.algebra.evaluation.function.Function;
    1.24 +import org.openrdf.query.algebra.evaluation.function.FunctionRegistry;
    1.25 +import org.openrdf.query.algebra.evaluation.function.spatial.stsparql.aggregate.ExtentFunc;
    1.26 +import org.openrdf.query.algebra.evaluation.function.spatial.stsparql.construct.UnionFunc;
    1.27 +import org.openrdf.sail.generaldb.algebra.base.GeneralDBQueryModelVisitorBase;
    1.28 +/**
    1.29 + *
    1.30 + * @author Stella Giannakopoulou <sgian@di.uoa.gr>
    1.31 + */
    1.32 +
    1.33 +public class AggregateOptimizer extends GeneralDBQueryModelVisitorBase<RuntimeException>
    1.34 +{
    1.35 +	public void optimize(TupleExpr tupleExpr)
    1.36 +	{
    1.37 +		tupleExpr.visit(this);
    1.38 +	}
    1.39 +
    1.40 +	@Override
    1.41 +	public void meet(Extension node) throws RuntimeException
    1.42 +	{
    1.43 +		if(!(node.getArg() instanceof Group))
    1.44 +		{
    1.45 +			Iterator<ExtensionElem> iter = node.getElements().iterator();
    1.46 +
    1.47 +			while(iter.hasNext())
    1.48 +			{
    1.49 +				ExtensionElem elem = iter.next();
    1.50 +				ValueExpr expr = elem.getExpr();
    1.51 +
    1.52 +				if(aggregateInQuery(expr) == true) //Union (or Extent) is used as an aggregate function on Select Clause!
    1.53 +				{
    1.54 +					Group group = new Group((TupleExpr) node.getArg());
    1.55 +					group.setParentNode(node.getArg().getParentNode());
    1.56 +					node.replaceChildNode(node.getArg(), group);
    1.57 +
    1.58 +					break;
    1.59 +				}
    1.60 +			}
    1.61 +		}
    1.62 +	}
    1.63 +
    1.64 +
    1.65 +	private boolean aggregateInQuery(ValueExpr expr)
    1.66 +	{
    1.67 +		if(expr instanceof FunctionCall)
    1.68 +		{
    1.69 +			Function function = FunctionRegistry.getInstance().get(((FunctionCall) expr).getURI());
    1.70 +			if((!(function instanceof UnionFunc) || !(((FunctionCall) expr).getArgs().size()==1))&&!(function instanceof ExtentFunc))
    1.71 +			{
    1.72 +				//Recursively check arguments
    1.73 +				boolean unionPresent = false;
    1.74 +				for(int i = 0 ; i< ((FunctionCall) expr).getArgs().size(); i++)
    1.75 +				{
    1.76 +					unionPresent = unionPresent || aggregateInQuery(((FunctionCall) expr).getArgs().get(i));
    1.77 +				}
    1.78 +				return unionPresent;
    1.79 +			}
    1.80 +			else
    1.81 +				return true;
    1.82 +		}
    1.83 +		else //var
    1.84 +		{
    1.85 +			return false;
    1.86 +		}
    1.87 +	}
    1.88 +}
     2.1 --- a/generaldb/src/main/java/org/openrdf/sail/generaldb/optimizers/GeneralDBQueryOptimizer.java	Fri Mar 22 16:38:53 2013 +0200
     2.2 +++ b/generaldb/src/main/java/org/openrdf/sail/generaldb/optimizers/GeneralDBQueryOptimizer.java	Fri Mar 22 16:44:16 2013 +0200
     2.3 @@ -16,7 +16,6 @@
     2.4  import org.openrdf.query.algebra.evaluation.impl.BindingAssigner;
     2.5  import org.openrdf.query.algebra.evaluation.impl.CompareOptimizer;
     2.6  import org.openrdf.query.algebra.evaluation.impl.ConjunctiveConstraintSplitter;
     2.7 -import org.openrdf.query.algebra.evaluation.impl.ConstantOptimizer;
     2.8  import org.openrdf.query.algebra.evaluation.impl.DisjunctiveConstraintOptimizer;
     2.9  import org.openrdf.query.algebra.evaluation.impl.SameTermFilterOptimizer;
    2.10  import org.openrdf.query.algebra.evaluation.impl.SpatialJoinOptimizer;
    2.11 @@ -85,8 +84,8 @@
    2.12  			tupleExpr = new QueryRoot(tupleExpr);
    2.13  		}
    2.14  
    2.15 +		fixAggregates(tupleExpr);
    2.16  		coreOptimizations(strategy, tupleExpr, dataset, bindings);
    2.17 -
    2.18  		rdbmsOptimizations(tupleExpr, dataset, bindings);
    2.19  
    2.20  		new GeneralDBSqlConstantOptimizer().optimize(tupleExpr, dataset, bindings);
    2.21 @@ -94,6 +93,12 @@
    2.22  		return tupleExpr;
    2.23  	}
    2.24  
    2.25 +	private void fixAggregates(TupleExpr expr)
    2.26 +	{
    2.27 +		AggregateOptimizer agg = new AggregateOptimizer();
    2.28 +		agg.optimize(expr);
    2.29 +	}
    2.30 +
    2.31  	private void coreOptimizations(EvaluationStrategy strategy, TupleExpr expr, Dataset dataset,
    2.32  			BindingSet bindings)
    2.33  	{