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.h 9210 2013-01-21 14:10:42Z rdempsey $
20 *
21 *
22 ***********************************************************************/
23 /** @file */
24 
25 #ifndef CONSTANTFILTER_H
26 #define CONSTANTFILTER_H
27 #include <string>
28 #include <iosfwd>
29 #include <vector>
30 
31 #include "filter.h"
32 #include "simplefilter.h"
33 #include "simplecolumn.h"
34 #include "operator.h"
35 
36 /**
37  * Namespace
38  */
39 namespace execplan
40 {
41 
42 class ReturnedColumn;
43 class AggregateColumn;
44 class WindowFunctionColumn;
45 
46 /**
47  * @brief A class to represent a simple column op constant predicate
48  *
49  * This class is a specialization of class Filter that handles one or
50  * more simple filters, where one side of operand is a constant. It
51  * contains a list of simple filters and every simplefilter has the
52  * same column. All the simple filters are connected by the same
53  * operator (either "and" or "or"). This class is introduced for
54  * easy operation combine for primitive processor.
55  */
56 class ConstantFilter : public Filter
57 {
58 
59 public:
60     /**
61      * Types and constants
62      */
63     typedef std::vector<SSFP> FilterList;
64 
65     /**
66      * Constructors
67      */
68     ConstantFilter();
69     ConstantFilter(const SOP& op, ReturnedColumn* lhs, ReturnedColumn* rhs);
70     ConstantFilter(SimpleFilter* sf);
71 
72     //not needed yet
73     //ConstantFilter(const ConstantFilter& rhs);
74 
75     /**
76      * Destructors
77      */
78     virtual ~ConstantFilter();
79 
80     /**
81      * Accessor Methods
82      */
filterList()83     const FilterList& filterList() const
84     {
85         return fFilterList;
86     }
filterList(const FilterList & filterList)87     void filterList( const FilterList& filterList)
88     {
89         fFilterList = filterList;
90     }
op()91     const SOP& op() const
92     {
93         return fOp;
94     }
op(const SOP & op)95     void op(const SOP& op)
96     {
97         fOp = op;
98     }
col()99     const SRCP& col() const
100     {
101         return fCol;
102     }
col(const SRCP & col)103     void col(const SRCP& col)
104     {
105         fCol = col;
106     }
107 
108     /**
109      * Operations
110      */
pushFilter(SimpleFilter * sf)111     void pushFilter (SimpleFilter* sf)
112     {
113         SSFP ssfp(sf);
114         fFilterList.push_back(ssfp);
115     }
116 
117     //virtual const std::string data() const;
118     virtual const std::string toString() const;
119 
120     /**
121      * The serialization interface
122      */
123     virtual void serialize(messageqcpp::ByteStream&) const;
124     virtual void unserialize(messageqcpp::ByteStream&);
125 
126     /** @brief Do a deep, strict (as opposed to semantic) equivalence test
127      *
128      * Do a deep, strict (as opposed to semantic) equivalence test.
129      * @return true iff every member of t is a duplicate copy of every member of this; false otherwise
130      */
131     virtual bool operator==(const TreeNode* t) const;
132 
133     /** @brief Do a deep, strict (as opposed to semantic) equivalence test
134      *
135      * Do a deep, strict (as opposed to semantic) equivalence test.
136      * @return true iff every member of t is a duplicate copy of every member of this; false otherwise
137      */
138     bool operator==(const ConstantFilter& t) const;
139 
140     /** @brief Do a deep, strict (as opposed to semantic) equivalence test
141      *
142      * Do a deep, strict (as opposed to semantic) equivalence test.
143      * @return false iff every member of t is a duplicate copy of every member of this; true otherwise
144      */
145     virtual bool operator!=(const TreeNode* t) const;
146 
147     /** @brief Do a deep, strict (as opposed to semantic) equivalence test
148      *
149      * Do a deep, strict (as opposed to semantic) equivalence test.
150      * @return false iff every member of t is a duplicate copy of every member of this; true otherwise
151      */
152     bool operator!=(const ConstantFilter& t) const;
153 
154     /** @brief test if this filter can be combined with the argument filter
155       *  This is for operation combine optimization
156       *  @param f the filter that this fiter tries to combine with
157       *  @param op the operator that connects the two filters. if one or both of the
158       *  two filters is constantFilter, need to make sure operator is consistant.
159       *  @return a filter(constantfilter) if successfully combined. otherwise
160       *     	 return NULL
161       */
162     // Used by Oracle frontend, deprecated now.
163     //virtual Filter* combinable(Filter* f, Operator* op);
164 
165     /** get function name
166      *
167      * get the function name for this function column
168      */
functionName()169     std::string functionName() const
170     {
171         return fFunctionName;
172     }
173 
174     /** set function name
175      *
176      * set the function name for this function column
177      */
functionName(const std::string & functionName)178     void functionName(const std::string& functionName)
179     {
180         fFunctionName = functionName;
181     }
182 
183     void setDerivedTable();
184     virtual void replaceRealCol(std::vector<SRCP>&);
185     virtual bool hasAggregate();
186 
187 private:
188     SOP fOp;       /// connect operator (and or)
189     FilterList fFilterList; /// vector of simple filters
190     SRCP fCol;     /// the common column
191     std::string fFunctionName;  /// function name
192 
193     /***********************************************************
194      *                  F&E framework                          *
195      ***********************************************************/
196 public:
197     ConstantFilter(const ConstantFilter& rhs);
clone()198     inline virtual ConstantFilter* clone() const
199     {
200         return new ConstantFilter (*this);
201     }
202 
203     inline virtual bool getBoolVal(rowgroup::Row& row, bool& isNull);
204 
205     // get all simple columns involved in this column
206     const std::vector<SimpleColumn*>& simpleColumnList();
207     // walk through the constant filter operands to re-populate fSimpleColumnList
208     void setSimpleColumnList();
209 
210     // get all aggregate columns involved in this column
aggColumnList()211     const std::vector<AggregateColumn*>& aggColumnList() const
212     {
213         return fAggColumnList;
214     }
215 
216 private:
217     std::vector<SimpleColumn*> fSimpleColumnList;
218     std::vector<AggregateColumn*> fAggColumnList;
219     std::vector<WindowFunctionColumn*> fWindowFunctionColumnList;
220 };
221 
getBoolVal(rowgroup::Row & row,bool & isNull)222 inline bool ConstantFilter::getBoolVal(rowgroup::Row& row, bool& isNull)
223 {
224     switch (fOp->op())
225     {
226         case OP_AND:
227             for (uint32_t i = 0; i < fFilterList.size(); i++)
228                 if (!fFilterList[i]->getBoolVal(row, isNull))
229                     return false;
230 
231             return true;
232 
233         case OP_OR:
234             for (uint32_t i = 0; i < fFilterList.size(); i++)
235                 if (fFilterList[i]->getBoolVal(row, isNull))
236                     return true;
237 
238             return false;
239 
240         default:
241         {
242             std::ostringstream oss;
243             oss << "ConstantFilter:: Non support logic operation: " << fOp->op();
244             throw logging::InvalidOperationExcept(oss.str());
245         }
246     }
247 }
248 
249 std::ostream& operator<<(std::ostream& output, const ConstantFilter& rhs);
250 
251 }
252 #endif //CONSTANTFILTER_H
253 
254