1 /* Copyright (C) 2014 InfiniDB, Inc.
2    Copyright (C) 2019 MariaDB Corporation
3 
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License
6    as published by the Free Software Foundation; version 2 of
7    the License.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17    MA 02110-1301, USA. */
18 
19 /***********************************************************************
20 *   $Id: simplecolumn_uint.h 8536 2012-05-21 21:27:17Z dhall $
21 *
22 *
23 ***********************************************************************/
24 /** @file */
25 
26 #ifndef SIMPLECOLUMNUINT_H
27 #define SIMPLECOLUMNUINT_H
28 #include <string>
29 
30 #include "simplecolumn.h"
31 #include "objectreader.h"
32 #include "joblisttypes.h"
33 #include "rowgroup.h"
34 
35 /**
36  * Namespace
37  */
38 namespace execplan
39 {
40 /**
41  * @brief A class to represent a simple returned column
42  *
43  * This class is a specialization of class ReturnedColumn that handles
44  * a column name.
45  */
46 template <int len>
47 class SimpleColumn_UINT : public SimpleColumn
48 {
49 
50     /**
51      * Public stuff
52      */
53 public:
54 
55     /** Constructors */
56     SimpleColumn_UINT();
57     SimpleColumn_UINT(const std::string& token, const uint32_t sessionID = 0);
58     SimpleColumn_UINT(const std::string& schema,
59                       const std::string& table,
60                       const std::string& col,
61                       const bool isColumnStore,
62                       const uint32_t sessionID = 0);
63     SimpleColumn_UINT(const SimpleColumn& rhs, const uint32_t sessionID = 0);
64 
65     /** Destructor */
~SimpleColumn_UINT()66     virtual ~SimpleColumn_UINT() {}
67 
clone()68     inline virtual SimpleColumn_UINT* clone() const
69     {
70         return new SimpleColumn_UINT<len> (*this);
71     }
72 
73     /** Evaluate methods */
74     virtual inline const std::string& getStrVal(rowgroup::Row& row, bool& isNull);
75     virtual inline int64_t getIntVal(rowgroup::Row& row, bool& isNull);
76     virtual inline uint64_t getUintVal(rowgroup::Row& row, bool& isNull);
77     virtual inline float getFloatVal(rowgroup::Row& row, bool& isNull);
78     virtual inline double getDoubleVal(rowgroup::Row& row, bool& isNull);
79     virtual inline long double getLongDoubleVal(rowgroup::Row& row, bool& isNull);
80     virtual inline IDB_Decimal getDecimalVal(rowgroup::Row& row, bool& isNull);
81 
82     /** The serialize interface */
83     virtual void serialize(messageqcpp::ByteStream&) const;
84     virtual void unserialize(messageqcpp::ByteStream&);
85     uint64_t fNullVal;
86 
87 private:
88     void setNullVal();
89 
90 };
91 
92 template<int len>
SimpleColumn_UINT()93 SimpleColumn_UINT<len>::SimpleColumn_UINT(): SimpleColumn()
94 {
95     setNullVal();
96 }
97 
98 template<int len>
SimpleColumn_UINT(const std::string & token,const uint32_t sessionID)99 SimpleColumn_UINT<len>::SimpleColumn_UINT(const std::string& token, const uint32_t sessionID):
100     SimpleColumn(token, sessionID)
101 {
102     setNullVal();
103 }
104 
105 template<int len>
SimpleColumn_UINT(const std::string & schema,const std::string & table,const std::string & col,const bool isColumnStore,const uint32_t sessionID)106 SimpleColumn_UINT<len>::SimpleColumn_UINT(const std::string& schema,
107         const std::string& table,
108         const std::string& col,
109         const bool isColumnStore,
110         const uint32_t sessionID) :
111     SimpleColumn(schema, table, col, isColumnStore, sessionID)
112 {
113     setNullVal();
114 }
115 
116 template<int len>
SimpleColumn_UINT(const SimpleColumn & rhs,const uint32_t sessionID)117 SimpleColumn_UINT<len>::SimpleColumn_UINT(const SimpleColumn& rhs, const uint32_t sessionID):
118     SimpleColumn(rhs, sessionID)
119 {
120     setNullVal();
121 }
122 
123 template<int len>
setNullVal()124 void SimpleColumn_UINT<len>::setNullVal()
125 {
126     switch (len)
127     {
128         case 8:
129             fNullVal = joblist::UBIGINTNULL;
130             break;
131 
132         case 4:
133             fNullVal = joblist::UINTNULL;
134             break;
135 
136         case 2:
137             fNullVal = joblist::USMALLINTNULL;
138             break;
139 
140         case 1:
141             fNullVal = joblist::UTINYINTNULL;
142             break;
143 
144         default:
145             fNullVal = joblist::UBIGINTNULL;
146     }
147 }
148 
149 template<int len>
getStrVal(rowgroup::Row & row,bool & isNull)150 inline const std::string& SimpleColumn_UINT<len>:: getStrVal(rowgroup::Row& row, bool& isNull)
151 {
152     if (row.equals<len>(fNullVal, fInputIndex))
153         isNull = true;
154     else
155     {
156 #ifndef __LP64__
157         snprintf(tmp, 21, "%llu", row.getUintField<len>(fInputIndex));
158 #else
159         snprintf(tmp, 21, "%lu", row.getUintField<len>(fInputIndex));
160 #endif
161     }
162 
163     fResult.strVal = std::string(tmp);
164     return fResult.strVal;
165 }
166 
167 template<int len>
getIntVal(rowgroup::Row & row,bool & isNull)168 inline int64_t SimpleColumn_UINT<len>:: getIntVal(rowgroup::Row& row, bool& isNull)
169 {
170     if (row.equals<len>(fNullVal, fInputIndex))
171         isNull = true;
172 
173     return (int64_t)row.getUintField<len>(fInputIndex);
174 }
175 
176 template<int len>
getUintVal(rowgroup::Row & row,bool & isNull)177 inline uint64_t SimpleColumn_UINT<len>:: getUintVal(rowgroup::Row& row, bool& isNull)
178 {
179     if (row.equals<len>(fNullVal, fInputIndex))
180         isNull = true;
181 
182     return (uint64_t)row.getUintField<len>(fInputIndex);
183 }
184 
185 template<int len>
getFloatVal(rowgroup::Row & row,bool & isNull)186 inline float SimpleColumn_UINT<len>::getFloatVal(rowgroup::Row& row, bool& isNull)
187 {
188     if (row.equals<len>(fNullVal, fInputIndex))
189         isNull = true;
190 
191     return (float)row.getUintField<len>(fInputIndex);
192 }
193 
194 template<int len>
getDoubleVal(rowgroup::Row & row,bool & isNull)195 inline double SimpleColumn_UINT<len>::getDoubleVal(rowgroup::Row& row, bool& isNull)
196 {
197     if (row.equals<len>(fNullVal, fInputIndex))
198         isNull = true;
199 
200     return (double)row.getUintField<len>(fInputIndex);
201 }
202 
203 template<int len>
getLongDoubleVal(rowgroup::Row & row,bool & isNull)204 inline long double SimpleColumn_UINT<len>::getLongDoubleVal(rowgroup::Row& row, bool& isNull)
205 {
206     if (row.equals<len>(fNullVal, fInputIndex))
207         isNull = true;
208 
209     return (long double)row.getUintField<len>(fInputIndex);
210 }
211 
212 template<int len>
getDecimalVal(rowgroup::Row & row,bool & isNull)213 inline IDB_Decimal SimpleColumn_UINT<len>::getDecimalVal(rowgroup::Row& row, bool& isNull)
214 {
215     if (row.equals<len>(fNullVal, fInputIndex))
216         isNull = true;
217 
218     fResult.decimalVal.value = (uint64_t)row.getUintField<len>(fInputIndex);
219     fResult.decimalVal.precision = 65;
220     fResult.decimalVal.scale = 0;
221     return fResult.decimalVal;
222 }
223 
224 template<int len>
serialize(messageqcpp::ByteStream & b)225 void SimpleColumn_UINT<len>::serialize(messageqcpp::ByteStream& b) const
226 {
227     switch (len)
228     {
229         case 1:
230             b << (ObjectReader::id_t) ObjectReader::SIMPLECOLUMN_UINT1;
231             break;
232 
233         case 2:
234             b << (ObjectReader::id_t) ObjectReader::SIMPLECOLUMN_UINT2;
235             break;
236 
237         case 4:
238             b << (ObjectReader::id_t) ObjectReader::SIMPLECOLUMN_UINT4;
239             break;
240 
241         case 8:
242             b << (ObjectReader::id_t) ObjectReader::SIMPLECOLUMN_UINT8;
243             break;
244     }
245 
246     SimpleColumn::serialize(b);
247 }
248 
249 template<int len>
unserialize(messageqcpp::ByteStream & b)250 void SimpleColumn_UINT<len>::unserialize(messageqcpp::ByteStream& b)
251 {
252     switch (len)
253     {
254         case 1:
255             ObjectReader::checkType(b, ObjectReader::SIMPLECOLUMN_UINT1);
256             break;
257 
258         case 2:
259             ObjectReader::checkType(b, ObjectReader::SIMPLECOLUMN_UINT2);
260             break;
261 
262         case 4:
263             ObjectReader::checkType(b, ObjectReader::SIMPLECOLUMN_UINT4);
264             break;
265 
266         case 8:
267             ObjectReader::checkType(b, ObjectReader::SIMPLECOLUMN_UINT8);
268             break;
269     }
270 
271     SimpleColumn::unserialize(b);
272 }
273 
274 }
275 #endif //SIMPLECOLUMN_INT_H
276 
277