jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject [2/2] jena git commit: JENA-1086: property functions (and prcoedures) filter placement.
Date Sat, 12 Dec 2015 16:34:10 GMT
JENA-1086: property functions (and prcoedures) filter placement.

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

Branch: refs/heads/master
Commit: 25c83a6720eeb480ba0651cb02dcd513efee8a20
Parents: c336763
Author: Andy Seaborne <andy@apache.org>
Authored: Sat Dec 12 16:33:52 2015 +0000
Committer: Andy Seaborne <andy@apache.org>
Committed: Sat Dec 12 16:33:52 2015 +0000

----------------------------------------------------------------------
 .../org/apache/jena/sparql/algebra/OpVars.java  |  13 +-
 .../jena/sparql/algebra/op/OpPropFunc.java      |   9 +-
 .../optimize/TransformFilterPlacement.java      |  49 +++-
 .../jena/sparql/pfunction/PropFuncArg.java      |  13 ++
 .../optimize/TestTransformFilterPlacement.java  | 227 ++++++++++++++++++-
 5 files changed, 285 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/25c83a67/jena-arq/src/main/java/org/apache/jena/sparql/algebra/OpVars.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/algebra/OpVars.java b/jena-arq/src/main/java/org/apache/jena/sparql/algebra/OpVars.java
index 1c5ceb7..79fb69a 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/algebra/OpVars.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/algebra/OpVars.java
@@ -260,17 +260,8 @@ public class OpVars
 
         @Override
         public void visit(OpPropFunc opPropFunc) {
-            addvars(opPropFunc.getSubjectArgs()) ;
-            addvars(opPropFunc.getObjectArgs()) ;
-        }
-
-        private void addvars(PropFuncArg pfArg) {
-            if (pfArg.isNode()) {
-                addVar(acc, pfArg.getArg()) ;
-                return ;
-            }
-            for (Node n : pfArg.getArgList())
-                addVar(acc, n) ;
+            PropFuncArg.addVars(acc, opPropFunc.getSubjectArgs()) ;
+            PropFuncArg.addVars(acc, opPropFunc.getObjectArgs()) ;
         }
 
         @Override

http://git-wip-us.apache.org/repos/asf/jena/blob/25c83a67/jena-arq/src/main/java/org/apache/jena/sparql/algebra/op/OpPropFunc.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/algebra/op/OpPropFunc.java b/jena-arq/src/main/java/org/apache/jena/sparql/algebra/op/OpPropFunc.java
index 74f86d3..1a0e003 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/algebra/op/OpPropFunc.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/algebra/op/OpPropFunc.java
@@ -47,19 +47,16 @@ public class OpPropFunc extends Op1
         this.objectArgs = args2 ;
     }
     
