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 #include <sstream>
19 #include <cstring>
20 
21 using namespace std;
22 
23 #include "bytestream.h"
24 using namespace messageqcpp;
25 
26 #include "rowgroup.h"
27 using namespace rowgroup;
28 
29 #include "joblisttypes.h"
30 using namespace joblist;
31 
32 #include "simplefilter.h"
33 #include "constantfilter.h"
34 #include "arithmeticcolumn.h"
35 #include "functioncolumn.h"
36 #include "objectreader.h"
37 #include "groupconcatcolumn.h"
38 
39 namespace execplan
40 {
41 
42 /**
43  * Constructors/Destructors
44  */
GroupConcatColumn()45 GroupConcatColumn::GroupConcatColumn():
46     AggregateColumn()
47 {
48 }
49 
GroupConcatColumn(const uint32_t sessionID)50 GroupConcatColumn::GroupConcatColumn(const uint32_t sessionID):
51     AggregateColumn(sessionID)
52 {
53 }
54 
GroupConcatColumn(const GroupConcatColumn & rhs,const uint32_t sessionID)55 GroupConcatColumn::GroupConcatColumn(const GroupConcatColumn& rhs, const uint32_t sessionID):
56     AggregateColumn(dynamic_cast<const AggregateColumn&>(rhs)),
57     fOrderCols(rhs.fOrderCols),
58     fSeparator(rhs.fSeparator)
59 {
60 }
61 
~GroupConcatColumn()62 GroupConcatColumn::~GroupConcatColumn()
63 {
64 }
65 
66 /**
67  * Methods
68  */
69 
toString() const70 const string GroupConcatColumn::toString() const
71 {
72     ostringstream output;
73     output << "GroupConcatColumn " << data() << endl;
74     output << AggregateColumn::toString() << endl;
75     output << "Group Concat Order Columns: " << endl;
76 
77     for (uint32_t i = 0; i < fOrderCols.size(); i++)
78     {
79         output << *fOrderCols[i];
80     }
81 
82     output << "\nSeparator: " << fSeparator << endl;
83     return output.str();
84 }
85 
operator <<(ostream & output,const GroupConcatColumn & rhs)86 ostream& operator<<(ostream& output, const GroupConcatColumn& rhs)
87 {
88     output << rhs.toString();
89     return output;
90 }
91 
serialize(messageqcpp::ByteStream & b) const92 void GroupConcatColumn::serialize(messageqcpp::ByteStream& b) const
93 {
94     b << (uint8_t) ObjectReader::GROUPCONCATCOLUMN;
95     AggregateColumn::serialize(b);
96 
97     CalpontSelectExecutionPlan::ReturnedColumnList::const_iterator rcit;
98     b << static_cast<uint32_t>(fOrderCols.size());
99 
100     for (rcit = fOrderCols.begin(); rcit != fOrderCols.end(); ++rcit)
101         (*rcit)->serialize(b);
102 
103     b << fSeparator;
104 }
105 
unserialize(messageqcpp::ByteStream & b)106 void GroupConcatColumn::unserialize(messageqcpp::ByteStream& b)
107 {
108     ObjectReader::checkType(b, ObjectReader::GROUPCONCATCOLUMN);
109     AggregateColumn::unserialize(b);
110     fOrderCols.erase(fOrderCols.begin(), fOrderCols.end());
111 
112     uint32_t size, i;
113     ReturnedColumn* rc;
114     b >> size;
115 
116     for (i = 0; i < size; i++)
117     {
118         rc = dynamic_cast<ReturnedColumn*>(ObjectReader::createTreeNode(b));
119         SRCP srcp(rc);
120         fOrderCols.push_back(srcp);
121     }
122 
123     b >> fSeparator;
124 }
125 
operator ==(const GroupConcatColumn & t) const126 bool GroupConcatColumn::operator==(const GroupConcatColumn& t) const
127 {
128     const AggregateColumn* rc1, *rc2;
129 
130     rc1 = static_cast<const AggregateColumn*>(this);
131     rc2 = static_cast<const AggregateColumn*>(&t);
132 
133     if (*rc1 != *rc2)
134         return false;
135 
136     for (uint32_t i = 0; i < fOrderCols.size(); i++)
137     {
138         if (fOrderCols[i].get() != NULL)
139         {
140             if (t.fOrderCols[i] == NULL)
141                 return false;
142 
143             if (*(fOrderCols[i].get()) != t.fOrderCols[i].get())
144                 return false;
145         }
146         else if (t.fOrderCols[i].get() != NULL)
147             return false;
148     }
149 
150     if (fSeparator != t.fSeparator)
151         return false;
152 
153     return true;
154 }
155 
operator ==(const TreeNode * t) const156 bool GroupConcatColumn::operator==(const TreeNode* t) const
157 {
158     const GroupConcatColumn* ac;
159 
160     ac = dynamic_cast<const GroupConcatColumn*>(t);
161 
162     if (ac == NULL)
163         return false;
164 
165     return *this == *ac;
166 }
167 
operator !=(const GroupConcatColumn & t) const168 bool GroupConcatColumn::operator!=(const GroupConcatColumn& t) const
169 {
170     return !(*this == t);
171 }
172 
operator !=(const TreeNode * t) const173 bool GroupConcatColumn::operator!=(const TreeNode* t) const
174 {
175     return !(*this == t);
176 }
177 
178 } // namespace execplan
179