jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1503145 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/query/ main/java/org/apache/jackrabbit/oak/query/ast/ test/resources/org/apache/jackrabbit/oak/query/
Date Mon, 15 Jul 2013 09:06:27 GMT
Author: thomasm
Date: Mon Jul 15 09:06:26 2013
New Revision: 1503145

URL: http://svn.apache.org/r1503145
Log:
OAK-899 Query: simplify duplicate conditions

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Query.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/AndImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/ConstraintImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/OrImpl.java
    jackrabbit/oak/trunk/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/sql2_index.txt

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Query.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Query.java?rev=1503145&r1=1503144&r2=1503145&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Query.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/Query.java
Mon Jul 15 09:06:26 2013
@@ -89,11 +89,11 @@ public class Query {
 
     final SourceImpl source;
     final String statement;
-    final ConstraintImpl constraint;
     final HashMap<String, PropertyValue> bindVariableMap = new HashMap<String, PropertyValue>();
     final HashMap<String, Integer> selectorIndexes = new HashMap<String, Integer>();
     final ArrayList<SelectorImpl> selectors = new ArrayList<SelectorImpl>();
-
+    ConstraintImpl constraint;
+    
     private QueryEngineImpl queryEngine;
     private final OrderingImpl[] orderings;
     private ColumnImpl[] columns;
@@ -291,6 +291,9 @@ public class Query {
             }
 
         }.visit(this);
+        if (constraint != null) {
+            constraint = constraint.simplify();
+        }
         source.setQueryConstraint(constraint);
         source.init(this);
         for (ColumnImpl column : columns) {

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/AndImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/AndImpl.java?rev=1503145&r1=1503144&r2=1503145&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/AndImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/AndImpl.java
Mon Jul 15 09:06:26 2013
@@ -46,6 +46,14 @@ public class AndImpl extends ConstraintI
     public ConstraintImpl getConstraint2() {
         return constraint2;
     }
+
+    @Override
+    public ConstraintImpl simplify() {
+        if (constraint1.equals(constraint2)) {
+            return constraint1;
+        }
+        return this;
+    }
     
     @Override
     public Set<PropertyExistenceImpl> getPropertyExistenceConditions() {

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/ConstraintImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/ConstraintImpl.java?rev=1503145&r1=1503144&r2=1503145&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/ConstraintImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/ConstraintImpl.java
Mon Jul 15 09:06:26 2013
@@ -25,6 +25,16 @@ import org.apache.jackrabbit.oak.query.i
  * The base class for constraints.
  */
 public abstract class ConstraintImpl extends AstElement {
+    
+    /**
+     * Simplify the expression if possible, for example by removing duplicate expressions.
+     * For example, "x=1 or x=1" should be simplified to "x=1".
+     * 
+     * @return the simplified constraint, or "this" if it is not possible to simplify
+     */
+    public ConstraintImpl simplify() {
+        return this;
+    }
 
     /**
      * Evaluate the result using the currently set values.
@@ -79,5 +89,20 @@ public abstract class ConstraintImpl ext
      * @param s the selector
      */
     public abstract void restrictPushDown(SelectorImpl s);
+    
+    @Override
+    public boolean equals(Object other) {
+        if (other == this) {
+            return true;
+        } else if (!(other instanceof ConstraintImpl)) {
+            return false;
+        }
+        return toString().equals(other.toString());
+    }
+
+    @Override
+    public int hashCode() {
+        return toString().hashCode();
+    }
 
 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/OrImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/OrImpl.java?rev=1503145&r1=1503144&r2=1503145&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/OrImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/query/ast/OrImpl.java
Mon Jul 15 09:06:26 2013
@@ -50,6 +50,14 @@ public class OrImpl extends ConstraintIm
     }
     
     @Override
+    public ConstraintImpl simplify() {
+        if (constraint1.equals(constraint2)) {
+            return constraint1;
+        }
+        return this;
+    }
+    
+    @Override
     public Set<PropertyExistenceImpl> getPropertyExistenceConditions() {
         // for the condition "x=1 or x=2", the existence condition
         // "x is not null" be be derived

Modified: jackrabbit/oak/trunk/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/sql2_index.txt
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/sql2_index.txt?rev=1503145&r1=1503144&r2=1503145&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/sql2_index.txt
(original)
+++ jackrabbit/oak/trunk/oak-core/src/test/resources/org/apache/jackrabbit/oak/query/sql2_index.txt
Mon Jul 15 09:06:26 2013
@@ -28,6 +28,9 @@
 explain select excerpt(.) from [nt:resource] where contains(*, 'jackrabbit')
 [nt:resource] as [nt:resource] /* traverse "*" where contains([nt:resource].[*], cast('jackrabbit'
as string)) */
 
+explain select excerpt(.) from [nt:resource] where contains(*, 'jackrabbit') or contains(*,
'jackrabbit')
+[nt:resource] as [nt:resource] /* traverse "*" where contains([nt:resource].[*], cast('jackrabbit'
as string)) */
+
 explain select * from [nt:base] where [jcr:uuid]=1 or [b]=2
 [nt:base] as [nt:base] /* traverse "*" */
 



Mime
View raw message