-    public PropFuncArg getSubjectArgs()
-    {
+    public PropFuncArg getSubjectArgs() {
         return subjectArgs ;
     } 
     
-    public PropFuncArg getObjectArgs()
-    {
+    public PropFuncArg getObjectArgs() {
         return objectArgs ;
     } 
     
     @Override
-    public Op apply(Transform transform, Op subOp)
-    {
+    public Op apply(Transform transform, Op subOp) {
         return transform.transform(this, subOp) ;
     }
 

http://git-wip-us.apache.org/repos/asf/jena/blob/25c83a67/jena-arq/src/main/java/org/apache/jena/sparql/algebra/optimize/TransformFilterPlacement.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/algebra/optimize/TransformFilterPlacement.java
b/jena-arq/src/main/java/org/apache/jena/sparql/algebra/optimize/TransformFilterPlacement.java
index fb90412..3056503 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/algebra/optimize/TransformFilterPlacement.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/algebra/optimize/TransformFilterPlacement.java
@@ -1,5 +1,5 @@
 /*
-z * Licensed to the Apache Software Foundation (ASF) under one
+ * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
  * regarding copyright ownership.  The ASF licenses this file
@@ -18,15 +18,12 @@ z * Licensed to the Apache Software Foundation (ASF) under one
 
 package org.apache.jena.sparql.algebra.optimize ;
 
-import java.util.Collection ;
-import java.util.Iterator ;
-import java.util.List ;
-import java.util.Objects;
-import java.util.Set ;
+import java.util.* ;
 
 import org.apache.jena.atlas.lib.CollectionUtils ;
 import org.apache.jena.atlas.lib.DS ;
 import org.apache.jena.atlas.lib.Lib ;
+import org.apache.jena.atlas.lib.SetUtils ;
 import org.apache.jena.graph.Node ;
 import org.apache.jena.graph.Triple ;
 import org.apache.jena.sparql.algebra.Op ;
@@ -38,6 +35,7 @@ import org.apache.jena.sparql.core.Var ;
 import org.apache.jena.sparql.expr.Expr ;
 import org.apache.jena.sparql.expr.ExprLib ;
 import org.apache.jena.sparql.expr.ExprList ;
+import org.apache.jena.sparql.pfunction.PropFuncArg ;
 import org.apache.jena.sparql.util.VarUtils ;
 
 /**
@@ -198,6 +196,10 @@ public class TransformFilterPlacement extends TransformCopy {
             placement = placeFilter(exprs, (OpFilter)input) ;
         else if ( input instanceof OpUnion )
             placement = placeUnion(exprs, (OpUnion)input) ;
+        else if ( input instanceof OpPropFunc )
+            placement = placePropertyFunction(exprs, (OpPropFunc)input) ;
+        else if ( input instanceof OpProcedure )
+            placement = placeProcedure(exprs, (OpProcedure)input) ;
         
         // These are operations where changing the order of operations
         // does not in itself make a difference but enables expressions
@@ -230,7 +232,6 @@ public class TransformFilterPlacement extends TransformCopy {
 
         return placement ;
     }
-    
     private Placement placeFilter(ExprList exprs, OpFilter input) {
         // If input.getSubOp is itself a filter, it has already been
         // processed because the Transform is applied bottom-up.
@@ -424,6 +425,39 @@ public class TransformFilterPlacement extends TransformCopy {
         return null ;
     }
 
+    private Placement placePropertyFunction(ExprList exprsIn, OpPropFunc input) {
+        Set<Var> argVars = DS.set() ;
+        PropFuncArg.addVars(argVars, input.getSubjectArgs()) ;
+        PropFuncArg.addVars(argVars, input.getObjectArgs()) ;
+        return placePropertyFunctionProcedure(exprsIn, argVars, input) ;
+    }
+
+    private Placement placeProcedure(ExprList exprsIn, OpProcedure input) {
+        Set<Var> argVars = DS.set() ;
+        input.getArgs().varsMentioned(argVars);
+        return placePropertyFunctionProcedure(exprsIn, argVars, input) ;
+    }
+    
+    private Placement placePropertyFunctionProcedure(ExprList exprsIn, Set<Var> varScope,
Op1 op) {
+        ExprList exprListPlaceable = new ExprList() ;
+        ExprList exprListRetain = new ExprList() ;
+        for ( Expr expr : exprsIn ) {
+            Set<Var> mentioned = expr.getVarsMentioned() ;
+            if ( SetUtils.disjoint(varScope, mentioned) )
+                exprListPlaceable.add(expr);
+            else
+                exprListRetain.add(expr);
+        }
+        if ( ! exprListPlaceable.isEmpty() ) {
+            Placement p = transform(exprListPlaceable, op.getSubOp()) ;
+            Op newOp = op.copy(p.op) ;
+            p.unplaced.addAll(exprListRetain);
+            return result(newOp, p.unplaced) ;
+        }
+        return resultNoChange(op);
+    }
+
+    
     /*
      * A Sequence is a number of joins where scoping means the LHS can be
      * substituted into the right, i.e. there are no scoping issues. Assuming a
@@ -435,7 +469,6 @@ public class TransformFilterPlacement extends TransformCopy {
      * different scope, which is a different variable with the same name in the
      * orginal query).
      */
-
     private Placement placeSequence(ExprList exprsIn, OpSequence opSequence) {
         ExprList exprs = ExprList.copy(exprsIn) ;
         Set<Var> varScope = DS.set() ;

http://git-wip-us.apache.org/repos/asf/jena/blob/25c83a67/jena-arq/src/main/java/org/apache/jena/sparql/pfunction/PropFuncArg.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/pfunction/PropFuncArg.java b/jena-arq/src/main/java/org/apache/jena/sparql/pfunction/PropFuncArg.java
index e2f240e..1c60368 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/pfunction/PropFuncArg.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/pfunction/PropFuncArg.java
@@ -18,10 +18,14 @@
 
 package org.apache.jena.sparql.pfunction;
 
+import static org.apache.jena.sparql.core.Vars.addVar ;
+
+import java.util.Collection ;
 import java.util.List ;
 
 import org.apache.jena.atlas.io.IndentedWriter ;
 import org.apache.jena.graph.Node ;
+import org.apache.jena.sparql.core.Var ;
 import org.apache.jena.sparql.expr.Expr ;
 import org.apache.jena.sparql.expr.ExprList ;
 import org.apache.jena.sparql.graph.NodeConst ;
@@ -131,4 +135,13 @@ public class PropFuncArg extends PrintSerializableBase
         if ( arg != null )
             out.print(FmtUtils.stringForNode(arg)) ;
     }
+
+    public static void addVars(Collection<Var> acc, PropFuncArg pfArg) {
+        if (pfArg.isNode()) {
+            addVar(acc, pfArg.getArg()) ;
+            return ;
+        }
+        for (Node n : pfArg.getArgList())
+            addVar(acc, n) ;
+    }
 }

http://git-wip-us.apache.org/repos/asf/jena/blob/25c83a67/jena-arq/src/test/java/org/apache/jena/sparql/algebra/optimize/TestTransformFilterPlacement.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/test/java/org/apache/jena/sparql/algebra/optimize/TestTransformFilterPlacement.java
b/jena-arq/src/test/java/org/apache/jena/sparql/algebra/optimize/TestTransformFilterPlacement.java
index 3a68e8b..f06ce3e 100644
--- a/jena-arq/src/test/java/org/apache/jena/sparql/algebra/optimize/TestTransformFilterPlacement.java
+++ b/jena-arq/src/test/java/org/apache/jena/sparql/algebra/optimize/TestTransformFilterPlacement.java
@@ -18,12 +18,24 @@
 
 package org.apache.jena.sparql.algebra.optimize ;
 
+import java.util.Objects ;
+
 import org.apache.jena.atlas.junit.BaseTest ;
 import org.apache.jena.atlas.lib.StrUtils ;
+import org.apache.jena.graph.Node ;
 import org.apache.jena.sparql.algebra.Op ;
 import org.apache.jena.sparql.algebra.Transform ;
 import org.apache.jena.sparql.algebra.Transformer ;
-import org.apache.jena.sparql.algebra.optimize.TransformFilterPlacement ;
+import org.apache.jena.sparql.engine.ExecutionContext ;
+import org.apache.jena.sparql.engine.QueryIterator ;
+import org.apache.jena.sparql.engine.binding.Binding ;
+import org.apache.jena.sparql.expr.ExprList ;
+import org.apache.jena.sparql.pfunction.PropFuncArg ;
+import org.apache.jena.sparql.pfunction.PropertyFunction ;
+import org.apache.jena.sparql.pfunction.PropertyFunctionRegistry ;
+import org.apache.jena.sparql.procedure.Procedure ;
+import org.apache.jena.sparql.procedure.ProcedureBase ;
+import org.apache.jena.sparql.procedure.ProcedureRegistry ;
 import org.apache.jena.sparql.sse.SSE ;
 import org.junit.Assert ;
 import org.junit.Test ;
@@ -799,6 +811,213 @@ public class TestTransformFilterPlacement extends BaseTest { //extends
AbstractT
         test( in, out ) ;
     }
     
+    private static String propertyFunctionURI = "http://example/PF" ;
+    // Dummy property 
+    private static PropertyFunction dummyPropertyFunction = new PropertyFunction() {
+        @Override
+        public QueryIterator exec(QueryIterator input, PropFuncArg argSubject, Node predicate,
PropFuncArg argObject, ExecutionContext execCxt) {
+            return null;
+        }
+
+        @Override
+        public void build(PropFuncArg argSubject, Node predicate, PropFuncArg argObject,
ExecutionContext execCxt) {}
+    };
+    
+    private static void test_property_function(Runnable action) {
+        PropertyFunctionRegistry.get().put(propertyFunctionURI, dummyPropertyFunction.getClass());
+        try {
+            action.run();
+        } finally {
+            PropertyFunctionRegistry.get().remove(propertyFunctionURI) ;
+        }
+    }
+    
+    @Test public void place_property_functions_01() {
+        // No filter.
+        test_property_function(()->{
+            String in = "(propfunc :PF ?pfSubjArg (?pfObjArg1 ?pfObjArg2) (bgp(?s ?p ?o)))"
;
+            String out = in ;
+            test( in, out ) ;
+        }) ;
+    }
+    
+    @Test public void place_property_functions_02() {
+        test_property_function(()->{
+            String in = StrUtils.strjoinNL
+                ("(filter ((= ?x 1)(= ?o 9))"
+                ,"    (propfunc :PF ?pfSubjArg (?pfObjArg1 ?pfObjArg2)"
+                ,"       (bgp (?s ?p ?o))"
+                ,"    ))"
+                 ) ;
+            String out = StrUtils.strjoinNL
+                ("(filter (= ?x 1)"
+                ,"  (propfunc :PF ?pfSubjArg (?pfObjArg1 ?pfObjArg2)"
+                ,"    (filter (= ?o 9)"
+                ,"      (bgp (triple ?s ?p ?o))"
+                ,"    )))"
+                ) ;
+            test( in, out ) ;
+        }) ;
+    }
+    
+    @Test public void place_property_functions_02a() {
+        test_property_function(()->{
+            String in = StrUtils.strjoinNL
+                ("(filter (= ?x 1)"
+                ,"    (propfunc :PF ?pfSubjArg (?pfObjArg1 ?pfObjArg2)"
+                ,"       (bgp (?s ?p ?o))"
+                ,"    ))"
+                 ) ;
+            String out = in ;
+            test( in, out ) ;
+        }) ;
+    }
+
+    @Test public void place_property_functions_02b() {
+        test_property_function(()->{
+            String in = StrUtils.strjoinNL
+                ("(filter (= ?o 9)"
+                ,"    (propfunc :PF ?pfSubjArg (?pfObjArg1 ?pfObjArg2)"
+                ,"       (bgp (?s ?p ?o))"
+                ,"    ))"
+                 ) ;
+            String out = StrUtils.strjoinNL
+                ("(propfunc :PF ?pfSubjArg (?pfObjArg1 ?pfObjArg2)"
+                ,"  (filter (= ?o 9)"
+                ,"   (bgp (triple ?s ?p ?o))"
+                ,"  ))"
+                ) ;
+            test( in, out ) ;
+        }) ;
+    }
+    
+    @Test public void place_property_functions_03() {
+        test_property_function(()->{
+            String in = StrUtils.strjoinNL
+                ("(filter ((= ?pfSubjArg 1)(= ?o 9))"
+                ,"    (propfunc :PF ?pfSubjArg (?pfObjArg1 ?pfObjArg2)"
+                ,"       (bgp (?s ?p ?o))"
+                ,"    ))"
+                 ) ;
+            String out = StrUtils.strjoinNL
+                ("(filter (= ?pfSubjArg 1)"
+                ,"  (propfunc :PF ?pfSubjArg (?pfObjArg1 ?pfObjArg2)"
+                ,"    (filter (= ?o 9)"
+                ,"      (bgp (triple ?s ?p ?o))"
+                ,"    )))"
+                ) ;
+            test( in, out ) ;
+        }) ;
+    }
+
+    @Test public void place_property_functions_04() {
+        test_property_function(()->{
+            String in = StrUtils.strjoinNL
+                ("(filter ((= ?pfObjArg 1)(= ?o 9))"
+                ,"    (propfunc :PF ?pfSubjArg (?pfObjArg1 ?pfObjArg2)"
+                ,"       (bgp (?s ?p ?o))"
+                ,"    ))"
+                 ) ;
+            String out = StrUtils.strjoinNL
+                ("(filter (= ?pfObjArg 1)"
+                ,"  (propfunc :PF ?pfSubjArg (?pfObjArg1 ?pfObjArg2)"
+                ,"    (filter (= ?o 9)"
+                ,"      (bgp (triple ?s ?p ?o))"
+                ,"    )))"
+                ) ;
+            test( in, out ) ;
+        }) ;
+    }
+     
+    private static String procedureURI = "http://example/PROC" ;
+    // Dummy procedure 
+    private static Procedure dummyProcedure = new ProcedureBase() {
+        @Override
+        public QueryIterator exec(Binding binding, Node name, ExprList args, ExecutionContext
execCxt) {
+            return null;
+        }
+    };
+
+    private static void test_procedure(Runnable action) {
+        ProcedureRegistry.get().put(procedureURI, dummyProcedure.getClass());
+        try {
+            action.run();
+        } finally {
+            ProcedureRegistry.get().remove(procedureURI) ;
+        }
+    }
+    
+    @Test public void place_procedure_01() {
+        test_procedure(()->{
+            String in = "(proc :PROC ((+ ?arg1 111) (?arg2)) (bgp (?s ?p ?o)))" ;
+            String out = in ;
+            test( in, out ) ;
+        }) ;
+    }
+    
+    @Test public void place_procedure_02() {
+        test_procedure(()->{
+            String in = StrUtils.strjoinNL
+                ("(filter (= ?o 9)"
+                ,"   (proc :PROC ((+ ?arg1 111) (?arg2))"
+                ,"      (bgp (?s ?p ?o))"
+                ,"   ))"
+                ) ;
+            String out = StrUtils.strjoinNL
+                ("(proc :PROC ((+ ?arg1 111) (?arg2))"
+                ,"  (filter (= ?o 9)"
+                ,"    (bgp (?s ?p ?o))"
+                ,"   ))"
+                ) ; 
+            test( in, out ) ;
+        }) ;
+    }
+    
+    @Test public void place_procedure_03() {
+        test_procedure(()->{
+            String in = StrUtils.strjoinNL
+                ("(filter (= ?x 9)"
+                ,"   (proc :PROC ((+ ?arg1 111) (?arg2))"
+                ,"      (bgp (?s ?p ?o))"
+                ,"   ))"
+                ) ;
+            String out = in ;
+            test( in, out ) ;
+        }) ;
+    }
+
+    @Test public void place_procedure_04() {
+        test_procedure(()->{
+            String in = StrUtils.strjoinNL
+                ("(filter (= ?arg1 9)"
+                ,"   (proc :PROC ((+ ?arg1 111) (?arg2))"
+                ,"      (bgp (?s ?p ?o))"
+                ,"   ))"
+                ) ;
+            String out = in ;
+            test( in, out ) ;
+        }) ;
+    }
+
+    @Test public void place_procedure_05() {
+        test_procedure(()->{
+            String in = StrUtils.strjoinNL
+                ("(filter ((= ?x 9) (= ?o 11) (= ?arg2 19))"
+                ,"   (proc :PROC ((+ ?arg1 111) (?arg2))"
+                ,"      (bgp (?s ?p ?o))"
+                ,"   ))"
+                ) ;
+            String out = StrUtils.strjoinNL
+                ("(filter ((= ?x 9) (= ?arg2 19))"
+                ,"   (proc :PROC ((+ ?arg1 111) (?arg2))"
+                ,"     (filter (= ?o 11)"
+                ,"       (bgp (?s ?p ?o))"
+                ,"     )))"
+                ) ;
+            test( in, out ) ;
+        }) ;
+    }
+
     @Test public void nondeterministic_functions_01() {
         testNoChange("(filter (= ?x (rand)) (bgp (?s ?p ?x) (?s1 ?p1 ?x)))") ;
     }
@@ -876,6 +1095,12 @@ public class TestTransformFilterPlacement extends BaseTest { //extends
AbstractT
         }
 
         Op op3 = SSE.parseOp(output) ;
+        if ( ! Objects.equals(op2,  op3) ) {
+            System.out.println("Expected:") ;
+            System.out.println(op3);
+            System.out.println("Got:") ;
+            System.out.println(op2);
+        }
         Assert.assertEquals(op3, op2) ;
     }
 }


Mime
View raw message