Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
types.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 #include "runtime/types.h"
16 #include <ostream>
17 #include <sstream>
18 
19 #include "gen-cpp/TCLIService_constants.h"
20 
21 #include "common/names.h"
22 
23 using namespace apache::hive::service::cli::thrift;
24 
25 namespace impala {
26 
27 PrimitiveType ThriftToType(TPrimitiveType::type ttype) {
28  switch (ttype) {
30  case TPrimitiveType::NULL_TYPE: return TYPE_NULL;
31  case TPrimitiveType::BOOLEAN: return TYPE_BOOLEAN;
32  case TPrimitiveType::TINYINT: return TYPE_TINYINT;
33  case TPrimitiveType::SMALLINT: return TYPE_SMALLINT;
34  case TPrimitiveType::INT: return TYPE_INT;
35  case TPrimitiveType::BIGINT: return TYPE_BIGINT;
36  case TPrimitiveType::FLOAT: return TYPE_FLOAT;
37  case TPrimitiveType::DOUBLE: return TYPE_DOUBLE;
38  case TPrimitiveType::DATE: return TYPE_DATE;
39  case TPrimitiveType::DATETIME: return TYPE_DATETIME;
40  case TPrimitiveType::TIMESTAMP: return TYPE_TIMESTAMP;
41  case TPrimitiveType::STRING: return TYPE_STRING;
42  case TPrimitiveType::VARCHAR: return TYPE_VARCHAR;
43  case TPrimitiveType::BINARY: return TYPE_BINARY;
44  case TPrimitiveType::DECIMAL: return TYPE_DECIMAL;
45  case TPrimitiveType::CHAR: return TYPE_CHAR;
46  default: return INVALID_TYPE;
47  }
48 }
49 
50 TPrimitiveType::type ToThrift(PrimitiveType ptype) {
51  switch (ptype) {
53  case TYPE_NULL: return TPrimitiveType::NULL_TYPE;
54  case TYPE_BOOLEAN: return TPrimitiveType::BOOLEAN;
55  case TYPE_TINYINT: return TPrimitiveType::TINYINT;
56  case TYPE_SMALLINT: return TPrimitiveType::SMALLINT;
57  case TYPE_INT: return TPrimitiveType::INT;
58  case TYPE_BIGINT: return TPrimitiveType::BIGINT;
59  case TYPE_FLOAT: return TPrimitiveType::FLOAT;
60  case TYPE_DOUBLE: return TPrimitiveType::DOUBLE;
61  case TYPE_DATE: return TPrimitiveType::DATE;
62  case TYPE_DATETIME: return TPrimitiveType::DATETIME;
63  case TYPE_TIMESTAMP: return TPrimitiveType::TIMESTAMP;
64  case TYPE_STRING: return TPrimitiveType::STRING;
65  case TYPE_VARCHAR: return TPrimitiveType::VARCHAR;
66  case TYPE_BINARY: return TPrimitiveType::BINARY;
67  case TYPE_DECIMAL: return TPrimitiveType::DECIMAL;
68  case TYPE_CHAR: return TPrimitiveType::CHAR;
69  default: return TPrimitiveType::INVALID_TYPE;
70  }
71 }
72 
74  switch (t) {
75  case INVALID_TYPE: return "INVALID";
76  case TYPE_NULL: return "NULL";
77  case TYPE_BOOLEAN: return "BOOLEAN";
78  case TYPE_TINYINT: return "TINYINT";
79  case TYPE_SMALLINT: return "SMALLINT";
80  case TYPE_INT: return "INT";
81  case TYPE_BIGINT: return "BIGINT";
82  case TYPE_FLOAT: return "FLOAT";
83  case TYPE_DOUBLE: return "DOUBLE";
84  case TYPE_DATE: return "DATE";
85  case TYPE_DATETIME: return "DATETIME";
86  case TYPE_TIMESTAMP: return "TIMESTAMP";
87  case TYPE_STRING: return "STRING";
88  case TYPE_VARCHAR: return "VARCHAR";
89  case TYPE_BINARY: return "BINARY";
90  case TYPE_DECIMAL: return "DECIMAL";
91  case TYPE_CHAR: return "CHAR";
92  };
93  return "";
94 }
95 
97  // ODBC driver requires types in lower case
98  switch (t) {
99  case INVALID_TYPE: return "invalid";
100  case TYPE_NULL: return "null";
101  case TYPE_BOOLEAN: return "boolean";
102  case TYPE_TINYINT: return "tinyint";
103  case TYPE_SMALLINT: return "smallint";
104  case TYPE_INT: return "int";
105  case TYPE_BIGINT: return "bigint";
106  case TYPE_FLOAT: return "float";
107  case TYPE_DOUBLE: return "double";
108  case TYPE_DATE: return "date";
109  case TYPE_DATETIME: return "datetime";
110  case TYPE_TIMESTAMP: return "timestamp";
111  case TYPE_STRING: return "string";
112  case TYPE_VARCHAR: return "string";
113  case TYPE_BINARY: return "binary";
114  case TYPE_DECIMAL: return "decimal";
115  case TYPE_CHAR: return "char";
116  };
117  return "unknown";
118 }
119 
120 TTypeEntry ColumnType::ToHs2Type() const {
121  TPrimitiveTypeEntry type_entry;
122  switch (type) {
123  // Map NULL_TYPE to BOOLEAN, otherwise Hive's JDBC driver won't
124  // work for queries like "SELECT NULL" (IMPALA-914).
125  case TYPE_NULL:
126  type_entry.__set_type(TTypeId::BOOLEAN_TYPE);
127  break;
128  case TYPE_BOOLEAN:
129  type_entry.__set_type(TTypeId::BOOLEAN_TYPE);
130  break;
131  case TYPE_TINYINT:
132  type_entry.__set_type(TTypeId::TINYINT_TYPE);
133  break;
134  case TYPE_SMALLINT:
135  type_entry.__set_type(TTypeId::SMALLINT_TYPE);
136  break;
137  case TYPE_INT:
138  type_entry.__set_type(TTypeId::INT_TYPE);
139  break;
140  case TYPE_BIGINT:
141  type_entry.__set_type(TTypeId::BIGINT_TYPE);
142  break;
143  case TYPE_FLOAT:
144  type_entry.__set_type(TTypeId::FLOAT_TYPE);
145  break;
146  case TYPE_DOUBLE:
147  type_entry.__set_type(TTypeId::DOUBLE_TYPE);
148  break;
149  case TYPE_TIMESTAMP:
150  type_entry.__set_type(TTypeId::TIMESTAMP_TYPE);
151  break;
152  case TYPE_STRING:
153  type_entry.__set_type(TTypeId::STRING_TYPE);
154  break;
155  case TYPE_BINARY:
156  type_entry.__set_type(TTypeId::BINARY_TYPE);
157  break;
158  case TYPE_DECIMAL: {
159  TTypeQualifierValue tprecision;
160  tprecision.__set_i32Value(precision);
161  TTypeQualifierValue tscale;
162  tscale.__set_i32Value(scale);
163 
164  TTypeQualifiers type_quals;
165  type_quals.qualifiers[g_TCLIService_constants.PRECISION] = tprecision;
166  type_quals.qualifiers[g_TCLIService_constants.SCALE] = tscale;
167  type_entry.__set_typeQualifiers(type_quals);
168  type_entry.__set_type(TTypeId::DECIMAL_TYPE);
169  break;
170  }
171  case TYPE_CHAR:
172  case TYPE_VARCHAR: {
173  TTypeQualifierValue tmax_len;
174  tmax_len.__set_i32Value(len);
175 
176  TTypeQualifiers type_quals;
177  type_quals.qualifiers[g_TCLIService_constants.CHARACTER_MAXIMUM_LENGTH] = tmax_len;
178  type_entry.__set_typeQualifiers(type_quals);
179  type_entry.__set_type(
180  (type == TYPE_CHAR) ? TTypeId::CHAR_TYPE : TTypeId::VARCHAR_TYPE);
181  break;
182  }
183  default:
184  // HiveServer2 does not have a type for invalid, date and datetime.
185  DCHECK(false) << "bad TypeToTValueType() type: " << DebugString();
186  type_entry.__set_type(TTypeId::STRING_TYPE);
187  };
188 
189  TTypeEntry result;
190  result.__set_primitiveEntry(type_entry);
191  return result;
192 }
193 
194 string ColumnType::DebugString() const {
195  stringstream ss;
196  switch (type) {
197  case TYPE_CHAR:
198  ss << "CHAR(" << len << ")";
199  return ss.str();
200  case TYPE_DECIMAL:
201  ss << "DECIMAL(" << precision << ", " << scale << ")";
202  return ss.str();
203  default:
204  return TypeToString(type);
205  }
206 }
207 
208 ostream& operator<<(ostream& os, const ColumnType& type) {
209  os << type.DebugString();
210  return os;
211 }
212 
213 }
PrimitiveType ThriftToType(TPrimitiveType::type ttype)
Definition: types.cc:27
string TypeToOdbcString(PrimitiveType t)
Definition: types.cc:96
string TypeToString(PrimitiveType t)
Definition: types.cc:73
std::string DebugString() const
Definition: types.cc:194
PrimitiveType
Definition: types.h:27
TPrimitiveType::type ToThrift(PrimitiveType ptype)
Definition: types.cc:50
std::string DebugString(const T &val)
Definition: udf-debug.h:27
ostream & operator<<(ostream &os, const ColumnType &type)
Definition: types.cc:208