Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
catalog-util.cc
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 
16 #include <boost/algorithm/string.hpp>
17 #include <sstream>
18 
19 #include "catalog/catalog-util.h"
20 #include "common/status.h"
21 #include "util/debug-util.h"
22 
23 #include "common/names.h"
24 
25 using boost::algorithm::to_upper_copy;
26 
27 namespace impala {
28 
29 TCatalogObjectType::type TCatalogObjectTypeFromName(const string& name) {
30  const string& upper = to_upper_copy(name);
31  if (upper == "DATABASE") {
32  return TCatalogObjectType::DATABASE;
33  } else if (upper == "TABLE") {
34  return TCatalogObjectType::TABLE;
35  } else if (upper == "VIEW") {
36  return TCatalogObjectType::VIEW;
37  } else if (upper == "FUNCTION") {
38  return TCatalogObjectType::FUNCTION;
39  } else if (upper == "CATALOG") {
40  return TCatalogObjectType::CATALOG;
41  } else if (upper == "DATA_SOURCE") {
42  return TCatalogObjectType::DATA_SOURCE;
43  } else if (upper == "HDFS_CACHE_POOL") {
44  return TCatalogObjectType::HDFS_CACHE_POOL;
45  } else if (upper == "ROLE") {
46  return TCatalogObjectType::ROLE;
47  } else if (upper == "PRIVILEGE") {
48  return TCatalogObjectType::PRIVILEGE;
49  }
51 }
52 
54  TCatalogObject* catalog_object) {
55  // Reconstruct the object based only on the key.
56  size_t pos = key.find(":");
57  if (pos == string::npos || pos >= key.size() - 1) {
58  stringstream error_msg;
59  error_msg << "Invalid topic entry key format: " << key;
60  return Status(error_msg.str());
61  }
62 
63  TCatalogObjectType::type object_type = TCatalogObjectTypeFromName(key.substr(0, pos));
64  string object_name = key.substr(pos + 1);
65  return TCatalogObjectFromObjectName(object_type, object_name, catalog_object);
66 }
67 
68 Status TCatalogObjectFromObjectName(const TCatalogObjectType::type& object_type,
69  const string& object_name, TCatalogObject* catalog_object) {
70  switch (object_type) {
71  case TCatalogObjectType::DATABASE:
72  catalog_object->__set_type(object_type);
73  catalog_object->__set_db(TDatabase());
74  catalog_object->db.__set_db_name(object_name);
75  break;
76  case TCatalogObjectType::TABLE:
77  case TCatalogObjectType::VIEW: {
78  catalog_object->__set_type(object_type);
79  catalog_object->__set_table(TTable());
80  // Parse what should be a fully qualified table name
81  int pos = object_name.find(".");
82  if (pos == string::npos || pos >= object_name.size() - 1) {
83  stringstream error_msg;
84  error_msg << "Invalid table name: " << object_name;
85  return Status(error_msg.str());
86  }
87  catalog_object->table.__set_db_name(object_name.substr(0, pos));
88  catalog_object->table.__set_tbl_name(object_name.substr(pos + 1));
89  break;
90  }
91  case TCatalogObjectType::FUNCTION: {
92  // The key looks like: <db>.fn(<args>). We need to parse out the
93  // db, fn and signature.
94  catalog_object->__set_type(object_type);
95  catalog_object->__set_fn(TFunction());
96  int dot = object_name.find(".");
97  int paren = object_name.find("(");
98  if (dot == string::npos || dot >= object_name.size() - 1 ||
99  paren == string::npos || paren >= object_name.size() - 1 ||
100  paren <= dot) {
101  stringstream error_msg;
102  error_msg << "Invalid function name: " << object_name;
103  return Status(error_msg.str());
104  }
105  catalog_object->fn.name.__set_db_name(object_name.substr(0, dot));
106  catalog_object->fn.name.__set_function_name(
107  object_name.substr(dot + 1, paren - dot - 1));
108  catalog_object->fn.__set_signature(object_name.substr(dot + 1));
109  break;
110  }
111  case TCatalogObjectType::DATA_SOURCE:
112  catalog_object->__set_type(object_type);
113  catalog_object->__set_data_source(TDataSource());
114  catalog_object->data_source.__set_name(object_name);
115  break;
116  case TCatalogObjectType::HDFS_CACHE_POOL:
117  catalog_object->__set_type(object_type);
118  catalog_object->__set_cache_pool(THdfsCachePool());
119  catalog_object->cache_pool.__set_pool_name(object_name);
120  break;
121  case TCatalogObjectType::ROLE:
122  catalog_object->__set_type(object_type);
123  catalog_object->__set_role(TRole());
124  catalog_object->role.__set_role_name(object_name);
125  break;
126  case TCatalogObjectType::PRIVILEGE: {
127  int pos = object_name.find(".");
128  if (pos == string::npos || pos >= object_name.size() - 1) {
129  stringstream error_msg;
130  error_msg << "Invalid privilege name: " << object_name;
131  return Status(error_msg.str());
132  }
133  catalog_object->__set_type(object_type);
134  catalog_object->__set_privilege(TPrivilege());
135  catalog_object->privilege.__set_role_id(atoi(object_name.substr(0, pos).c_str()));
136  catalog_object->privilege.__set_privilege_name(object_name.substr(pos + 1));
137  break;
138  }
139  case TCatalogObjectType::CATALOG:
141  default:
142  stringstream error_msg;
143  error_msg << "Unexpected object type: " << object_type;
144  return Status(error_msg.str());
145  }
146  return Status::OK;
147 }
148 
149 string TCatalogObjectToEntryKey(const TCatalogObject& catalog_object) {
150  // The key format is: "TCatalogObjectType:<fully qualified object name>"
151  stringstream entry_key;
152  entry_key << PrintTCatalogObjectType(catalog_object.type) << ":";
153  switch (catalog_object.type) {
154  case TCatalogObjectType::DATABASE:
155  entry_key << catalog_object.db.db_name;
156  break;
157  case TCatalogObjectType::TABLE:
158  case TCatalogObjectType::VIEW:
159  entry_key << catalog_object.table.db_name << "." << catalog_object.table.tbl_name;
160  break;
161  case TCatalogObjectType::FUNCTION:
162  entry_key << catalog_object.fn.name.db_name << "."
163  << catalog_object.fn.signature;
164  break;
165  case TCatalogObjectType::CATALOG:
166  entry_key << catalog_object.catalog.catalog_service_id;
167  break;
168  case TCatalogObjectType::DATA_SOURCE:
169  entry_key << catalog_object.data_source.name;
170  break;
171  case TCatalogObjectType::HDFS_CACHE_POOL:
172  entry_key << catalog_object.cache_pool.pool_name;
173  break;
174  case TCatalogObjectType::ROLE:
175  entry_key << catalog_object.role.role_name;
176  break;
177  case TCatalogObjectType::PRIVILEGE:
178  entry_key << catalog_object.privilege.role_id << "."
179  << catalog_object.privilege.privilege_name;
180  break;
181  default:
182  break;
183  }
184  return entry_key.str();
185 }
186 
187 
188 }
Status TCatalogObjectFromObjectName(const TCatalogObjectType::type &object_type, const string &object_name, TCatalogObject *catalog_object)
Definition: catalog-util.cc:68
string TCatalogObjectToEntryKey(const TCatalogObject &catalog_object)
std::string PrintTCatalogObjectType(const TCatalogObjectType::type &type)
TCatalogObjectType::type TCatalogObjectTypeFromName(const string &name)
Definition: catalog-util.cc:29
Status TCatalogObjectFromEntryKey(const string &key, TCatalogObject *catalog_object)
Definition: catalog-util.cc:53
static const Status OK
Definition: status.h:87
string name
Definition: cpu-info.cc:50