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 2013/04/15 18:46:17 UTC

svn commit: r1468142 - in /jena/Scratch/AFS/Jena-Dev/trunk/src/dev: Jena432_SubQueryRewrites.java JenaNNN_FilterEquality.java

Author: andy
Date: Mon Apr 15 16:46:17 2013
New Revision: 1468142

URL: http://svn.apache.org/r1468142
Log:
development

Added:
    jena/Scratch/AFS/Jena-Dev/trunk/src/dev/JenaNNN_FilterEquality.java
      - copied, changed from r1467937, jena/Scratch/AFS/Jena-Dev/trunk/src/dev/Jena432_SubQueryRewrites.java
Removed:
    jena/Scratch/AFS/Jena-Dev/trunk/src/dev/Jena432_SubQueryRewrites.java

Copied: jena/Scratch/AFS/Jena-Dev/trunk/src/dev/JenaNNN_FilterEquality.java (from r1467937, jena/Scratch/AFS/Jena-Dev/trunk/src/dev/Jena432_SubQueryRewrites.java)
URL: http://svn.apache.org/viewvc/jena/Scratch/AFS/Jena-Dev/trunk/src/dev/JenaNNN_FilterEquality.java?p2=jena/Scratch/AFS/Jena-Dev/trunk/src/dev/JenaNNN_FilterEquality.java&p1=jena/Scratch/AFS/Jena-Dev/trunk/src/dev/Jena432_SubQueryRewrites.java&r1=1467937&r2=1468142&rev=1468142&view=diff
==============================================================================
--- jena/Scratch/AFS/Jena-Dev/trunk/src/dev/Jena432_SubQueryRewrites.java (original)
+++ jena/Scratch/AFS/Jena-Dev/trunk/src/dev/JenaNNN_FilterEquality.java Mon Apr 15 16:46:17 2013
@@ -19,6 +19,7 @@
 package dev;
 
 import org.apache.jena.atlas.lib.StrUtils ;
+import org.junit.Assert ;
 import org.junit.Test ;
 
 import com.hp.hpl.jena.query.Query ;
@@ -27,34 +28,29 @@ import com.hp.hpl.jena.sparql.algebra.Al
 import com.hp.hpl.jena.sparql.algebra.Op ;
 import com.hp.hpl.jena.sparql.algebra.Transform ;
 import com.hp.hpl.jena.sparql.algebra.Transformer ;
-import com.hp.hpl.jena.sparql.algebra.op.OpLeftJoin ;
-import com.hp.hpl.jena.sparql.algebra.optimize.* ;
-import com.hp.hpl.jena.sparql.engine.main.LeftJoinClassifier ;
+import com.hp.hpl.jena.sparql.algebra.optimize.TransformExpandOneOf ;
+import com.hp.hpl.jena.sparql.algebra.optimize.TransformFilterDisjunction ;
+import com.hp.hpl.jena.sparql.algebra.optimize.TransformFilterEquality ;
+import com.hp.hpl.jena.sparql.algebra.optimize.TransformFilterPlacement ;
+import com.hp.hpl.jena.sparql.sse.SSE ;
 
