Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
types.h
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 #ifndef IMPALA_RUNTIME_TYPE_H
17 #define IMPALA_RUNTIME_TYPE_H
18 
19 #include <string>
20 
21 #include "common/logging.h"
22 #include "gen-cpp/Types_types.h" // for TPrimitiveType
23 #include "gen-cpp/TCLIService_types.h" // for HiveServer2 Type
24 
25 namespace impala {
26 
39  TYPE_DATE, // Not implemented
40  TYPE_DATETIME, // Not implemented
41  TYPE_BINARY, // Not implemented
43 
49 };
50 
51 PrimitiveType ThriftToType(TPrimitiveType::type ttype);
52 TPrimitiveType::type ToThrift(PrimitiveType ptype);
53 std::string TypeToString(PrimitiveType t);
54 std::string TypeToOdbcString(PrimitiveType t);
55 
59 struct ColumnType {
62  int len;
63  static const int MAX_VARCHAR_LENGTH = 65355;
64  static const int MAX_CHAR_LENGTH = 255;
65  static const int MAX_CHAR_INLINE_LENGTH = 128;
66 
69 
71  static const int MAX_PRECISION = 38;
72  static const int MAX_SCALE = MAX_PRECISION;
73 
75  static const int MAX_DECIMAL4_PRECISION = 9;
77  static const int MAX_DECIMAL8_PRECISION = 18;
78 
80  : type(type), len(-1), precision(-1), scale(-1) {
81  DCHECK_NE(type, TYPE_CHAR);
82  DCHECK_NE(type, TYPE_DECIMAL);
83  }
84 
86  DCHECK_GE(len, 1);
87  DCHECK_LE(len, MAX_CHAR_LENGTH);
88  ColumnType ret;
89  ret.type = TYPE_CHAR;
90  ret.len = len;
91  return ret;
92  }
93 
95  DCHECK_GE(len, 1);
96  DCHECK_LE(len, MAX_VARCHAR_LENGTH);
97  ColumnType ret;
98  ret.type = TYPE_VARCHAR;
99  ret.len = len;
100  return ret;
101  }
102 
104  DCHECK_LE(precision, MAX_PRECISION);
105  DCHECK_LE(scale, MAX_SCALE);
106  DCHECK_GE(precision, 0);
107  DCHECK_LE(scale, precision);
108  ColumnType ret;
109  ret.type = TYPE_DECIMAL;
110  ret.precision = precision;
111  ret.scale = scale;
112  return ret;
113  }
114 
115  ColumnType(const TColumnType& t) {
116  len = precision = scale = -1;
117  DCHECK_EQ(1, t.types.size());
118  const TTypeNode& node = t.types[0];
119  DCHECK(node.__isset.scalar_type);
120  const TScalarType scalar_type = node.scalar_type;
121  type = ThriftToType(scalar_type.type);
122  if (type == TYPE_CHAR || type == TYPE_VARCHAR) {
123  DCHECK(scalar_type.__isset.len);
124  len = scalar_type.len;
125  } else if (type == TYPE_DECIMAL) {
126  DCHECK(scalar_type.__isset.precision);
127  DCHECK(scalar_type.__isset.scale);
128  precision = scalar_type.precision;
129  scale = scalar_type.scale;
130  } else {
131  DCHECK_NE(type, TYPE_DECIMAL);
132  DCHECK_NE(type, TYPE_CHAR);
133  }
134  }
135 
136  bool operator==(const ColumnType& o) const {
137  if (type != o.type) return false;
138  if (type == TYPE_CHAR) return len == o.len;
139  if (type == TYPE_DECIMAL) return precision == o.precision && scale == o.scale;
140  return true;
141  }
142 
143  bool operator!=(const ColumnType& other) const {
144  return !(*this == other);
145  }
146 
147  TColumnType ToThrift() const {
148  // TODO: Decimal and complex types.
149  TColumnType thrift_type;
150  thrift_type.types.push_back(TTypeNode());
151  TTypeNode& node = thrift_type.types.back();
152  node.type = TTypeNodeType::SCALAR;
153  node.__set_scalar_type(TScalarType());
154  TScalarType& scalar_type = node.scalar_type;
155  scalar_type.__set_type(impala::ToThrift(type));
156  if (type == TYPE_CHAR || type == TYPE_VARCHAR) {
157  DCHECK_NE(len, -1);
158  scalar_type.__set_len(len);
159  } else if (type == TYPE_DECIMAL) {
160  DCHECK_NE(precision, -1);
161  DCHECK_NE(scale, -1);
162  scalar_type.__set_precision(precision);
163  scalar_type.__set_scale(scale);
164  }
165  return thrift_type;
166  }
167 
168  inline bool IsStringType() const {
169  return type == TYPE_STRING || type == TYPE_VARCHAR || type == TYPE_CHAR;
170  }
171 
172  inline bool IsVarLen() const {
173  return type == TYPE_STRING || type == TYPE_VARCHAR ||
175  }
176 
178  inline int GetByteSize() const {
179  switch (type) {
180  case TYPE_STRING:
181  case TYPE_VARCHAR:
182  return 0;
183  case TYPE_CHAR:
184  if (IsVarLen()) return 0;
185  return len;
186  case TYPE_NULL:
187  case TYPE_BOOLEAN:
188  case TYPE_TINYINT:
189  return 1;
190  case TYPE_SMALLINT:
191  return 2;
192  case TYPE_INT:
193  case TYPE_FLOAT:
194  return 4;
195  case TYPE_BIGINT:
196  case TYPE_DOUBLE:
197  return 8;
198  case TYPE_TIMESTAMP:
199  // This is the size of the slot, the actual size of the data is 12.
200  return 16;
201  case TYPE_DECIMAL:
203  case TYPE_DATE:
204  case INVALID_TYPE:
205  default:
206  DCHECK(false);
207  }
208  return 0;
209  }
210 
212  inline int GetSlotSize() const {
213  switch (type) {
214  case TYPE_STRING:
215  case TYPE_VARCHAR:
216  return 16;
217  case TYPE_CHAR:
218  if (IsVarLen()) return 16;
219  return len;
220  default:
221  return GetByteSize();
222  }
223  }
224 
225  static inline int GetDecimalByteSize(int precision) {
226  DCHECK_GT(precision, 0);
227  if (precision <= MAX_DECIMAL4_PRECISION) return 4;
228  if (precision <= MAX_DECIMAL8_PRECISION) return 8;
229  return 16;
230  }
231 
232  apache::hive::service::cli::thrift::TTypeEntry ToHs2Type() const;
233  std::string DebugString() const;
234 };
235 
236 std::ostream& operator<<(std::ostream& os, const ColumnType& type);
237 
238 }
239 
240 #endif
static const int MAX_CHAR_LENGTH
Definition: types.h:64
bool IsVarLen() const
Definition: types.h:172
static const int MAX_CHAR_INLINE_LENGTH
Definition: types.h:65
ColumnType(PrimitiveType type=INVALID_TYPE)
Definition: types.h:79
static const int MAX_DECIMAL8_PRECISION
The maximum precision representable by a 8-byte decimal (Decimal8Value)
Definition: types.h:77
int precision
Only set if type == TYPE_DECIMAL.
Definition: types.h:68
static int GetDecimalByteSize(int precision)
Definition: types.h:225
PrimitiveType ThriftToType(TPrimitiveType::type ttype)
Definition: types.cc:27
bool operator!=(const ColumnType &other) const
Definition: types.h:143
int GetSlotSize() const
Returns the size of a slot for this type.
Definition: types.h:212
string TypeToOdbcString(PrimitiveType t)
Definition: types.cc:96
bool operator==(const ColumnType &o) const
Definition: types.h:136
string TypeToString(PrimitiveType t)
Definition: types.cc:73
bool IsStringType() const
Definition: types.h:168
std::string DebugString() const
Definition: types.cc:194
PrimitiveType type
Definition: types.h:60
static ColumnType CreateVarcharType(int len)
Definition: types.h:94
PrimitiveType
Definition: types.h:27
static ColumnType CreateCharType(int len)
Definition: types.h:85
TPrimitiveType::type ToThrift(PrimitiveType ptype)
Definition: types.cc:50
static const int MAX_VARCHAR_LENGTH
Definition: types.h:63
int GetByteSize() const
Returns the byte size of this type. Returns 0 for variable length types.
Definition: types.h:178
apache::hive::service::cli::thrift::TTypeEntry ToHs2Type() const
Definition: types.cc:120
int len
Only set if type == TYPE_CHAR or type == TYPE_VARCHAR.
Definition: types.h:62
static const int MAX_DECIMAL4_PRECISION
The maximum precision representable by a 4-byte decimal (Decimal4Value)
Definition: types.h:75
ColumnType(const TColumnType &t)
Definition: types.h:115
static const int MAX_SCALE
Definition: types.h:72
TColumnType ToThrift() const
Definition: types.h:147
static ColumnType CreateDecimalType(int precision, int scale)
Definition: types.h:103
ostream & operator<<(ostream &os, const map< TNetworkAddress, llama::TAllocatedResource > &resources)
static const int MAX_PRECISION
Must be kept in sync with FE's max precision/scale.
Definition: types.h:71