1 /* Copyright (C) 2014 InfiniDB, Inc.
2    Copyright (C) 2019 MariaDB Corporation
3 
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License
6    as published by the Free Software Foundation; version 2 of
7    the License.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17    MA 02110-1301, USA. */
18 
19 /***********************************************************************
20 *   $Id: predicateoperator.h 9667 2013-07-08 16:37:10Z bpaul $
21 *
22 *
23 ***********************************************************************/
24 /** @file */
25 
26 #ifndef PREDICATEOPERATOR_H
27 #define PREDICATEOPERATOR_H
28 
29 #include <string>
30 #include <sstream>
31 #if defined(_MSC_VER)
32 #include <malloc.h>
33 #elif defined(__FreeBSD__)
34 #include <cstdlib>
35 #else
36 #include <alloca.h>
37 #endif
38 #include <cstring>
39 #include <cmath>
40 #include <boost/regex.hpp>
41 
42 #include "utils_utf8.h"
43 #include "expressionparser.h"
44 #include "returnedcolumn.h"
45 #include "dataconvert.h"
46 
47 #include "collation.h"
48 
49 namespace messageqcpp
50 {
51 class ByteStream;
52 }
53 
54 namespace execplan
55 {
56 
57 class PredicateOperator : public Operator
58 {
59 
60 public:
61     PredicateOperator();
62     PredicateOperator(const std::string& operatorName);
63     PredicateOperator(const PredicateOperator& rhs);
64     virtual ~PredicateOperator();
65 
66 
67     /** return a copy of this pointer
68      *
69      * deep copy of this pointer and return the copy
70      */
clone()71     inline virtual PredicateOperator* clone() const
72     {
73         return new PredicateOperator (*this);
74     }
75 
76     /**
77      * The serialization interface
78      */
79     virtual void serialize(messageqcpp::ByteStream&) const;
80     virtual void unserialize(messageqcpp::ByteStream&);
81 
82     /** @brief Do a deep, strict (as opposed to semantic) equivalence test
83      *
84      * Do a deep, strict (as opposed to semantic) equivalence test.
85      * @return true iff every member of t is a duplicate copy of every member of this; false otherwise
86     	 */
87     virtual bool operator==(const TreeNode* t) const;
88 
89     /** @brief Do a deep, strict (as opposed to semantic) equivalence test
90      *
91      * Do a deep, strict (as opposed to semantic) equivalence test.
92      * @return true iff every member of t is a duplicate copy of every member of this; false otherwise
93      */
94     bool operator==(const PredicateOperator& t) const;
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 false iff every member of t is a duplicate copy of every member of this; true 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 false iff every member of t is a duplicate copy of every member of this; true otherwise
107      */
108     bool operator!=(const PredicateOperator& t) const;
109 
getCharset()110     const CHARSET_INFO* getCharset() const
111     {
112         return cs;
113     }
114     /***********************************************************
115      *                  F&E framework                          *
116      ***********************************************************/
117     using Operator::getBoolVal;
118     virtual bool getBoolVal(rowgroup::Row& row, bool& isNull, ReturnedColumn* lop, ReturnedColumn* rop);
119     void setOpType(Type& l, Type& r);
120 
121 private:
122     template <typename result_t>
123     inline bool numericCompare(result_t op1, result_t op2);
124     inline bool strTrimCompare(const std::string& op1, const std::string& op2);
125 
126     const CHARSET_INFO* cs;
127 };
128 
129 
130 template <typename result_t>
numericCompare(result_t op1,result_t op2)131 inline bool PredicateOperator::numericCompare(result_t op1, result_t op2)
132 {
133     switch (fOp)
134     {
135         case OP_EQ:
136             return op1 == op2;
137 
138         case OP_NE:
139             return op1 != op2;
140 
141         case OP_GT:
142             return op1 > op2;
143 
144         case OP_GE:
145             return op1 >= op2;
146 
147         case OP_LT:
148             return op1 < op2;
149 
150         case OP_LE:
151             return op1 <= op2;
152 
153         default:
154         {
155             std::ostringstream oss;
156             oss << "invalid predicate operation: " << fOp;
157             throw logging::InvalidOperationExcept(oss.str());
158         }
159     }
160 }
161 
162 std::ostream& operator<<(std::ostream& os, const PredicateOperator& rhs);
163 }
164 
165 #endif //PREDICATEOPERATOR_H
166 
167