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

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


The following commit(s) were added to refs/heads/4.x-HBase-1.4 by this push:
     new d031442  PHOENIX-5386: Disallow creating views on top of SYSTEM tables
d031442 is described below

commit d031442f4b9152916f238a35e686322de7ffd08e
Author: Chinmay Kulkarni <chinmayskulka...@gmail.com>
AuthorDate: Mon Jul 8 13:44:15 2019 -0700

    PHOENIX-5386: Disallow creating views on top of SYSTEM tables
---
 .../src/it/java/org/apache/phoenix/end2end/ViewIT.java  | 17 +++++++++++++++++
 .../org/apache/phoenix/compile/CreateTableCompiler.java |  6 ++++++
 .../org/apache/phoenix/exception/SQLExceptionCode.java  |  3 +++
 3 files changed, 26 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 f2f7834..9ab7bb3 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.phoenix.coprocessor.BaseMetaDataEndpointObserver;
 import org.apache.phoenix.coprocessor.PhoenixMetaDataCoprocessorHost;
 import 
org.apache.phoenix.coprocessor.PhoenixMetaDataCoprocessorHost.PhoenixMetaDataControllerEnvironment;
 import org.apache.phoenix.exception.PhoenixIOException;
+import org.apache.phoenix.exception.SQLExceptionCode;
 import org.apache.phoenix.jdbc.PhoenixStatement;
 import org.apache.phoenix.query.KeyRange;
 import org.apache.phoenix.query.QueryServices;
@@ -956,6 +957,22 @@ public class ViewIT extends SplitSystemCatalogIT {
         }
     }
 
+    @Test
+    public void testDisallowCreatingViewsOnSystemTable() throws SQLException {
+        try (Connection conn = DriverManager.getConnection(getUrl())) {
+            String viewDDL = "CREATE VIEW " + generateUniqueName() + " AS 
SELECT * FROM " +
+                    "SYSTEM.CATALOG";
+            try {
+                conn.createStatement().execute(viewDDL);
+                fail("Should have thrown an exception");
+            } catch (SQLException sqlE) {
+                assertEquals("Expected a different Error code",
+                        
SQLExceptionCode.CANNOT_CREATE_VIEWS_ON_SYSTEM_TABLES.getErrorCode(),
+                        sqlE.getErrorCode());
+            }
+        }
+    }
+
     private class CreateViewRunnable implements Callable<Exception> {
         private final String fullTableName;
         private final String fullViewName;
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 39d8d18..e806d9c 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
@@ -125,6 +125,12 @@ public class CreateTableCompiler {
             // Used to track column references in a view
             ExpressionCompiler expressionCompiler = new 
ColumnTrackingExpressionCompiler(context, isViewColumnReferencedToBe);
             parentToBe = tableRef.getTable();
+            // Disallow creating views on top of SYSTEM tables. See 
PHOENIX-5386
+            if (parentToBe.getType() == PTableType.SYSTEM) {
+                throw new SQLExceptionInfo
+                        
.Builder(SQLExceptionCode.CANNOT_CREATE_VIEWS_ON_SYSTEM_TABLES)
+                        .build().buildException();
+            }
             viewTypeToBe = parentToBe.getViewType() == ViewType.MAPPED ? 
ViewType.MAPPED : ViewType.UPDATABLE;
             if (whereNode == null) {
                 viewStatementToBe = parentToBe.getViewStatement();
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java 
b/phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java
index 9a6985c..4c59fc6 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java
@@ -35,6 +35,7 @@ import 
org.apache.phoenix.schema.ConcurrentTableMutationException;
 import org.apache.phoenix.schema.FunctionAlreadyExistsException;
 import org.apache.phoenix.schema.FunctionNotFoundException;
 import org.apache.phoenix.schema.IndexNotFoundException;
+import org.apache.phoenix.schema.PTableType;
 import org.apache.phoenix.schema.ReadOnlyTableException;
 import org.apache.phoenix.schema.SchemaAlreadyExistsException;
 import org.apache.phoenix.schema.SchemaNotFoundException;
@@ -406,6 +407,8 @@ public enum SQLExceptionCode {
     INVALID_IMMUTABLE_STORAGE_SCHEME_AND_COLUMN_QUALIFIER_BYTES(1137, "XCL37", 
"If IMMUTABLE_STORAGE_SCHEME property is not set to ONE_CELL_PER_COLUMN 
COLUMN_ENCODED_BYTES cannot be 0"),
     INVALID_IMMUTABLE_STORAGE_SCHEME_CHANGE(1138, "XCL38", 
"IMMUTABLE_STORAGE_SCHEME property cannot be changed from/to 
ONE_CELL_PER_COLUMN "),
     CANNOT_SET_GUIDE_POST_WIDTH(1139, "XCL39", "Guide post width can only be 
set on base data tables"),
+    CANNOT_CREATE_VIEWS_ON_SYSTEM_TABLES(1141, "XCL41", "Cannot create views 
on tables of type" +
+            PTableType.SYSTEM),
     /**
      * Implementation defined class. Phoenix internal error. (errorcode 20, 
sqlstate INT).
      */

Reply via email to