Skip to content

Commit

Permalink
Cleanup bitwise and arithmetic operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Mytherin committed Dec 16, 2019
1 parent d1d4cd4 commit 7ddd9fc
Show file tree
Hide file tree
Showing 17 changed files with 231 additions and 367 deletions.
1 change: 0 additions & 1 deletion src/common/vector_operations/CMakeLists.txt
Expand Up @@ -10,7 +10,6 @@ add_library_unity(duckdb_vector_operations
gather.cpp
hash.cpp
null_operations.cpp
numeric_bitwise_operators.cpp
numeric_inplace_operators.cpp
numeric_inplace_bitwise_operators.cpp
numeric_binary_operators.cpp
Expand Down
71 changes: 0 additions & 71 deletions src/common/vector_operations/numeric_bitwise_operators.cpp

This file was deleted.

12 changes: 2 additions & 10 deletions src/function/scalar/operators/CMakeLists.txt
@@ -1,15 +1,7 @@
add_library_unity(duckdb_func_ops
OBJECT
add.cpp
subtract.cpp
multiply.cpp
divide.cpp
mod.cpp
left_shift.cpp
right_shift.cpp
bitwise_and.cpp
bitwise_or.cpp
bitwise_xor.cpp)
arithmetic.cpp
bitwise.cpp)
set(ALL_OBJECT_FILES
${ALL_OBJECT_FILES} $<TARGET_OBJECTS:duckdb_func_ops>
PARENT_SCOPE)
32 changes: 0 additions & 32 deletions src/function/scalar/operators/add.cpp

This file was deleted.

120 changes: 120 additions & 0 deletions src/function/scalar/operators/arithmetic.cpp
@@ -0,0 +1,120 @@
#include "duckdb/function/scalar/operators.hpp"
#include "duckdb/common/vector_operations/vector_operations.hpp"

using namespace std;

namespace duckdb {

//===--------------------------------------------------------------------===//
// + [add]
//===--------------------------------------------------------------------===//
static void add_function(ExpressionExecutor &exec, Vector inputs[], index_t input_count, BoundFunctionExpression &expr,
Vector &result) {
result.Initialize(inputs[0].type);
VectorOperations::Add(inputs[0], inputs[1], result);
}

void AddFun::RegisterFunction(BuiltinFunctions &set) {
ScalarFunctionSet functions("+");
// binary add function adds two numbers together
for (auto &type : SQLType::NUMERIC) {
functions.AddFunction(ScalarFunction({type, type}, type, add_function));
}
// we can add integers to dates
functions.AddFunction(ScalarFunction({SQLType::DATE, SQLType::INTEGER}, SQLType::DATE, add_function));
functions.AddFunction(ScalarFunction({SQLType::INTEGER, SQLType::DATE}, SQLType::DATE, add_function));
// unary add function is a nop, but only exists for numeric types
for (auto &type : SQLType::NUMERIC) {
functions.AddFunction(ScalarFunction({type}, type, ScalarFunction::NopFunction));
}
set.AddFunction(functions);
}

//===--------------------------------------------------------------------===//
// - [subtract]
//===--------------------------------------------------------------------===//
static void subtract_function(ExpressionExecutor &exec, Vector inputs[], index_t input_count,
BoundFunctionExpression &expr, Vector &result) {
result.Initialize(inputs[0].type);
VectorOperations::Subtract(inputs[0], inputs[1], result);
}

static void unary_subtract_function(ExpressionExecutor &exec, Vector inputs[], index_t input_count,
BoundFunctionExpression &expr, Vector &result) {
Value minus_one = Value::Numeric(inputs[0].type, -1);
Vector right;
right.Reference(minus_one);

result.Initialize(inputs[0].type);
VectorOperations::Multiply(inputs[0], right, result);
}

void SubtractFun::RegisterFunction(BuiltinFunctions &set) {
ScalarFunctionSet functions("-");
// binary subtract function "a - b", subtracts b from a
for (auto &type : SQLType::NUMERIC) {
functions.AddFunction(ScalarFunction({type, type}, type, subtract_function));
}
functions.AddFunction(ScalarFunction({SQLType::DATE, SQLType::DATE}, SQLType::INTEGER, subtract_function));
functions.AddFunction(ScalarFunction({SQLType::DATE, SQLType::INTEGER}, SQLType::DATE, subtract_function));
// unary subtract function, negates the input (i.e. multiplies by -1)
for (auto &type : SQLType::NUMERIC) {
functions.AddFunction(ScalarFunction({type}, type, unary_subtract_function));
}
set.AddFunction(functions);
}

//===--------------------------------------------------------------------===//
// * [multiply]
//===--------------------------------------------------------------------===//
static void multiply_function(ExpressionExecutor &exec, Vector inputs[], index_t input_count,
BoundFunctionExpression &expr, Vector &result) {
result.Initialize(inputs[0].type);
VectorOperations::Multiply(inputs[0], inputs[1], result);
}

void MultiplyFun::RegisterFunction(BuiltinFunctions &set) {
ScalarFunctionSet functions("*");
for (auto &type : SQLType::NUMERIC) {
functions.AddFunction(ScalarFunction({type, type}, type, multiply_function));
}
set.AddFunction(functions);
}

//===--------------------------------------------------------------------===//
// / [divide]
//===--------------------------------------------------------------------===//
static void divide_function(ExpressionExecutor &exec, Vector inputs[], index_t input_count,
BoundFunctionExpression &expr, Vector &result) {
result.Initialize(inputs[0].type);
VectorOperations::Divide(inputs[0], inputs[1], result);
}

void DivideFun::RegisterFunction(BuiltinFunctions &set) {
ScalarFunctionSet functions("/");
for (auto &type : SQLType::NUMERIC) {
functions.AddFunction(ScalarFunction({type, type}, type, divide_function));
}
set.AddFunction(functions);
}

//===--------------------------------------------------------------------===//
// % [modulo]
//===--------------------------------------------------------------------===//
static void mod_function(ExpressionExecutor &exec, Vector inputs[], index_t input_count, BoundFunctionExpression &expr,
Vector &result) {
result.Initialize(inputs[0].type);
VectorOperations::Modulo(inputs[0], inputs[1], result);
}

void ModFun::RegisterFunction(BuiltinFunctions &set) {
ScalarFunctionSet functions("%");
for (auto &type : SQLType::NUMERIC) {
functions.AddFunction(ScalarFunction({type, type}, type, mod_function));
}
set.AddFunction(functions);
functions.name = "mod";
set.AddFunction(functions);
}

}
109 changes: 109 additions & 0 deletions src/function/scalar/operators/bitwise.cpp
@@ -0,0 +1,109 @@
#include "duckdb/function/scalar/operators.hpp"
#include "duckdb/common/vector_operations/vector_operations.hpp"

