You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by gj...@apache.org on 2019/03/04 23:39:05 UTC

[phoenix] branch 4.x-HBase-1.3 updated: PHOENIX-2265 Disallow creation of view over HBase table if PK not specified

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

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


The following commit(s) were added to refs/heads/4.x-HBase-1.3 by this push:
     new 8103312  PHOENIX-2265 Disallow creation of view over HBase table if PK not specified
8103312 is described below

commit 810331203afc85db3732b1e92716b1f83100ba4b
Author: Xinyi Yan <xy...@salesforce.com>
AuthorDate: Thu Feb 28 12:55:05 2019 -0800

    PHOENIX-2265 Disallow creation of view over HBase table if PK not specified
    
    Signed-off-by: Geoffrey Jacoby <gj...@apache.org>
---
 .../it/java/org/apache/phoenix/end2end/ViewIT.java | 47 ++++++++++++++++++++++
 .../phoenix/compile/CreateTableCompiler.java       | 18 +++++++++
 2 files changed, 65 insertions(+)

diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ViewIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ViewIT.java
index 6318dca..84fae7c 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ViewIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ViewIT.java
@@ -63,6 +63,7 @@ import org.apache.hadoop.hbase.client.Admin;
 import org.apache.hadoop.hbase.client.HBaseAdmin;
 import org.apache.hadoop.hbase.client.Scan;
 import org.apache.hadoop.hbase.coprocessor.ObserverContext;
+import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.hadoop.hbase.util.Pair;
 import org.apache.phoenix.compile.QueryPlan;
 import org.apache.phoenix.coprocessor.BaseMetaDataEndpointObserver;
@@ -72,6 +73,7 @@ import org.apache.phoenix.exception.PhoenixIOException;
 import org.apache.phoenix.exception.SQLExceptionCode;
 import org.apache.phoenix.jdbc.PhoenixConnection;
 import org.apache.phoenix.jdbc.PhoenixStatement;
+import org.apache.phoenix.mapreduce.util.ConnectionUtil;
 import org.apache.phoenix.query.KeyRange;
 import org.apache.phoenix.query.QueryServices;
 import org.apache.phoenix.schema.ColumnAlreadyExistsException;
@@ -1359,6 +1361,51 @@ public class ViewIT extends SplitSystemCatalogIT {
         // we should be able to load the second view
         PhoenixRuntime.getTableNoCache(conn, fullViewName2);
     }
+
+    @Test
+    public void testCreateViewFromHBaseTable() throws Exception {
+        String tableNameStr = generateUniqueName();
+        String familyNameStr = generateUniqueName();
+
+        HBaseAdmin admin = driver.getConnectionQueryServices(getUrl(),
+                TestUtil.TEST_PROPERTIES).getAdmin();
+
+        HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableNameStr));
+        desc.addFamily(new HColumnDescriptor(familyNameStr));
+        admin.createTable(desc);
+        Connection conn = DriverManager.getConnection(getUrl());
+
+        //PK is not specified, without where clause
+        try {
+            conn.createStatement().executeUpdate("CREATE VIEW \"" + tableNameStr +
+                    "\" (ROWKEY VARCHAR, \"" + familyNameStr + "\".a VARCHAR)");
+            fail();
+        } catch (SQLException e) {
+            assertEquals(SQLExceptionCode.PRIMARY_KEY_MISSING.getErrorCode(), e.getErrorCode());
+        }
+
+        // No error, as PK is specified
+        conn.createStatement().executeUpdate("CREATE VIEW \"" + tableNameStr +
+                "\" (ROWKEY VARCHAR PRIMARY KEY, \"" + familyNameStr + "\".a VARCHAR)");
+
+        conn.createStatement().executeUpdate("DROP VIEW \"" + tableNameStr + "\"");
+
+        //PK is not specified, with where clause
+        try {
+            conn.createStatement().executeUpdate("CREATE VIEW \"" + tableNameStr +
+                    "\" (ROWKEY VARCHAR, \"" + familyNameStr + "\".a VARCHAR) AS SELECT * FROM \""
+                    + tableNameStr + "\" WHERE ROWKEY = '1'");
+            fail();
+        } catch (SQLException e) {
+            assertEquals(SQLExceptionCode.PRIMARY_KEY_MISSING.getErrorCode(), e.getErrorCode());
+        }
+
+        conn.createStatement().executeUpdate("CREATE VIEW \"" + tableNameStr +
+                "\" (ROWKEY VARCHAR PRIMARY KEY, \"" + familyNameStr + "\".a VARCHAR) AS SELECT " +
+                "* FROM \"" + tableNameStr + "\" WHERE ROWKEY = '1'");
+
+        conn.createStatement().executeUpdate("DROP VIEW \"" + tableNameStr + "\"");
+    }
     
     @Test
     public void testConcurrentViewCreationAndTableDrop() throws Exception {
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/compile/CreateTableCompiler.java b/phoenix-core/src/main/java/org/apache/phoenix/compile/CreateTableCompiler.java
index c9fd3b2..39d8d18 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/compile/CreateTableCompiler.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/compile/CreateTableCompiler.java
@@ -158,6 +158,24 @@ public class CreateTableCompiler {
                     }
                 }
             }
+
+            if (viewTypeToBe == ViewType.MAPPED && parentToBe.getPKColumns().size() == 0) {
+                boolean isPKMissed = true;
+                if (pkConstraint.getColumnNames().size() > 0) {
+                    isPKMissed = false;
+                } else {
+                    for (ColumnDef columnDef: columnDefs) {
+                        if (columnDef.isPK()){
+                            isPKMissed = false;
+                            break;
+                        }
+                    }
+                }
+                if (isPKMissed) {
+                    throw new SQLExceptionInfo.Builder(SQLExceptionCode.PRIMARY_KEY_MISSING)
+                            .build().buildException();
+                }
+            }
         }
         final ViewType viewType = viewTypeToBe;
         final String viewStatement = viewStatementToBe;