You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by am...@apache.org on 2019/10/22 09:28:00 UTC

[ignite] branch master updated: IGNITE-12267: ClassCastException after change column type (drop then add). This closes #6945.

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

amashenkov 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 1550781  IGNITE-12267: ClassCastException after change column type (drop then add). This closes #6945.
1550781 is described below

commit 15507816160fddc5b3e5add31b1185f5bf619e6b
Author: Kirill Tkalenko <tk...@yandex.ru>
AuthorDate: Tue Oct 22 12:26:40 2019 +0300

    IGNITE-12267: ClassCastException after change column type (drop then add).
    This closes #6945.
---
 .../processors/query/GridQueryProcessor.java       |  41 ++++-
 .../query/IgniteSqlKeyValueFieldsTest.java         | 194 ++++++++++++++++++++-
 2 files changed, 227 insertions(+), 8 deletions(-)

diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java
index 3f62f18..5632664 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java
@@ -17,8 +17,6 @@
 
 package org.apache.ignite.internal.processors.query;
 
-import javax.cache.Cache;
-import javax.cache.CacheException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -34,12 +32,15 @@ import java.util.UUID;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.atomic.AtomicBoolean;
+import javax.cache.Cache;
+import javax.cache.CacheException;
 import org.apache.ignite.IgniteCheckedException;
 import org.apache.ignite.IgniteDataStreamer;
 import org.apache.ignite.IgniteException;
 import org.apache.ignite.IgniteSystemProperties;
 import org.apache.ignite.binary.BinaryObject;
 import org.apache.ignite.binary.BinaryObjectException;
+import org.apache.ignite.binary.BinaryType;
 import org.apache.ignite.binary.Binarylizable;
 import org.apache.ignite.cache.CacheAtomicityMode;
 import org.apache.ignite.cache.CacheKeyConfiguration;
@@ -120,10 +121,15 @@ import org.apache.ignite.spi.indexing.IndexingQueryFilter;
 import org.apache.ignite.thread.IgniteThread;
 import org.jetbrains.annotations.Nullable;
 
+import static java.util.Objects.isNull;
+import static java.util.Objects.nonNull;
 import static org.apache.ignite.events.EventType.EVT_CACHE_QUERY_EXECUTED;
 import static org.apache.ignite.internal.GridTopic.TOPIC_SCHEMA;
 import static org.apache.ignite.internal.IgniteComponentType.INDEXING;
+import static org.apache.ignite.internal.binary.BinaryUtils.fieldTypeName;
+import static org.apache.ignite.internal.binary.BinaryUtils.typeByClass;
 import static org.apache.ignite.internal.managers.communication.GridIoPolicy.SCHEMA_POOL;
