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/16 09:13:02 UTC

[ignite] branch master 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 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 4131935  IGNITE-12068 Fix wrong single row result when a part of complex PK is looked up - Fixes #6778.
4131935 is described below

commit 41319353a1eb2747ff0aa32968fb7af9a07fa913
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>
---
 .../processors/query/h2/database/H2TreeIndex.java  |  28 ++++-
 .../query/SqlPartOfComplexPkLookupTest.java        | 116 +++++++++++++++++++++
 .../IgniteBinaryCacheQueryTestSuite2.java          |   5 +-
 3 files changed, 143 insertions(+), 6 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 03ada75..7711b42 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
@@ -33,6 +33,8 @@ import org.apache.ignite.internal.GridKernalContext;
 import org.apache.ignite.internal.GridTopic;
 import org.apache.ignite.internal.managers.communication.GridIoPolicy;
 import org.apache.ignite.internal.managers.communication.GridMessageListener;
+import org.apache.ignite.internal.metric.IoStatisticsHolder;
+import org.apache.ignite.internal.metric.IoStatisticsHolderIndex;
 import org.apache.ignite.internal.processors.cache.GridCacheContext;
 import org.apache.ignite.internal.processors.cache.mvcc.MvccSnapshot;
 import org.apache.ignite.internal.processors.cache.persistence.IgniteCacheDatabaseSharedManager;
@@ -62,8 +64,6 @@ import org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2RowRange
 import org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2RowRangeBounds;
 import org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2ValueMessage;
 import org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2ValueMessageFactory;
-import org.apache.ignite.internal.metric.IoStatisticsHolder;
-import org.apache.ignite.internal.metric.IoStatisticsHolderIndex;
 import org.apache.ignite.internal.util.GridSpinBusyLock;
 import org.apache.ignite.internal.util.IgniteTree;
 import org.apache.ignite.internal.util.lang.GridCursor;
@@ -81,6 +81,7 @@ import org.h2.index.IndexType;
 import org.h2.index.SingleRowCursor;
 import org.h2.message.DbException;
 import org.h2.result.SearchRow;
+import org.h2.table.Column;
 import org.h2.table.IndexColumn;
 import org.h2.table.TableFilter;
 import org.h2.value.Value;
@@ -88,10 +89,10 @@ import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
 import static java.util.Collections.singletonList;
+import static org.apache.ignite.internal.metric.IoStatisticsType.SORTED_INDEX;
 import static org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2IndexRangeResponse.STATUS_ERROR;
 import static org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2IndexRangeResponse.STATUS_NOT_FOUND;
 import static org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2IndexRangeResponse.STATUS_OK;
-import static org.apache.ignite.internal.metric.IoStatisticsType.SORTED_INDEX;
 import static org.h2.result.Row.MEMORY_CALCULATE;
 
 /**
@@ -349,8 +350,8 @@ public class H2TreeIndex extends H2TreeIndexBase {
 
             H2Tree tree = treeForRead(seg);
 
-            if (!cctx.mvccEnabled() && indexType.isPrimaryKey() && lower != null && upper != null &&
-                tree.compareRows((H2Row)lower, (H2Row)upper) == 0) {
+            // If it is known that only one row will be returned an optimization is employed
+            if (isSingleRowLookup(lower, upper, tree)) {
                 H2Row row = tree.findOne((H2Row)lower, filter(qryCtxRegistry.getThreadLocal()), null);
 
                 if (row == null || isExpired(row))
@@ -368,6 +369,23 @@ public class H2TreeIndex extends H2TreeIndexBase {
         }
     }
 
+    /** */
+    private boolean isSingleRowLookup(SearchRow lower, SearchRow upper, H2Tree tree) {
+        return !cctx.mvccEnabled() && indexType.isPrimaryKey() && lower != null && upper != null &&
+            tree.compareRows((H2Row)lower, (H2Row)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 H2CacheRow put(H2CacheRow 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..78a0557
--- /dev/null
+++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlPartOfComplexPkLookupTest.java
@@ -0,0 +1,116 @@
+/*
+ * 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;
+import org.junit.Test;
+
+/** */
+public class SqlPartOfComplexPkLookupTest extends GridCommonAbstractTest {
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        stopAllGrids();
+    }
+
+    /** */
+    @Test
+    public void testPartOfComplexPkLookupDdl() throws Exception {
+        IgniteEx ign = startGrid(0);
+
+        IgniteCache<Object, Object> cache = ign.getOrCreateCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME));
+
+        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);
+    }
+
+    /** */
+    @Test
+    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/IgniteBinaryCacheQueryTestSuite2.java b/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteBinaryCacheQueryTestSuite2.java
index f469820..682fd8f 100644
--- a/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteBinaryCacheQueryTestSuite2.java
+++ b/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteBinaryCacheQueryTestSuite2.java
@@ -56,6 +56,7 @@ import org.apache.ignite.internal.processors.query.IgniteSqlCreateTableTemplateT
 import org.apache.ignite.internal.processors.query.LocalQueryLazyTest;
 import org.apache.ignite.internal.processors.query.LongRunningQueryTest;
 import org.apache.ignite.internal.processors.query.SqlLocalQueryConnectionAndStatementTest;
+import org.apache.ignite.internal.processors.query.SqlPartOfComplexPkLookupTest;
 import org.apache.ignite.internal.processors.query.h2.CacheQueryEntityWithDateTimeApiFieldsTest;
 import org.apache.ignite.internal.processors.query.h2.DmlStatementsProcessorTest;
 import org.apache.ignite.internal.processors.query.h2.twostep.CacheQueryMemoryLeakTest;
@@ -145,7 +146,9 @@ import org.junit.runners.Suite;
     DmlBatchSizeDeadlockTest.class,
 
     GridCachePartitionedTxMultiNodeSelfTest.class,
-    GridCacheReplicatedTxMultiNodeBasicTest.class
+    GridCacheReplicatedTxMultiNodeBasicTest.class,
+
+    SqlPartOfComplexPkLookupTest.class
 })
 public class IgniteBinaryCacheQueryTestSuite2 {
 }