Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
AlterTableDropColStmt.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.hive.metastore.api.FieldSchema;
18 
22 import com.cloudera.impala.thrift.TAlterTableDropColParams;
23 import com.cloudera.impala.thrift.TAlterTableParams;
24 import com.cloudera.impala.thrift.TAlterTableType;
25 import com.google.common.base.Preconditions;
26 
32 public class AlterTableDropColStmt extends AlterTableStmt {
33  private final String colName_;
34 
35  public AlterTableDropColStmt(TableName tableName, String colName) {
36  super(tableName);
37  Preconditions.checkState(colName != null && !colName.isEmpty());
38  colName_ = colName;
39  }
40 
41  public String getColName() { return colName_; }
42 
43  @Override
44  public TAlterTableParams toThrift() {
45  TAlterTableParams params = super.toThrift();
46  params.setAlter_type(TAlterTableType.DROP_COLUMN);
47  TAlterTableDropColParams dropColParams = new TAlterTableDropColParams(colName_);
48  params.setDrop_col_params(dropColParams);
49  return params;
50  }
51 
52  @Override
53  public void analyze(Analyzer analyzer) throws AnalysisException {
54  super.analyze(analyzer);
55  Table t = getTargetTable();
56  // TODO: Support column-level DDL on HBase tables. Requires updating the column
57  // mappings along with the table columns.
58  if (t instanceof HBaseTable) {
59  throw new AnalysisException("ALTER TABLE DROP COLUMN not currently supported " +
60  "on HBase tables.");
61  }
62  String tableName = getDb() + "." + getTbl();
63 
64  for (FieldSchema fs: t.getMetaStoreTable().getPartitionKeys()) {
65  if (fs.getName().toLowerCase().equals(colName_.toLowerCase())) {
66  throw new AnalysisException("Cannot drop partition column: " + fs.getName());
67  }
68  }
69 
70  if (t.getColumns().size() - t.getMetaStoreTable().getPartitionKeysSize() <= 1) {
71  throw new AnalysisException(String.format(
72  "Cannot drop column '%s' from %s. Tables must contain at least 1 column.",
73  colName_, tableName));
74  }
75 
76  if (t.getColumn(colName_) == null) {
77  throw new AnalysisException(String.format(
78  "Column '%s' does not exist in table: %s", colName_, tableName));
79  }
80  }
81 }
AlterTableDropColStmt(TableName tableName, String colName)