You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by na...@apache.org on 2021/11/19 12:31:24 UTC

[ignite] branch master updated: IGNITE-15943 Fix comparison inlined fields with decimal search key (#9573)

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

namelchev pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/master by this push:
     new 2e7235d  IGNITE-15943 Fix comparison inlined fields with decimal search key (#9573)
2e7235d is described below

commit 2e7235d39b0940ee384a609c2621498bd2e57e27
Author: Maksim Timonin <ti...@gmail.com>
AuthorDate: Fri Nov 19 15:31:08 2021 +0300

    IGNITE-15943 Fix comparison inlined fields with decimal search key (#9573)
---
 .../processors/query/h2/index/H2RowComparator.java |   8 +-
 .../IgniteSqlQueryDecimalArgumentsWithTest.java    | 125 +++++++++++++++++++++
 .../IgniteBinaryCacheQueryTestSuite.java           |   2 +
 3 files changed, 132 insertions(+), 3 deletions(-)

diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/index/H2RowComparator.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/index/H2RowComparator.java
index d9722fd..c8dc974 100644
--- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/index/H2RowComparator.java
+++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/index/H2RowComparator.java
@@ -26,6 +26,7 @@ import org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndexKey
 import org.apache.ignite.internal.cache.query.index.sorted.inline.types.NullableInlineIndexKeyType;
 import org.apache.ignite.internal.cache.query.index.sorted.keys.IndexKey;
 import org.apache.ignite.internal.cache.query.index.sorted.keys.IndexKeyFactory;
+import org.apache.ignite.internal.cache.query.index.sorted.keys.NullIndexKey;
 import org.apache.ignite.internal.processors.cache.CacheObjectContext;
 import org.apache.ignite.internal.processors.query.h2.H2Utils;
 import org.apache.ignite.internal.processors.query.h2.opt.GridH2Table;
@@ -68,7 +69,7 @@ public class H2RowComparator extends IndexRowCompartorImpl {
         if (cmp != COMPARE_UNSUPPORTED)
             return cmp;
 
-        int objType = InlineIndexKeyTypeRegistry.get(key, curType, keyTypeSettings).type();
+        int objType = key == NullIndexKey.INSTANCE ? curType : key.type();
 
         int highOrder = Value.getHigherOrder(curType, objType);
 
@@ -82,8 +83,9 @@ public class H2RowComparator extends IndexRowCompartorImpl {
 
             InlineIndexKeyType highType = InlineIndexKeyTypeRegistry.get(objHighOrder, highOrder, keyTypeSettings);
 
-            // The only way to invoke inline comparation again.
-            return ((NullableInlineIndexKeyType) highType).compare0(pageAddr, off, objHighOrder);
+            // The only way to invoke inline comparison again.
+            if (highType != null)
+                return ((NullableInlineIndexKeyType) highType).compare0(pageAddr, off, objHighOrder);
         }
 
         return COMPARE_UNSUPPORTED;
diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlQueryDecimalArgumentsWithTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlQueryDecimalArgumentsWithTest.java
new file mode 100644
index 0000000..d96f7a7
--- /dev/null
+++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlQueryDecimalArgumentsWithTest.java
@@ -0,0 +1,125 @@
+/*
+ * 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.internal.processors.query;
+
+import java.math.BigDecimal;
+import java.util.List;
+import java.util.regex.Pattern;
+import org.apache.ignite.cache.query.SqlFieldsQuery;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.cache.query.index.IndexName;
+import org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndex;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+/** Checks whether correctly compare non-inlined decimal search key with inlined types. */
+@RunWith(Parameterized.class)
+public class IgniteSqlQueryDecimalArgumentsWithTest extends GridCommonAbstractTest {
+    /** */
+    private static final String IDX_NAME = "FLD_IDX";
+
+    /** */
+    private static final String TABLE_NAME = "FLD_TABLE";
+
+    /** */
+    private static final String TABLE_CACHE_NAME = "FLD_TABLE_CACHE";
+
+    /** */
+    private IgniteEx node;
+
+    /** */
+    @Parameterized.Parameter()
+    public String idxFldName;
+
+    /** */
+    @Parameterized.Parameter(1)
+    public String idxFldType;
+
+    /** */
+    @Parameterized.Parameters(name = "fld={0} type={1}")
+    public static List<Object[]> params() {
+        return F.asList(
+            new Object[] {"intFld", "int"},
+            new Object[] {"dblFld", "double"}
+        );
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTest() throws Exception {
+        node = startGrid();
+
+        query("create table " + TABLE_NAME + "(id varchar primary key, " + idxFldName + " " + idxFldType + ")" +
+            " WITH \"CACHE_NAME=" + TABLE_CACHE_NAME + "\";");
+
+        query("create index " + IDX_NAME + " on " + TABLE_NAME + "(" + idxFldName + ");");
+
+        query("insert into " + TABLE_NAME + "(id, " + idxFldName + ") values('a1', 1);");
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        stopAllGrids();
+    }
+
+    /** Math calculations produces decimal values for searching index tree. */
+    @Test
+    public void testDecimalCalculatedField() {
+        check("select * from " + TABLE_NAME + " where " + idxFldName + " < 100 * 0.5;");
+    }
+
+    /** */
+    @Test
+    public void testExplicitDecimalArgument() {
+        check("select * from " + TABLE_NAME + " where " + idxFldName + " < ?;", new BigDecimal("50.0"));
+    }
+
+    /** */
+    private void check(String sql, Object... arg) {
+        assertTrue(idxInlineSize() > 0);
+
+        String explain = "explain " + sql;
+
+        String res = query(explain, arg).get(0).get(0).toString();
+
+        // Assert if index is not used for searching.
+        assertTrue(Pattern.compile(IDX_NAME + "[^\\s]+: " + idxFldName.toUpperCase() + " <").matcher(res).find());
+
+        List<List<?>> result = query(sql, arg);
+
+        assertEquals(1, result.size());
+    }
+
+    /** */
+    private List<List<?>> query(String qry, Object... args) {
+        return node.context().query().querySqlFields(new SqlFieldsQuery(qry).setArgs(args), false).getAll();
+    }
+
+    /** */
+    private int idxInlineSize() {
+        GridCacheContext<?, ?> cctx = node.cachex(TABLE_CACHE_NAME).context();
+
+        InlineIndex idx = (InlineIndex)node.context().indexProcessor().index(
+            new IndexName(cctx.name(), "PUBLIC", TABLE_NAME, IDX_NAME));
+
+        return idx.inlineSize();
+    }
+}
diff --git a/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteBinaryCacheQueryTestSuite.java b/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteBinaryCacheQueryTestSuite.java
index 2518d8a..c1a6f79 100644
--- a/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteBinaryCacheQueryTestSuite.java
+++ b/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteBinaryCacheQueryTestSuite.java
@@ -216,6 +216,7 @@ import org.apache.ignite.internal.processors.query.IgniteSqlGroupConcatNotColloc
 import org.apache.ignite.internal.processors.query.IgniteSqlKeyValueFieldsTest;
 import org.apache.ignite.internal.processors.query.IgniteSqlNotNullConstraintTest;
 import org.apache.ignite.internal.processors.query.IgniteSqlParameterizedQueryTest;
+import org.apache.ignite.internal.processors.query.IgniteSqlQueryDecimalArgumentsWithTest;
 import org.apache.ignite.internal.processors.query.IgniteSqlQueryParallelismTest;
 import org.apache.ignite.internal.processors.query.IgniteSqlRoutingTest;
 import org.apache.ignite.internal.processors.query.IgniteSqlSchemaIndexingTest;
@@ -496,6 +497,7 @@ import org.junit.runners.Suite;
     GridCacheQueryIndexingDisabledSelfTest.class,
     GridOrderedMessageCancelSelfTest.class,
     CacheQueryEvictDataLostTest.class,
+    IgniteSqlQueryDecimalArgumentsWithTest.class,
 
     // Full text queries.
     GridCacheFullTextQueryFailoverTest.class,