You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by zs...@apache.org on 2021/10/07 10:05:11 UTC

[ignite] branch master updated: IGNITE-15547 Accept Classes/Enums extending an Interface which is used as cache indexed field - Fixes #9427.

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

zstan 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 c745041  IGNITE-15547 Accept Classes/Enums extending an Interface which is used as cache indexed field - Fixes #9427.
c745041 is described below

commit c7450412ab12b7b972529a70e7910b538ec3f14c
Author: Andrian Boscanean <An...@arhs-developments.com>
AuthorDate: Thu Oct 7 12:45:00 2021 +0300

    IGNITE-15547 Accept Classes/Enums extending an Interface which is used as cache indexed field - Fixes #9427.
    
    Signed-off-by: zstan <st...@gmail.com>
---
 .../processors/query/QueryTypeDescriptorImpl.java  |  17 ++
 .../EnumClassImplementingIndexedInterfaceTest.java | 242 +++++++++++++++++++++
 .../IgniteCacheWithIndexingTestSuite.java          |   5 +-
 3 files changed, 263 insertions(+), 1 deletion(-)

diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryTypeDescriptorImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryTypeDescriptorImpl.java
index 4e686f3..d209f62 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryTypeDescriptorImpl.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryTypeDescriptorImpl.java
@@ -28,6 +28,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteLogger;
 import org.apache.ignite.binary.BinaryObject;
 import org.apache.ignite.cache.QueryIndexType;
 import org.apache.ignite.internal.processors.cache.CacheObject;
