Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ColumnDef.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.MetaStoreUtils;
18 
21 import com.cloudera.impala.thrift.TColumn;
22 import com.google.common.base.Preconditions;
23 
32 public class ColumnDef {
33  private final String colName_;
34  private final String comment_;
35 
36  // Required in CREATE/ALTER TABLE stmts. Set to NULL in CREATE/ALTER VIEW stmts,
37  // for which we setType() after analyzing the defining view definition stmt.
38  private final TypeDef typeDef_;
39  private Type type_;
40 
41  public ColumnDef(String colName, TypeDef typeDef, String comment) {
42  colName_ = colName;
43  typeDef_ = typeDef;
44  comment_ = comment;
45  }
46 
47  public void setType(Type type) { type_ = type; }
48  public Type getType() { return type_; }
49  public TypeDef getTypeDef() { return typeDef_; }
50  public String getColName() { return colName_; }
51  public String getComment() { return comment_; }
52 
53  public void analyze() throws AnalysisException {
54  // Check whether the column name meets the Metastore's requirements.
55  if (!MetaStoreUtils.validateName(colName_)) {
56  throw new AnalysisException("Invalid column/field name: " + colName_);
57  }
58  if (typeDef_ != null) {
59  typeDef_.analyze(null);
60  type_ = typeDef_.getType();
61  }
62  Preconditions.checkNotNull(type_);
63  Preconditions.checkState(type_.isValid());
64  }
65 
66  @Override
67  public String toString() {
68  StringBuilder sb = new StringBuilder(colName_);
69  if (type_ != null) {
70  sb.append(" " + type_.toString());
71  } else {
72  sb.append(" " + typeDef_.toString());
73  }
74  if (comment_ != null) sb.append(String.format(" COMMENT '%s'", comment_));
75  return sb.toString();
76  }
77 
78  public TColumn toThrift() {
79  TColumn col = new TColumn(new TColumn(getColName(), type_.toThrift()));
80  col.setComment(getComment());
81  return col;
82  }
83 }
ColumnDef(String colName, TypeDef typeDef, String comment)
Definition: ColumnDef.java:41