Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
CreateViewStmt.java
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.cloudera.impala.analysis;
16 
17 import java.util.ArrayList;
18 
22 import com.cloudera.impala.thrift.TAccessEvent;
23 import com.cloudera.impala.thrift.TCatalogObjectType;
24 
25 import com.google.common.base.Joiner;
26 import com.google.common.base.Preconditions;
27 
32 
33  public CreateViewStmt(boolean ifNotExists, TableName tableName,
34  ArrayList<ColumnDef> columnDefs, String comment, QueryStmt viewDefStmt) {
35  super(ifNotExists, tableName, columnDefs, comment, viewDefStmt);
36  }
37 
38  @Override
39  public void analyze(Analyzer analyzer) throws AnalysisException {
40  tableName_.analyze();
41  // Use a child analyzer to let views have complex-typed columns.
42  Analyzer viewAnalyzerr = new Analyzer(analyzer);
43  // Enforce Hive column labels for view compatibility.
44  viewAnalyzerr.setUseHiveColLabels(true);
45  viewDefStmt_.analyze(viewAnalyzerr);
46 
47  Preconditions.checkState(tableName_ != null && !tableName_.isEmpty());
48  dbName_ = analyzer.getTargetDbName(tableName_);
49  owner_ = analyzer.getUser().getName();
50  if (analyzer.dbContainsTable(dbName_, tableName_.getTbl(), Privilege.CREATE) &&
51  !ifNotExists_) {
53  String.format("%s.%s", dbName_, tableName_.getTbl()));
54  }
55  analyzer.addAccessEvent(new TAccessEvent(dbName_ + "." + tableName_.getTbl(),
56  TCatalogObjectType.VIEW, Privilege.CREATE.toString()));
57 
58  createColumnAndViewDefs(analyzer);
59  if (RuntimeEnv.INSTANCE.computeLineage() || RuntimeEnv.INSTANCE.isTestEnv()) {
60  computeLineageGraph(analyzer);
61  }
62  }
63 
64  @Override
65  public String toSql() {
66  StringBuilder sb = new StringBuilder();
67  sb.append("CREATE VIEW ");
68  if (ifNotExists_) sb.append("IF NOT EXISTS ");
69  if (tableName_.getDb() != null) sb.append(tableName_.getDb() + ".");
70  sb.append(tableName_.getTbl() + " (");
71  sb.append(Joiner.on(", ").join(columnDefs_));
72  sb.append(") AS ");
73  sb.append(viewDefStmt_.toSql());
74  return sb.toString();
75  }
76 }
static final String TBL_ALREADY_EXISTS_ERROR_MSG
Definition: Analyzer.java:110
CreateViewStmt(boolean ifNotExists, TableName tableName, ArrayList< ColumnDef > columnDefs, String comment, QueryStmt viewDefStmt)