@@ -135,6 +136,9 @@ public class QueryTypeDescriptorImpl implements GridQueryTypeDescriptor {
     /** Primary key fields. */
     private Set<String> pkFields;
 
+    /** Logger. */
+    private final IgniteLogger log;
+
     /**
      * Constructor.
      *
@@ -144,6 +148,7 @@ public class QueryTypeDescriptorImpl implements GridQueryTypeDescriptor {
     public QueryTypeDescriptorImpl(String cacheName, CacheObjectContext coCtx) {
         this.cacheName = cacheName;
         this.coCtx = coCtx;
+        this.log = coCtx.kernalContext().log(getClass());
     }
 
     /**
@@ -721,6 +726,18 @@ public class QueryTypeDescriptorImpl implements GridQueryTypeDescriptor {
                 }
                 else if (coCtx.kernalContext().cacheObjects().typeId(propType.getName()) !=
                     ((BinaryObject)propVal).type().typeId()) {
+                    // Check for classes/enums implementing indexed interfaces.
+                    String clsName = ((BinaryObject) propVal).type().typeName();
+                    try {
+                        final Class<?> cls = Class.forName(clsName);
+
+                        if (propType.isAssignableFrom(cls))
+                            continue;
+                    } catch (ClassNotFoundException e) {
+                        if (log.isDebugEnabled())
+                            U.error(log, "Failed to find child class: " + clsName, e);
+                    }
+
                     throw new IgniteSQLException("Type for a column '" + idxField +
                         "' is not compatible with index definition. Expected '" +
                         propType.getSimpleName() + "', actual type '" +
diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/EnumClassImplementingIndexedInterfaceTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/EnumClassImplementingIndexedInterfaceTest.java
new file mode 100644
index 0000000..b5df0d9
--- /dev/null
+++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/EnumClassImplementingIndexedInterfaceTest.java
@@ -0,0 +1,242 @@
+/*
+ * 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.cache;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Objects;
+
+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 EnumClassImplementingIndexedInterfaceTest extends GridCommonAbstractTest {
+    /** */
+    private static final String PERSON_CACHE = "Person";
+
+    /** */
+    private static IgniteEx ignite;
+
+    /** */
+    private static final int KEY = 0;
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTestsStarted() throws Exception {
+        super.beforeTestsStarted();
+
+        ignite = startGrids(2);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        ignite.destroyCache(PERSON_CACHE);
+    }
+
+    /** */
+    @Test
+    public void testInsertTableVarColumns() {
+        checkCachePutInsert(startSqlPersonCache());
+    }
+
+    /** */
+    @Test
+    public void testInsertValueVarColumns() {
+        checkCachePutInsert(startPersonCache());
+    }
+
+    /** */
+    private void checkCachePutInsert(IgniteCache<Integer, Person> cache) {
+        Arrays.stream(RoleEnum.values()).forEach(role -> {
+            Title title = role.ordinal() % 2 == 0 ? new TitleClass1() : new TitleClass2();
+            Person person = new Person(role, title, role.toString());
+
+            cache.put(KEY, person);
+            assertEquals(person, cache.get(KEY));
+
+            cache.clear();
+
+            cache.query(sqlInsertQuery(role, title, role.toString()));
+            assertEquals(person, cache.get(KEY));
+
+            cache.clear();
+        });
+    }
+
+    /** */
+    private IgniteCache<Integer, Person> startPersonCache() {
+        return ignite.createCache(new CacheConfiguration<Integer, Person>()
+            .setName(PERSON_CACHE)
+            .setQueryEntities(Collections.singletonList(personQueryEntity())));
+    }
+
+    /** */
+    private IgniteCache<Integer, Person> startSqlPersonCache() {
+        ignite.context().query().querySqlFields(new SqlFieldsQuery(
+            "create table " + PERSON_CACHE + "(" +
+            "   id int PRIMARY KEY," +
+            "   role Object," +
+            "   title Object," +
+            "   desc varchar(5)" +
+            ") with \"CACHE_NAME=" + PERSON_CACHE + ",VALUE_TYPE=" + Person.class.getName() + "\""), false);
+
+        return ignite.cache(PERSON_CACHE);
+    }
+
+    /** */
+    private SqlFieldsQuery sqlInsertQuery(Role role, Title title, String description) {
+        return new SqlFieldsQuery("insert into " + PERSON_CACHE + "(id, role, title, desc) values (?, ?, ?, ?)")
+            .setArgs(KEY, role, title, description);
+    }
+
+    /** */
+    private QueryEntity personQueryEntity() {
+        QueryEntity entity = new QueryEntity(Integer.class, Person.class);
+        entity.setKeyFieldName("id");
+        entity.addQueryField("id", Integer.class.getName(), "ID");
+
+        return entity;
+    }
+
+    /** */
+    static interface Role {
+    }
+
+    /** */
+    enum RoleEnum implements Role {
+        /** */
+        ROLE1,
+
+        /** */
+        ROLE2,
+
+        /** */
+        ROLE3
+    }
+
+    /** */
+    static interface Title {
+        /** */
+        public String getTitle();
+    }
+
+    /** */
+    static class TitleClass1 implements Title {
+        /** */
+        private final String title1 = "title1";
+
+        /** */
+        @Override public String getTitle() {
+            return title1;
+        }
+
+        /** {@inheritDoc} */
+        @Override public boolean equals(Object o) {
+            if (this == o)
+                return true;
+
+            if (o == null || getClass() != o.getClass())
+                return false;
+
+            TitleClass1 titleCls = (TitleClass1)o;
+
+            return Objects.equals(title1, titleCls.title1);
+        }
+
+        /** */
+        @Override public int hashCode() {
+            return 0;
+        }
+    }
+
+    /** */
+    static class TitleClass2 implements Title {
+        /** */
+        private final String title2 = "title2";
+
+        /** */
+        @Override public String getTitle() {
+            return title2;
+        }
+
+        /** {@inheritDoc} */
+        @Override public boolean equals(Object o) {
+            if (this == o)
+                return true;
+
+            if (o == null || getClass() != o.getClass())
+                return false;
+
+            TitleClass2 titleCls = (TitleClass2)o;
+
+            return Objects.equals(title2, titleCls.title2);
+        }
+
+        /** */
+        @Override public int hashCode() { return 1; }
+    }
+
+    /** */
+    static class Person {
+        /** */
+        @QuerySqlField(index = true)
+        private final Role role;
+
+        /** */
+        @QuerySqlField(index = true)
+        private final Title title;
+
+        /** */
+        @QuerySqlField
+        private final String desc;
+
+        /** */
+        Person(Role role, Title title, String desc) {
+            this.role = role;
+            this.title = title;
+            this.desc = desc;
+        }
+
+        /** {@inheritDoc} */
+        @Override public boolean equals(Object o) {
+            if (this == o)
+                return true;
+
+            if (o == null || getClass() != o.getClass())
+                return false;
+
+            Person person = (Person)o;
+
+            return Objects.equals(role, person.role) && Objects.equals(title, person.title) && Objects.equals(desc,
+                    person.desc);
+        }
+
+        /** {@inheritDoc} */
+        @Override public int hashCode() {
+            int result = Objects.hash(role);
+            result = 31 * result + Objects.hash(title);
+            result = 31 * result + Objects.hash(desc);
+            return result;
+        }
+    }
+}
diff --git a/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheWithIndexingTestSuite.java b/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheWithIndexingTestSuite.java
index c7afcfc..3d6f85d 100644
--- a/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheWithIndexingTestSuite.java
+++ b/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheWithIndexingTestSuite.java
@@ -33,6 +33,7 @@ import org.apache.ignite.internal.processors.cache.CacheRegisterMetadataLocallyT
 import org.apache.ignite.internal.processors.cache.ClientReconnectAfterClusterRestartTest;
 import org.apache.ignite.internal.processors.cache.ClusterReadOnlyModeDoesNotBreakSqlSelectTest;
 import org.apache.ignite.internal.processors.cache.ClusterReadOnlyModeSqlTest;
+import org.apache.ignite.internal.processors.cache.EnumClassImplementingIndexedInterfaceTest;
 import org.apache.ignite.internal.processors.cache.FieldsPrecisionTest;
 import org.apache.ignite.internal.processors.cache.GridCacheOffHeapSelfTest;
 import org.apache.ignite.internal.processors.cache.GridCacheOffheapIndexEntryEvictTest;
@@ -120,7 +121,9 @@ import org.junit.runners.Suite;
 
     IndexPagesMetricsInMemoryTest.class,
 
-    FieldsPrecisionTest.class
+    FieldsPrecisionTest.class,
+
+    EnumClassImplementingIndexedInterfaceTest.class
 })
 public class IgniteCacheWithIndexingTestSuite {
 }