Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
CreateTableLikeStmt.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 org.apache.hadoop.fs.permission.FsAction;
18 
21 import com.cloudera.impala.thrift.TAccessEvent;
22 import com.cloudera.impala.thrift.TCatalogObjectType;
23 import com.cloudera.impala.thrift.TCreateTableLikeParams;
24 import com.cloudera.impala.thrift.THdfsFileFormat;
25 import com.cloudera.impala.thrift.TTableName;
26 import com.google.common.base.Preconditions;
27 
32 public class CreateTableLikeStmt extends StatementBase {
33  private final TableName tableName_;
34  private final TableName srcTableName_;
35  private final boolean isExternal_;
36  private final String comment_;
37  private final THdfsFileFormat fileFormat_;
38  private final HdfsUri location_;
39  private final boolean ifNotExists_;
40 
41  // Set during analysis
42  private String dbName_;
43  private String srcDbName_;
44  private String owner_;
45 
56  public CreateTableLikeStmt(TableName tableName, TableName srcTableName,
57  boolean isExternal, String comment, THdfsFileFormat fileFormat, HdfsUri location,
58  boolean ifNotExists) {
59  Preconditions.checkNotNull(tableName);
60  Preconditions.checkNotNull(srcTableName);
61  this.tableName_ = tableName;
62  this.srcTableName_ = srcTableName;
63  this.isExternal_ = isExternal;
64  this.comment_ = comment;
65  this.fileFormat_ = fileFormat;
66  this.location_ = location;
67  this.ifNotExists_ = ifNotExists;
68  }
69 
70  public String getTbl() { return tableName_.getTbl(); }
71  public String getSrcTbl() { return srcTableName_.getTbl(); }
72  public boolean isExternal() { return isExternal_; }
73  public boolean getIfNotExists() { return ifNotExists_; }
74  public String getComment() { return comment_; }
75  public THdfsFileFormat getFileFormat() { return fileFormat_; }
76  public HdfsUri getLocation() { return location_; }
77 
82  public String getDb() {
83  Preconditions.checkNotNull(dbName_);
84  return dbName_;
85  }
86 
91  public String getSrcDb() {
92  Preconditions.checkNotNull(srcDbName_);
93  return srcDbName_;
94  }
95 
96  public String getOwner() {
97  Preconditions.checkNotNull(owner_);
98  return owner_;
99  }
100 
101  @Override
102  public String toSql() {
103  StringBuilder sb = new StringBuilder("CREATE ");
104  if (isExternal_) sb.append("EXTERNAL ");
105  sb.append("TABLE ");
106  if (ifNotExists_) sb.append("IF NOT EXISTS ");
107  if (tableName_.getDb() != null) sb.append(tableName_.getDb() + ".");
108  sb.append(tableName_.getTbl() + " LIKE ");
109  if (srcTableName_.getDb() != null) sb.append(srcTableName_.getDb() + ".");
110  sb.append(srcTableName_.getTbl());
111  if (comment_ != null) sb.append(" COMMENT '" + comment_ + "'");
112  if (fileFormat_ != null) sb.append(" STORED AS " + fileFormat_);
113  if (location_ != null) sb.append(" LOCATION '" + location_ + "'");
114  return sb.toString();
115  }
116 
117  public TCreateTableLikeParams toThrift() {
118  TCreateTableLikeParams params = new TCreateTableLikeParams();
119  params.setTable_name(new TTableName(getDb(), getTbl()));
120  params.setSrc_table_name(new TTableName(getSrcDb(), getSrcTbl()));
121  params.setOwner(getOwner());
122  params.setIs_external(isExternal());
123  params.setComment(comment_);
124  if (fileFormat_ != null) params.setFile_format(fileFormat_);
125  params.setLocation(location_ == null ? null : location_.toString());
126  params.setIf_not_exists(getIfNotExists());
127  return params;
128  }
129 
130  @Override
131  public void analyze(Analyzer analyzer) throws AnalysisException {
132  Preconditions.checkState(tableName_ != null && !tableName_.isEmpty());
133  Preconditions.checkState(srcTableName_ != null && !srcTableName_.isEmpty());
134  // Make sure the source table exists and the user has permission to access it.
135  srcDbName_ = analyzer
137  .getDb().getName();
138  tableName_.analyze();
139  dbName_ = analyzer.getTargetDbName(tableName_);
140  owner_ = analyzer.getUser().getName();
141 
142  if (analyzer.dbContainsTable(dbName_, tableName_.getTbl(), Privilege.CREATE) &&
143  !ifNotExists_) {
145  String.format("%s.%s", dbName_, getTbl()));
146  }
147  analyzer.addAccessEvent(new TAccessEvent(dbName_ + "." + tableName_.getTbl(),
148  TCatalogObjectType.TABLE, Privilege.CREATE.toString()));
149 
150  if (location_ != null) {
151  location_.analyze(analyzer, Privilege.ALL, FsAction.READ_WRITE);
152  }
153  }
154 }
CreateTableLikeStmt(TableName tableName, TableName srcTableName, boolean isExternal, String comment, THdfsFileFormat fileFormat, HdfsUri location, boolean ifNotExists)
static final String TBL_ALREADY_EXISTS_ERROR_MSG
Definition: Analyzer.java:110