diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java index 674818670c..17ad4f8813 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java @@ -14123,6 +14123,10 @@ private void validateCreateView() } } + if (createVwDesc.isMaterialized() && !hasTableDefined(qb)) { + throw new SemanticException("Materialized view must have a table defined."); + } + if (createVwDesc.isMaterialized() && createVwDesc.isRewriteEnabled()) { if (!ctx.isCboSucceeded()) { String msg = "Cannot enable automatic rewriting for materialized view."; @@ -14195,6 +14199,23 @@ private void validateCreateView() } } + // returns false when the query block doesn't have + // a table defined, e.g. "select 5" + private boolean hasTableDefined(QB qb) { + // A table is not defined when there only exists a dummy + // table in the alias, so if it is blank, we know a table + // is defined. + if (qb.getTabAliases().size() == 0) { + return true; + } + for (String alias : qb.getTabAliases()) { + if (!DUMMY_TABLE.equals(alias)) { + return true; + } + } + return false; + } + // Process the position alias in GROUPBY and ORDERBY public void processPositionAlias(ASTNode ast) throws SemanticException { boolean isBothByPos = HiveConf.getBoolVar(conf, ConfVars.HIVE_GROUPBY_ORDERBY_POSITION_ALIAS); diff --git a/ql/src/test/results/clientnegative/materialized_view_no_tbl.q.out b/ql/src/test/results/clientnegative/materialized_view_no_tbl.q.out new file mode 100644 index 0000000000..8de568e47a --- /dev/null +++ b/ql/src/test/results/clientnegative/materialized_view_no_tbl.q.out @@ -0,0 +1 @@ +FAILED: SemanticException Materialized view must have a table defined.