jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject jena git commit: Neater multiple OPTIONALs
Date Tue, 14 Jul 2015 17:38:10 GMT
Repository: jena
Updated Branches:
  refs/heads/master a31c7d271 -> 16a6ddf9a


Neater multiple OPTIONALs

Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/16a6ddf9
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/16a6ddf9
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/16a6ddf9

Branch: refs/heads/master
Commit: 16a6ddf9a3600aca60956bb1c0800a49982dac32
Parents: a31c7d2
Author: Andy Seaborne <andy@apache.org>
Authored: Tue Jul 14 18:37:59 2015 +0100
Committer: Andy Seaborne <andy@apache.org>
Committed: Tue Jul 14 18:37:59 2015 +0100

----------------------------------------------------------------------
 .../apache/jena/sparql/algebra/OpAsQuery.java   | 52 ++++++++++++--------
 .../jena/sparql/algebra/TestOpAsQuery.java      | 10 ++--
 2 files changed, 39 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/16a6ddf9/jena-arq/src/main/java/org/apache/jena/sparql/algebra/OpAsQuery.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/algebra/OpAsQuery.java b/jena-arq/src/main/java/org/apache/jena/sparql/algebra/OpAsQuery.java
index a6e339c..ebdeb9d 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/algebra/OpAsQuery.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/algebra/OpAsQuery.java
@@ -69,7 +69,14 @@ import org.apache.jena.vocabulary.RDF ;
 public class OpAsQuery {
     // Query cleaning is done in fixupGroupsOfOne by applying an ElementTransform. 
     
-    // TODO OpTopN (which is an optimizer additional algebra operator).
+    // Some thigns that can be done further:
+    
+    // TODO Optimization formats like OpTopN (which is an optimizer additional algebra operator).
+    // TODO More group flattening.
+    // OPTIONAL (LeftJoin) unbundles the LHS to avoid { { P OPTIONAL{} } OPTIONAL{} }
+    // This is actually a general situation.
+    // Adding onto the end of a group when the item added can not merge into the existing
last element.  
+    // e.g. BIND, VALUES.
 
     static class /* struct */ QueryLevelDetails {
         // The stack of processing in a query is:
@@ -552,13 +559,6 @@ public class OpAsQuery {
             return ;
         }
 
-        private static boolean emptyGroup(Element element) {
-            if ( !(element instanceof ElementGroup) )
-                return false ;
-            ElementGroup eg = (ElementGroup)element ;
-            return eg.isEmpty() ;
-        }
-
         @Override
         public void visit(OpLeftJoin opLeftJoin) {
             Element eLeft = asElement(opLeftJoin.getLeft()) ;
@@ -569,13 +569,7 @@ public class OpAsQuery {
             // OPTIONAL {{ ?s ?p ?o FILTER (?o>34) }} is not the same as
             // OPTIONAL { ?s ?p ?o FILTER (?o>34) }
             
-            boolean mustProtect = false ;
-            for ( Element el : eRight.getElements() ) {
-                if ( el instanceof ElementFilter ) {
-                    mustProtect = true ;
-                    break ;
-                }
-            }
+            boolean mustProtect = eRight.getElements().stream().anyMatch(el -> el instanceof
ElementFilter ) ;
                 
             if ( mustProtect ) {
                 ElementGroup eRight2 = new ElementGroup() ;
@@ -590,8 +584,13 @@ public class OpAsQuery {
                 }
             }
             ElementGroup g = currentGroup() ;
-            if ( !emptyGroup(eLeft) )
-                g.addElement(eLeft) ;
+            if ( !emptyGroup(eLeft) ) {
+                if ( eLeft instanceof ElementGroup )
+                    g.getElements().addAll(((ElementGroup)eLeft).getElements()) ;
+                else
+                    g.addElement(eLeft) ;
+            }
+                
             ElementOptional opt = new ElementOptional(eRight) ;
             g.addElement(opt) ;
         }
@@ -799,6 +798,20 @@ public class OpAsQuery {
             throw new ARQNotImplemented("OpTopN") ;
         }
 
+        private static boolean emptyGroup(Element element) {
+            if ( !(element instanceof ElementGroup) )
+                return false ;
+            ElementGroup eg = (ElementGroup)element ;
+            return eg.isEmpty() ;
+        }
+
+        private static boolean groupOfOne(Element element) {
+            if ( !(element instanceof ElementGroup) )
+                return false ;
+            ElementGroup eg = (ElementGroup)element ;
+            return eg.size() == 1 ;
+        }
+
         /** Insert into a group, skip initial empty subgroups; recombining ElementPathBlock
*/
         private static void insertIntoGroup(ElementGroup eg, Element e) {
             // Skip initial empty subgroup.
@@ -837,10 +850,9 @@ public class OpAsQuery {
         }
 
         private static Element getElementOfGroupOfOne(Element e) {
-            if ( e instanceof ElementGroup ) {
+            if ( groupOfOne(e) ) {
                 ElementGroup eg = (ElementGroup)e ;
-                if ( eg.size() == 1 )
-                    return eg.get(0) ;
+                return eg.get(0) ;
             }
             return null ;
         }

http://git-wip-us.apache.org/repos/asf/jena/blob/16a6ddf9/jena-arq/src/test/java/org/apache/jena/sparql/algebra/TestOpAsQuery.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/test/java/org/apache/jena/sparql/algebra/TestOpAsQuery.java b/jena-arq/src/test/java/org/apache/jena/sparql/algebra/TestOpAsQuery.java
index ab8666f..24169d5 100644
--- a/jena-arq/src/test/java/org/apache/jena/sparql/algebra/TestOpAsQuery.java
+++ b/jena-arq/src/test/java/org/apache/jena/sparql/algebra/TestOpAsQuery.java
@@ -53,9 +53,13 @@ public class TestOpAsQuery {
     @Test public void testOptional02() 
     { test_roundTripQuery("SELECT * WHERE { ?s ?p ?o OPTIONAL { { ?s ?q ?z FILTER (?foo)
} } }") ; }
     
-    /**
-     * Test of asQuery method, of class OpAsQuery.
-     */
+    @Test public void testOptional03() 
+    // Don't currently unnest the LHS of the second optional.  See testOptional03a
+    { test_roundTripQuery("SELECT * WHERE { ?s ?p ?o OPTIONAL { ?s ?p1 ?o1 } OPTIONAL { ?s
?p2 ?o2 } } ") ; }
+
+    @Test public void testOptional04() 
+    { test_roundTripQuery("SELECT * WHERE { ?s ?p ?o OPTIONAL { ?s ?p1 ?o1 } OPTIONAL { ?s
?p2 ?o2 } OPTIONAL { ?s ?p3 ?o3 }} ") ; }
+
     @Test
     public void testCountStar() {
         test_roundTripQuery("select (count(*) as ?cs) { ?s ?p ?o }");


Mime
View raw message