You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2021/10/04 15:48:50 UTC

[GitHub] [ignite] anton-vinogradov commented on a change in pull request #9429: IGNITE-15535 Allow specify multiple criteria for the same field

anton-vinogradov commented on a change in pull request #9429:
URL: https://github.com/apache/ignite/pull/9429#discussion_r721465853



##########
File path: modules/indexing/src/test/java/org/apache/ignite/cache/query/RepeatedFieldIndexQueryTest.java
##########
@@ -0,0 +1,347 @@
+/*
+ * 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.ignite.cache.query;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+import javax.cache.Cache;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.cache.query.annotations.QuerySqlField;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.cache.query.RangeIndexQueryCriterion;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.T2;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.between;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.eq;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.gt;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.gte;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lt;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lte;
+
+/** */
+@RunWith(Parameterized.class)
+public class RepeatedFieldIndexQueryTest extends GridCommonAbstractTest {
+    /** */
+    private static final String CACHE = "TEST_CACHE";
+
+    /** */
+    private static final String ID_IDX = "ID_IDX";
+
+    /** */
+    private static final String DESC_ID_IDX = "DESC_ID_IDX";
+
+    /** */
+    private static final int CNT = 10_000;
+
+    /** */
+    private static IgniteCache<Integer, Person> cache;
+
+    /** */
+    @Parameterized.Parameter
+    public String idxName;
+
+    /** */
+    @Parameterized.Parameter(1)
+    public String fldName;
+
+    /** */
+    @Parameterized.Parameters(name = "idx={0} fldName={1}")
+    public static List<Object[]> params() {
+        return F.asList(
+            new Object[] {ID_IDX, "id"},
+            new Object[] {DESC_ID_IDX, "descId"}
+        );
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTestsStarted() throws Exception {
+        Ignite crd = startGrids(2);
+
+        cache = crd.cache(CACHE);
+
+        for (int i = 0; i < CNT; i++)
+            cache.put(i, new Person(i));
+    }
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+        CacheConfiguration<?, ?> ccfg1 = new CacheConfiguration<>()
+            .setName(CACHE)
+            .setIndexedTypes(Integer.class, Person.class);
+
+        cfg.setCacheConfiguration(ccfg1);
+
+        return cfg;
+    }
+
+    /** */
+    @Test
+    public void testRangeQueriesWithTwoCriteriaSingleField() {
+        int lower = new Random().nextInt(CNT / 2);
+        int upper = CNT / 2 + new Random().nextInt(CNT / 2 - 1);
+
+        List<T2<RangeIndexQueryCriterion, RangeIndexQueryCriterion>> cc = new ArrayList<>();
+
+        Stream.of(new T2<>(lower, upper), new T2<>(upper, lower)).forEach(pair -> {
+            for (int i = 0; i < 6; i++) {
+                for (int j = 0; j < 6; j++) {

Review comment:
       should this be some const?

##########
File path: modules/indexing/src/test/java/org/apache/ignite/cache/query/RepeatedFieldIndexQueryTest.java
##########
@@ -0,0 +1,347 @@
+/*
+ * 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.ignite.cache.query;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+import javax.cache.Cache;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.cache.query.annotations.QuerySqlField;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.cache.query.RangeIndexQueryCriterion;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.T2;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.between;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.eq;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.gt;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.gte;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lt;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lte;
+
+/** */
+@RunWith(Parameterized.class)
+public class RepeatedFieldIndexQueryTest extends GridCommonAbstractTest {
+    /** */
+    private static final String CACHE = "TEST_CACHE";
+
+    /** */
+    private static final String ID_IDX = "ID_IDX";
+
+    /** */
+    private static final String DESC_ID_IDX = "DESC_ID_IDX";
+
+    /** */
+    private static final int CNT = 10_000;
+
+    /** */
+    private static IgniteCache<Integer, Person> cache;
+
+    /** */
+    @Parameterized.Parameter
+    public String idxName;
+
+    /** */
+    @Parameterized.Parameter(1)
+    public String fldName;
+
+    /** */
+    @Parameterized.Parameters(name = "idx={0} fldName={1}")
+    public static List<Object[]> params() {
+        return F.asList(
+            new Object[] {ID_IDX, "id"},
+            new Object[] {DESC_ID_IDX, "descId"}
+        );
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTestsStarted() throws Exception {
+        Ignite crd = startGrids(2);
+
+        cache = crd.cache(CACHE);
+
+        for (int i = 0; i < CNT; i++)
+            cache.put(i, new Person(i));
+    }
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+        CacheConfiguration<?, ?> ccfg1 = new CacheConfiguration<>()
+            .setName(CACHE)
+            .setIndexedTypes(Integer.class, Person.class);
+
+        cfg.setCacheConfiguration(ccfg1);
+
+        return cfg;
+    }
+
+    /** */
+    @Test
+    public void testRangeQueriesWithTwoCriteriaSingleField() {
+        int lower = new Random().nextInt(CNT / 2);
+        int upper = CNT / 2 + new Random().nextInt(CNT / 2 - 1);
+
+        List<T2<RangeIndexQueryCriterion, RangeIndexQueryCriterion>> cc = new ArrayList<>();
+
+        Stream.of(new T2<>(lower, upper), new T2<>(upper, lower)).forEach(pair -> {
+            for (int i = 0; i < 6; i++) {
+                for (int j = 0; j < 6; j++) {
+                    RangeIndexQueryCriterion c1 = (RangeIndexQueryCriterion)criterion(i, fldName, pair.get1(), pair.get2());
+                    RangeIndexQueryCriterion c2 = (RangeIndexQueryCriterion)criterion(j, fldName, pair.get2(), pair.get1());
+
+                    cc.add(new T2<>(c1, c2));
+                }
+            }
+        });
+
+        cc.forEach(c -> checkTwoCriteria(c.get1(), c.get2()));
+    }
+
+    /** */
+    @Test
+    public void testMultipleEqualsCriteria() {
+        int lower = new Random().nextInt(CNT / 2);
+        int upper = CNT / 2 + new Random().nextInt(CNT / 2 - 1);
+
+        IndexQuery<Integer, Person> qry = new IndexQuery<Integer, Person>(Person.class, idxName)
+            .setCriteria(eq(fldName, upper), eq(fldName, lower), between(fldName, 0, CNT));
+
+        assertTrue(cache.query(qry).getAll().isEmpty());
+
+        qry = new IndexQuery<Integer, Person>(Person.class, idxName)
+            .setCriteria(eq(fldName, lower), eq(fldName, upper), between(fldName, 0, CNT));
+
+        assertTrue(cache.query(qry).getAll().isEmpty());
+
+        qry = new IndexQuery<Integer, Person>(Person.class, idxName)
+            .setCriteria(eq(fldName, lower), eq(fldName, upper), between(fldName, CNT, 0));
+
+        assertTrue(cache.query(qry).getAll().isEmpty());
+
+        qry = new IndexQuery<Integer, Person>(Person.class, idxName)
+            .setCriteria(eq(fldName, lower), eq(fldName, upper), between(fldName, CNT, 0));
+
+        assertTrue(cache.query(qry).getAll().isEmpty());

Review comment:
       It looks like we are able to have some method creates a query and checks it's produces an empty set.
   This will make the test smaller and more readable.
   And, possible we'll check more cases %) 

##########
File path: modules/indexing/src/test/java/org/apache/ignite/cache/query/RepeatedFieldIndexQueryTest.java
##########
@@ -0,0 +1,347 @@
+/*
+ * 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.ignite.cache.query;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+import javax.cache.Cache;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.cache.query.annotations.QuerySqlField;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.cache.query.RangeIndexQueryCriterion;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.T2;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.between;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.eq;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.gt;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.gte;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lt;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lte;
+
+/** */
+@RunWith(Parameterized.class)
+public class RepeatedFieldIndexQueryTest extends GridCommonAbstractTest {
+    /** */
+    private static final String CACHE = "TEST_CACHE";
+
+    /** */
+    private static final String ID_IDX = "ID_IDX";
+
+    /** */
+    private static final String DESC_ID_IDX = "DESC_ID_IDX";
+
+    /** */
+    private static final int CNT = 10_000;
+
+    /** */
+    private static IgniteCache<Integer, Person> cache;
+
+    /** */
+    @Parameterized.Parameter
+    public String idxName;
+
+    /** */
+    @Parameterized.Parameter(1)
+    public String fldName;
+
+    /** */
+    @Parameterized.Parameters(name = "idx={0} fldName={1}")
+    public static List<Object[]> params() {
+        return F.asList(
+            new Object[] {ID_IDX, "id"},
+            new Object[] {DESC_ID_IDX, "descId"}
+        );
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTestsStarted() throws Exception {
+        Ignite crd = startGrids(2);
+
+        cache = crd.cache(CACHE);
+
+        for (int i = 0; i < CNT; i++)
+            cache.put(i, new Person(i));
+    }
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+        CacheConfiguration<?, ?> ccfg1 = new CacheConfiguration<>()
+            .setName(CACHE)
+            .setIndexedTypes(Integer.class, Person.class);
+
+        cfg.setCacheConfiguration(ccfg1);
+
+        return cfg;
+    }
+
+    /** */
+    @Test
+    public void testRangeQueriesWithTwoCriteriaSingleField() {
+        int lower = new Random().nextInt(CNT / 2);
+        int upper = CNT / 2 + new Random().nextInt(CNT / 2 - 1);
+
+        List<T2<RangeIndexQueryCriterion, RangeIndexQueryCriterion>> cc = new ArrayList<>();
+
+        Stream.of(new T2<>(lower, upper), new T2<>(upper, lower)).forEach(pair -> {
+            for (int i = 0; i < 6; i++) {
+                for (int j = 0; j < 6; j++) {
+                    RangeIndexQueryCriterion c1 = (RangeIndexQueryCriterion)criterion(i, fldName, pair.get1(), pair.get2());
+                    RangeIndexQueryCriterion c2 = (RangeIndexQueryCriterion)criterion(j, fldName, pair.get2(), pair.get1());
+
+                    cc.add(new T2<>(c1, c2));
+                }
+            }
+        });
+
+        cc.forEach(c -> checkTwoCriteria(c.get1(), c.get2()));
+    }
+
+    /** */
+    @Test
+    public void testMultipleEqualsCriteria() {
+        int lower = new Random().nextInt(CNT / 2);
+        int upper = CNT / 2 + new Random().nextInt(CNT / 2 - 1);
+
+        IndexQuery<Integer, Person> qry = new IndexQuery<Integer, Person>(Person.class, idxName)
+            .setCriteria(eq(fldName, upper), eq(fldName, lower), between(fldName, 0, CNT));
+
+        assertTrue(cache.query(qry).getAll().isEmpty());
+
+        qry = new IndexQuery<Integer, Person>(Person.class, idxName)
+            .setCriteria(eq(fldName, lower), eq(fldName, upper), between(fldName, 0, CNT));
+
+        assertTrue(cache.query(qry).getAll().isEmpty());
+
+        qry = new IndexQuery<Integer, Person>(Person.class, idxName)
+            .setCriteria(eq(fldName, lower), eq(fldName, upper), between(fldName, CNT, 0));
+
+        assertTrue(cache.query(qry).getAll().isEmpty());
+
+        qry = new IndexQuery<Integer, Person>(Person.class, idxName)
+            .setCriteria(eq(fldName, lower), eq(fldName, upper), between(fldName, CNT, 0));
+
+        assertTrue(cache.query(qry).getAll().isEmpty());
+    }
+
+    /** */
+    @Test
+    public void testCommonBoundary() {
+        int upper, lower = upper = new Random().nextInt(CNT / 2);
+
+        IndexQuery<Integer, Person> qry = new IndexQuery<Integer, Person>(Person.class, idxName)
+            .setCriteria(lt(fldName, lower), gt(fldName, upper));
+
+        assertTrue(cache.query(qry).getAll().isEmpty());
+
+        qry = new IndexQuery<Integer, Person>(Person.class, idxName)
+            .setCriteria(lte(fldName, lower), gt(fldName, upper));
+
+        assertTrue(cache.query(qry).getAll().isEmpty());
+
+        qry = new IndexQuery<Integer, Person>(Person.class, idxName)
+            .setCriteria(lt(fldName, lower), gte(fldName, upper));
+
+        assertTrue(cache.query(qry).getAll().isEmpty());
+
+        qry = new IndexQuery<Integer, Person>(Person.class, idxName)
+            .setCriteria(lte(fldName, lower), gte(fldName, upper));
+
+        check(null, cache.query(qry), lower, upper + 1);
+
+        qry = new IndexQuery<Integer, Person>(Person.class, idxName)
+            .setCriteria(between(fldName, 0, lower), between(fldName, upper, CNT));
+
+        check(null, cache.query(qry), lower, upper + 1);

Review comment:
       same here. every 4 lines can be presented by one.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org