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 mk...@apache.org on 2022/12/21 07:19:12 UTC

[jackrabbit-oak] branch trunk updated: OAK-10033:Conditions on dates use the wrong range (#795)

This is an automated email from the ASF dual-hosted git repository.

mkataria pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 9d91389b6e OAK-10033:Conditions on dates use the wrong range (#795)
9d91389b6e is described below

commit 9d91389b6eaf1a0bb790204643b2ce03531fe624
Author: Mohit Kataria <mk...@apache.org>
AuthorDate: Wed Dec 21 12:49:07 2022 +0530

    OAK-10033:Conditions on dates use the wrong range (#795)
    
    * OAK-10033:Conditions on dates use the wrong range
---
 .../plugins/index/lucene/LucenePropertyIndex.java  |  4 +-
 .../oak/plugins/index/PropertyIndexCommonTest.java | 52 ++++++++++++++++++++++
 2 files changed, 54 insertions(+), 2 deletions(-)

diff --git a/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java b/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java
index 0a471253ad..4f03cb22fd 100644
--- a/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java
+++ b/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LucenePropertyIndex.java
@@ -1223,8 +1223,8 @@ public class LucenePropertyIndex extends FulltextIndex {
                     }
                     return in;
                 } else if (pr.isNotNullRestriction()) {
-                    // not null. For date lower bound of zero can be used
-                    return NumericRangeQuery.newLongRange(propertyName, 0L, Long.MAX_VALUE, true, true);
+                    // not null. As we are indexing generic dates which can be beyond epoch. So using complete numeric range
+                    return NumericRangeQuery.newLongRange(propertyName, Long.MIN_VALUE, Long.MAX_VALUE, true, true);
                 } else if (pr.isNot && pr.not != null) {
                     // -[property]=[value]
                     BooleanQuery bool = new BooleanQuery();
diff --git a/oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/PropertyIndexCommonTest.java b/oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/PropertyIndexCommonTest.java
index dc34078d9f..0c02f3e97b 100644
--- a/oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/PropertyIndexCommonTest.java
+++ b/oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/PropertyIndexCommonTest.java
@@ -25,14 +25,21 @@ import org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants;
 import org.apache.jackrabbit.oak.plugins.index.search.util.IndexDefinitionBuilder;
 import org.apache.jackrabbit.oak.plugins.nodetype.write.NodeTypeRegistry;
 import org.apache.jackrabbit.oak.query.AbstractQueryTest;
+import org.apache.jackrabbit.util.ISO8601;
 import org.junit.Test;
 
+import javax.jcr.PropertyType;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
 import java.util.Arrays;
+import java.util.Calendar;
 
+import static java.util.Arrays.asList;
 import static java.util.Collections.singletonList;
 import static javax.jcr.PropertyType.TYPENAME_DATE;
 import static org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants.PROPDEF_PROP_NODE_NAME;
 import static org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants.PROP_NAME;
+import static org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants.PROP_NODE;
 import static org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants.PROP_NOT_NULL_CHECK_ENABLED;
 import static org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants.PROP_NULL_CHECK_ENABLED;
 import static org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants.PROP_PROPERTY_INDEX;
@@ -358,6 +365,40 @@ public abstract class PropertyIndexCommonTest extends AbstractQueryTest {
                 ImmutableList.of("/test/i"));
     }
 
+    @Test
+    public void rangeQueriesWithBeforeEpoch() throws Exception {
+        Tree idx = indexOptions.setIndex(
+                root,
+                "test1",
+                indexOptions.createIndex(indexOptions.createIndexDefinitionBuilder(), false, "propa")
+        );
+        Tree propIdx = idx.getChild("indexRules").getChild("nt:base").getChild(PROP_NODE).getChild("propa");
+        propIdx.setProperty(FulltextIndexConstants.PROP_TYPE, PropertyType.TYPENAME_DATE);
+        root.commit();
+
+        Tree test = root.getTree("/").addChild("test");
+        test.addChild("a").setProperty("propa", createCal("14/02/1768"));
+        test.addChild("b").setProperty("propa", createCal("14/03/1769"));
+        test.addChild("c").setProperty("propa", createCal("14/04/1770"));
+        root.commit();
+
+        assertEventually(() ->
+                {
+                    try {
+                        assertQuery("select [jcr:path] from [nt:base] where [propa] >= " + dt("15/02/1768"), asList("/test/b", "/test/c"));
+                    } catch (ParseException e) {
+                        throw new RuntimeException(e);
+                    }
+                }
+        );
+
+        assertQuery("select [jcr:path] from [nt:base] where [propa] <=" + dt("15/03/1769"), asList("/test/b", "/test/a"));
+        assertQuery("select [jcr:path] from [nt:base] where [propa] < " + dt("14/03/1769"), asList("/test/a"));
+        assertQuery("select [jcr:path] from [nt:base] where [propa] <> " + dt("14/03/1769"), asList("/test/a", "/test/c"));
+        assertQuery("select [jcr:path] from [nt:base] where [propa] > " + dt("15/02/1768") + " and [propa] < " + dt("13/04/1770"), asList("/test/b"));
+        assertQuery("select [jcr:path] from [nt:base] where propa is not null", asList("/test/a", "/test/b", "/test/c"));
+    }
+
     protected String explain(String query) {
         String explain = "explain " + query;
         return executeQuery(explain, "JCR-SQL2").get(0);
@@ -368,4 +409,15 @@ public abstract class PropertyIndexCommonTest extends AbstractQueryTest {
         t.setProperty(JcrConstants.JCR_PRIMARYTYPE, typeName, Type.NAME);
         return t;
     }
+
+    private static Calendar createCal(String dt) throws java.text.ParseException {
+        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
+        Calendar cal = Calendar.getInstance();
+        cal.setTime(sdf.parse(dt));
+        return cal;
+    }
+
+    private static String dt(String date) throws ParseException {
+        return String.format("CAST ('%s' AS DATE)", ISO8601.format(createCal(date)));
+    }
 }