Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
operators.cc
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "exprs/operators.h"
16 #include "exprs/anyval-util.h"
17 #include "runtime/string-value.h"
19 
20 #define BINARY_OP_FN(NAME, TYPE, OP) \
21  TYPE Operators::NAME##_##TYPE##_##TYPE(\
22  FunctionContext* c, const TYPE& v1, const TYPE& v2) {\
23  if (v1.is_null || v2.is_null) return TYPE::null();\
24  return TYPE(v1.val OP v2.val);\
25  }
26 
27 #define BINARY_OP_CHECK_ZERO_FN(NAME, TYPE, OP) \
28  TYPE Operators::NAME##_##TYPE##_##TYPE(\
29  FunctionContext* c, const TYPE& v1, const TYPE& v2) {\
30  if (v1.is_null || v2.is_null || v2.val == 0) return TYPE::null();\
31  return TYPE(v1.val OP v2.val);\
32  }
33 
34 #define BITNOT_FN(TYPE)\
35  TYPE Operators::Bitnot_##TYPE(FunctionContext* c, const TYPE& v) {\
36  if (v.is_null) return TYPE::null();\
37  return TYPE(~v.val);\
38  }
39 
40 #define BINARY_PREDICATE_NUMERIC_FN(NAME, TYPE, OP) \
41  BooleanVal Operators::NAME##_##TYPE##_##TYPE(\
42  FunctionContext* c, const TYPE& v1, const TYPE& v2) {\
43  if (v1.is_null || v2.is_null) return BooleanVal::null();\
44  return BooleanVal(v1.val OP v2.val);\
45  }
46 
47 #define BINARY_PREDICATE_NONNUMERIC_FN(NAME, TYPE, IMPALA_TYPE, OP) \
48  BooleanVal Operators::NAME##_##TYPE##_##TYPE(\
49  FunctionContext* c, const TYPE& v1, const TYPE& v2) {\
50  if (v1.is_null || v2.is_null) return BooleanVal::null();\
51  IMPALA_TYPE iv1 = IMPALA_TYPE::From##TYPE(v1);\
52  IMPALA_TYPE iv2 = IMPALA_TYPE::From##TYPE(v2);\
53  return BooleanVal(iv1 OP iv2);\
54  }
55 
56 #define BINARY_PREDICATE_CHAR(NAME, OP) \
57  BooleanVal Operators::NAME##_Char_Char(\
58  FunctionContext* c, const StringVal& v1, const StringVal& v2) {\
59  if (v1.is_null || v2.is_null) return BooleanVal::null();\
60  const ColumnType& v1_type = AnyValUtil::TypeDescToColumnType(*c->GetArgType(0));\
61  const ColumnType& v2_type = AnyValUtil::TypeDescToColumnType(*c->GetArgType(1));\
62  StringValue iv1 = StringValue::FromStringVal(v1);\
63  StringValue iv2 = StringValue::FromStringVal(v2);\
64  iv1.len = StringValue::UnpaddedCharLength(iv1.ptr, v1_type.len);\
65  iv2.len = StringValue::UnpaddedCharLength(iv2.ptr, v2_type.len);\
66  return BooleanVal(iv1 OP iv2);\
67  }
68 
69 #define BINARY_OP_NUMERIC_TYPES(NAME, OP) \
70  BINARY_OP_FN(NAME, TinyIntVal, OP); \
71  BINARY_OP_FN(NAME, SmallIntVal, OP);\
72  BINARY_OP_FN(NAME, IntVal, OP);\
73  BINARY_OP_FN(NAME, BigIntVal, OP);\
74  BINARY_OP_FN(NAME, FloatVal, OP);\
75  BINARY_OP_FN(NAME, DoubleVal, OP);
76 
77 #define BINARY_OP_INT_TYPES(NAME, OP) \
78  BINARY_OP_FN(NAME, TinyIntVal, OP); \
79  BINARY_OP_FN(NAME, SmallIntVal, OP);\
80  BINARY_OP_FN(NAME, IntVal, OP);\
81  BINARY_OP_FN(NAME, BigIntVal, OP);\
82 
83 #define BINARY_OP_CHECK_ZERO_INT_TYPES(NAME, OP) \
84  BINARY_OP_CHECK_ZERO_FN(NAME, TinyIntVal, OP); \
85  BINARY_OP_CHECK_ZERO_FN(NAME, SmallIntVal, OP);\
86  BINARY_OP_CHECK_ZERO_FN(NAME, IntVal, OP);\
87  BINARY_OP_CHECK_ZERO_FN(NAME, BigIntVal, OP);\
88 
89 #define BINARY_PREDICATE_ALL_TYPES(NAME, OP) \
90  BINARY_PREDICATE_NUMERIC_FN(NAME, BooleanVal, OP); \
91  BINARY_PREDICATE_NUMERIC_FN(NAME, TinyIntVal, OP); \
92  BINARY_PREDICATE_NUMERIC_FN(NAME, SmallIntVal, OP);\
93  BINARY_PREDICATE_NUMERIC_FN(NAME, IntVal, OP);\
94  BINARY_PREDICATE_NUMERIC_FN(NAME, BigIntVal, OP);\
95  BINARY_PREDICATE_NUMERIC_FN(NAME, FloatVal, OP);\
96  BINARY_PREDICATE_NUMERIC_FN(NAME, DoubleVal, OP);\
97  BINARY_PREDICATE_NONNUMERIC_FN(NAME, StringVal, StringValue, OP);\
98  BINARY_PREDICATE_NONNUMERIC_FN(NAME, TimestampVal, TimestampValue, OP);\
99  BINARY_PREDICATE_CHAR(NAME, OP);
100 
101 namespace impala {
102 
104 BINARY_OP_NUMERIC_TYPES(Subtract, -);
105 BINARY_OP_NUMERIC_TYPES(Multiply, *);
106 
107 BINARY_OP_FN(Divide, DoubleVal, /);
108 
109 BINARY_OP_CHECK_ZERO_INT_TYPES(Int_divide, /);
111 BINARY_OP_INT_TYPES(Bitand, &);
112 BINARY_OP_INT_TYPES(Bitxor, ^);
113 BINARY_OP_INT_TYPES(Bitor, |);
114 
119 
126 
127 } // namespace impala
BINARY_OP_INT_TYPES(Bitand,&)
BINARY_PREDICATE_ALL_TYPES(Eq,==)
BINARY_OP_NUMERIC_TYPES(Add,+)
BINARY_OP_CHECK_ZERO_INT_TYPES(Int_divide,/)
BINARY_OP_FN(Divide, DoubleVal,/)
BITNOT_FN(TinyIntVal)