Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TableName.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 java.util.List;
18 
19 import org.apache.hadoop.hive.metastore.MetaStoreUtils;
20 
22 import com.cloudera.impala.thrift.TTableName;
23 import com.google.common.base.Preconditions;
24 import com.google.common.collect.Lists;
25 
33 public class TableName {
34  private final String db_;
35  private final String tbl_;
36 
37  public TableName(String db, String tbl) {
38  super();
39  Preconditions.checkArgument(db == null || !db.isEmpty());
40  this.db_ = db;
41  Preconditions.checkNotNull(tbl);
42  this.tbl_ = tbl;
43  }
44 
45  public String getDb() { return db_; }
46  public String getTbl() { return tbl_; }
47  public boolean isEmpty() { return tbl_.isEmpty(); }
48 
52  public void analyze() throws AnalysisException {
53  if (db_ != null) {
54  if (!MetaStoreUtils.validateName(db_)) {
55  throw new AnalysisException("Invalid database name: " + db_);
56  }
57  }
58  Preconditions.checkNotNull(tbl_);
59  if (!MetaStoreUtils.validateName(tbl_)) {
60  throw new AnalysisException("Invalid table/view name: " + tbl_);
61  }
62  }
63 
68  public boolean isFullyQualified() {
69  return db_ != null && !db_.isEmpty() && !tbl_.isEmpty();
70  }
71 
72  public String toSql() {
73  // Enclose the database and/or table name in quotes if Hive cannot parse them
74  // without quotes. This is needed for view compatibility between Impala and Hive.
75  if (db_ == null) {
76  return ToSqlUtils.getIdentSql(tbl_);
77  } else {
78  return ToSqlUtils.getIdentSql(db_) + "." + ToSqlUtils.getIdentSql(tbl_);
79  }
80  }
81 
82  @Override
83  public String toString() {
84  if (db_ == null) {
85  return tbl_;
86  } else {
87  return db_ + "." + tbl_;
88  }
89  }
90 
91  public List<String> toPath() {
92  List<String> result = Lists.newArrayListWithCapacity(2);
93  if (db_ != null) result.add(db_);
94  result.add(tbl_);
95  return result;
96  }
97 
98  public static TableName fromThrift(TTableName tableName) {
99  return new TableName(tableName.getDb_name(), tableName.getTable_name());
100  }
101 
102  public TTableName toThrift() { return new TTableName(db_, tbl_); }
103 
108  @Override
109  public boolean equals(Object anObject) {
110  if (anObject instanceof TableName) {
111  return toString().toLowerCase().equals(anObject.toString().toLowerCase());
112  }
113  return false;
114  }
115 
116  @Override
117  public int hashCode() {
118  return toString().toLowerCase().hashCode();
119  }
120 }
static TableName fromThrift(TTableName tableName)
Definition: TableName.java:98
boolean equals(Object anObject)
Definition: TableName.java:109
TableName(String db, String tbl)
Definition: TableName.java:37
static String getIdentSql(String ident)
Definition: ToSqlUtils.java:66