Author: catholicon
Date: Wed Apr 27 21:09:23 2016
New Revision: 1741343

URL: http://svn.apache.org/viewvc?rev=1741343&view=rev
Log:
OAK-4313: QueryImpl should avoid traversal with queries containing native 
constraints

Added:
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/NativeQueryTest.java
   (with props)
Modified:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/query/Cursors.java

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/query/Cursors.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/query/Cursors.java?rev=1741343&r1=1741342&r2=1741343&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/query/Cursors.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/query/Cursors.java
 Wed Apr 27 21:09:23 2016
@@ -241,6 +241,12 @@ public class Cursors {
             NodeState parent = null;
             NodeState node = rootState;
             
+            if (filter.containsNativeConstraint()) {
+                // OAK-4313: if no other index was found,
+                // then, for native queries, we won't match anything
+                return;
+            }
+
             if (filter.isAlwaysFalse()) {
                 // nothing can match this filter, leave nodes empty
                 return;

Added: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/NativeQueryTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/NativeQueryTest.java?rev=1741343&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/NativeQueryTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/NativeQueryTest.java
 Wed Apr 27 21:09:23 2016
@@ -0,0 +1,92 @@
+/*
+ * 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 to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.oak.query;
+
+import org.apache.jackrabbit.oak.api.Result;
+import org.apache.jackrabbit.oak.api.ResultRow;
+import org.apache.jackrabbit.oak.api.Type;
+import org.apache.jackrabbit.oak.core.ImmutableRoot;
+import org.apache.jackrabbit.oak.namepath.NamePathMapper;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.junit.Test;
+
+import java.text.ParseException;
+import java.util.Iterator;
+
+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.assertEquals;
+import static org.junit.Assert.assertFalse;
+
+public class NativeQueryTest {
+
+    private final NodeState types = INITIAL_CONTENT.getChildNode(JCR_SYSTEM)
+            .getChildNode(JCR_NODE_TYPES);
+
+    private final ImmutableRoot ROOT = new ImmutableRoot(INITIAL_CONTENT);
+    private final QueryEngineImpl QUERY_ENGINE = 
(QueryEngineImpl)ROOT.getQueryEngine();
+
+    private final SQL2Parser p = new SQL2Parser(NamePathMapper.DEFAULT, types, 
new QueryEngineSettings());
+
+    @Test
+    public void dontTraverseForSuggest() throws Exception {
+        String sql = "select [rep:suggest()] from [nt:base] where 
suggest('test')";
+        assertDontTraverseFor(sql);
+    }
+
+    @Test
+    public void dontTraverseForSpellcheck() throws Exception {
+        String sql = "select [rep:spellcheck()] from [nt:base] where 
spellcheck('test')";
+        assertDontTraverseFor(sql);
+    }
+
+    @Test
+    public void dontTraverseForNative() throws Exception {
+        String sql = "select [jcr:path] from [nt:base] where native('solr', 
'name:(Hello OR World)')";
+        assertDontTraverseFor(sql);
+    }
+
+    @Test
+    public void dontTraverseForSimilar() throws Exception {
+        String sql = "select [rep:similar()] from [nt:base] where similar(., 
'/test/a')";
+        assertDontTraverseFor(sql);
+    }
+
+    private void assertDontTraverseFor(String sql) throws ParseException {
+        QueryImpl query = (QueryImpl)p.parse(sql);
+        query.setExecutionContext(QUERY_ENGINE.getExecutionContext());
+        Result result = query.executeQuery();
+        Iterator<? extends ResultRow> it = result.getRows().iterator();
+        assertFalse("Zero results expected", it.hasNext());
+
+        query = (QueryImpl)p.parse("measure " + sql);
+        query.setExecutionContext(QUERY_ENGINE.getExecutionContext());
+        result = query.executeQuery();
+        it = result.getRows().iterator();
+        while(it.hasNext()) {
+            ResultRow row = it.next();
+            String selector = row.getValue("selector").getValue(Type.STRING);
+            if ("nt:base".equals(selector)) {
+                long scanCount = row.getValue("scanCount").getValue(Type.LONG);
+                // we expect that no was scanned that's it
+                // - no traversal of the whole respository
+                assertEquals("Repository's scan count doesn't match", 0, 
scanCount);
+            }
+        }
+    }
+}
\ No newline at end of file

Propchange: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/query/NativeQueryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to