| 1 | #include "duckdb/function/scalar/operators.hpp" | 
|---|
| 2 | #include "duckdb/common/vector_operations/vector_operations.hpp" | 
|---|
| 3 |  | 
|---|
| 4 | using namespace duckdb; | 
|---|
| 5 | using namespace std; | 
|---|
| 6 |  | 
|---|
| 7 | namespace duckdb { | 
|---|
| 8 |  | 
|---|
| 9 | //===--------------------------------------------------------------------===// | 
|---|
| 10 | // & [bitwise_and] | 
|---|
| 11 | //===--------------------------------------------------------------------===// | 
|---|
| 12 | struct BitwiseANDOperator { | 
|---|
| 13 | template <class TA, class TB, class TR> static inline TR Operation(TA left, TB right) { | 
|---|
| 14 | return left & right; | 
|---|
| 15 | } | 
|---|
| 16 | }; | 
|---|
| 17 |  | 
|---|
| 18 | void BitwiseAndFun::RegisterFunction(BuiltinFunctions &set) { | 
|---|
| 19 | ScalarFunctionSet functions( "&"); | 
|---|
| 20 | for (auto &type : SQLType::INTEGRAL) { | 
|---|
| 21 | functions.AddFunction(ScalarFunction({type, type}, type, | 
|---|
| 22 | ScalarFunction::GetScalarIntegerBinaryFunction<BitwiseANDOperator>(type))); | 
|---|
| 23 | } | 
|---|
| 24 | set.AddFunction(functions); | 
|---|
| 25 | } | 
|---|
| 26 |  | 
|---|
| 27 | //===--------------------------------------------------------------------===// | 
|---|
| 28 | // | [bitwise_or] | 
|---|
| 29 | //===--------------------------------------------------------------------===// | 
|---|
| 30 | struct BitwiseOROperator { | 
|---|
| 31 | template <class TA, class TB, class TR> static inline TR Operation(TA left, TB right) { | 
|---|
| 32 | return left | right; | 
|---|
| 33 | } | 
|---|
| 34 | }; | 
|---|
| 35 |  | 
|---|
| 36 | void BitwiseOrFun::RegisterFunction(BuiltinFunctions &set) { | 
|---|
| 37 | ScalarFunctionSet functions( "|"); | 
|---|
| 38 | for (auto &type : SQLType::INTEGRAL) { | 
|---|
| 39 | functions.AddFunction(ScalarFunction({type, type}, type, | 
|---|
| 40 | ScalarFunction::GetScalarIntegerBinaryFunction<BitwiseOROperator>(type))); | 
|---|
| 41 | } | 
|---|
| 42 | set.AddFunction(functions); | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | //===--------------------------------------------------------------------===// | 
|---|
| 46 | // # [bitwise_xor] | 
|---|
| 47 | //===--------------------------------------------------------------------===// | 
|---|
| 48 | struct BitwiseXOROperator { | 
|---|
| 49 | template <class TA, class TB, class TR> static inline TR Operation(TA left, TB right) { | 
|---|
| 50 | return left ^ right; | 
|---|
| 51 | } | 
|---|
| 52 | }; | 
|---|
| 53 |  | 
|---|
| 54 | void BitwiseXorFun::RegisterFunction(BuiltinFunctions &set) { | 
|---|
| 55 | ScalarFunctionSet functions( "#"); | 
|---|
| 56 | for (auto &type : SQLType::INTEGRAL) { | 
|---|
| 57 | functions.AddFunction(ScalarFunction({type, type}, type, | 
|---|
| 58 | ScalarFunction::GetScalarIntegerBinaryFunction<BitwiseXOROperator>(type))); | 
|---|
| 59 | } | 
|---|
| 60 | set.AddFunction(functions); | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | //===--------------------------------------------------------------------===// | 
|---|
| 64 | // << [bitwise_left_shift] | 
|---|
| 65 | //===--------------------------------------------------------------------===// | 
|---|
| 66 | template <class T> bool ShiftInRange(T shift) { | 
|---|
| 67 | return shift >= 0 && shift < (T)(sizeof(T) * 8); | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | struct BitwiseShiftLeftOperator { | 
|---|
| 71 | template <class TA, class TB, class TR> static inline TR Operation(TA input, TB shift) { | 
|---|
| 72 | return ShiftInRange(shift) ? input << shift : 0; | 
|---|
| 73 | } | 
|---|
| 74 | }; | 
|---|
| 75 |  | 
|---|
| 76 | void LeftShiftFun::RegisterFunction(BuiltinFunctions &set) { | 
|---|
| 77 | ScalarFunctionSet functions( "<<"); | 
|---|
| 78 | for (auto &type : SQLType::INTEGRAL) { | 
|---|
| 79 | functions.AddFunction(ScalarFunction( | 
|---|
| 80 | {type, type}, type, ScalarFunction::GetScalarIntegerBinaryFunction<BitwiseShiftLeftOperator>(type))); | 
|---|
| 81 | } | 
|---|
| 82 | set.AddFunction(functions); | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | //===--------------------------------------------------------------------===// | 
|---|
| 86 | // >> [bitwise_right_shift] | 
|---|
| 87 | //===--------------------------------------------------------------------===// | 
|---|
| 88 | struct BitwiseShiftRightOperator { | 
|---|
| 89 | template <class TA, class TB, class TR> static inline TR Operation(TA input, TB shift) { | 
|---|
| 90 | return ShiftInRange(shift) ? input >> shift : 0; | 
|---|
| 91 | } | 
|---|
| 92 | }; | 
|---|
| 93 |  | 
|---|
| 94 | void RightShiftFun::RegisterFunction(BuiltinFunctions &set) { | 
|---|
| 95 | ScalarFunctionSet functions( ">>"); | 
|---|
| 96 | for (auto &type : SQLType::INTEGRAL) { | 
|---|
| 97 | functions.AddFunction(ScalarFunction( | 
|---|
| 98 | {type, type}, type, ScalarFunction::GetScalarIntegerBinaryFunction<BitwiseShiftRightOperator>(type))); | 
|---|
| 99 | } | 
|---|
| 100 | set.AddFunction(functions); | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | } // namespace duckdb | 
|---|
| 104 |  | 
|---|