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/07/23 17:12:31 UTC

[phoenix] branch 4.14-HBase-1.3 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.14-HBase-1.3
in repository https://gitbox.apache.org/repos/asf/phoenix.git


The following commit(s) were added to refs/heads/4.14-HBase-1.3 by this push:
     new 94a7d29  PHOENIX-5209 Cannot add non-PK column to table when the last PK column is of type VARBINARY or ARRAY
94a7d29 is described below

commit 94a7d29d6f9feef170ba116c5d1d475f6fe4f0ac
Author: Toshihiro Suzuki <br...@gmail.com>
AuthorDate: Sun Jul 14 11:38:58 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   | 26 +++++++++++++++++++++-
 .../org/apache/phoenix/schema/MetaDataClient.java  | 17 ++++++++++----
 2 files changed, 38 insertions(+), 5 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 b1949ed..2585d17 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
@@ -1345,6 +1345,30 @@ public class AlterTableIT extends ParallelStatsDisabledIT {
             assertFalse(rs.next());
         }
     }
-    
+
+    @Test
+    public void testAddNonPKColumnWhenlastPKIsVARBINARYOrARRAY() throws Exception {
+        String tableName1 = generateUniqueName();
+        String tableName2 = generateUniqueName();
+        Properties props = PropertiesUtil.deepCopy(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 48bbf67..3dead42 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
@@ -3295,15 +3295,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();