You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2011/06/28 23:47:13 UTC
svn commit: r1140862 - in /incubator/jena/Jena2/ARQ/trunk: ./ src-dev/dev/
src-test/com/hp/hpl/jena/sparql/lang/ src/com/hp/hpl/jena/sparql/lang/
src/com/hp/hpl/jena/sparql/syntax/
Author: andy
Date: Tue Jun 28 21:47:12 2011
New Revision: 1140862
URL: http://svn.apache.org/viewvc?rev=1140862&view=rev
Log:
Fix bad testing of scope in subqueries across UNION
Modified:
incubator/jena/Jena2/ARQ/trunk/Q.rq
incubator/jena/Jena2/ARQ/trunk/src-dev/dev/RunARQ.java
incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/lang/TestVarScope.java
incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/lang/SyntaxVarScope.java
incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/syntax/ElementWalker.java
incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/syntax/PatternVars.java
Modified: incubator/jena/Jena2/ARQ/trunk/Q.rq
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/Q.rq?rev=1140862&r1=1140861&r2=1140862&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/Q.rq (original)
+++ incubator/jena/Jena2/ARQ/trunk/Q.rq Tue Jun 28 21:47:12 2011
@@ -1,7 +1,21 @@
-PREFIX : <http://example/>
-PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
-
-SELECT (SUM(?val) as ?value)
+PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
+SELECT DISTINCT ?stw ?stw_pref ?rel ?thesoz_pref ?thesoz
WHERE {
- ?a rdf:value ?val .
-} GROUP BY ?a
+ {
+ SELECT DISTINCT ?stw ('=' as ?rel) ?thesoz
+ WHERE {
+ ?stw skos:exactMatch ?thesoz .
+ ?stw skos:inScheme <http://zbw.eu/stw> .
+ ?thesoz skos:inScheme <http://lod.gesis.org/thesoz/> .
+ }
+ } UNION {
+ SELECT DISTINCT ?stw ('<' as ?rel) ?thesoz
+ WHERE {
+ ?stw skos:broadMatch ?thesoz .
+ ?stw skos:inScheme <http://zbw.eu/stw> .
+ ?thesoz skos:inScheme <http://lod.gesis.org/thesoz/> .
+ }
+ } .
+ ?stw skos:prefLabel ?stw_pref .
+ ?thesoz skos:prefLabel ?thesoz_pref
+}
Modified: incubator/jena/Jena2/ARQ/trunk/src-dev/dev/RunARQ.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src-dev/dev/RunARQ.java?rev=1140862&r1=1140861&r2=1140862&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src-dev/dev/RunARQ.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src-dev/dev/RunARQ.java Tue Jun 28 21:47:12 2011
@@ -127,7 +127,8 @@ public class RunARQ
public static void main(String[] argv) throws Exception
{
- arq.sparql.main("--data=D.ttl", "--query=Q.rq") ;
+ //arq.sparql.main("--data=D.ttl", "--query=Q.rq") ;
+ arq.qparse.main("--query=Q.rq") ;
exit(0) ;
Modified: incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/lang/TestVarScope.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/lang/TestVarScope.java?rev=1140862&r1=1140861&r2=1140862&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/lang/TestVarScope.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src-test/com/hp/hpl/jena/sparql/lang/TestVarScope.java Tue Jun 28 21:47:12 2011
@@ -53,9 +53,8 @@ public class TestVarScope extends BaseTe
@Test public void scope_22() { scope("SELECT * { ?s ?p ?o OPTIONAL{?s ?p2 ?o2} BIND(?o2+5 AS ?z) }") ; }
- // This is turned off.
-// @Test(expected=QueryException.class)
-// public void scope_23() { scope("SELECT * { ?s ?p ?o OPTIONAL{?s ?p2 ?o2} BIND(?HAH+5 AS ?z) }") ; }
+ @Test(expected=QueryException.class)
+ public void scope_23() { scope("SELECT * { ?s ?p ?o OPTIONAL{?s ?p2 ?o2} BIND(5 AS ?o2) }") ; }
@Test(expected=QueryException.class)
public void scope_24() { scope("SELECT * { ?s ?p ?o OPTIONAL{?s ?p2 ?o2} BIND(?o+5 AS ?o2) }") ; }
@@ -64,6 +63,26 @@ public class TestVarScope extends BaseTe
public void scope_25() { scope("SELECT * { ?s ?p ?o OPTIONAL{?s ?p2 ?o2} BIND(5 AS ?o) }") ; }
// Subqueries
+
+ @Test(expected=QueryException.class)
+ public void scope_30() { scope("SELECT * { SELECT (?o+1 AS ?o) { ?s ?p ?o }}") ; }
+
+ @Test
+ public void scope_31()
+ {
+ scope("SELECT ?y { " +
+ "{ { SELECT (?x AS ?y) { ?s ?p ?x } } } UNION { { SELECT (?x AS ?y) { ?s ?p ?x } } }" +
+ "}") ;
+ }
+
+ @Test(expected=QueryException.class)
+ public void scope_32()
+ {
+ scope("SELECT ?y { " +
+ "{ { SELECT (?o+1 AS ?x) (?o+1 AS ?x) { ?s ?p ?o } } UNION { ?s ?p ?x } }" +
+ "}") ;
+ }
+
}
/*
Modified: incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/lang/SyntaxVarScope.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/lang/SyntaxVarScope.java?rev=1140862&r1=1140861&r2=1140862&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/lang/SyntaxVarScope.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/lang/SyntaxVarScope.java Tue Jun 28 21:47:12 2011
@@ -18,61 +18,94 @@ import com.hp.hpl.jena.query.Syntax ;
import com.hp.hpl.jena.sparql.core.Var ;
import com.hp.hpl.jena.sparql.core.VarExprList ;
import com.hp.hpl.jena.sparql.expr.Expr ;
+import com.hp.hpl.jena.sparql.syntax.Element ;
import com.hp.hpl.jena.sparql.syntax.ElementBind ;
+import com.hp.hpl.jena.sparql.syntax.ElementMinus ;
import com.hp.hpl.jena.sparql.syntax.ElementSubQuery ;
+import com.hp.hpl.jena.sparql.syntax.ElementVisitor ;
+import com.hp.hpl.jena.sparql.syntax.ElementVisitorBase ;
+import com.hp.hpl.jena.sparql.syntax.ElementWalker ;
import com.hp.hpl.jena.sparql.syntax.PatternVars ;
import com.hp.hpl.jena.sparql.syntax.PatternVarsVisitor ;
/** Calculate in-scope variables from the AST */
public class SyntaxVarScope
{
+ /* SPARQL 1.1 "in scope" rules
+ Syntax Form In-scope variables
+
+ Basic Graph Pattern (BGP) v occurs in the BGP
+ Path v occurs in the path
+ Group { P1 P2 ... } v is in-scope if in-scope in one or more of P1, P2, ...
+ GRAPH term { P } v is term or v is in-scope in P
+ { P1 } UNION { P2 } v is in-scope in P1 or in-scope in P2
+ OPTIONAL {P} v is in-scope in P
+ SERVICE term {P} v is term or v is in-scope in P
+ (expr AS v) for BIND, SELECT and GROUP BY v is in-scope
+ SELECT ..v .. { P } v is in-scope if v is mentioned as a project variable
+ SELECT * { P } v is in-scope in P
+ BINDINGS varlist (values) v is in-scope if v is in varlist
+ */
+
/*
* Check for non-group-keys vars
* Check for unused vars (warning?)
- * 1/ Combine finalization with findAndAddNamedVars/setResultVars
+ * Check for out of scope
+ * two cases: BIND and SubQuery
+ * BIND done during variable accumulation.
+ * SubQuery done as a separate pass.
+ * Combine finalization with findAndAddNamedVars/setResultVars
*/
- /** Accumulate pattern variables but include some checking as well */
- static class PatternVarsVisitorChecker extends PatternVarsVisitor
- {
- public PatternVarsVisitorChecker(Set<Var> s)
- {
- super(s) ;
- }
-
- @Override
- public void visit(ElementBind el)
- {
- Var var = el.getVar() ;
-
- if ( acc.contains(var) )
- throw new QueryParseException("Variable used when already in-scope: "+var+" in "+el, -1 , -1) ;
- checkAssignment(acc, el.getExpr(), var) ;
- }
-
- @Override
- public void visit(ElementSubQuery el)
- {
- check(el.getQuery(), acc) ;
- super.visit(el) ;
- }
- }
-
public static void check(Query query)
{
if ( query.getQueryPattern() == null )
// DESCRIBE may not have a pattern
return ;
- // And now check down the element for subqueries.
- LinkedHashSet<Var> queryVars = new LinkedHashSet<Var>() ;
- PatternVarsVisitorChecker visitor = new PatternVarsVisitorChecker(queryVars) ;
+ checkSubQuery(query.getQueryPattern()) ;
+ checkBind(query) ;
+ // Check this level.
+ checkQueryScope(query) ;
+
+ // Other checks.
+ Collection<Var> vars = varsOfQuery(query) ;
+ check(query, vars) ;
+ }
+
+ // Check BIND by accumulating variables and making sure BIND does not attempt to reuse one
+ private static void checkBind(Query query)
+ {
+ LinkedHashSet<Var> queryVars = new LinkedHashSet<Var>() ;
+ BindScopeChecker visitor = new BindScopeChecker(queryVars) ;
PatternVars.vars(query.getQueryPattern(), visitor) ;
- queryVars.addAll(query.getGroupBy().getVars()) ;
-
- check(query, queryVars) ;
}
+ // Check subquery by finding subquries and recurisively checking.
+ // Includes appling all checks to nested subqueries.
+ private static void checkSubQuery(Element el)
+ {
+ ElementVisitor v = new SubQueryScopeChecker() ;
+ ElementWalker.walk(el, v) ;
+ }
+
+ // Check one level of query - SELECT expressions
+ private static void checkQueryScope(Query query)
+ {
+ Collection<Var> vars = varsOfQuery(query) ;
+ checkExprListAssignment(vars, query.getProject()) ;
+ }
+
+ // get all vars of a query
+ private static Collection<Var> varsOfQuery(Query query)
+ {
+ Collection<Var> vars = PatternVars.vars(query.getQueryPattern()) ;
+ if ( query.hasBindings() )
+ vars.addAll(query.getBindingVariables()) ;
+ return vars ;
+ }
+
+ // Other check (not scioping at this level) of a query
private static void check(Query query, Collection<Var> vars)
{
// Check any expressions are assigned to fresh variables.
@@ -187,6 +220,64 @@ public class SyntaxVarScope
{
return "("+expr+" AS "+var+")" ;
}
+
+ // Modifed walked for variables.
+
+ /** Visitor for subqueries scope rules . */
+ private static class SubQueryScopeChecker extends ElementVisitorBase
+ {
+ @Override
+ public void visit(ElementSubQuery el)
+ {
+ Query query = el.getQuery() ;
+ checkQueryScope(query) ;
+ // Recursively check sub-queries in sub-queries.
+ check(el.getQuery()) ;
+ }
+ }
+
+ /** Accumulate pattern variables but include some checking (BIND) as well */
+ private static class BindScopeChecker extends PatternVarsVisitor
+ {
+ public BindScopeChecker(Set<Var> s)
+ {
+ super(s) ;
+ }
+
+ @Override
+ public void visit(ElementBind el)
+ {
+ Var var = el.getVar() ;
+
+ if ( acc.contains(var) )
+ throw new QueryParseException("BIND: Variable used when already in-scope: "+var+" in "+el, -1 , -1) ;
+ checkAssignment(acc, el.getExpr(), var) ;
+ }
+ }
+
+ public static void varsWalk(Element element, PatternVarsVisitor visitor)
+ {
+ ElementWalker.Walker walker = new WalkerStack(visitor) ;
+ ElementWalker.walk(element, walker) ;
+ }
+
+ public static class WalkerStack extends ElementWalker.Walker
+ {
+ protected WalkerStack(ElementVisitor visitor)
+ {
+ super(visitor) ;
+ }
+
+ @Override
+ public void visit(ElementMinus el)
+ {
+// if ( el.getMinusElement() != null )
+// el.getMinusElement().visit(this) ;
+ proc.visit(el) ;
+ }
+ }
+
+
}
/*
Modified: incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/syntax/ElementWalker.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/syntax/ElementWalker.java?rev=1140862&r1=1140861&r2=1140862&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/syntax/ElementWalker.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/syntax/ElementWalker.java Tue Jun 28 21:47:12 2011
@@ -9,10 +9,10 @@ package com.hp.hpl.jena.sparql.syntax;
-/** An element visitor that walks the graph pattern tree, applying a visitor
- * at each Element traversed.
+/** An element visitor that walks the graph pattern tree,
+ * applying a visitor at each Element traversed.
* Only walks one level of the query (not subqueries -- sub SELECT, (NOT)EXISTS
- * these wil need to call down themselves if it is meaningful for the visitor.
+ * these will need to call down themselves if it is meaningful for the visitor.
* Bottom-up walk - apply to subelements before applying to current element. */
public class ElementWalker
@@ -54,13 +54,11 @@ public class ElementWalker
{
proc.visit(el) ;
}
-
public void visit(ElementBind el)
{
proc.visit(el) ;
}
-
public void visit(ElementUnion el)
{
Modified: incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/syntax/PatternVars.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/syntax/PatternVars.java?rev=1140862&r1=1140861&r2=1140862&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/syntax/PatternVars.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/com/hp/hpl/jena/sparql/syntax/PatternVars.java Tue Jun 28 21:47:12 2011
@@ -1,6 +1,6 @@
/*
* (c) Copyright 2005, 2006, 2007, 2008, 2009 Hewlett-Packard Development Company, LP
- * (c) Copyright 2010 Epimorphics Ltd.
+ * (c) Copyright 2010, 2011 Epimorphics Ltd.
* All rights reserved.
* [See end of file]
*/
@@ -52,7 +52,7 @@ public class PatternVars
/*
* (c) Copyright 2005, 2006, 2007, 2008, 2009 Hewlett-Packard Development Company, LP
- * (c) Copyright 2010 Epimorphics Ltd.
+ * (c) Copyright 2010, 2011 Epimorphics Ltd.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without