1 /* Copyright (C) 2014 InfiniDB, Inc.
2
3 This program is free software; you can redistribute it and/or
4 modify it under the terms of the GNU General Public License
5 as published by the Free Software Foundation; version 2 of
6 the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16 MA 02110-1301, USA. */
17
18 /***********************************************************************
19 * $Id: constantfilter.cpp 9210 2013-01-21 14:10:42Z rdempsey $
20 *
21 *
22 ***********************************************************************/
23 #include <exception>
24 #include <stdexcept>
25 #include <string>
26 #include <iostream>
27 #include <sstream>
28 using namespace std;
29
30 #include "constantfilter.h"
31 #include "returnedcolumn.h"
32 #include "operator.h"
33 #include "simplecolumn.h"
34 #include "simplefilter.h"
35 #include "bytestream.h"
36 #include "objectreader.h"
37 #include "aggregatecolumn.h"
38 #include "windowfunctioncolumn.h"
39
40 namespace
41 {
42 template<class T> struct deleter : public unary_function<T&, void>
43 {
operator ()__anonc7de5e130111::deleter44 void operator()(T& x)
45 {
46 delete x;
47 }
48 };
49 }
50
51 namespace execplan
52 {
53
54 /**
55 * Constructors/Destructors
56 */
ConstantFilter()57 ConstantFilter::ConstantFilter()
58 {}
59
ConstantFilter(const SOP & op,ReturnedColumn * lhs,ReturnedColumn * rhs)60 ConstantFilter::ConstantFilter(const SOP& op, ReturnedColumn* lhs, ReturnedColumn* rhs)
61 {
62 SSFP ssfp(new SimpleFilter(op, lhs, rhs));
63 fFilterList.push_back(ssfp);
64 SimpleColumn* sc = dynamic_cast<SimpleColumn*>(lhs);
65 fCol.reset(sc->clone());
66 }
67
ConstantFilter(SimpleFilter * sf)68 ConstantFilter::ConstantFilter(SimpleFilter* sf)
69 {
70 SSFP ssfp(sf);
71 fFilterList.push_back(ssfp);
72 const SimpleColumn* sc = dynamic_cast<const SimpleColumn*>(sf->lhs());
73 fCol.reset(sc->clone());
74 }
75
ConstantFilter(const ConstantFilter & rhs)76 ConstantFilter::ConstantFilter(const ConstantFilter& rhs):
77 Filter(rhs),
78 fOp(rhs.fOp),
79 fCol(rhs.fCol)
80 {
81 fFilterList.clear();
82 fSimpleColumnList.clear();
83 fWindowFunctionColumnList.clear();
84 SSFP ssfp;
85
86 for (uint32_t i = 0; i < rhs.fFilterList.size(); i++)
87 {
88 ssfp.reset(rhs.fFilterList[i]->clone());
89 fFilterList.push_back(ssfp);
90 fSimpleColumnList.insert(fSimpleColumnList.end(),
91 ssfp->simpleColumnList().begin(),
92 ssfp->simpleColumnList().end());
93 fAggColumnList.insert(fAggColumnList.end(),
94 ssfp->aggColumnList().begin(),
95 ssfp->aggColumnList().end());
96 fWindowFunctionColumnList.insert(fWindowFunctionColumnList.end(),
97 ssfp->windowfunctionColumnList().begin(),
98 ssfp->windowfunctionColumnList().end());
99 }
100 }
101
~ConstantFilter()102 ConstantFilter::~ConstantFilter()
103 {
104 }
105
106 /**
107 * Methods
108 */
109
toString() const110 const string ConstantFilter::toString() const
111 {
112 ostringstream output;
113 output << "ConstantFilter" << endl;
114
115 if (fOp) output << " " << *fOp << endl;
116
117 if (!fFunctionName.empty()) output << " Func: " << fFunctionName << endl;
118
119 if (fCol) output << " " << *fCol << endl;
120
121 for (unsigned int i = 0; i < fFilterList.size(); i++)
122 output << " " << *fFilterList[i] << endl;
123
124 return output.str();
125 }
126
operator <<(ostream & output,const ConstantFilter & rhs)127 ostream& operator<<(ostream& output, const ConstantFilter& rhs)
128 {
129 output << rhs.toString();
130 return output;
131 }
132
hasAggregate()133 bool ConstantFilter::hasAggregate()
134 {
135 if (fAggColumnList.empty())
136 {
137 for (uint32_t i = 0; i < fFilterList.size(); i++)
138 {
139 if (fFilterList[i]->hasAggregate())
140 {
141 fAggColumnList.insert(fAggColumnList.end(),
142 fFilterList[i]->aggColumnList().begin(),
143 fFilterList[i]->aggColumnList().end());
144 }
145 }
146 }
147
148 if (!fAggColumnList.empty())
149 {
150 return true;
151 }
152
153 return false;
154 }
155
serialize(messageqcpp::ByteStream & b) const156 void ConstantFilter::serialize(messageqcpp::ByteStream& b) const
157 {
158 FilterList::const_iterator it;
159 b << static_cast<ObjectReader::id_t>(ObjectReader::CONSTANTFILTER);
160 Filter::serialize(b);
161
162 if (fOp != NULL)
163 fOp->serialize(b);
164 else
165 b << static_cast<ObjectReader::id_t>(ObjectReader::NULL_CLASS);
166
167 if (fCol != NULL)
168 fCol->serialize(b);
169 else
170 b << static_cast<ObjectReader::id_t>(ObjectReader::NULL_CLASS);
171
172 b << static_cast<uint32_t>(fFilterList.size());
173
174 for (it = fFilterList.begin(); it != fFilterList.end(); it++)
175 (*it)->serialize(b);
176
177 b << fFunctionName;
178 }
179
unserialize(messageqcpp::ByteStream & b)180 void ConstantFilter::unserialize(messageqcpp::ByteStream& b)
181 {
182 uint32_t size, i;
183 ObjectReader::checkType(b, ObjectReader::CONSTANTFILTER);
184 SimpleFilter* sf;
185
186 Filter::unserialize(b);
187 fOp.reset(dynamic_cast<Operator*>(ObjectReader::createTreeNode(b)));
188 fCol.reset(dynamic_cast<ReturnedColumn*>(ObjectReader::createTreeNode(b)));
189 b >> size;
190 fFilterList.clear();
191 fSimpleColumnList.clear();
192 fAggColumnList.clear();
193 fWindowFunctionColumnList.clear();
194
195 for (i = 0; i < size; i++)
196 {
197 sf = dynamic_cast<SimpleFilter*>(ObjectReader::createTreeNode(b));
198 SSFP ssfp(sf);
199 fFilterList.push_back(ssfp);
200 fSimpleColumnList.insert(fSimpleColumnList.end(),
201 ssfp->simpleColumnList().begin(),
202 ssfp->simpleColumnList().end());
203 fAggColumnList.insert(fAggColumnList.end(),
204 ssfp->aggColumnList().begin(),
205 ssfp->aggColumnList().end());
206 fWindowFunctionColumnList.insert(fWindowFunctionColumnList.end(),
207 ssfp->windowfunctionColumnList().begin(),
208 ssfp->windowfunctionColumnList().end());
209 }
210
211 b >> fFunctionName;
212 }
213
operator ==(const ConstantFilter & t) const214 bool ConstantFilter::operator==(const ConstantFilter& t) const
215 {
216 const Filter* f1, *f2;
217 FilterList::const_iterator it, it2;
218
219 f1 = static_cast<const Filter*>(this);
220 f2 = static_cast<const Filter*>(&t);
221
222 if (*f1 != *f2)
223 return false;
224
225 if (fOp != NULL)
226 {
227 if (*fOp != *t.fOp)
228 return false;
229 }
230 else if (t.fOp != NULL)
231 return false;
232
233 //fFilterList
234 if (fFilterList.size() != t.fFilterList.size())
235 return false;
236
237 for (it = fFilterList.begin(), it2 = t.fFilterList.begin();
238 it != fFilterList.end(); ++it, ++it2)
239 {
240 if (**it != **it2)
241 return false;
242 }
243
244 return true;
245 }
246
operator ==(const TreeNode * t) const247 bool ConstantFilter::operator==(const TreeNode* t) const
248 {
249 const ConstantFilter* o;
250
251 o = dynamic_cast<const ConstantFilter*>(t);
252
253 if (o == NULL)
254 return false;
255
256 return *this == *o;
257 }
258
operator !=(const ConstantFilter & t) const259 bool ConstantFilter::operator!=(const ConstantFilter& t) const
260 {
261 return (!(*this == t));
262 }
263
operator !=(const TreeNode * t) const264 bool ConstantFilter::operator!=(const TreeNode* t) const
265 {
266 return (!(*this == t));
267 }
268
setDerivedTable()269 void ConstantFilter::setDerivedTable()
270 {
271 if (fCol->hasAggregate())
272 {
273 fDerivedTable = "";
274 return;
275 }
276 for (unsigned i = 0; i < fFilterList.size(); i++)
277 {
278 fFilterList[i]->setDerivedTable();
279 }
280
281 if (!fFilterList.empty())
282 {
283 fDerivedTable = fFilterList[0]->derivedTable();
284 }
285 else
286 {
287 fDerivedTable = "";
288 }
289 }
290
replaceRealCol(std::vector<SRCP> & derivedColList)291 void ConstantFilter::replaceRealCol(std::vector<SRCP>& derivedColList)
292 {
293 ReturnedColumn* tmp = derivedColList[fCol->colPosition()]->clone();
294 fCol.reset(tmp);
295
296 for (unsigned i = 0; i < fFilterList.size(); i++)
297 fFilterList[i]->replaceRealCol(derivedColList);
298 }
299
simpleColumnList()300 const std::vector<SimpleColumn*>& ConstantFilter::simpleColumnList()
301 {
302 return fSimpleColumnList;
303 }
304
setSimpleColumnList()305 void ConstantFilter::setSimpleColumnList()
306 {
307 fSimpleColumnList.clear();
308
309 for (uint32_t i = 0; i < fFilterList.size(); i++)
310 {
311 fFilterList[i]->setSimpleColumnList();
312 fSimpleColumnList.insert(fSimpleColumnList.end(),
313 fFilterList[i]->simpleColumnList().begin(),
314 fFilterList[i]->simpleColumnList().end());
315 }
316 }
317
318
319 } // namespace execplan
320 // vim:ts=4 sw=4:
321