You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by th...@apache.org on 2013/11/27 14:02:07 UTC

svn commit: r1546022 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/query/xpath/Expression.java main/java/org/apache/jackrabbit/oak/query/xpath/Statement.java test/resources/org/apache/jackrabbit/oak/query/xpath.txt

Author: thomasm
Date: Wed Nov 27 13:02:07 2013
New Revision: 1546022

URL: http://svn.apache.org/r1546022
Log:
OAK-1229 XPath queries of the form [@x=1 or @x=2] should not use union

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/xpath/Expression.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/xpath/Statement.java
    jackrabbit/oak/trunk/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/xpath.txt

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/xpath/Expression.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/xpath/Expression.java?rev=1546022&r1=1546021&r2=1546022&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/xpath/Expression.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/xpath/Expression.java Wed Nov 27 13:02:07 2013
@@ -147,6 +147,19 @@ abstract class Expression {
         int getPrecedence() {
             return precedence;
         }
+        
+        /**
+         * Get the left-hand-side expression for equality conditions. 
+         * For example, for x=1, it is x. If it is not equality, return null.
+         * 
+         * @return the left-hand-side expression, or null
+         */        
+        public String getCommonLeftPart() {
+            if (!"=".equals(operator)) {
+                return null;
+            }
+            return left.toString();
+        }
     
         @Override
         public String toString() {
@@ -211,6 +224,25 @@ abstract class Expression {
         OrCondition(Expression left, Expression right) {
             super(left, "or", right, Expression.PRECEDENCE_OR);
         }
+
+        /**
+         * Get the left-hand-side expression if it is the same for
+         * both sides. For example, for x=1 or x=2, it is x,
+         * but for x=1 or y=2, it is null
+         * 
+         * @return the left-hand-side expression, or null
+         */
+        @Override
+        public String getCommonLeftPart() {
+            if (left instanceof Condition && right instanceof Condition) {
+                String l = ((Condition) left).getCommonLeftPart();
+                String r = ((Condition) right).getCommonLeftPart();
+                if (l != null && r != null && l.equals(r)) {
+                    return l;
+                }
+            }
+            return null;
+        }
         
     }
     

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/xpath/Statement.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/xpath/Statement.java?rev=1546022&r1=1546021&r2=1546022&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/xpath/Statement.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/xpath/Statement.java Wed Nov 27 13:02:07 2013
@@ -58,18 +58,27 @@ public class Statement {
         }
         if (where instanceof OrCondition) {
             OrCondition or = (OrCondition) where;
-            Statement s1 = new Statement();
-            s1.columnSelector = columnSelector;
-            s1.selectors = selectors;
-            s1.columnList = columnList;
-            s1.where = or.left;
-            Statement s2 = new Statement();
-            s2.columnSelector = columnSelector;
-            s2.selectors = selectors;
-            s2.columnList = columnList;
-            s2.where = or.right;
-            s2.xpathQuery = xpathQuery;
-            return new UnionStatement(s1, s2);
+            if (or.getCommonLeftPart() != null) {
+                // @x = 1 or @x = 2 
+                // is automatically converted to 
+                // @x in (1, 2)
+                // within the query engine
+            } else {
+                // @x = 1 or @y = 2
+                // or similar
+                Statement s1 = new Statement();
+                s1.columnSelector = columnSelector;
+                s1.selectors = selectors;
+                s1.columnList = columnList;
+                s1.where = or.left;
+                Statement s2 = new Statement();
+                s2.columnSelector = columnSelector;
+                s2.selectors = selectors;
+                s2.columnList = columnList;
+                s2.where = or.right;
+                s2.xpathQuery = xpathQuery;
+                return new UnionStatement(s1, s2);
+            }
         }
         return this;
     }

Modified: jackrabbit/oak/trunk/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/xpath.txt
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/xpath.txt?rev=1546022&r1=1546021&r2=1546022&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/xpath.txt (original)
+++ jackrabbit/oak/trunk/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/xpath.txt Wed Nov 27 13:02:07 2013
@@ -23,6 +23,20 @@
 # * new tests are typically be added on top, after the syntax docs
 # * use ascii character only
 
+# union and in(..)
+
+xpath2sql //*[@x = 1 or @x = 2]
+select [jcr:path], [jcr:score], * from [nt:base] as a where [x] = 1 or [x] = 2 /* xpath: //*[@x = 1 or @x = 2] */
+
+xpath2sql //*[@x = 1 or @x = 2 or @x = 3]
+select [jcr:path], [jcr:score], * from [nt:base] as a where [x] = 1 or [x] = 2 or [x] = 3 /* xpath: //*[@x = 1 or @x = 2 or @x = 3] */
+
+xpath2sql //*[@x = 1 or @y = 2]
+select [jcr:path], [jcr:score], * from [nt:base] as a where [x] = 1 union select [jcr:path], [jcr:score], * from [nt:base] as a where [y] = 2 /* xpath: //*[@x = 1 or @y = 2] */
+
+xpath2sql //*[@x = 1 or @x = 2 or @y = 3]
+select [jcr:path], [jcr:score], * from [nt:base] as a where [x] = 1 or [x] = 2 union select [jcr:path], [jcr:score], * from [nt:base] as a where [y] = 3 /* xpath: //*[@x = 1 or @x = 2 or @y = 3] */
+
 # xpath name escaping
 
 xpath2sql //My_x0020_Documents