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 2016/05/03 12:08:54 UTC

svn commit: r1742079 - in /jackrabbit/oak/branches/1.2/oak-core/src: main/java/org/apache/jackrabbit/oak/query/xpath/Expression.java test/java/org/apache/jackrabbit/oak/query/SQL2ParserTest.java test/resources/org/apache/jackrabbit/oak/query/xpath.txt

Author: thomasm
Date: Tue May  3 10:08:54 2016
New Revision: 1742079

URL: http://svn.apache.org/viewvc?rev=1742079&view=rev
Log:
OAK-4324 Backport OAK-3991 (Incorrect resultset from XPATH, multiple ORs and Lucene full-text) to 1.2

Modified:
    jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak/query/xpath/Expression.java
    jackrabbit/oak/branches/1.2/oak-core/src/test/java/org/apache/jackrabbit/oak/query/SQL2ParserTest.java
    jackrabbit/oak/branches/1.2/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/xpath.txt

Modified: jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak/query/xpath/Expression.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak/query/xpath/Expression.java?rev=1742079&r1=1742078&r2=1742079&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak/query/xpath/Expression.java (original)
+++ jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak/query/xpath/Expression.java Tue May  3 10:08:54 2016
@@ -428,23 +428,37 @@ abstract class Expression {
         
         @Override
         AndCondition pullOrRight() {
-            if (right instanceof OrCondition) {
-                return this;
-            } else if (left instanceof OrCondition) {
-                return new AndCondition(right, left);
+            ArrayList<Expression> list = getAllAndConditions();
+            OrCondition or = null;
+            Expression result = null;
+            for(Expression e : list) {
+                if (e instanceof OrCondition && or == null) {
+                    or = (OrCondition) e;
+                } else if (result == null) {
+                    result = e;
+                } else {
+                    result = new AndCondition(result, e);
+                }
+            }
+            if (or != null) {
+                result = new AndCondition(result, or);
+            }
+            return (AndCondition) result;
+        }
+        
+        private ArrayList<Expression> getAllAndConditions() {
+            ArrayList<Expression> list = new ArrayList<Expression>();
+            if (left instanceof AndCondition) {
+                list.addAll(((AndCondition) left).getAllAndConditions());
+            } else {
+                list.add(left);
             }
             if (right instanceof AndCondition) {
-                // pull up x:
-                // a and (b and (x)) -> (a and b) and (x)
-                AndCondition r2 = (AndCondition) right;
-                r2 = r2.pullOrRight();
-                AndCondition l2 = new AndCondition(left, r2.left);
-                l2 = l2.pullOrRight();
-                return new AndCondition(l2, r2.right);
-            } else if (left instanceof AndCondition) {
-                return new AndCondition(right, left).pullOrRight();
+                list.addAll(((AndCondition) right).getAllAndConditions());
+            } else {
+                list.add(right);
             }
-            return this;
+            return list;
         }
         
         @Override

Modified: jackrabbit/oak/branches/1.2/oak-core/src/test/java/org/apache/jackrabbit/oak/query/SQL2ParserTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.2/oak-core/src/test/java/org/apache/jackrabbit/oak/query/SQL2ParserTest.java?rev=1742079&r1=1742078&r2=1742079&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.2/oak-core/src/test/java/org/apache/jackrabbit/oak/query/SQL2ParserTest.java (original)
+++ jackrabbit/oak/branches/1.2/oak-core/src/test/java/org/apache/jackrabbit/oak/query/SQL2ParserTest.java Tue May  3 10:08:54 2016
@@ -16,10 +16,10 @@
  */
 package org.apache.jackrabbit.oak.query;
 
-import static junit.framework.Assert.assertTrue;
 import static org.apache.jackrabbit.JcrConstants.JCR_SYSTEM;
 import static org.apache.jackrabbit.oak.plugins.nodetype.NodeTypeConstants.JCR_NODE_TYPES;
 import static org.apache.jackrabbit.oak.plugins.nodetype.write.InitialContent.INITIAL_CONTENT;
+import static org.junit.Assert.assertTrue;
 
 import java.text.ParseException;
 
@@ -74,4 +74,28 @@ public class SQL2ParserTest {
         assertTrue(q.contains(token));
     }
     
+    @Test
+    public void convertXPath() throws ParseException {
+        String query = "/jcr:root/content//element(*, nt:base)[" +
+                "jcr:contains(., 'to') and jcr:contains(., 'write') " +
+                "and jcr:contains(., 'to') and jcr:contains(., '.') " +
+                "and jcr:contains(., 'Free') and jcr:contains(., 'some') " +
+                "and jcr:contains(., 'space') and jcr:contains(., 'on') " +
+                "and jcr:contains(., 'this') and jcr:contains(., 'drive,') " +
+                "and jcr:contains(., 'or') and jcr:contains(., 'save') " +
+                "and jcr:contains(., 'the') and jcr:contains(., 'document') " +
+                "and jcr:contains(., 'on') and jcr:contains(., 'another') " +
+                "and jcr:contains(., 'disk.') and jcr:contains(., 'Try') " +
+                "and jcr:contains(., 'one') and jcr:contains(., 'or') " +
+                "and jcr:contains(., 'more') and jcr:contains(., 'of') " +
+                "and jcr:contains(., 'the') and @following = 'Close' " +
+                "and jcr:contains(., 'any') and jcr:contains(., 'unneeded') " +
+                "and jcr:contains(., 'documents,') and jcr:contains(., 'programs,') " +
+                "and jcr:contains(., 'and') and jcr:contains(., 'windows.') " +
+                "and jcr:contains(., 'Save') and jcr:contains(., 'the') " +
+                "] ";
+ 
+        new XPathToSQL2Converter().convert(query);
+    }    
+    
 }

Modified: jackrabbit/oak/branches/1.2/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/xpath.txt
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.2/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/xpath.txt?rev=1742079&r1=1742078&r2=1742079&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.2/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/xpath.txt (original)
+++ jackrabbit/oak/branches/1.2/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/xpath.txt Tue May  3 10:08:54 2016
@@ -64,26 +64,26 @@ select [jcr:path], [jcr:score], *
 xpath2sql /jcr:root/content//*[((@i = '1' or @i = '2') or (@s = 'x')) and (@t = 'a' or @t = 'b')]
 select [jcr:path], [jcr:score], *
   from [nt:base] as a
-  where isdescendantnode(a, '/content')
-  and [t] in('a', 'b')
+  where [t] in('a', 'b')
+  and isdescendantnode(a, '/content')
   and [i] in('1', '2')
   union select [jcr:path], [jcr:score], *
   from [nt:base] as a
-  where isdescendantnode(a, '/content')
-  and [t] in('a', 'b')
+  where [t] in('a', 'b')
+  and isdescendantnode(a, '/content')
   and [s] = 'x'
   /* xpath ... */
 
 xpath2sql /jcr:root/content//*[((@i = '1' or @i = '2') or (@s = 'x')) and (@t = 'a')]
 select [jcr:path], [jcr:score], *
   from [nt:base] as a
-  where isdescendantnode(a, '/content')
-  and [t] = 'a'
+  where [t] = 'a'
+  and isdescendantnode(a, '/content')
   and [i] in('1', '2')
   union select [jcr:path], [jcr:score], *
   from [nt:base] as a
-  where isdescendantnode(a, '/content')
-  and [t] = 'a'
+  where [t] = 'a'
+  and isdescendantnode(a, '/content')
   and [s] = 'x'
   /* xpath ... */
 
@@ -102,13 +102,13 @@ select [jcr:path], [jcr:score], *
 xpath2sql /jcr:root/content//*[((@i = '1' or @i = '2') or (@s = 'x')) and (@t = 'a' or @t = 'b')] order by @a
 select [jcr:path], [jcr:score], *
   from [nt:base] as a
-  where isdescendantnode(a, '/content')
-  and [t] in('a', 'b')
+  where [t] in('a', 'b')
+  and isdescendantnode(a, '/content')
   and [i] in('1', '2')
   union select [jcr:path], [jcr:score], *
   from [nt:base] as a
-  where isdescendantnode(a, '/content')
-  and [t] in('a', 'b')
+  where [t] in('a', 'b')
+  and isdescendantnode(a, '/content')
   and [s] = 'x'
   order by [a]
   /* xpath ... */
@@ -116,13 +116,13 @@ select [jcr:path], [jcr:score], *
 xpath2sql /jcr:root/content//*[((@i = '1' or @i = '2') or (@s = 'x')) and (@t = 'a')] order by @a
 select [jcr:path], [jcr:score], *
   from [nt:base] as a
-  where isdescendantnode(a, '/content')
-  and [t] = 'a'
+  where [t] = 'a'
+  and isdescendantnode(a, '/content')
   and [i] in('1', '2')
   union select [jcr:path], [jcr:score], *
   from [nt:base] as a
-  where isdescendantnode(a, '/content')
-  and [t] = 'a'
+  where [t] = 'a'
+  and isdescendantnode(a, '/content')
   and [s] = 'x'
   order by [a]
   /* xpath ... */
@@ -684,29 +684,29 @@ select b.[jcr:path] as [jcr:path], b.[jc
   from [nt:base] as a
   inner join [nt:base] as b
   on ischildnode(b, a)
-  where a.[a] = 1
-  and name(b) = 'sub'
+  where name(b) = 'sub'
+  and a.[a] = 1
   and b.[c] = 1
   union select b.[jcr:path] as [jcr:path], b.[jcr:score] as [jcr:score], b.*
   from [nt:base] as a
   inner join [nt:base] as b
   on ischildnode(b, a)
-  where a.[a] = 1
-  and name(b) = 'sub'
+  where name(b) = 'sub'
+  and a.[a] = 1
   and b.[d] = 1
   union select b.[jcr:path] as [jcr:path], b.[jcr:score] as [jcr:score], b.*
   from [nt:base] as a
   inner join [nt:base] as b
   on ischildnode(b, a)
-  where a.[b] = 1
-  and name(b) = 'sub'
+  where name(b) = 'sub'
+  and a.[b] = 1
   and b.[c] = 1
   union select b.[jcr:path] as [jcr:path], b.[jcr:score] as [jcr:score], b.*
   from [nt:base] as a
   inner join [nt:base] as b
   on ischildnode(b, a)
-  where a.[b] = 1
-  and name(b) = 'sub'
+  where name(b) = 'sub'
+  and a.[b] = 1
   and b.[d] = 1
   /* xpath ... */
 
@@ -1088,52 +1088,52 @@ xpath2sql /jcr:root/content/campaigns//e
   order by @onTime
 select [jcr:path], [jcr:score], *
   from [PageContent] as a
-  where [onTime] < cast('2012-04-01T00:00:00.000+02:00' as date)
-  and [offTime] >= cast('2012-02-26T00:00:00.000+01:00' as date)
-  and isdescendantnode(a, '/content/campaigns')
+  where isdescendantnode(a, '/content/campaigns')
   and [sling:resourceType] in('teaser', 'newsletter')
-  union select [jcr:path], [jcr:score], *
-  from [PageContent] as a
-  where [onTime] < cast('2012-04-01T00:00:00.000+02:00' as date)
+  and [onTime] < cast('2012-04-01T00:00:00.000+02:00' as date)
   and [offTime] >= cast('2012-02-26T00:00:00.000+01:00' as date)
-  and isdescendantnode(a, '/content/campaigns')
-  and [teaserPageType] in('newsletter', 'tweet')
   union select [jcr:path], [jcr:score], *
   from [PageContent] as a
-  where [onTime] is null
-  and [offTime] >= cast('2012-02-26T00:00:00.000+01:00' as date)
-  and isdescendantnode(a, '/content/campaigns')
+  where isdescendantnode(a, '/content/campaigns')
   and [sling:resourceType] in('teaser', 'newsletter')
+  and [onTime] < cast('2012-04-01T00:00:00.000+02:00' as date)
+  and [offTime] is null
   union select [jcr:path], [jcr:score], *
   from [PageContent] as a
-  where [onTime] is null
+  where isdescendantnode(a, '/content/campaigns')
+  and [sling:resourceType] in('teaser', 'newsletter')
+  and [onTime] is null
   and [offTime] >= cast('2012-02-26T00:00:00.000+01:00' as date)
-  and isdescendantnode(a, '/content/campaigns')
-  and [teaserPageType] in('newsletter', 'tweet')
   union select [jcr:path], [jcr:score], *
   from [PageContent] as a
-  where [onTime] < cast('2012-04-01T00:00:00.000+02:00' as date)
-  and [offTime] is null
-  and isdescendantnode(a, '/content/campaigns')
+  where isdescendantnode(a, '/content/campaigns')
   and [sling:resourceType] in('teaser', 'newsletter')
+  and [onTime] is null
+  and [offTime] is null
   union select [jcr:path], [jcr:score], *
   from [PageContent] as a
-  where [onTime] < cast('2012-04-01T00:00:00.000+02:00' as date)
-  and [offTime] is null
-  and isdescendantnode(a, '/content/campaigns')
+  where isdescendantnode(a, '/content/campaigns')
   and [teaserPageType] in('newsletter', 'tweet')
+  and [onTime] < cast('2012-04-01T00:00:00.000+02:00' as date)
+  and [offTime] >= cast('2012-02-26T00:00:00.000+01:00' as date)
   union select [jcr:path], [jcr:score], *
   from [PageContent] as a
-  where [onTime] is null
+  where isdescendantnode(a, '/content/campaigns')
+  and [teaserPageType] in('newsletter', 'tweet')
+  and [onTime] < cast('2012-04-01T00:00:00.000+02:00' as date)
   and [offTime] is null
-  and isdescendantnode(a, '/content/campaigns')
-  and [sling:resourceType] in('teaser', 'newsletter')
   union select [jcr:path], [jcr:score], *
   from [PageContent] as a
-  where [onTime] is null
-  and [offTime] is null
-  and isdescendantnode(a, '/content/campaigns')
+  where isdescendantnode(a, '/content/campaigns')
+  and [teaserPageType] in('newsletter', 'tweet')
+  and [onTime] is null
+  and [offTime] >= cast('2012-02-26T00:00:00.000+01:00' as date)
+  union select [jcr:path], [jcr:score], *
+  from [PageContent] as a
+  where isdescendantnode(a, '/content/campaigns')
   and [teaserPageType] in('newsletter', 'tweet')
+  and [onTime] is null
+  and [offTime] is null
   order by [onTime]
   /* xpath ... */