using namespace duckdb;
using namespace std;

namespace duckdb {

template <class OP> static scalar_function_t GetScalarIntegerBinaryFunction(SQLType type) {
switch (type.id) {
case SQLTypeId::TINYINT:
return ScalarFunction::BinaryFunction<int8_t, int8_t, int8_t, OP>;
case SQLTypeId::SMALLINT:
return ScalarFunction::BinaryFunction<int16_t, int16_t, int16_t, OP>;
case SQLTypeId::INTEGER:
return ScalarFunction::BinaryFunction<int32_t, int32_t, int32_t, OP>;
case SQLTypeId::BIGINT:
return ScalarFunction::BinaryFunction<int64_t, int64_t, int64_t, OP>;
default:
throw NotImplementedException("Unimplemented type for GetScalarIntegerBinaryFunction");
}
}

//===--------------------------------------------------------------------===//
// & [bitwise_and]
//===--------------------------------------------------------------------===//
struct BitwiseANDOperator {
template <class T> static inline T Operation(T left, T right) {
return left & right;
}
};

void BitwiseAndFun::RegisterFunction(BuiltinFunctions &set) {
ScalarFunctionSet functions("&");
for (auto &type : SQLType::INTEGRAL) {
functions.AddFunction(ScalarFunction({type, type}, type, GetScalarIntegerBinaryFunction<BitwiseANDOperator>(type)));
}
set.AddFunction(functions);
}

//===--------------------------------------------------------------------===//
// | [bitwise_or]
//===--------------------------------------------------------------------===//
struct BitwiseOROperator {
template <class T> static inline T Operation(T left, T right) {
return left | right;
}
};

void BitwiseOrFun::RegisterFunction(BuiltinFunctions &set) {
ScalarFunctionSet functions("|");
for (auto &type : SQLType::INTEGRAL) {
functions.AddFunction(ScalarFunction({type, type}, type, GetScalarIntegerBinaryFunction<BitwiseOROperator>(type)));
}
set.AddFunction(functions);
}

//===--------------------------------------------------------------------===//
// # [bitwise_xor]
//===--------------------------------------------------------------------===//
struct BitwiseXOROperator {
template <class T> static inline T Operation(T left, T right) {
return left ^ right;
}
};

void BitwiseXorFun::RegisterFunction(BuiltinFunctions &set) {
ScalarFunctionSet functions("#");
for (auto &type : SQLType::INTEGRAL) {
functions.AddFunction(ScalarFunction({type, type}, type, GetScalarIntegerBinaryFunction<BitwiseXOROperator>(type)));
}
set.AddFunction(functions);
}

//===--------------------------------------------------------------------===//
// << [bitwise_left_shift]
//===--------------------------------------------------------------------===//
struct BitwiseShiftLeftOperator {
template <class T> static inline T Operation(T left, T right) {
return left << right;
}
};

void LeftShiftFun::RegisterFunction(BuiltinFunctions &set) {
ScalarFunctionSet functions("<<");
for (auto &type : SQLType::INTEGRAL) {
functions.AddFunction(ScalarFunction({type, type}, type, GetScalarIntegerBinaryFunction<BitwiseShiftLeftOperator>(type)));
}
set.AddFunction(functions);
}

//===--------------------------------------------------------------------===//
// >> [bitwise_right_shift]
//===--------------------------------------------------------------------===//
struct BitwiseShiftRightOperator {
template <class T> static inline T Operation(T left, T right) {
return left >> right;
}
};

void RightShiftFun::RegisterFunction(BuiltinFunctions &set) {
ScalarFunctionSet functions(">>");
for (auto &type : SQLType::INTEGRAL) {
functions.AddFunction(ScalarFunction({type, type}, type, GetScalarIntegerBinaryFunction<BitwiseShiftRightOperator>(type)));
}
set.AddFunction(functions);
}

}
19 changes: 0 additions & 19 deletions src/function/scalar/operators/bitwise_and.cpp

This file was deleted.

0 comments on commit 7ddd9fc

Please sign in to comment.