+import static org.apache.ignite.internal.processors.query.schema.SchemaOperationException.CODE_COLUMN_EXISTS;
 
 /**
  * Indexing processor.
@@ -1148,8 +1154,10 @@ public class GridQueryProcessor extends GridProcessorAdapter {
                             nop = true;
                         }
                         else
-                            err = new SchemaOperationException(SchemaOperationException.CODE_COLUMN_EXISTS, col.name());
+                            err = new SchemaOperationException(CODE_COLUMN_EXISTS, col.name());
                     }
+                    else if (!checkFieldOnBinaryType(type.typeId(), col))
+                        err = new SchemaOperationException(CODE_COLUMN_EXISTS, "with a different type.");
                 }
             }
         }
@@ -1193,6 +1201,31 @@ public class GridQueryProcessor extends GridProcessorAdapter {
     }
 
     /**
+     * It is checked that if a new column already existed in BinaryType,
+     * then its type does not change.
+     *
+     * @param typeId Binary type id.
+     * @param qryField New query field.
+     * @return {@code True} if the field is not added or type does not change.
+     */
+    private boolean checkFieldOnBinaryType(int typeId, QueryField qryField) {
+        assert nonNull(qryField);
+
+        try {
+            BinaryType binaryType = ctx.cacheObjects().metadata(typeId);
+            String binaryFieldType = nonNull(binaryType) ? binaryType.fieldTypeName(qryField.name()) : null;
+
+            return isNull(binaryFieldType) ||
+                binaryFieldType.equals(fieldTypeName(typeByClass(Class.forName(qryField.typeName()))));
+        }
+        catch (ClassNotFoundException e) {
+            throw new IgniteException(
+                "Class not found for property [name=" + qryField.name() + ", type=" + qryField.typeName() + ']'
+            );
+        }
+    }
+
+    /**
      * Prepare operation on non-started cache.
      *
      * @param op Operation.
@@ -1311,7 +1344,7 @@ public class GridQueryProcessor extends GridProcessorAdapter {
                             nop = true;
                         }
                         else
-                            err = new SchemaOperationException(SchemaOperationException.CODE_COLUMN_EXISTS, fld.name());
+                            err = new SchemaOperationException(CODE_COLUMN_EXISTS, fld.name());
                     }
                 }
             }
diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlKeyValueFieldsTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlKeyValueFieldsTest.java
index 0d0093f..869afc9 100644
--- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlKeyValueFieldsTest.java
+++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlKeyValueFieldsTest.java
@@ -17,11 +17,14 @@
 
 package org.apache.ignite.internal.processors.query;
 
+import java.sql.Timestamp;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.LinkedHashMap;
+import java.util.LinkedList;
 import java.util.List;
+import java.util.Queue;
 import java.util.concurrent.Callable;
+import javax.cache.CacheException;
 import org.apache.ignite.IgniteCache;
 import org.apache.ignite.IgniteCheckedException;
 import org.apache.ignite.cache.QueryEntity;
@@ -38,6 +41,11 @@ import org.apache.ignite.internal.util.typedef.F;
 import org.apache.ignite.testframework.GridTestUtils;
 import org.junit.Test;
 
+import static java.util.Arrays.asList;
+import static java.util.Objects.nonNull;
+import static org.apache.ignite.internal.processors.query.QueryUtils.DFLT_SCHEMA;
+import static org.apache.ignite.testframework.GridTestUtils.assertThrows;
+
 /**
  * Test hidden _key, _val, _ver columns
  */
