You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by ya...@apache.org on 2020/07/20 17:54:21 UTC

[phoenix] branch 4.x updated: PHOENIX-5976 Cannot drop a column when the index view is involved

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

yanxinyi pushed a commit to branch 4.x
in repository https://gitbox.apache.org/repos/asf/phoenix.git


The following commit(s) were added to refs/heads/4.x by this push:
     new 2fcb854  PHOENIX-5976 Cannot drop a column when the index view is involved
2fcb854 is described below

commit 2fcb8541c9dd7317e62239bd208ff4377ba794e2
Author: Xinyi Yan <xy...@salesforce.com>
AuthorDate: Wed Jul 15 10:27:16 2020 -0700

    PHOENIX-5976 Cannot drop a column when the index view is involved
    
    Signed-off-by: Xinyi Yan <ya...@apache.org>
---
 .../apache/phoenix/end2end/index/DropColumnIT.java | 169 ++++++++++++++++++++-
 .../phoenix/coprocessor/DropColumnMutator.java     |  22 ++-
 2 files changed, 187 insertions(+), 4 deletions(-)

diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/DropColumnIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/DropColumnIT.java
index f0f9b26..ef02e54 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/DropColumnIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/DropColumnIT.java
@@ -29,6 +29,7 @@ import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.PreparedStatement;
 import java.sql.SQLException;
+import java.sql.Statement;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Properties;
@@ -411,5 +412,171 @@ public class DropColumnIT extends ParallelStatsDisabledIT {
             assertNull(results.next());
         }
     }
