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: logicoperator.h 9210 2013-01-21 14:10:42Z rdempsey $
20 *
21 *
22 ***********************************************************************/
23 /** @file */
24 
25 #ifndef LOGICOPERATOR_H
26 #define LOGICOPERATOR_H
27 #include <string>
28 #include <iosfwd>
29 #include <boost/shared_ptr.hpp>
30 
31 //#include "expressionparser.h"
32 #include "operator.h"
33 #include "parsetree.h"
34 
35 namespace messageqcpp
36 {
37 class ByteStream;
38 }
39 
40 namespace rowgroup
41 {
42 class Row;
43 }
44 
45 /**
46  * Namespace
47  */
48 namespace execplan
49 {
50 class ParseTree;
51 /**@brief a class to represent an operator
52  *
53  * This class is a representation of an predicate operator as
54  * "AND, OR, LIKE" or arithmetic operator as "+, -, *, /, (, ),
55  * "unary+, unary-, function(, function)"
56  */
57 
58 class LogicOperator : public Operator
59 {
60 
61     /**
62      * Public stuff
63      */
64 public:
65     /**
66      * Constructors
67      */
68     LogicOperator();
69     LogicOperator(const std::string& operatorName);
70     LogicOperator(const LogicOperator& rhs);
71 
72     /**
73      * Destructors
74      */
75     virtual ~LogicOperator();
76 
77     /**
78      * Accessor Methods
79      */
80 
81     /** return a copy of this pointer
82      *
83      * deep copy of this pointer and return the copy
84      */
clone()85     inline virtual LogicOperator* clone() const
86     {
87         return new LogicOperator (*this);
88     }
89 
90     /**
91      * The serialization interface
92      */
93     virtual void serialize(messageqcpp::ByteStream&) const;
94     virtual void unserialize(messageqcpp::ByteStream&);
95 
96     /** @brief Do a deep, strict (as opposed to semantic) equivalence test
97      *
98      * Do a deep, strict (as opposed to semantic) equivalence test.
99      * @return true iff every member of t is a duplicate copy of every member of this; false otherwise
100     	 */
101     virtual bool operator==(const TreeNode* t) const;
102 
103     /** @brief Do a deep, strict (as opposed to semantic) equivalence test
104      *
105      * Do a deep, strict (as opposed to semantic) equivalence test.
106      * @return true iff every member of t is a duplicate copy of every member of this; false otherwise
107      */
108     bool operator==(const LogicOperator& t) const;
109 
110     /** @brief Do a deep, strict (as opposed to semantic) equivalence test
111      *
112      * Do a deep, strict (as opposed to semantic) equivalence test.
113      * @return false iff every member of t is a duplicate copy of every member of this; true otherwise
114      */
115     virtual bool operator!=(const TreeNode* t) const;
116 
117     /** @brief Do a deep, strict (as opposed to semantic) equivalence test
118      *
119      * Do a deep, strict (as opposed to semantic) equivalence test.
120      * @return false iff every member of t is a duplicate copy of every member of this; true otherwise
121      */
122     bool operator!=(const LogicOperator& t) const;
123     //template <typename result_t>
124     //result_t evaluate(result_t op1, result_t op2);
125 
126     // F&E framework
127     using Operator::getBoolVal;
getBoolVal(rowgroup::Row & row,bool & isNull,ParseTree * lop,ParseTree * rop)128     inline virtual bool getBoolVal(rowgroup::Row& row, bool& isNull, ParseTree* lop, ParseTree* rop)
129     {
130         switch (fOp)
131         {
132             case OP_AND:
133                 return (lop->getBoolVal(row, isNull) && rop->getBoolVal(row, isNull));
134 
135             case OP_OR:
136             {
137                 if (lop->getBoolVal(row, isNull))
138                     return true;
139 
140                 isNull = false;
141                 //return (lop->getBoolVal(row, isNull) || rop->getBoolVal(row, isNull));
142                 return rop->getBoolVal(row, isNull);
143             }
144 
145             case OP_XOR:
146             {
147                 // Logical XOR. Returns NULL if either operand is NULL.
148                 // For non-NULL operands, evaluates to 1 if an odd number of operands is nonzero,
149                 // otherwise 0 is returned.
150                 bool lopv = lop->getBoolVal(row, isNull);
151 
152                 if (isNull)
153                     return false;
154 
155                 bool ropv = rop->getBoolVal(row, isNull);
156 
157                 if (isNull)
158                     return false;
159 
160                 if ((lopv && !ropv) || (ropv && !lopv))
161                     return true;
162                 else
163                     return false;
164             }
165 
166             default:
167                 throw std::runtime_error("invalid logical operation");
168         }
169     }
170 
171     using TreeNode::evaluate;
evaluate(rowgroup::Row & row,bool & isNull,ParseTree * lop,ParseTree * rop)172     inline virtual void evaluate(rowgroup::Row& row, bool& isNull, ParseTree* lop, ParseTree* rop)
173     {
174         fResult.boolVal = getBoolVal(row, isNull, lop, rop);
175     }
176 
177 private:
178     // default okay
179     //Operator& operator=(const Operator& rhs);
180     //std::string fData;
181 
182 };
183 
184 //typedef boost::shared_ptr<Operator> SOP;
185 
186 std::ostream& operator<<(std::ostream& os, const LogicOperator& rhs);
187 }
188 
189 #endif
190 
191