Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
expr-value.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 #ifndef IMPALA_EXPRS_EXPR_VALUE_H
16 #define IMPALA_EXPRS_EXPR_VALUE_H
17 
18 #include "runtime/decimal-value.h"
19 #include "runtime/string-value.h"
21 
22 namespace impala {
23 
25 struct ExprValue {
26  bool bool_val;
27  int8_t tinyint_val;
28  int16_t smallint_val;
29  int32_t int_val;
30  int64_t bigint_val;
31  float float_val;
32  double double_val;
38 
40  : bool_val(false),
41  tinyint_val(0),
42  smallint_val(0),
43  int_val(0),
44  bigint_val(0),
45  float_val(0.0),
46  double_val(0.0),
47  string_val(NULL, 0),
48  timestamp_val(),
49  decimal4_val(),
50  decimal8_val(),
51  decimal16_val() {
52  }
53 
54  ExprValue(bool v) : bool_val(v) {}
55  ExprValue(int8_t v) : tinyint_val(v) {}
56  ExprValue(int16_t v) : smallint_val(v) {}
57  ExprValue(int32_t v) : int_val(v) {}
58  ExprValue(int64_t v) : bigint_val(v) {}
59  ExprValue(float v) : float_val(v) {}
60  ExprValue(double v) : double_val(v) {}
61  ExprValue(int64_t t, int64_t n) : timestamp_val(t, n) {}
62 
64  ExprValue(const std::string& str)
65  : string_data(str) {
66  string_val.ptr = const_cast<char*>(string_data.data());
67  string_val.len = string_data.size();
68  }
69 
71  void* SetToZero(const ColumnType& type) {
72  switch (type.type) {
73  case TYPE_NULL:
74  return NULL;
75  case TYPE_BOOLEAN:
76  bool_val = false;
77  return &bool_val;
78  case TYPE_TINYINT:
79  tinyint_val = 0;
80  return &tinyint_val;
81  case TYPE_SMALLINT:
82  smallint_val = 0;
83  return &smallint_val;
84  case TYPE_INT:
85  int_val = 0;
86  return &int_val;
87  case TYPE_BIGINT:
88  bigint_val = 0;
89  return &bigint_val;
90  case TYPE_FLOAT:
91  float_val = 0;
92  return &float_val;
93  case TYPE_DOUBLE:
94  double_val = 0;
95  return &double_val;
96  default:
97  DCHECK(false);
98  return NULL;
99  }
100  }
101 
103  void* SetToMin(const ColumnType& type) {
104  switch (type.type) {
105  case TYPE_NULL:
106  return NULL;
107  case TYPE_BOOLEAN:
108  bool_val = false;
109  return &bool_val;
110  case TYPE_TINYINT:
111  tinyint_val = std::numeric_limits<int8_t>::min();
112  return &tinyint_val;
113  case TYPE_SMALLINT:
114  smallint_val = std::numeric_limits<int16_t>::min();
115  return &smallint_val;
116  case TYPE_INT:
117  int_val = std::numeric_limits<int32_t>::min();
118  return &int_val;
119  case TYPE_BIGINT:
120  bigint_val = std::numeric_limits<int64_t>::min();
121  return &bigint_val;
122  case TYPE_FLOAT:
123  // For floats and doubles, numeric_limits::min() is the smallest positive
124  // representable value.
125  float_val = -std::numeric_limits<float>::max();
126  return &float_val;
127  case TYPE_DOUBLE:
128  double_val = -std::numeric_limits<double>::max();
129  return &double_val;
130  default:
131  DCHECK(false);
132  return NULL;
133  }
134  }
135 
137  void* SetToMax(const ColumnType& type) {
138  switch (type.type) {
139  case TYPE_NULL:
140  return NULL;
141  case TYPE_BOOLEAN:
142  bool_val = true;
143  return &bool_val;
144  case TYPE_TINYINT:
145  tinyint_val = std::numeric_limits<int8_t>::max();
146  return &tinyint_val;
147  case TYPE_SMALLINT:
148  smallint_val = std::numeric_limits<int16_t>::max();
149  return &smallint_val;
150  case TYPE_INT:
151  int_val = std::numeric_limits<int32_t>::max();
152  return &int_val;
153  case TYPE_BIGINT:
154  bigint_val = std::numeric_limits<int64_t>::max();
155  return &bigint_val;
156  case TYPE_FLOAT:
157  float_val = std::numeric_limits<float>::max();
158  return &float_val;
159  case TYPE_DOUBLE:
160  double_val = std::numeric_limits<double>::max();
161  return &double_val;
162  default:
163  DCHECK(false);
164  return NULL;
165  }
166  }
167 
168  private:
169  std::string string_data; // Stores the data for string_val if necessary.
170 };
171 
172 }
173 
174 #endif
ExprValue(bool v)
Definition: expr-value.h:54
void * SetToZero(const ColumnType &type)
Sets the value for type to '0' and returns a pointer to the data.
Definition: expr-value.h:71
ExprValue(int32_t v)
Definition: expr-value.h:57
ExprValue(const std::string &str)
c'tor for string values
Definition: expr-value.h:64
void * SetToMin(const ColumnType &type)
Sets the value for type to min and returns a pointer to the data.
Definition: expr-value.h:103
Decimal8Value decimal8_val
Definition: expr-value.h:36
The materialized value returned by ExprContext::GetValue().
Definition: expr-value.h:25
TimestampValue timestamp_val
Definition: expr-value.h:34
ExprValue(double v)
Definition: expr-value.h:60
void * SetToMax(const ColumnType &type)
Sets the value for type to max and returns a pointer to the data.
Definition: expr-value.h:137
StringValue string_val
Definition: expr-value.h:33
PrimitiveType type
Definition: types.h:60
std::string string_data
Definition: expr-value.h:169
ExprValue(int8_t v)
Definition: expr-value.h:55
ExprValue(int64_t v)
Definition: expr-value.h:58
int64_t bigint_val
Definition: expr-value.h:30
ExprValue(int64_t t, int64_t n)
Definition: expr-value.h:61
Decimal4Value decimal4_val
Definition: expr-value.h:35
ExprValue(float v)
Definition: expr-value.h:59
Decimal16Value decimal16_val
Definition: expr-value.h:37
int16_t smallint_val
Definition: expr-value.h:28
int8_t tinyint_val
Definition: expr-value.h:27
ExprValue(int16_t v)
Definition: expr-value.h:56