From: <mrp...@us...> - 2011-01-12 19:38:04
|
Revision: 4078 http://bigdata.svn.sourceforge.net/bigdata/?rev=4078&view=rev Author: mrpersonick Date: 2011-01-12 19:37:57 +0000 (Wed, 12 Jan 2011) Log Message: ----------- allowing non-native Sesame filters to run Modified Paths: -------------- branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataEvaluationStrategyImpl3.java branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/sop/SOpTreeBuilder.java Added Paths: ----------- branches/QUADS_QUERY_BRANCH/bigdata-sails/src/test/com/bigdata/rdf/sail/TestSesameFilters.java Modified: branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataEvaluationStrategyImpl3.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataEvaluationStrategyImpl3.java 2011-01-12 18:25:21 UTC (rev 4077) +++ branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/BigdataEvaluationStrategyImpl3.java 2011-01-12 19:37:57 UTC (rev 4078) @@ -548,7 +548,7 @@ * bigdata constraint, we can run it as a FilterIterator after the * query has run natively. */ - final Collection<ValueExpr> sesameFilters = new LinkedList<ValueExpr>(); + final Collection<Filter> sesameFilters = new LinkedList<Filter>(); /* * We need to prune Sesame filters that we cannot translate into native @@ -563,7 +563,19 @@ for (SOp sop : sopTree) { final QueryModelNode op = sop.getOperator(); if (op instanceof ValueExpr) { + /* + * If we have a raw ValueExpr and not a Filter we know it must + * be the condition of a LeftJoin, in which case we cannot + * use the Sesame FilterIterator to safely evaluate it. A + * UnsupportedOperatorException here must just flow through + * to Sesame evaluation of the entire query. + */ final ValueExpr ve = (ValueExpr) op; + final IConstraint bop = toConstraint(ve); + sop.setBOp(bop); + } else if (op instanceof Filter) { + final Filter filter = (Filter) op; + final ValueExpr ve = filter.getCondition(); try { final IConstraint bop = toConstraint(ve); sop.setBOp(bop); @@ -580,7 +592,7 @@ */ if (sop.getGroup() == SOpTreeBuilder.ROOT_GROUP_ID) { sopsToPrune.add(sop); - sesameFilters.add(ve); + sesameFilters.add(filter); } else { throw ex; } @@ -635,7 +647,7 @@ protected CloseableIteration<BindingSet, QueryEvaluationException> _evaluateNatively(final PipelineOp query, final BindingSet bs, final QueryEngine queryEngine, - final Collection<ValueExpr> sesameConstraints) + final Collection<Filter> sesameFilters) throws QueryEvaluationException { try { @@ -658,10 +670,12 @@ runningQuery.get(); // use the basic filter iterator for remaining filters - if (sesameConstraints != null) { - for (ValueExpr ve : sesameConstraints) { - final Filter filter = new Filter(null, ve); - result = new FilterIterator(filter, result, this); + if (sesameFilters != null) { + for (Filter f : sesameFilters) { + if (log.isDebugEnabled()) { + log.debug("attaching sesame filter: " + f); + } + result = new FilterIterator(f, result, this); } } @@ -1093,7 +1107,7 @@ } protected IVariable[] gatherRequiredVariables(final TupleExpr root, - final Collection<ValueExpr> sesameFilters) { + final Collection<Filter> sesameFilters) { /* * Collect a set of variables required beyond just the join (i.e. @@ -1116,8 +1130,8 @@ } if (sesameFilters.size() > 0) { - for (ValueExpr ve : sesameFilters) { - required.addAll(collectVariables((UnaryTupleOperator) ve)); + for (Filter f : sesameFilters) { + required.addAll(collectVariables(f.getCondition())); } } @@ -1140,48 +1154,47 @@ * they can be added to the list of required variables in the query for * correct binding set pruning. * - * @param uto + * @param op * the <code>UnaryTupleOperator</code> * @return * the variables it uses */ - protected Set<String> collectVariables(UnaryTupleOperator uto) { + protected Set<String> collectVariables(final QueryModelNode op) { final Set<String> vars = new HashSet<String>(); - if (uto instanceof Projection) { - List<ProjectionElem> elems = - ((Projection) uto).getProjectionElemList().getElements(); + if (op instanceof Projection) { + final List<ProjectionElem> elems = + ((Projection) op).getProjectionElemList().getElements(); for (ProjectionElem elem : elems) { vars.add(elem.getSourceName()); } - } else if (uto instanceof MultiProjection) { - List<ProjectionElemList> elemLists = - ((MultiProjection) uto).getProjections(); + } else if (op instanceof MultiProjection) { + final List<ProjectionElemList> elemLists = + ((MultiProjection) op).getProjections(); for (ProjectionElemList list : elemLists) { List<ProjectionElem> elems = list.getElements(); for (ProjectionElem elem : elems) { vars.add(elem.getSourceName()); } } - } else if (uto instanceof Filter) { - Filter f = (Filter) uto; - ValueExpr ve = f.getCondition(); + } else if (op instanceof ValueExpr) { + final ValueExpr ve = (ValueExpr) op; ve.visit(new QueryModelVisitorBase<RuntimeException>() { @Override public void meet(Var v) { vars.add(v.getName()); } }); - } else if (uto instanceof Group) { - Group g = (Group) uto; + } else if (op instanceof Group) { + final Group g = (Group) op; g.visit(new QueryModelVisitorBase<RuntimeException>() { @Override public void meet(Var v) { vars.add(v.getName()); } }); - } else if (uto instanceof Order) { - Order o = (Order) uto; + } else if (op instanceof Order) { + final Order o = (Order) op; o.visit(new QueryModelVisitorBase<RuntimeException>() { @Override public void meet(Var v) { Modified: branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/sop/SOpTreeBuilder.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/sop/SOpTreeBuilder.java 2011-01-12 18:25:21 UTC (rev 4077) +++ branches/QUADS_QUERY_BRANCH/bigdata-sails/src/java/com/bigdata/rdf/sail/sop/SOpTreeBuilder.java 2011-01-12 19:37:57 UTC (rev 4078) @@ -222,7 +222,7 @@ final ValueExpr ve = filter.getCondition(); // make a constraint, attach it to the rule if (ve != null) { - sops.add(new SOp(sopId.incrementAndGet(), ve, g, pg, rslj)); + sops.add(new SOp(sopId.incrementAndGet(), filter, g, pg, rslj)); } final TupleExpr arg = filter.getArg(); Added: branches/QUADS_QUERY_BRANCH/bigdata-sails/src/test/com/bigdata/rdf/sail/TestSesameFilters.java =================================================================== --- branches/QUADS_QUERY_BRANCH/bigdata-sails/src/test/com/bigdata/rdf/sail/TestSesameFilters.java (rev 0) +++ branches/QUADS_QUERY_BRANCH/bigdata-sails/src/test/com/bigdata/rdf/sail/TestSesameFilters.java 2011-01-12 19:37:57 UTC (rev 4078) @@ -0,0 +1,195 @@ +/** +Copyright (C) SYSTAP, LLC 2006-2007. All rights reserved. + +Contact: + SYSTAP, LLC + 4501 Tower Road + Greensboro, NC 27410 + lic...@bi... + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; version 2 of the License. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ +/* + * Created on Sep 16, 2009 + */ + +package com.bigdata.rdf.sail; + +import java.util.Collection; +import java.util.LinkedList; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.log4j.Logger; +import org.openrdf.model.Literal; +import org.openrdf.model.URI; +import org.openrdf.model.ValueFactory; +import org.openrdf.model.vocabulary.RDF; +import org.openrdf.model.vocabulary.RDFS; +import org.openrdf.query.BindingSet; +import org.openrdf.query.QueryLanguage; +import org.openrdf.query.TupleQueryResult; +import org.openrdf.query.algebra.Projection; +import org.openrdf.query.algebra.QueryRoot; +import org.openrdf.query.algebra.TupleExpr; +import org.openrdf.query.impl.BindingImpl; +import org.openrdf.repository.RepositoryConnection; +import org.openrdf.repository.sail.SailTupleQuery; + +import com.bigdata.bop.BOpUtility; +import com.bigdata.bop.PipelineOp; +import com.bigdata.bop.engine.QueryEngine; +import com.bigdata.rdf.axioms.NoAxioms; +import com.bigdata.rdf.sail.sop.SOp; +import com.bigdata.rdf.sail.sop.SOp2BOpUtility; +import com.bigdata.rdf.sail.sop.SOpTree; +import com.bigdata.rdf.sail.sop.SOpTree.SOpGroup; +import com.bigdata.rdf.sail.sop.SOpTree.SOpGroups; +import com.bigdata.rdf.sail.sop.SOpTreeBuilder; +import com.bigdata.rdf.store.AbstractTripleStore; +import com.bigdata.rdf.store.BD; +import com.bigdata.rdf.vocab.NoVocabulary; + +public class TestSesameFilters extends ProxyBigdataSailTestCase { + + protected static final Logger log = Logger.getLogger(TestSesameFilters.class); + + protected static final boolean INFO = log.isInfoEnabled(); + + @Override + public Properties getProperties() { + + Properties props = super.getProperties(); + + props.setProperty(BigdataSail.Options.AXIOMS_CLASS, NoAxioms.class.getName()); + props.setProperty(BigdataSail.Options.VOCABULARY_CLASS, NoVocabulary.class.getName()); + props.setProperty(BigdataSail.Options.TRUTH_MAINTENANCE, "false"); + props.setProperty(BigdataSail.Options.JUSTIFY, "false"); + props.setProperty(BigdataSail.Options.TEXT_INDEX, "false"); + + return props; + + } + + /** + * + */ + public TestSesameFilters() { + } + + /** + * @param arg0 + */ + public TestSesameFilters(String arg0) { + super(arg0); + } + + public void testRegex() throws Exception { + +// final Sail sail = new MemoryStore(); +// sail.initialize(); +// final Repository repo = new SailRepository(sail); + + final BigdataSail sail = getSail(); + sail.initialize(); + final BigdataSailRepository repo = new BigdataSailRepository(sail); + + final RepositoryConnection cxn = repo.getConnection(); + cxn.setAutoCommit(false); + + try { + + final ValueFactory vf = sail.getValueFactory(); + + /* + * Create some terms. + */ + final URI mike = vf.createURI(BD.NAMESPACE + "mike"); + final URI bryan = vf.createURI(BD.NAMESPACE + "bryan"); + final URI person = vf.createURI(BD.NAMESPACE + "Person"); + final Literal l1 = vf.createLiteral("mike personick"); + final Literal l2 = vf.createLiteral("bryan thompson"); + + /* + * Create some statements. + */ + cxn.add(mike, RDF.TYPE, person); + cxn.add(mike, RDFS.LABEL, l1); + cxn.add(bryan, RDF.TYPE, person); + cxn.add(bryan, RDFS.LABEL, l2); + + /* + * Note: The either flush() or commit() is required to flush the + * statement buffers to the database before executing any operations + * that go around the sail. + */ + cxn.commit(); + + { + + String query = + "prefix bd: <"+BD.NAMESPACE+"> " + + "prefix rdf: <"+RDF.NAMESPACE+"> " + + "prefix rdfs: <"+RDFS.NAMESPACE+"> " + + "select * " + + "where { " + + " ?s rdf:type bd:Person . " + + " ?s rdfs:label ?label . " + + " FILTER regex(?label, \"mike\") . " + + "}"; + + final SailTupleQuery tupleQuery = (SailTupleQuery) + cxn.prepareTupleQuery(QueryLanguage.SPARQL, query); + tupleQuery.setIncludeInferred(false /* includeInferred */); + + if (log.isInfoEnabled()) { + + final BigdataSailTupleQuery bdTupleQuery = + (BigdataSailTupleQuery) tupleQuery; + final QueryRoot root = (QueryRoot) bdTupleQuery.getTupleExpr(); + final Projection p = (Projection) root.getArg(); + final TupleExpr tupleExpr = p.getArg(); + final SOpTreeBuilder stb = new SOpTreeBuilder(); + final SOpTree tree = stb.collectSOps(tupleExpr); + + log.info(tree); + log.info(query); + + final TupleQueryResult result = tupleQuery.evaluate(); + while (result.hasNext()) { + log.info(result.next()); + } + + } + + final Collection<BindingSet> answer = new LinkedList<BindingSet>(); + answer.add(createBindingSet( + new BindingImpl("s", mike), + new BindingImpl("label", l1) + )); + + final TupleQueryResult result = tupleQuery.evaluate(); + compare(result, answer); + + } + + } finally { + cxn.close(); + sail.shutDown(); + } + + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |