You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by rv...@apache.org on 2013/04/01 22:45:30 UTC

svn commit: r1463297 - in /jena/trunk/jena-arq/src: main/java/com/hp/hpl/jena/sparql/algebra/OpAsQuery.java test/java/com/hp/hpl/jena/sparql/algebra/OpAsQueryTest.java

Author: rvesse
Date: Mon Apr  1 20:45:30 2013
New Revision: 1463297

URL: http://svn.apache.org/r1463297
Log:
Add test and fix for JENA-429, when an OpExtend occurs inside a OpGroup and is not itself a group recombine turn into an ElementBind rather than a project expression

Modified:
    jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpAsQuery.java
    jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/algebra/OpAsQueryTest.java

Modified: jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpAsQuery.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpAsQuery.java?rev=1463297&r1=1463296&r2=1463297&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpAsQuery.java (original)
+++ jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/algebra/OpAsQuery.java Mon Apr  1 20:45:30 2013
@@ -90,6 +90,7 @@ public class OpAsQuery
         private Deque<ElementGroup> stack = new ArrayDeque<ElementGroup>() ;
         private Collection<Var> projectVars = allocProjectVars();
         private Map<Var, Expr> varExpression = new HashMap<Var, Expr>() ;
+        private int groupDepth = 0;
         
         public Converter(Query query)
         {
@@ -477,14 +478,20 @@ public class OpAsQuery
                 // If in top level we defer assignment to SELECT section
                 // This also covers the GROUP recombine
                 if (inTopLevel()) {
-                    if (!inGroupRecombine(opExtend)) {
-                        // If not wrapped over a Group then we need to ensure we add the variable
-                        // to the list or otherwise the BIND will not round trip
-                        // Note - This does mean top level BIND will manifest as a project expression
-                        //        rather than a BIND but this is semantically equivalent so is not an issue
-                        addProjectVar(projectVars, v) ;
+                    if (groupDepth == 0 || inGroupRecombine(opExtend)) {
+                        if (!inGroupRecombine(opExtend)) {
+                            // If not wrapped over a Group then we need to ensure we add the variable
+                            // to the list or otherwise the BIND will not round trip
+                            // Note - This does mean top level BIND will manifest as a project expression
+                            //        rather than a BIND but this is semantically equivalent so is not an issue
+                            addProjectVar(projectVars, v) ;
+                        }
+                        varExpression.put(v, tr);
+                    } else {
+                        Element elt = new ElementBind(v, tr) ;
+                        ElementGroup g = currentGroup() ;
+                        g.addElement(elt);
                     }
-                    varExpression.put(v, tr);
                 } else {
                     Element elt = new ElementBind(v, tr) ;
                     ElementGroup g = currentGroup() ;
@@ -542,7 +549,7 @@ public class OpAsQuery
         }
 
         @Override
-        public void visit(OpGroup opGroup) {            
+        public void visit(OpGroup opGroup) {               
             List<ExprAggregator> a = opGroup.getAggregators();
             
             // Aggregators are broken up in the algebra, split between a
@@ -568,7 +575,9 @@ public class OpAsQuery
 
                 }
             }
+            groupDepth++;
             opGroup.getSubOp().visit(this);
+            groupDepth--;
         }
 
         @Override

Modified: jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/algebra/OpAsQueryTest.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/algebra/OpAsQueryTest.java?rev=1463297&r1=1463296&r2=1463297&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/algebra/OpAsQueryTest.java (original)
+++ jena/trunk/jena-arq/src/test/java/com/hp/hpl/jena/sparql/algebra/OpAsQueryTest.java Mon Apr  1 20:45:30 2013
@@ -21,6 +21,9 @@ package com.hp.hpl.jena.sparql.algebra;
 import com.hp.hpl.jena.query.Syntax;
 import com.hp.hpl.jena.query.QueryFactory;
 import com.hp.hpl.jena.query.Query;
+
+import org.apache.jena.atlas.lib.StrUtils;
+import org.junit.Assert;
 import org.junit.Test;
 import static org.junit.Assert.*;
 
@@ -158,13 +161,49 @@ public class OpAsQueryTest {
     }
     
     @Test
+    public void testExtend3() {
+        //JENA-429
+        String query = StrUtils.strjoinNL
+                ("PREFIX : <http://www.cipe.accamargo.org.br/ontologias/h2tc.owl#>" ,
+                 "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>" ,
+                 "PREFIX mylib: <java:dateadd.lib.pkgfor.arq.>",
+                 "",
+                 "SELECT ?yearmonth ( count(?document) as ?total )", 
+                 "{" ,
+                 "    ?document a :Document;",
+                 "   :documentDateOfCreation ?date ;",
+                 "   :documentType \"exam results\" ." ,
+                 "    BIND( mylib:DateFormat( xsd:string(?date), \"yyyy-MM\" ) as ?yearmonth )",
+                "} group by ?yearmonth") ;
+        
+        Query[] r = checkQuery(query);
+        // Won't be equal due to lack of prefixes
+        Assert.assertNotEquals(r[0], r[1]);
+        
+        String query2 = r[1].toString();
+        Query q = QueryFactory.create(query2);
+    }
+    
+    @Test
+    public void testExtend4() {
+        //Simplified repo of JENA-429
+        String query  = "SELECT ?key (COUNT(?member) AS ?total) WHERE { ?s ?p ?o . BIND(LCASE(?o) AS ?key) } GROUP BY ?key";
+        
+        Query[] r = checkQuery(query);
+        Assert.assertEquals(r[0], r[1]);
+        
+        String query2 = r[1].toString();
+        Query q = QueryFactory.create(query2);
+    }
+    
+    @Test
     public void testExtendInService() {
         //Original test case from JENA-422
         Query[] result = checkQuery("SELECT * WHERE { SERVICE <http://example/endpoint> { ?s ?p ?o . BIND(?o AS ?x) } }");
         assertEquals(result[0], result[1]);
         assertTrue(result[1].toString().contains("BIND"));
     }
-    
+        
     public Query[] checkQuery(String query) {
         Query orig = QueryFactory.create(query, Syntax.syntaxSPARQL_11);
         Op toReconstruct = Algebra.compile(orig);