You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by sk...@apache.org on 2019/06/30 21:22:40 UTC

[phoenix] branch 4.x-HBase-1.5 updated: PHOENIX-5209 Cannot add non-PK column to table when the last PK column is of type VARBINARY or ARRAY

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

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


The following commit(s) were added to refs/heads/4.x-HBase-1.5 by this push:
     new 1657fac  PHOENIX-5209 Cannot add non-PK column to table when the last PK column is of type VARBINARY or ARRAY
1657fac is described below

commit 1657fac6391d949a3ba3de1fd93a943e5911678e
Author: Toshihiro Suzuki <br...@gmail.com>
AuthorDate: Sun Jun 9 22:25:18 2019 +0900

    PHOENIX-5209 Cannot add non-PK column to table when the last PK column is of type VARBINARY or ARRAY
    
    Signed-off-by: s.kadam <sk...@apache.org>
---
 .../org/apache/phoenix/end2end/AlterTableIT.java   | 24 ++++++++++++++++++++++
 .../org/apache/phoenix/schema/MetaDataClient.java  | 17 +++++++++++----
 2 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterTableIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterTableIT.java
index c9b2ab1..669f1c1 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterTableIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterTableIT.java
@@ -1387,5 +1387,29 @@ public class AlterTableIT extends ParallelStatsDisabledIT {
 		}
 	}
 
+    @Test
+    public void testAddNonPKColumnWhenlastPKIsVARBINARYOrARRAY() throws Exception {
+        String tableName1 = generateUniqueName();
+        String tableName2 = generateUniqueName();
+        Properties props = PropertiesUtil.deepCopy(TestUtil.TEST_PROPERTIES);
+        try (Connection conn = DriverManager.getConnection(getUrl(), props);
+            Statement stmt = conn.createStatement()) {
+            conn.setAutoCommit(false);
+
+            String ddl = "CREATE TABLE " + tableName1 + " (id VARBINARY PRIMARY KEY, "
+                + "col1 INTEGER)";
+            stmt.execute(ddl);
+
+            String alterDdl = "ALTER TABLE " + tableName1 + " ADD col2 INTEGER";
+            stmt.execute(alterDdl);
+
+            String ddl2 = "CREATE TABLE " + tableName2 + " (id INTEGER ARRAY PRIMARY KEY, "
+                + "col1 INTEGER)";
+            stmt.execute(ddl2);
+
+            String alterDdl2 = "ALTER TABLE " + tableName2 + " ADD col2 INTEGER";
+            stmt.execute(alterDdl2);
+        }
+    }
 }
  
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java
index ab7d881..a3c5224 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java
@@ -3592,15 +3592,24 @@ public class MetaDataClient {
 
                 int position = table.getColumns().size();
 
-                List<PColumn> currentPKs = table.getPKColumns();
-                if (numCols > 0) {
+                boolean addPKColumns = false;
+                for (ColumnDef columnDef : columnDefs) {
+                    if (columnDef.isPK()) {
+                        addPKColumns = true;
+                        break;
+                    }
+                }
+                if (addPKColumns) {
+                    List<PColumn> currentPKs = table.getPKColumns();
                     PColumn lastPK = currentPKs.get(currentPKs.size()-1);
-                    // Disallow adding columns if the last column is VARBIANRY.
+                    // Disallow adding columns if the last column in the primary key is VARBIANRY
+                    // or ARRAY.
                     if (lastPK.getDataType() == PVarbinary.INSTANCE || lastPK.getDataType().isArrayType()) {
                         throw new SQLExceptionInfo.Builder(SQLExceptionCode.VARBINARY_LAST_PK)
                         .setColumnName(lastPK.getName().getString()).build().buildException();
                     }
-                    // Disallow adding columns if last column is fixed width and nullable.
+                    // Disallow adding columns if last column in the primary key is fixed width
+                    // and nullable.
                     if (lastPK.isNullable() && lastPK.getDataType().isFixedWidth()) {
                         throw new SQLExceptionInfo.Builder(SQLExceptionCode.NULLABLE_FIXED_WIDTH_LAST_PK)
                         .setColumnName(lastPK.getName().getString()).build().buildException();