OmniSciDB  04ee39c94c
Encoder.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "Encoder.h"
18 #include "ArrayNoneEncoder.h"
19 #include "DateDaysEncoder.h"
21 #include "FixedLengthEncoder.h"
22 #include "NoneEncoder.h"
23 #include "Shared/Logger.h"
24 #include "StringNoneEncoder.h"
25 
27  const SQLTypeInfo sqlType) {
28  switch (sqlType.get_compression()) {
29  case kENCODING_NONE: {
30  switch (sqlType.get_type()) {
31  case kBOOLEAN: {
32  return new NoneEncoder<int8_t>(buffer);
33  break;
34  }
35  case kTINYINT: {
36  return new NoneEncoder<int8_t>(buffer);
37  break;
38  }
39  case kSMALLINT: {
40  return new NoneEncoder<int16_t>(buffer);
41  break;
42  }
43  case kINT: {
44  return new NoneEncoder<int32_t>(buffer);
45  break;
46  }
47  case kBIGINT:
48  case kNUMERIC:
49  case kDECIMAL: {
50  return new NoneEncoder<int64_t>(buffer);
51  break;
52  }
53  case kFLOAT: {
54  return new NoneEncoder<float>(buffer);
55  break;
56  }
57  case kDOUBLE: {
58  return new NoneEncoder<double>(buffer);
59  break;
60  }
61  case kTEXT:
62  case kVARCHAR:
63  case kCHAR:
64  return new StringNoneEncoder(buffer);
65  case kARRAY: {
66  if (sqlType.get_size() > 0) {
67  return new FixedLengthArrayNoneEncoder(buffer, sqlType.get_size());
68  }
69  return new ArrayNoneEncoder(buffer);
70  }
71  case kTIME:
72  case kTIMESTAMP:
73  case kDATE:
74  return new NoneEncoder<int64_t>(buffer);
75  case kPOINT:
76  case kLINESTRING:
77  case kPOLYGON:
78  case kMULTIPOLYGON:
79  return new StringNoneEncoder(buffer);
80  default: {
81  return 0;
82  }
83  }
84  break;
85  }
87  switch (sqlType.get_type()) {
88  case kDATE:
89  switch (sqlType.get_comp_param()) {
90  case 0:
91  case 32:
92  return new DateDaysEncoder<int64_t, int32_t>(buffer);
93  break;
94  case 16:
95  return new DateDaysEncoder<int64_t, int16_t>(buffer);
96  break;
97  default:
98  return 0;
99  break;
100  }
101  break;
102  default:
103  return 0;
104  break;
105  }
106  }
107  case kENCODING_FIXED: {
108  switch (sqlType.get_type()) {
109  case kSMALLINT: {
110  switch (sqlType.get_comp_param()) {
111  case 8:
112  return new FixedLengthEncoder<int16_t, int8_t>(buffer);
113  break;
114  case 16:
115  return new NoneEncoder<int16_t>(buffer);
116  break;
117  default:
118  return 0;
119  break;
120  }
121  break;
122  }
123  case kINT: {
124  switch (sqlType.get_comp_param()) {
125  case 8:
126  return new FixedLengthEncoder<int32_t, int8_t>(buffer);
127  break;
128  case 16:
129  return new FixedLengthEncoder<int32_t, int16_t>(buffer);
130  break;
131  case 32:
132  return new NoneEncoder<int32_t>(buffer);
133  break;
134  default:
135  return 0;
136  break;
137  }
138  break;
139  }
140  case kBIGINT:
141  case kNUMERIC:
142  case kDECIMAL: {
143  switch (sqlType.get_comp_param()) {
144  case 8:
145  return new FixedLengthEncoder<int64_t, int8_t>(buffer);
146  break;
147  case 16:
148  return new FixedLengthEncoder<int64_t, int16_t>(buffer);
149  break;
150  case 32:
151  return new FixedLengthEncoder<int64_t, int32_t>(buffer);
152  break;
153  case 64:
154  return new NoneEncoder<int64_t>(buffer);
155  break;
156  default:
157  return 0;
158  break;
159  }
160  break;
161  }
162  case kTIME:
163  case kTIMESTAMP:
164  case kDATE:
165  return new FixedLengthEncoder<int64_t, int32_t>(buffer);
166  break;
167  default: {
168  return 0;
169  break;
170  }
171  } // switch (sqlType)
172  break;
173  } // Case: kENCODING_FIXED
174  case kENCODING_DICT: {
175  if (sqlType.get_type() == kARRAY) {
176  CHECK(IS_STRING(sqlType.get_subtype()));
177  if (sqlType.get_size() > 0) {
178  return new FixedLengthArrayNoneEncoder(buffer, sqlType.get_size());
179  }
180  return new ArrayNoneEncoder(buffer);
181  } else {
182  CHECK(sqlType.is_string());
183  switch (sqlType.get_size()) {
184  case 1:
185  return new NoneEncoder<uint8_t>(buffer);
186  break;
187  case 2:
188  return new NoneEncoder<uint16_t>(buffer);
189  break;
190  case 4:
191  return new NoneEncoder<int32_t>(buffer);
192  break;
193  default:
194  CHECK(false);
195  break;
196  }
197  }
198  break;
199  }
200  case kENCODING_GEOINT: {
201  switch (sqlType.get_type()) {
202  case kPOINT:
203  case kLINESTRING:
204  case kPOLYGON:
205  case kMULTIPOLYGON:
206  return new StringNoneEncoder(buffer);
207  default: {
208  return 0;
209  }
210  }
211  break;
212  }
213  default: {
214  return 0;
215  break;
216  }
217  } // switch (encodingType)
218  return 0;
219 }
220 
222  : num_elems_(0)
223  , buffer_(buffer)
224  , decimal_overflow_validator_(buffer ? buffer->sqlType : SQLTypeInfo())
225  , date_days_overflow_validator_(buffer ? buffer->sqlType : SQLTypeInfo()){};
226 
227 void Encoder::getMetadata(ChunkMetadata& chunkMetadata) {
228  // chunkMetadata = metadataTemplate_; // invoke copy constructor
229  chunkMetadata.sqlType = buffer_->sqlType;
230  chunkMetadata.numBytes = buffer_->size();
231  chunkMetadata.numElements = num_elems_;
232 }
HOST DEVICE int get_size() const
Definition: sqltypes.h:333
size_t num_elems_
Definition: Encoder.h:179
Definition: sqltypes.h:51
DecimalOverflowValidator decimal_overflow_validator_
Definition: Encoder.h:184
virtual size_t size() const =0
static Encoder * Create(Data_Namespace::AbstractBuffer *buffer, const SQLTypeInfo sqlType)
Definition: Encoder.cpp:26
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:323
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:331
Data_Namespace::AbstractBuffer * buffer_
Definition: Encoder.h:181
virtual void getMetadata(ChunkMetadata &chunkMetadata)
Definition: Encoder.cpp:227
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:324
An AbstractBuffer is a unit of data management for a data manager.
Encoder(Data_Namespace::AbstractBuffer *buffer)
Definition: Encoder.cpp:221
DateDaysOverflowValidator date_days_overflow_validator_
Definition: Encoder.h:185
Definition: sqltypes.h:54
Definition: sqltypes.h:55
Definition: sqltypes.h:43
#define IS_STRING(T)
Definition: sqltypes.h:163
unencoded fixed length array encoder
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:332
#define CHECK(condition)
Definition: Logger.h:187
For unencoded strings.
Definition: sqltypes.h:47
unencoded array encoder
bool is_string() const
Definition: sqltypes.h:450
SQLTypeInfo sqlType
Definition: ChunkMetadata.h:32
size_t numElements
Definition: ChunkMetadata.h:34