-    
+
+    @Test
+    public void testDropViewIndexColumn() throws Exception {
+        String table = generateUniqueName();
+        String view = generateUniqueName();
+        String index = generateUniqueName();
+
+        try (Connection conn = DriverManager.getConnection(getUrl())) {
+            conn.setAutoCommit(true);
+            try (Statement stmt = conn.createStatement()) {
+                stmt.execute("CREATE TABLE IF NOT EXISTS " + table +
+                        " (ID CHAR(10) NOT NULL,KEY_PREFIX CHAR(3) NOT NULL," +
+                        " CONSTRAINT PK PRIMARY KEY (ID,KEY_PREFIX))");
+                // create view
+                stmt.execute(
+                        "CREATE VIEW IF NOT EXISTS " + view +
+                                "  (PK1 DATE NOT NULL,PK2 CHAR(15) NOT NULL," +
+                                "NON_PK1 CHAR(15),NON_PK2 CHAR(15) CONSTRAINT " +
+                                "PKVIEW PRIMARY KEY (PK1,PK2)) " +
+                                "AS SELECT * FROM " + table + " WHERE KEY_PREFIX = '123'");
+                // create index
+                stmt.execute("CREATE INDEX " + index + " ON " + view +
+                        " (PK2, PK1) INCLUDE (NON_PK1, NON_PK2)");
+                // drop column
+                stmt.execute("ALTER VIEW "  + view + " DROP COLUMN NON_PK1");
+            }
+        }
+    }
+
+    @Test
+    public void testDropViewIndexColumnForMultiTenantTable() throws Exception {
+        String table = generateUniqueName();
+        String view = generateUniqueName();
+        String index = generateUniqueName();
+
+        try (Connection conn = DriverManager.getConnection(getUrl())) {
+            conn.setAutoCommit(true);
+            try (Statement stmt = conn.createStatement()) {
+                stmt.execute("CREATE TABLE IF NOT EXISTS "  + table +
+                        " (ID CHAR(10) NOT NULL,KEY_PREFIX CHAR(3) NOT NULL," +
+                        " CONSTRAINT PK PRIMARY KEY (ID,KEY_PREFIX)) MULTI_TENANT=TRUE");
+                // create view
+                stmt.execute(
+                        "CREATE VIEW IF NOT EXISTS "  + view +
+                                "  (PK1 DATE NOT NULL,PK2 CHAR(15) NOT NULL," +
+                                "NON_PK1 CHAR(15),NON_PK2 CHAR(15) CONSTRAINT " +
+                                "PKVIEW PRIMARY KEY (PK1,PK2)) " +
+                                "AS SELECT * FROM "  + table + " WHERE KEY_PREFIX = '123'");
+                // create index
+                stmt.execute("CREATE INDEX " + index + " ON " + view +
+                        " (PK2, PK1) INCLUDE (NON_PK1, NON_PK2)");
+                // drop column
+                stmt.execute("ALTER VIEW "  + view + " DROP COLUMN NON_PK1");
+            }
+        }
+    }
+
+    @Test
+    public void testDropIndexColumn() throws Exception {
+        String table = generateUniqueName();
+        String index = generateUniqueName();
+
+        try (Connection conn = DriverManager.getConnection(getUrl())) {
+            conn.setAutoCommit(true);
+            try (Statement stmt = conn.createStatement()) {
+                stmt.execute("CREATE TABLE IF NOT EXISTS " + table +
+                        " (ID CHAR(10) NOT NULL,KEY_PREFIX CHAR(3) NOT NULL, NON_PK1 CHAR(15),NON_PK2 CHAR(15), " +
+                        " CONSTRAINT PK PRIMARY KEY (ID,KEY_PREFIX))");
+                // create index
+                stmt.execute("CREATE INDEX " + index + " ON " + table +
+                        " (KEY_PREFIX, ID) INCLUDE (NON_PK1, NON_PK2)");
+                // drop column
+                stmt.execute("ALTER TABLE " + table + " DROP COLUMN NON_PK1");
+            }
+        }
+    }
+
+    @Test
+    public void testDropColumnForMultiTenantTable() throws Exception {
+        String table = generateUniqueName();
+        String view = generateUniqueName();
+
+        try (Connection conn = DriverManager.getConnection(getUrl())) {
+            conn.setAutoCommit(true);
+            try (Statement stmt = conn.createStatement()) {
+                stmt.execute("CREATE TABLE IF NOT EXISTS " + table +
+                        " (ID CHAR(10) NOT NULL,KEY_PREFIX CHAR(3) NOT NULL, NON_PK3 CHAR(15)," +
+                        " CONSTRAINT PK PRIMARY KEY (ID,KEY_PREFIX)) MULTI_TENANT=TRUE");
+                // create view
+                stmt.execute(
+                        "CREATE VIEW IF NOT EXISTS " +  view  +
+                                "  (PK1 DATE NOT NULL,PK2 CHAR(15) NOT NULL," +
+                                "NON_PK1 CHAR(15),NON_PK2 CHAR(15) CONSTRAINT " +
+                                "PKVIEW PRIMARY KEY (PK1,PK2)) " +
+                                "AS SELECT * FROM " + table + " WHERE KEY_PREFIX = '123'");
+                // drop column
+                stmt.execute("ALTER VIEW " + view + " DROP COLUMN NON_PK3");
+            }
+        }
+    }
+
+    @Test
+    public void testDropColumnForMultiTenantTableWithIndex() throws Exception {
+        String table = generateUniqueName();
+        String view = generateUniqueName();
+        String index = generateUniqueName();
+
+        try (Connection conn = DriverManager.getConnection(getUrl())) {
+            conn.setAutoCommit(true);
+            try (Statement stmt = conn.createStatement()) {
+                stmt.execute("CREATE TABLE IF NOT EXISTS " + table +
+                        " (ID CHAR(10) NOT NULL,KEY_PREFIX CHAR(3) NOT NULL, NON_PK3 CHAR(15)," +
+                        " CONSTRAINT PK PRIMARY KEY (ID,KEY_PREFIX)) MULTI_TENANT=TRUE");
+                // create view
+                stmt.execute(
+                        "CREATE VIEW IF NOT EXISTS " +  view  +
+                                "  (PK1 DATE NOT NULL,PK2 CHAR(15) NOT NULL," +
+                                "NON_PK1 CHAR(15),NON_PK2 CHAR(15) CONSTRAINT PKVIEW PRIMARY KEY (PK1,PK2)) " +
+                                "AS SELECT * FROM " + table + " WHERE KEY_PREFIX = '123'");
+
+                stmt.execute("CREATE INDEX " + index + " ON " + view +
+                        " (PK2, PK1) INCLUDE (NON_PK1, NON_PK2)");
+                // drop column
+                stmt.execute("ALTER VIEW " + view + " DROP COLUMN NON_PK3");
+            }
+        }
+    }
+
+    @Test
+    public void testDropColumnForTableWithIndex() throws Exception {
+        String table = generateUniqueName();
+        String index = generateUniqueName();
+
+        try (Connection conn = DriverManager.getConnection(getUrl())) {
+            conn.setAutoCommit(true);
+            try (Statement stmt = conn.createStatement()) {
+                stmt.execute("CREATE TABLE IF NOT EXISTS " + table +
+                        " (ID CHAR(10) NOT NULL,KEY_PREFIX CHAR(3) NOT NULL, NON_PK1 CHAR(15)," +
+                        " CONSTRAINT PK PRIMARY KEY (ID,KEY_PREFIX))");
+                stmt.execute("CREATE INDEX " + index + " ON " + table +
+                        " (KEY_PREFIX, ID) INCLUDE (NON_PK1)");
+                stmt.execute("ALTER TABLE " + table + " DROP COLUMN NON_PK1");
+            }
+        }
+    }
+
+    @Test
+    public void testDropColumnForTableWithView() throws Exception {
+        String table = generateUniqueName();
+        String view = generateUniqueName();
+
+        try (Connection conn = DriverManager.getConnection(getUrl())) {
+            conn.setAutoCommit(true);
+            try (Statement stmt = conn.createStatement()) {
+                stmt.execute("CREATE TABLE IF NOT EXISTS " + table +
+                        " (ID CHAR(10) NOT NULL,KEY_PREFIX CHAR(3) NOT NULL, NON_PK1 CHAR(15)," +
+                        " CONSTRAINT PK PRIMARY KEY (ID,KEY_PREFIX))");
+
+                stmt.execute(
+                        "CREATE VIEW IF NOT EXISTS " +  view  +
+                                "  (PK1 DATE NOT NULL,PK2 CHAR(15) NOT NULL," +
+                                "NON_PK2 CHAR(15),NON_PK3 CHAR(15) CONSTRAINT PKVIEW PRIMARY KEY (PK1,PK2)) " +
+                                "AS SELECT * FROM " + table + " WHERE KEY_PREFIX = '123'");
+                stmt.execute("ALTER TABLE " + table + " DROP COLUMN NON_PK1");
+            }
+        }
+    }
 }
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/DropColumnMutator.java b/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/DropColumnMutator.java
index 5ceada3..6422475 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/DropColumnMutator.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/DropColumnMutator.java
@@ -42,6 +42,7 @@ import org.apache.phoenix.schema.ColumnNotFoundException;
 import org.apache.phoenix.schema.ColumnRef;
 import org.apache.phoenix.schema.PColumn;
 import org.apache.phoenix.schema.PTable;