@@ -63,6 +71,9 @@ public class IgniteSqlKeyValueFieldsTest  extends AbstractIndexingCommonTest {
     /** */
     private static String CACHE_JOB = "Job";
 
+    /**  */
+    private static String CACHE_SQL = "Sql";
+
     /** */
     private boolean oldAllowKeyValCol;
 
@@ -81,6 +92,7 @@ public class IgniteSqlKeyValueFieldsTest  extends AbstractIndexingCommonTest {
         ccfgs.add(buildCacheConfiguration(CACHE_INT_NO_KV_TYPE));
         ccfgs.add(buildCacheConfiguration(CACHE_PERSON));
         ccfgs.add(buildCacheConfiguration(CACHE_JOB));
+        ccfgs.add(buildCacheConfiguration(CACHE_SQL));
 
         c.setCacheConfiguration(ccfgs.toArray(new CacheConfiguration[ccfgs.size()]));
         if (gridName.equals(NODE_CLIENT))
@@ -142,7 +154,7 @@ public class IgniteSqlKeyValueFieldsTest  extends AbstractIndexingCommonTest {
 
             entity.setFields(fields);
 
-            ccfg.setQueryEntities(Arrays.asList(entity));
+            ccfg.setQueryEntities(asList(entity));
             return ccfg;
         }
         else if (name.equals(CACHE_INT_NO_KV_TYPE)) {
@@ -161,7 +173,7 @@ public class IgniteSqlKeyValueFieldsTest  extends AbstractIndexingCommonTest {
 
             entity.setFields(fields);
 
-            ccfg.setQueryEntities(Arrays.asList(entity));
+            ccfg.setQueryEntities(asList(entity));
             return ccfg;
         }
         else if (name.equals(CACHE_PERSON)) {
@@ -184,7 +196,7 @@ public class IgniteSqlKeyValueFieldsTest  extends AbstractIndexingCommonTest {
 
             entity.setFields(fields);
 
-            ccfg.setQueryEntities(Arrays.asList(entity));
+            ccfg.setQueryEntities(asList(entity));
             return ccfg;
         }
         else if (name.equals(CACHE_JOB)) {
@@ -192,6 +204,9 @@ public class IgniteSqlKeyValueFieldsTest  extends AbstractIndexingCommonTest {
             ccfg.setIndexedTypes(Integer.class, Integer.class);
             return ccfg;
         }
+        else if (name.equals(CACHE_SQL))
+            return new CacheConfiguration<>(name).setSqlSchema(DFLT_SCHEMA);
+
         return null;
     }
 
@@ -321,6 +336,177 @@ public class IgniteSqlKeyValueFieldsTest  extends AbstractIndexingCommonTest {
         assertTrue(((String)results.get(0).get(0)).contains("\"_key_PK\""));
     }
 
+    /**
+     * Test to verify that there will be an error when changing the column type
+     * through sql "alter table" drop and then add column, and also when trying
+     * to create an index, there will be an error.
+     */
+    @Test
+    public void testChangeColumnTypeByAlterTableDropAddColumn() {
+        // test1
+        changeSqlColumnType(
+            "int",
+            1,
+            asList("varchar", "date", "tinyint", "long", "datetime"),
+            new LinkedList<>(asList("a", "b,a", "a", "a,b", "a"))
+        );
+
+        // test2
+        changeSqlColumnType(
+            "varchar",
+            "1",
+            asList("int", "date", "tinyint", "long", "datetime"),
+            new LinkedList<>(asList("a", "b,a", "a", "a,b", "a"))
+        );
+
+        // test3
+        changeSqlColumnType(
+            "date",
+            new Timestamp(0),
+            asList("int", "long", "tinyint", "datetime", "varchar"),
+            new LinkedList<>(asList("a", "b,a", "a,b", "a", "a"))
+        );
+
+        // test4
+        changeSqlColumnType(
+            "datetime",
+            new Timestamp(0),
+            asList("int", "long", "tinyint", "date", "varchar"),
+            new LinkedList<>(asList("a", "b,a", "a,b", "a", "a"))
+        );
+    }
+
+    /**
+     * Test to verify that when changing column type, there will be no error
+     * without changing type through sql "alter table" drop and then add column
+     * and also when trying to create an index there will be no error.
+     */
+    @Test
+    public void testReturnColumnTypeByAlterTableDropAddColumn() {
+        checkRecreateSqlColumn("int", 1, asList("int"), new LinkedList<>(asList("a")));
+
+        checkRecreateSqlColumn("varchar", 1, asList("varchar"), new LinkedList<>(asList("b,a")));
+
+        checkRecreateSqlColumn("date", new java.sql.Date(0), asList("date"), new LinkedList<>(asList("b")));
+
+        checkRecreateSqlColumn("tinyint", 1, asList("tinyint"), new LinkedList<>(asList("a,b")));
+
+        checkRecreateSqlColumn("long", 1L, asList("long"), new LinkedList<>(asList("a")));
+
+        checkRecreateSqlColumn("datetime", new Timestamp(0), asList("datetime"), new LinkedList<>(asList("a")));
+    }
+
+    /**
+     * Verifies that when changing a sql column type by sql "alter table"
+     * drop column and then add column, an exception will be thrown and also
+     * after that when trying to create an index on this column
+     * will lead to an error. Here used table
+     * "create table TEST (id int primary key, a x, b int)", where all logic
+     * is focused on column "a".
+     *
+     * @param aColType The initial type of column is "a".
+     * @param aInsertVal The value for insert into "a" column.
+     * @param aColTypes Column types for changing "a" column.
+     * @param idxCols Columns (single or comma-separated) to create indexes.
+     */
+    private void changeSqlColumnType(
+        String aColType,
+        Object aInsertVal,
+        List<String> aColTypes,
+        Queue<String> idxCols
+    ) {
+        assert nonNull(aInsertVal);
+        assert nonNull(aColType);
+        assert nonNull(aColTypes);
+        assert nonNull(idxCols);
+
+        IgniteCache<Object, Object> cache = grid(0).cache(CACHE_SQL);
+
+        cache.query(
+            new SqlFieldsQuery("create table TEST (id int primary key, a " + aColType + ", b int)")
+        ).getAll();
+
+        cache.query(new SqlFieldsQuery("insert into TEST (id, a, b) VALUES (?,?,?)").setArgs(1, aInsertVal, 1))
+            .getAll();
+
+        for (String columnType : aColTypes) {
+            cache.query(new SqlFieldsQuery("alter table TEST drop column a")).getAll();
+
+            assertThrows(
+                log,
+                () -> cache.query(new SqlFieldsQuery("alter table TEST add column a " + columnType)).getAll(),
+                CacheException.class,
+                "Column already exists: with a different type."
+            );
+
+            assertThrows(
+                log,
+                () -> cache.query(new SqlFieldsQuery("create index tidx on TEST (" + idxCols.poll() + ")")).getAll(),
+                CacheException.class,
+                "Column doesn't exist: A"
+            );
+
+            cache.query(new SqlFieldsQuery("alter table TEST add column a " + aColType)).getAll();
+        }
+
+        cache.query(new SqlFieldsQuery("DROP TABLE TEST")).getAll();
+    }
+
+    /**
+     * Checking that when using sql "alter table" drop column and then add
+     * column without changing the column type will not throw an exception
+     * and you can also create indexes without errors. Here used table
+     * "create table TEST (id int primary key, a x, b int)", where all logic
+     * is focused on column "a".
+     *
+     * @param aColType The initial type of column is "a".
+     * @param aInsertVal The value for insert into "a" column.
+     * @param aColTypes Column types for changing "a" column.
+     * @param idxCols Columns (single or comma-separated) to create indexes.
+     */
+    private void checkRecreateSqlColumn(
+        String aColType,
+        Object aInsertVal,
+        List<String> aColTypes,
+        Queue<String> idxCols
+    ) {
+        assert nonNull(aInsertVal);
+        assert nonNull(aColType);
+        assert nonNull(aColTypes);
+        assert nonNull(idxCols);
+
+        IgniteCache<Object, Object> cache = grid(0).cache(CACHE_SQL);
+
+        cache.query(
+            new SqlFieldsQuery("create table TEST (id int primary key, a " + aColType + ", b int)")
+        ).getAll();
+
+        cache.query(new SqlFieldsQuery("insert into TEST (id, a, b) VALUES (?,?,?)").setArgs(1, aInsertVal, 1)).getAll();
+
+        SqlFieldsQuery selectQry = new SqlFieldsQuery("select id,a,b from TEST");
+
+        for (String columnType : aColTypes) {
+            List<List<?>> rowsBeforeColManipulation = cache.query(selectQry).getAll();
+
+            assertEquals(1, rowsBeforeColManipulation.size());
+            assertEquals(3, rowsBeforeColManipulation.get(0).size());
+
+            cache.query(new SqlFieldsQuery("alter table TEST drop column a")).getAll();
+            cache.query(new SqlFieldsQuery("alter table TEST add column a " + columnType)).getAll();
+
+            List<List<?>> rowsAfterColManipulation = cache.query(selectQry).getAll();
+
+            assertEquals(1, rowsAfterColManipulation.size());
+            assertEquals(rowsBeforeColManipulation.get(0), rowsAfterColManipulation.get(0));
+
+            cache.query(new SqlFieldsQuery("create index tidx on TEST (" + idxCols.poll() + ")")).getAll();
+        }
+
+        cache.query(
+            new SqlFieldsQuery("DROP TABLE TEST")
+        ).getAll();
+    }
+
     /** */
     private GridCacheVersion getVersion(IgniteCache<?, ?> cache, int key) {
         QueryCursor<List<?>> cursor = cache.query(new SqlFieldsQuery("select _ver from Person where id = ?").setArgs(key));