Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
AlterTableChangeColStmt.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.TAlterTableChangeColParams;
23 import com.cloudera.impala.thrift.TAlterTableParams;
24 import com.cloudera.impala.thrift.TAlterTableType;
25 import com.google.common.base.Preconditions;
26 
33  private final String colName_;
34  private final ColumnDef newColDef_;
35 
36  public AlterTableChangeColStmt(TableName tableName, String colName,
37  ColumnDef newColDef) {
38  super(tableName);
39  Preconditions.checkNotNull(newColDef);
40  Preconditions.checkState(colName != null && !colName.isEmpty());
41  colName_ = colName;
42  newColDef_ = newColDef;
43  }
44 
45  public String getColName() { return colName_; }
46  public ColumnDef getNewColDef() { return newColDef_; }
47 
48  @Override
49  public TAlterTableParams toThrift() {
50  TAlterTableParams params = super.toThrift();
51  params.setAlter_type(TAlterTableType.CHANGE_COLUMN);
52  TAlterTableChangeColParams colParams = new TAlterTableChangeColParams();
53  colParams.setCol_name(colName_);
54  colParams.setNew_col_def(newColDef_.toThrift());
55  params.setChange_col_params(colParams);
56  return params;
57  }
58 
59  @Override
60  public void analyze(Analyzer analyzer) throws AnalysisException {
61  super.analyze(analyzer);
62  Table t = getTargetTable();
63  // TODO: Support column-level DDL on HBase tables. Requires updating the column
64  // mappings along with the table columns.
65  if (t instanceof HBaseTable) {
66  throw new AnalysisException("ALTER TABLE CHANGE COLUMN not currently supported " +
67  "on HBase tables.");
68  }
69  String tableName = getDb() + "." + getTbl();
70 
71  // Verify there are no conflicts with partition columns.
72  for (FieldSchema fs: t.getMetaStoreTable().getPartitionKeys()) {
73  if (fs.getName().toLowerCase().equals(colName_.toLowerCase())) {
74  throw new AnalysisException("Cannot modify partition column: " + colName_);
75  }
76  if (fs.getName().toLowerCase().equals(newColDef_.getColName().toLowerCase())) {
77  throw new AnalysisException(
78  "Column name conflicts with existing partition column: " +
80  }
81  }
82 
83  // Verify the column being modified exists in the table
84  if (t.getColumn(colName_) == null) {
85  throw new AnalysisException(String.format(
86  "Column '%s' does not exist in table: %s", colName_, tableName));
87  }
88 
89  // Check that the new column def's name is valid.
90  newColDef_.analyze();
91  // Verify that if the column name is being changed, the new name doesn't conflict
92  // with an existing column.
93  if (!colName_.toLowerCase().equals(newColDef_.getColName().toLowerCase()) &&
94  t.getColumn(newColDef_.getColName()) != null) {
95  throw new AnalysisException("Column already exists: " + newColDef_.getColName());
96  }
97  }
98 }
AlterTableChangeColStmt(TableName tableName, String colName, ColumnDef newColDef)