Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup bitwise and arithmetic operations
- Loading branch information
Showing
17 changed files
with
231 additions
and
367 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 0 additions & 71 deletions
71
src/common/vector_operations/numeric_bitwise_operators.cpp
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.