You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by ip...@apache.org on 2019/08/17 04:31:19 UTC

[ignite] branch ignite-2.7.6 updated: IGNITE-12068 Fix wrong single row result when a part of complex PK is looked up - Fixes #6778.

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

ipavlukhin pushed a commit to branch ignite-2.7.6
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/ignite-2.7.6 by this push:
     new 45d719e  IGNITE-12068 Fix wrong single row result when a part of complex PK is looked up - Fixes #6778.
45d719e is described below

commit 45d719e66c95df8a115af467792f8b89dace83e3
Author: ipavlukhin <vo...@gmail.com>
AuthorDate: Fri Aug 16 12:10:55 2019 +0300

    IGNITE-12068 Fix wrong single row result when a part of complex PK is looked up - Fixes #6778.
    
    Signed-off-by: ipavlukhin <vo...@gmail.com>
    (cherry picked from commit 41319353a1eb2747ff0aa32968fb7af9a07fa913)
---
 .../processors/query/h2/database/H2TreeIndex.java  |  21 +++-
 .../query/SqlPartOfComplexPkLookupTest.java        | 114 +++++++++++++++++++++
 .../testsuites/IgniteCacheQuerySelfTestSuite2.java |   3 +
 3 files changed, 136 insertions(+), 2 deletions(-)

diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/database/H2TreeIndex.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/database/H2TreeIndex.java
index e9cca9e..bd8713d 100644
--- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/database/H2TreeIndex.java
+++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/database/H2TreeIndex.java
@@ -248,8 +248,8 @@ public class H2TreeIndex extends GridH2IndexBase {
 
             H2Tree tree = treeForRead(seg);
 
-            if (!cctx.mvccEnabled() && indexType.isPrimaryKey() && lower != null && upper != null &&
-                tree.compareRows((GridH2SearchRow)lower, (GridH2SearchRow)upper) == 0) {
+            // If it is known that only one row will be returned an optimization is employed
+            if (isSingleRowLookup(lower, upper, tree)) {
                 GridH2Row row = tree.findOne((GridH2SearchRow)lower, filter(GridH2QueryContext.get()), null);
 
                 return (row == null) ? GridH2Cursor.EMPTY : new SingleRowCursor(row);
@@ -264,6 +264,23 @@ public class H2TreeIndex extends GridH2IndexBase {
         }
     }
 
+    /** */
+    private boolean isSingleRowLookup(SearchRow lower, SearchRow upper, H2Tree tree) {
+        return !cctx.mvccEnabled() && indexType.isPrimaryKey() && lower != null && upper != null &&
+            tree.compareRows((GridH2SearchRow)lower, (GridH2SearchRow)upper) == 0 && hasAllIndexColumns(lower);
+    }
+
+    /** */
+    private boolean hasAllIndexColumns(SearchRow searchRow) {
+        for (Column c : columns) {
+            // Java null means that column is not specified in a search row, for SQL NULL a special constant is used
+            if (searchRow.getValue(c.getColumnId()) == null)
+                return false;
+        }
+
+        return true;
+    }
+
     /** {@inheritDoc} */
     @Override public GridH2Row put(GridH2Row row) {
         try {
diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlPartOfComplexPkLookupTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlPartOfComplexPkLookupTest.java
new file mode 100644
index 0000000..ed4550a
--- /dev/null
+++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlPartOfComplexPkLookupTest.java
@@ -0,0 +1,114 @@
+/*
+ * 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 com.google.common.collect.ImmutableSet;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.cache.QueryEntity;
+import org.apache.ignite.cache.query.SqlFieldsQuery;
+import org.apache.ignite.cache.query.annotations.QuerySqlField;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+
+/** */
+public class SqlPartOfComplexPkLookupTest extends GridCommonAbstractTest {
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        stopAllGrids();
+    }
+
+    /** */
+    public void testPartOfComplexPkLookupDdl() throws Exception {
+        IgniteEx ign = startGrid(0);
+
+        IgniteCache<Object, Object> cache = ign.getOrCreateCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME)
+            .setSqlSchema("PUBLIC"));
+
+        cache.query(new SqlFieldsQuery("" +
+            "CREATE TABLE Person(\n" +
+            "  id int,\n" +
+            "  city_id int,\n" +
+            "  name varchar,\n" +
+            "  PRIMARY KEY (id, city_id)\n" +
+            ");"));
+
+        cache.query(new SqlFieldsQuery("INSERT INTO Person (id, city_id, name) VALUES (1, 3, 'John Doe');"));
+        cache.query(new SqlFieldsQuery("INSERT INTO Person (id, city_id, name) VALUES (1, 4, 'John Dean');"));
+
+        checkPartialPkLookup(cache);
+    }
+
+    /** */
+    public void testPartOfComplexPkLookupQueryEntity() throws Exception {
+        IgniteEx ign = startGrid(0);
+
+        IgniteCache<Object, Object> cache = ign.getOrCreateCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME)
+            .setQueryEntities(Collections.singleton(new QueryEntity(TestPk.class, String.class)
+                .setTableName("Person")
+                .addQueryField("name", String.class.getName(), "name")
+                .setValueFieldName("name"))));
+
+        cache.put(new TestPk(1, 3), "John Doe");
+        cache.put(new TestPk(1, 4), "John Dean");
+
+        checkPartialPkLookup(cache);
+    }
+
+    /** */
+    private void checkPartialPkLookup(IgniteCache<Object, Object> cache) {
+        assertTrue(cache.query(new SqlFieldsQuery("SELECT name FROM Person WHERE id = 1 and city_id is null")).getAll()
+            .isEmpty());
+
+        assertTrue(cache.query(new SqlFieldsQuery("SELECT name FROM Person WHERE id = 1 and city_id = null")).getAll()
+            .isEmpty());
+
+        List<List<?>> rows = cache.query(new SqlFieldsQuery("SELECT name FROM Person WHERE id = 1")).getAll();
+
+        assertEquals(2, rows.size());
+        assertEquals(
+            ImmutableSet.of("John Doe", "John Dean"),
+            rows.stream().map(row -> (String)row.get(0)).collect(Collectors.toSet()));
+
+        List<List<?>> rows2 = cache.query(new SqlFieldsQuery("SELECT name FROM Person WHERE id = 1 AND city_id = 3"))
+            .getAll();
+
+        assertEquals(1, rows2.size());
+        assertEquals("John Doe", rows2.get(0).get(0));
+    }
+
+    /** */
+    public static class TestPk {
+        /** */
+        @QuerySqlField(name = "id")
+        private final int id;
+
+        /** */
+        @QuerySqlField(name = "city_id")
+        private final int cityId;
+
+        /** */
+        public TestPk(int id, int cityId) {
+            this.id = id;
+            this.cityId = cityId;
+        }
+    }
+}
diff --git a/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheQuerySelfTestSuite2.java b/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheQuerySelfTestSuite2.java
index 4b91b43..e13f8f2 100644
--- a/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheQuerySelfTestSuite2.java
+++ b/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheQuerySelfTestSuite2.java
@@ -50,6 +50,7 @@ import org.apache.ignite.internal.processors.query.IgniteCacheGroupsCompareQuery
 import org.apache.ignite.internal.processors.query.IgniteCacheGroupsSqlDistributedJoinSelfTest;
 import org.apache.ignite.internal.processors.query.IgniteCacheGroupsSqlSegmentedIndexMultiNodeSelfTest;
 import org.apache.ignite.internal.processors.query.IgniteCacheGroupsSqlSegmentedIndexSelfTest;
+import org.apache.ignite.internal.processors.query.SqlPartOfComplexPkLookupTest;
 import org.apache.ignite.internal.processors.query.h2.twostep.CacheQueryMemoryLeakTest;
 import org.apache.ignite.internal.processors.query.h2.twostep.DisappearedCacheCauseRetryMessageSelfTest;
 import org.apache.ignite.internal.processors.query.h2.twostep.DisappearedCacheWasNotFoundMessageSelfTest;
@@ -122,6 +123,8 @@ public class IgniteCacheQuerySelfTestSuite2 extends TestSuite {
 
         suite.addTestSuite(TableViewSubquerySelfTest.class);
 
+        suite.addTestSuite(SqlPartOfComplexPkLookupTest.class);
+
         return suite;
     }
 }