You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@impala.apache.org by ta...@apache.org on 2019/06/13 06:37:16 UTC

[impala] 02/07: IMPALA-8436: Prohibit write/alter operations on materialized view

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

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

commit fcaec278ccf4638692701fd03309a4f40e8fd611
Author: arorasudhanshu <su...@cloudera.com>
AuthorDate: Mon Jun 3 15:40:40 2019 -0700

    IMPALA-8436: Prohibit write/alter operations on materialized view
    
    Instead of creating an in memory instance of View, we were
    creating instance of HdfsTable. Modified the code to create instance of
    View for materialized view.
    
    Testing Done:
    - Added tests in AnalyzerTest.
    
    Change-Id: Idcd619303e19b5a2551876a63d67569c76bd22f0
    Reviewed-on: http://gerrit.cloudera.org:8080/13503
    Tested-by: Impala Public Jenkins <im...@cloudera.com>
    Reviewed-by: Zoltan Borok-Nagy <bo...@cloudera.com>
---
 .../org/apache/impala/compat/MetastoreShim.java    | 24 ++++++++++++++++++++++
 .../main/java/org/apache/impala/catalog/Table.java |  4 +++-
 .../org/apache/impala/analysis/AnalyzerTest.java   | 19 +++++++++++++++++
 .../functional/functional_schema_template.sql      | 10 +++++++++
 4 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/fe/src/compat-hive-2/java/org/apache/impala/compat/MetastoreShim.java b/fe/src/compat-hive-2/java/org/apache/impala/compat/MetastoreShim.java
index c79dca9..de58116 100644
--- a/fe/src/compat-hive-2/java/org/apache/impala/compat/MetastoreShim.java
+++ b/fe/src/compat-hive-2/java/org/apache/impala/compat/MetastoreShim.java
@@ -159,6 +159,30 @@ public class MetastoreShim {
           .put("INDEX_TABLE", TABLE_TYPE_TABLE)
           .put("VIRTUAL_VIEW", TABLE_TYPE_VIEW).build();
 
+  public static String mapToInternalTableType(String typeStr) {
+    String defaultTableType = TABLE_TYPE_TABLE;
+
+    TableType tType;
+
+    if (typeStr == null) return defaultTableType;
+    try {
+      tType = TableType.valueOf(typeStr.toUpperCase());
+    } catch (Exception e) {
+      return defaultTableType;
+    }
+    switch (tType) {
+      case EXTERNAL_TABLE:
+      case MANAGED_TABLE:
+      case INDEX_TABLE:
+        return TABLE_TYPE_TABLE;
+      case VIRTUAL_VIEW:
+        return TABLE_TYPE_VIEW;
+      default:
+        return defaultTableType;
+    }
+
+  }
+
   /**
    * Wrapper method which returns ExtendedJSONMessageFactory in case Impala is
    * building against Hive-2 to keep compatibility with Sentry
diff --git a/fe/src/main/java/org/apache/impala/catalog/Table.java b/fe/src/main/java/org/apache/impala/catalog/Table.java
index ab44c55..f3ddd63 100644
--- a/fe/src/main/java/org/apache/impala/catalog/Table.java
+++ b/fe/src/main/java/org/apache/impala/catalog/Table.java
@@ -39,6 +39,7 @@ import org.apache.impala.common.ImpalaRuntimeException;
 import org.apache.impala.common.Metrics;
 import org.apache.impala.common.Pair;
 import org.apache.impala.common.RuntimeEnv;
+import org.apache.impala.service.MetadataOp;
 import org.apache.impala.thrift.TAccessLevel;
 import org.apache.impala.thrift.TCatalogObject;
 import org.apache.impala.thrift.TCatalogObjectType;
@@ -327,7 +328,8 @@ public abstract class Table extends CatalogObjectImpl implements FeTable {
     CatalogInterners.internFieldsInPlace(msTbl);
     Table table = null;
     // Create a table of appropriate type
-    if (TableType.valueOf(msTbl.getTableType()) == TableType.VIRTUAL_VIEW) {
+    if (MetadataOp.TABLE_TYPE_VIEW.equals(
+          MetastoreShim.mapToInternalTableType(msTbl.getTableType()))) {
       table = new View(msTbl, db, msTbl.getTableName(), msTbl.getOwner());
     } else if (HBaseTable.isHBaseTable(msTbl)) {
       table = new HBaseTable(msTbl, db, msTbl.getTableName(), msTbl.getOwner());
diff --git a/fe/src/test/java/org/apache/impala/analysis/AnalyzerTest.java b/fe/src/test/java/org/apache/impala/analysis/AnalyzerTest.java
index 4177bf7..d5f42f8 100644
--- a/fe/src/test/java/org/apache/impala/analysis/AnalyzerTest.java
+++ b/fe/src/test/java/org/apache/impala/analysis/AnalyzerTest.java
@@ -615,6 +615,25 @@ public class AnalyzerTest extends FrontendTestBase {
     AnalyzesOk("show column stats functional.insert_only_transactional_table");
   }
 
+  @Test
+  public void TestAnalyzeMaterializedView() {
+    Assume.assumeTrue(MetastoreShim.getMajorVersion() > 2);
+    AnalysisError("alter table functional.materialized_view " +
+        "set tblproperties ('foo'='bar')",
+      "ALTER TABLE not allowed on a " +
+      "view: functional.materialized_view");
+
+    AnalysisError("insert into table functional.materialized_view " +
+        "select * from functional.insert_only_transactional_table",
+      "Impala does not support INSERTing into views:" +
+        " functional.materialized_view");
+
+    AnalysisError("drop table functional.materialized_view ",
+      "DROP TABLE not allowed on a view: functional.materialized_view");
+
+    AnalyzesOk("Select * from functional.materialized_view");
+  }
+
   private Function createFunction(boolean hasVarArgs, Type... args) {
     return new Function(new FunctionName("test"), args, Type.INVALID, hasVarArgs);
   }
diff --git a/testdata/datasets/functional/functional_schema_template.sql b/testdata/datasets/functional/functional_schema_template.sql
index 3de8363..79d551d 100644
--- a/testdata/datasets/functional/functional_schema_template.sql
+++ b/testdata/datasets/functional/functional_schema_template.sql
@@ -2152,6 +2152,16 @@ TBLPROPERTIES('transactional'='true');
 ---- DATASET
 functional
 ---- BASE_TABLE_NAME
+materialized_view
+---- HIVE_MAJOR_VERSION
+3
+---- CREATE_HIVE
+CREATE MATERIALIZED VIEW IF NOT EXISTS {db_name}{db_suffix}.{table_name}
+  AS SELECT * FROM {db_name}{db_suffix}.insert_only_transactional_table;
+====
+---- DATASET
+functional
+---- BASE_TABLE_NAME
 testescape_16_lf
 ---- CREATE
 CREATE EXTERNAL TABLE IF NOT EXISTS {db_name}{db_suffix}.{table_name} (