-public class Jena432_SubQueryRewrites
+public class JenaNNN_FilterEquality
 {
     // remove assignment from projection?
+    // No need! It's going to be a no-op!
     
     // JENA-432 : Filter optimization messes up when a nested select is present in the same block
     // JENA-383 : The query optimizer generates a suboptimal query plan in case of nested optionals followed by a filter
-    // JENA-384 : SubstitueFilterOptimize :: Interaction of optimization (TransformFilterEquality) and initial binding
+    // JENA-294 : TransformFilterEquality does not handle starting OPTIONAL well
+
+    // Unrelated
     // JENA-293 : Allow the filter placement optimziation to push FILTER though BIND.
-    // JENA-294 : TransformFilterEquality does not handle starting OPTIONAL well 
+    // JENA-384 : SubstitueFilterOptimize :: Interaction of optimization (TransformFilterEquality) and initial binding
+
     
-    public static void main(String... args) throws Exception
+    public static void main(String... args) throws Exception 
     {
-        //        // Notes:
-        //        // check TestTransformQuads.quads10,20,21,30
-        //        
-        //        if ( false )
-        //        {
-        //            Op op = SSE.parseOp("(leftjoin (bgp (?s1 ?p1 ?o1)) (project (?s2) (bgp (?s2 ?p2 ?o2))))") ;
-        //            System.out.println(op) ;
-        //            Set<Var> vars = OpVars.visibleVars(op) ;
-        //            System.out.println(vars) ;
-        //            System.exit(0) ;
-        //        }
-
+        // Scoping.
         String qs1 = StrUtils.strjoinNL
             ( "PREFIX : <http://example/> SELECT * {"
               , "    OPTIONAL { ?x :q ?o }"
@@ -62,31 +58,38 @@ public class Jena432_SubQueryRewrites
               , "    ?x :p ?o2"
               , "}"
                 ) ;
+        
 
+        // Scoping issues : works
+        String qs2 = StrUtils.strjoinNL
+            ( "PREFIX : <http://example/> SELECT * {"
+              , "    ?x :p ?o2"
+              , "    OPTIONAL { ?z :q ?o }"     // No ?x
+              , "    FILTER(?x = :x)"
+              , "}"
+                ) ;
 
-        String[] x = { qs1 } ;
+        String[] x = { qs1 , qs2 } ;
         for ( String qs : x )
         {
             Query query = QueryFactory.create(qs) ;
             Op op = Algebra.compile(query) ;
-
-            if ( op instanceof OpLeftJoin )
-                LeftJoinClassifier.isLinear((OpLeftJoin)op) ;
-
-            System.out.println(query) ;
-            System.out.println(op) ;
-
-            Op op1 = op ;
-            if ( false )
-            {
-                op1 = TransformScopeRename.transform(op1) ; 
-                //        Op op1 = Transformer.transform(new TransformJoinStrategy(), op) ;
-                //        Op op1 = Transformer.transform(new TransformFilterDisjunction(), op) ;
-                op1 = Transformer.transform(new TransformFilterEquality(), op1) ;
-            }
-            else
-                op1 = Algebra.optimize(op) ; 
+            System.out.println("- - - - - Query") ;
+            System.out.print(query) ;
+            System.out.println("- - - - - Algebra") ;
+            System.out.print(op) ;
+
+            
+//            System.out.println("- - - - - ScopeRename+TransformFilterEquality") ;
+//            Op op1 = TransformScopeRename.transform(op) ; 
+            //        Op op1 = Transformer.transform(new TransformJoinStrategy(), op) ;
+            //        Op op1 = Transformer.transform(new TransformFilterDisjunction(), op) ;
+            System.out.println("- - - - - TransformFilterEquality") ;
+            Op op1 = Transformer.transform(new TransformFilterEquality(), op) ;
             System.out.print(op1) ;
+            System.out.println("- - - - - Optimize") ;
+            Op op2 = Algebra.optimize(op) ; 
+            System.out.print(op2) ;
             System.out.println("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -") ;
 
         }
@@ -97,7 +100,10 @@ public class Jena432_SubQueryRewrites
     private Transform t_placement   = new TransformFilterPlacement() ;
     private Transform t_expandOneOf = new TransformExpandOneOf() ;
     
-    @Test public void test_OptEqualityJoin_01() {
+    // Scope of variable (optional, defined) cases 
+    // Related to JENA-294 part II
+    // Trailing fixed BGP. 
+    @Test public void test_OptEqualityScope_01() {
         String qs = StrUtils.strjoinNL
             ( "PREFIX : <http://example/> SELECT * {"
               , "    OPTIONAL { ?x :q ?o }"
@@ -105,19 +111,71 @@ public class Jena432_SubQueryRewrites
               , "    ?x :p ?o2"
               , "}"
                 ) ;
+        // Safe to transform:  This (sequence) always defined ?x 
         String ops = StrUtils.strjoinNL
-            ("(filter (= ?x <http://example/x>)"
+            ("(assign ((?x <http://example/x>))"
             , "   (sequence"
             , "       (conditional"
             , "         (table unit)"
-            , "         (bgp (triple ?x <http://example/q> ?o)))"
-            , "       (bgp (triple ?x <http://example/p> ?o2))))"
+            , "         (bgp (triple <http://example/x> <http://example/q> ?o)))"
+            , "       (bgp (triple <http://example/x> <http://example/p> ?o2))))"
             ) ;
         check(qs, ops) ;
     }
 
+    // JENA-294 part II
+    @Test public void test_OptEqualityScope_02() {
+        String qs = StrUtils.strjoinNL
+            ( "PREFIX : <http://example/> SELECT * {"
+              , "    ?x :p ?o2"
+              , "    OPTIONAL { ?x :q ?o }"
+              , "    FILTER(?x = :x)"
+              , "}"
+                ) ;
+        // Safe to transform:  ?x is fixed. 
+        String ops = StrUtils.strjoinNL
+            ( "(assign ((?x <http://example/x>))"
+            , "   (conditional"
+            , "     (bgp (triple <http://example/x> <http://example/p> ?o2))"
+            , "     (bgp (triple <http://example/x> <http://example/q> ?o))"
+            , "   ))"
+            ) ;
+        check(qs, ops) ;
+    }
+    
+    // Scope of variable (optional, defined) cases 
+    @Test public void test_OptEqualityScope_03() {
+        String qs = StrUtils.strjoinNL
+            ( "PREFIX : <http://example/> SELECT * {"
+              , "    OPTIONAL { ?x :q ?o }"
+              , "    FILTER(?x = :x)"
+              , "}"
+                ) ;
+        // Unsafe to transform:  This may not defined ?x, then FILTER -> unbound -> error -> false 
+        String ops = StrUtils.strjoinNL
+            ("(assign ((?x <http://example/x>))"
+            , "   (sequence"
+            , "       (conditional"
+            , "         (table unit)"
+            , "         (bgp (triple <http://example/x> <http://example/q> ?o)))"
+            , "       (bgp (triple <http://example/x> <http://example/p> ?o2))))"
+            ) ;
+        check(qs, ops) ;
+    }
+    
     
+
+    //JENA-294 part II
+    /*
+SELECT *
+{
+?x :p ?o2
+OPTIONAL { ?x :q ?o }
+FILTER(?x = :x)
+}
+ */   
     
+    // JENA-432
     @Test public void test_OptEqualitySubQuery_01() {
         String qs = StrUtils.strjoinNL
             ( "SELECT * {"
@@ -144,6 +202,7 @@ public class Jena432_SubQueryRewrites
         check(qs, ops) ;
     }
     
+    // JENA-432
     @Test public void test_OptEqualitySubQuery_02() {
         String qs = StrUtils.strjoinNL
                 ( "SELECT *",
@@ -170,6 +229,8 @@ public class Jena432_SubQueryRewrites
         
         check(qs, ops) ;
     }
+
+    // JENA-432
     @Test public void test_OptEqualitySubQuery_03() {
         String qs = StrUtils.strjoinNL
             ( "SELECT *"
@@ -194,6 +255,8 @@ public class Jena432_SubQueryRewrites
             ) ;
         check(qs, ops) ;
     }
+    
+    // JENA-432
     @Test public void test_OptEqualitySubQuery_04() {
         String qs = StrUtils.strjoinNL
             ( "SELECT *"
@@ -218,9 +281,35 @@ public class Jena432_SubQueryRewrites
         
         check(qs, ops) ;
     }
+    
+
+    
+    // JENA-383, simplified.
+    @Test public void test_OptEqualitySubQuery_05() {
+        String qs = StrUtils.strjoinNL
+            ( "PREFIX : <http://example/> SELECT * {"
+              , "    OPTIONAL { ?x :q ?o }"
+              , "    FILTER(?x = :x)"
+              , "    ?x :p ?o2"
+              , "}"
+                ) ;
+        String ops = StrUtils.strjoinNL
+            ( "(filter (= ?var <http://example.org/test#i>)"
+            , "  (conditional"
+            , "     (bgp (triple ?var <http://example.org/test#p1> ?x))"
+            , "        (conditional"
+            , "           (bgp (triple ?x <http://example.org/test#p2> ?y))"
+            , "           (bgp (triple ?y <http://example.org/test#p3> ?z)))"
+            , " ))" 
+            ) ;
+        check(qs, ops) ;
+    }
+
+        
   
     // JENA-383
-    @Test public void test_OptEqualitySubQuery_05() {
+    @Test public void test_OptEqualityScopingNested() {
+        // Can't transform: ?var not used in middle OPTIONAL.
         String qs = StrUtils.strjoinNL
             ( "PREFIX ex: <http://example.org/test#>"
             , "SELECT * {"
@@ -236,9 +325,9 @@ public class Jena432_SubQueryRewrites
             ( "(filter (= ?var <http://example.org/test#i>)"
             , "  (conditional"
             , "     (bgp (triple ?var <http://example.org/test#p1> ?x))"
-            , "        (conditional"
-            , "           (bgp (triple ?x <http://example.org/test#p2> ?y))"
-            , "           (bgp (triple ?y <http://example.org/test#p3> ?z)))"
+            , "     (conditional"
+            , "        (bgp (triple ?x <http://example.org/test#p2> ?y))"
+            , "        (bgp (triple ?y <http://example.org/test#p3> ?z)))"
             , " ))" 
             ) ;
         check(qs, ops) ;
@@ -248,62 +337,28 @@ public class Jena432_SubQueryRewrites
 
     private void check(String qs, String ops)
     {
-        TestOptimizer.check(qs, ops) ;
+        //TestOptimizer.check(qs, ops) ;
 
-//        //String queryString = "PREFIX : <http://example/>\n"+qs ;
-//        String queryString = qs ;
-//        Query query = QueryFactory.create(queryString) ;
-//        Op opQuery = Algebra.compile(query) ;
-//        Op opOptimize = Algebra.optimize(opQuery) ;
-//        Op opExpected = SSE.parseOp(ops) ;
-//        
+        //String queryString = "PREFIX : <http://example/>\n"+qs ;
+        String queryString = qs ;
+        Query query = QueryFactory.create(queryString) ;
+        Op opQuery = Algebra.compile(query) ;
+        Op opOptimize = Algebra.optimize(opQuery) ;
+        Op opExpected = SSE.parseOp(ops) ;
+        
+        if ( ! opExpected.equals(opOptimize) )
+        {
+            System.out.println("*** Expected") ;
+            System.out.print(opExpected) ;
+            System.out.println("*** Actual") ;
+            System.out.print(opOptimize) ;
+        }
+        Assert.assertEquals(opExpected, opOptimize) ;
+        
 //        System.out.println(query) ;
 //        System.out.print(opOptimize) ;
 //        System.out.print(opExpected) ;
 //        System.out.println("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -") ;
     }
-
-//  String qs1 = StrUtils.strjoinNL
-//  ( "SELECT *",
-//    "WHERE {",
-//    "  ?test ?p1 ?o1.",
-//    "  FILTER ( ?test = <http://localhost/t1> || ?test = <http://localhost/t2> )",
-//    //"  FILTER ( ?test = <http://localhost/t1> )",
-//    "  OPTIONAL {",
-//    "    SELECT ?s1", // Not ?test
-//    "    { ?s1 ?p2 ?o2 }", //" GROUP BY ?s1",
-//    "  }" ,
-//    "}" ) ;
-//
-//// No (conditional) optimization. 
-//String qs2 = StrUtils.strjoinNL
-//  ( "SELECT *",
-//    "WHERE {",
-//    "  ?test ?p1 ?o1.",
-//    "  OPTIONAL {",
-//    "    SELECT ?test",   
-//    "    { ?s1 ?p2 ?o2 }", //" GROUP BY ?s1",
-//    "  }" ,
-//    "}" ) ;
-//
-//// ?test in SELECT can't be simply substituted.
-//String qs3 = StrUtils.strjoinNL
-//  ( "SELECT *"
-//  , "WHERE {"
-//  , "    ?test ?p1 ?X." 
-//  , "    FILTER ( ?test = <http://localhost/t1> )"
-//  , "    { SELECT ?s1 ?test { ?test ?p2 ?o2 } }"
-//  , "}") ; 
-//
-//String qs4 = StrUtils.strjoinNL
-//  ( "SELECT *"
-//  , "WHERE {"
-//  , "    ?test ?p1 ?X." 
-//  , "    FILTER ( ?test = <http://localhost/t1> )"
-//  , "    { SELECT ?s1 { ?test ?p2 ?o2 } }"
-//  , "}") ; 
-//
-
-
 }