+import org.apache.phoenix.schema.PTableImpl;
 import org.apache.phoenix.schema.PTableType;
 import org.apache.phoenix.schema.TableRef;
 import org.apache.phoenix.schema.types.PInteger;
@@ -181,13 +182,28 @@ public class DropColumnMutator implements ColumnMutator {
             byte[] key = mutation.getRow();
             int pkCount = getVarChars(key, rowKeyMetaData);
             if (isView && mutation instanceof Put) {
-                PColumn column = MetaDataUtil.getColumn(pkCount, rowKeyMetaData, table);
+                PColumn column = null;
+                // checking put from the view or index
+                if (Bytes.compareTo(schemaName, rowKeyMetaData[SCHEMA_NAME_INDEX]) == 0
+                        && Bytes.compareTo(tableName, rowKeyMetaData[TABLE_NAME_INDEX]) == 0) {
+                    column = MetaDataUtil.getColumn(pkCount, rowKeyMetaData, table);
+                } else {
+                    for(int i = 0; i < table.getIndexes().size(); i++) {
+                        PTableImpl indexTable = (PTableImpl) table.getIndexes().get(i);
+                        byte[] indexTableName = indexTable.getTableName().getBytes();
+                        byte[] indexSchema = indexTable.getSchemaName().getBytes();
+                        if (Bytes.compareTo(indexSchema, rowKeyMetaData[SCHEMA_NAME_INDEX]) == 0
+                                && Bytes.compareTo(indexTableName, rowKeyMetaData[TABLE_NAME_INDEX]) == 0) {
+                            column = MetaDataUtil.getColumn(pkCount, rowKeyMetaData, indexTable);
+                            break;
+                        }
+                    }
+                }
                 if (column == null)
                     continue;
                 // ignore any puts that modify the ordinal positions of columns
                 iterator.remove();
-            }
-            else if (mutation instanceof Delete) {
+            } else if (mutation instanceof Delete) {
                 if (pkCount > COLUMN_NAME_INDEX
                         && Bytes.compareTo(schemaName, rowKeyMetaData[SCHEMA_NAME_INDEX]) == 0
                         && Bytes.compareTo(tableName, rowKeyMetaData[TABLE_NAME_INDEX]) == 0) {