Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
AlterTableOrViewRenameStmt.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 
20 import com.cloudera.impala.thrift.TAccessEvent;
21 import com.cloudera.impala.thrift.TAlterTableOrViewRenameParams;
22 import com.cloudera.impala.thrift.TAlterTableParams;
23 import com.cloudera.impala.thrift.TAlterTableType;
24 import com.cloudera.impala.thrift.TTableName;
25 import com.google.common.base.Preconditions;
26 
31  protected final TableName newTableName_;
32 
33  // Set during analysis
34  protected String newDbName_;
35 
36  // True if we are renaming a table. False if we are renaming a view.
37  protected final boolean renameTable_;
38 
39  public AlterTableOrViewRenameStmt(TableName oldTableName, TableName newTableName,
40  boolean renameTable) {
41  super(oldTableName);
42  Preconditions.checkState(newTableName != null && !newTableName.isEmpty());
43  newTableName_ = newTableName;
44  renameTable_ = renameTable;
45  }
46 
47  public String getNewTbl() {
48  return newTableName_.getTbl();
49  }
50 
51  public String getNewDb() {
52  Preconditions.checkNotNull(newDbName_);
53  return newDbName_;
54  }
55 
56  @Override
57  public TAlterTableParams toThrift() {
58  TAlterTableParams params = super.toThrift();
59  params.setAlter_type(
60  (renameTable_) ? TAlterTableType.RENAME_TABLE : TAlterTableType.RENAME_VIEW);
61  TAlterTableOrViewRenameParams renameParams =
62  new TAlterTableOrViewRenameParams(new TTableName(getNewDb(), getNewTbl()));
63  params.setRename_params(renameParams);
64  return params;
65  }
66 
67  @Override
68  public void analyze(Analyzer analyzer) throws AnalysisException {
69  newTableName_.analyze();
70  table_ = analyzer.getTable(tableName_, Privilege.ALTER);
71  if (table_ instanceof View && renameTable_) {
72  throw new AnalysisException(String.format(
73  "ALTER TABLE not allowed on a view: %s", table_.getFullName()));
74  }
75  if (!(table_ instanceof View) && !renameTable_) {
76  throw new AnalysisException(String.format(
77  "ALTER VIEW not allowed on a table: %s", table_.getFullName()));
78  }
79  newDbName_ = analyzer.getTargetDbName(newTableName_);
80  if (analyzer.dbContainsTable(newDbName_, newTableName_.getTbl(), Privilege.CREATE)) {
82  String.format("%s.%s", newDbName_, getNewTbl()));
83  }
84  analyzer.addAccessEvent(new TAccessEvent(newDbName_ + "." + newTableName_.getTbl(),
85  table_.getCatalogObjectType(), Privilege.CREATE.toString()));
86  }
87 }
AlterTableOrViewRenameStmt(TableName oldTableName, TableName newTableName, boolean renameTable)
static final String TBL_ALREADY_EXISTS_ERROR_MSG
Definition: Analyzer.java:110