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

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


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

commit 4c03f087f2c3b562459f5c09814d7eeafc727b11
Author: Xinyi Yan <x...@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 <yanxi...@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 5855922..ac4896c 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;
@@ -412,5 +413,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 ea16a0a..15a3a13 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
@@ -43,6 +43,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;
@@ -182,13 +183,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) {

Reply via email to