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: simplescalarfilter.cpp 6310 2010-03-04 19:46:18Z zzhu $
20 *
21 *
22 ***********************************************************************/
23 #include <string>
24 #include <iostream>
25 #include <sstream>
26 using namespace std;
27 
28 #include "simplescalarfilter.h"
29 #include "bytestream.h"
30 #include "objectreader.h"
31 
32 namespace execplan
33 {
34 /**
35  * Constructors/Destructors
36  */
SimpleScalarFilter()37 SimpleScalarFilter::SimpleScalarFilter()
38 {}
39 
SimpleScalarFilter(const vector<SRCP> & cols,const SOP & op,SCSEP & sub)40 SimpleScalarFilter::SimpleScalarFilter(const vector<SRCP>& cols,
41                                        const SOP& op,
42                                        SCSEP& sub) :
43     fCols(cols),
44     fOp(op),
45     fSub(sub),
46     fData("simple scalar")
47 {}
48 
SimpleScalarFilter(const SimpleScalarFilter & rhs)49 SimpleScalarFilter::SimpleScalarFilter(const SimpleScalarFilter& rhs):
50     Filter(rhs),
51     fCols (rhs.fCols),
52     fOp (rhs.fOp),
53     fSub (rhs.fSub),
54     fData (rhs.fData)
55 {}
56 
~SimpleScalarFilter()57 SimpleScalarFilter::~SimpleScalarFilter()
58 {}
59 
toString() const60 const string SimpleScalarFilter::toString() const
61 {
62     ostringstream oss;
63     oss << "SimpleScalarFilter" << endl;
64 
65     for (uint32_t i = 0; i < fCols.size(); i++)
66         oss << fCols[i]->toString();
67 
68     oss << fOp->toString() << endl;
69     oss << *(fSub.get());
70     return oss.str();
71 }
72 
operator <<(ostream & output,const SimpleScalarFilter & rhs)73 ostream& operator<<(ostream& output, const SimpleScalarFilter& rhs)
74 {
75     output << rhs.toString();
76     return output;
77 }
78 
serialize(messageqcpp::ByteStream & b) const79 void SimpleScalarFilter::serialize(messageqcpp::ByteStream& b) const
80 {
81     b << static_cast<ObjectReader::id_t>(ObjectReader::SIMPLESCALARFILTER);
82     Filter::serialize(b);
83     b << static_cast<uint32_t>(fCols.size());
84 
85     for (uint32_t i = 0; i < fCols.size(); i++)
86     {
87         if (fCols[i] != NULL)
88             fCols[i]->serialize(b);
89         else
90             b << static_cast<ObjectReader::id_t>(ObjectReader::NULL_CLASS);
91     }
92 
93     if (fOp != NULL)
94         fOp->serialize(b);
95     else
96         b << static_cast<ObjectReader::id_t>(ObjectReader::NULL_CLASS);
97 
98     if (fSub.get() != NULL)
99         fSub->serialize(b);
100     else
101         b << static_cast<ObjectReader::id_t>(ObjectReader::NULL_CLASS);
102 }
103 
unserialize(messageqcpp::ByteStream & b)104 void SimpleScalarFilter::unserialize(messageqcpp::ByteStream& b)
105 {
106     ObjectReader::checkType(b, ObjectReader::SIMPLESCALARFILTER);
107 
108     Filter::unserialize(b);
109     uint32_t size;
110     b >> size;
111     fCols.clear();
112     SRCP srcp;
113 
114     for (uint32_t i = 0; i < size; i++)
115     {
116         srcp.reset(dynamic_cast<ReturnedColumn*>(ObjectReader::createTreeNode(b)));
117         fCols.push_back(srcp);
118     }
119 
120     fOp.reset(dynamic_cast<Operator*>(ObjectReader::createTreeNode(b)));
121     fSub.reset(dynamic_cast<CalpontSelectExecutionPlan*>(ObjectReader::createExecutionPlan(b)));
122 }
123 
operator ==(const SimpleScalarFilter & t) const124 bool SimpleScalarFilter::operator==(const SimpleScalarFilter& t) const
125 {
126     const Filter* f1, *f2;
127 
128     f1 = static_cast<const Filter*>(this);
129     f2 = static_cast<const Filter*>(&t);
130 
131     if (*f1 != *f2)
132         return false;
133 
134     if (fCols.size() != t.fCols.size())
135         return false;
136 
137     for (uint32_t i = 0; i < fCols.size(); i++)
138     {
139         if (fCols[i].get() != NULL)
140         {
141             if (*(fCols[i].get()) != *(t.fCols[i]).get())
142                 return false;
143         }
144         else if (t.fCols[i].get() != NULL)
145             return false;
146     }
147 
148     if (fOp != NULL)
149     {
150         if (*fOp != *t.fOp)
151             return false;
152     }
153     else if (t.fOp != NULL)
154         return false;
155 
156     if (fSub != NULL)
157     {
158         if (*fSub != t.fSub.get())
159             return false;
160     }
161     else if (t.fSub != NULL)
162         return false;
163 
164     if (fData != t.fData)
165         return false;
166 
167     return true;
168 }
169 
operator ==(const TreeNode * t) const170 bool SimpleScalarFilter::operator==(const TreeNode* t) const
171 {
172     const SimpleScalarFilter* o;
173 
174     o = dynamic_cast<const SimpleScalarFilter*>(t);
175 
176     if (o == NULL)
177         return false;
178 
179     return *this == *o;
180 }
181 
operator !=(const SimpleScalarFilter & t) const182 bool SimpleScalarFilter::operator!=(const SimpleScalarFilter& t) const
183 {
184     return (!(*this == t));
185 }
186 
operator !=(const TreeNode * t) const187 bool SimpleScalarFilter::operator!=(const TreeNode* t) const
188 {
189     return (!(*this == t));
190 }
191 
192 }
193