#pragma once #include "common/exception/binder.h" #include "common/exception/catalog.h" #include "common/type_utils.h" #include "common/types/string_t.h" #include "function/scalar_function.h" namespace lbug { namespace function { struct UnaryUDFExecutor { template static inline void operation(OPERAND_TYPE& input, RESULT_TYPE& result, void* udfFunc) { typedef RESULT_TYPE (*unary_udf_func)(OPERAND_TYPE); auto unaryUDFFunc = (unary_udf_func)udfFunc; result = unaryUDFFunc(input); } }; struct BinaryUDFExecutor { template static inline void operation(LEFT_TYPE& left, RIGHT_TYPE& right, RESULT_TYPE& result, void* udfFunc) { typedef RESULT_TYPE (*binary_udf_func)(LEFT_TYPE, RIGHT_TYPE); auto binaryUDFFunc = (binary_udf_func)udfFunc; result = binaryUDFFunc(left, right); } }; struct TernaryUDFExecutor { template static inline void operation(A_TYPE& a, B_TYPE& b, C_TYPE& c, RESULT_TYPE& result, void* udfFunc) { typedef RESULT_TYPE (*ternary_udf_func)(A_TYPE, B_TYPE, C_TYPE); auto ternaryUDFFunc = (ternary_udf_func)udfFunc; result = ternaryUDFFunc(a, b, c); } }; struct UDF { template static bool templateValidateType(const common::LogicalTypeID& type) { auto logicalType = common::LogicalType{type}; auto physicalType = logicalType.getPhysicalType(); auto physicalTypeMatch = common::TypeUtils::visit(physicalType, [](T1) { return std::is_same::value; }); auto logicalTypeMatch = common::TypeUtils::visit(logicalType, [](T1) { return std::is_same::value; }); return logicalTypeMatch || physicalTypeMatch; } template static void validateType(const common::LogicalTypeID& type) { if (!templateValidateType(type)) { throw common::CatalogException{ "Incompatible udf parameter/return type and templated type."}; } } template static function::scalar_func_exec_t createEmptyParameterExecFunc(RESULT_TYPE (*)(Args...), const std::vector&) { UNREACHABLE_CODE; } template static function::scalar_func_exec_t createEmptyParameterExecFunc(RESULT_TYPE (*udfFunc)(), const std::vector&) { UNUSED(udfFunc); // Disable compiler warnings. return [udfFunc]( [[maybe_unused]] const std::vector>& params, [[maybe_unused]] const std::vector& paramSelVectors, common::ValueVector& result, common::SelectionVector* resultSelVector, void* /*dataPtr*/ = nullptr) -> void { DASSERT(params.empty() && paramSelVectors.empty()); for (auto i = 0u; i < resultSelVector->getSelSize(); ++i) { auto resultPos = (*resultSelVector)[i]; result.copyFromValue(resultPos, common::Value(udfFunc())); } }; } template static function::scalar_func_exec_t createUnaryExecFunc(RESULT_TYPE (* /*udfFunc*/)(Args...), const std::vector& /*parameterTypes*/) { UNREACHABLE_CODE; } template static function::scalar_func_exec_t createUnaryExecFunc(RESULT_TYPE (*udfFunc)(OPERAND_TYPE), const std::vector& parameterTypes) { if (parameterTypes.size() != 1) { throw common::CatalogException{ "Expected exactly one parameter type for unary udf. Got: " + std::to_string(parameterTypes.size()) + "."}; } validateType(parameterTypes[0]); function::scalar_func_exec_t execFunc = [udfFunc](const std::vector>& params, const std::vector& paramSelVectors, common::ValueVector& result, common::SelectionVector* resultSelVector, void* /*dataPtr*/ = nullptr) -> void { DASSERT(params.size() == 1); UnaryFunctionExecutor::executeSwitch(*params[0], paramSelVectors[0], result, resultSelVector, (void*)udfFunc); }; return execFunc; } template static function::scalar_func_exec_t createBinaryExecFunc(RESULT_TYPE (* /*udfFunc*/)(Args...), const std::vector& /*parameterTypes*/) { UNREACHABLE_CODE; } template static function::scalar_func_exec_t createBinaryExecFunc( RESULT_TYPE (*udfFunc)(LEFT_TYPE, RIGHT_TYPE), const std::vector& parameterTypes) { if (parameterTypes.size() != 2) { throw common::CatalogException{ "Expected exactly two parameter types for binary udf. Got: " + std::to_string(parameterTypes.size()) + "."}; } validateType(parameterTypes[0]); validateType(parameterTypes[1]); function::scalar_func_exec_t execFunc = [udfFunc](const std::vector>& params, const std::vector& paramSelVectors, common::ValueVector& result, common::SelectionVector* resultSelVector, void* /*dataPtr*/ = nullptr) -> void { DASSERT(params.size() == 2); BinaryFunctionExecutor::executeSwitch(*params[0], paramSelVectors[0], *params[1], paramSelVectors[1], result, resultSelVector, (void*)udfFunc); }; return execFunc; } template static function::scalar_func_exec_t createTernaryExecFunc(RESULT_TYPE (* /*udfFunc*/)(Args...), const std::vector& /*parameterTypes*/) { UNREACHABLE_CODE; } template static function::scalar_func_exec_t createTernaryExecFunc( RESULT_TYPE (*udfFunc)(A_TYPE, B_TYPE, C_TYPE), std::vector parameterTypes) { if (parameterTypes.size() != 3) { throw common::CatalogException{ "Expected exactly three parameter types for ternary udf. Got: " + std::to_string(parameterTypes.size()) + "."}; } validateType(parameterTypes[0]); validateType(parameterTypes[1]); validateType(parameterTypes[2]); function::scalar_func_exec_t execFunc = [udfFunc](const std::vector>& params, const std::vector& paramSelVectors, common::ValueVector& result, common::SelectionVector* resultSelVector, void* /*dataPtr*/ = nullptr) -> void { DASSERT(params.size() == 3); TernaryFunctionExecutor::executeSwitch(*params[0], paramSelVectors[0], *params[1], paramSelVectors[1], *params[2], paramSelVectors[2], result, resultSelVector, (void*)udfFunc); }; return execFunc; } template static scalar_func_exec_t getScalarExecFunc(TR (*udfFunc)(Args...), std::vector parameterTypes) { constexpr auto numArgs = sizeof...(Args); switch (numArgs) { case 0: return createEmptyParameterExecFunc(udfFunc, std::move(parameterTypes)); case 1: return createUnaryExecFunc(udfFunc, std::move(parameterTypes)); case 2: return createBinaryExecFunc(udfFunc, std::move(parameterTypes)); case 3: return createTernaryExecFunc(udfFunc, std::move(parameterTypes)); default: throw common::BinderException("UDF function only supported until ternary!"); } } template static common::LogicalTypeID getParameterType() { if (std::is_same()) { return common::LogicalTypeID::BOOL; } else if (std::is_same()) { return common::LogicalTypeID::INT8; } else if (std::is_same()) { return common::LogicalTypeID::INT16; } else if (std::is_same()) { return common::LogicalTypeID::INT32; } else if (std::is_same()) { return common::LogicalTypeID::INT64; } else if (std::is_same()) { return common::LogicalTypeID::INT128; } else if (std::is_same()) { return common::LogicalTypeID::UINT8; } else if (std::is_same()) { return common::LogicalTypeID::UINT16; } else if (std::is_same()) { return common::LogicalTypeID::UINT32; } else if (std::is_same()) { return common::LogicalTypeID::UINT64; } else if (std::is_same()) { return common::LogicalTypeID::FLOAT; } else if (std::is_same()) { return common::LogicalTypeID::DOUBLE; } else if (std::is_same()) { return common::LogicalTypeID::STRING; } else { UNREACHABLE_CODE; } } template static void getParameterTypesRecursive(std::vector& arguments) { arguments.push_back(getParameterType()); } template static void getParameterTypesRecursive(std::vector& arguments) { arguments.push_back(getParameterType()); getParameterTypesRecursive(arguments); } template static std::vector getParameterTypes() { std::vector parameterTypes; if constexpr (sizeof...(Args) > 0) { getParameterTypesRecursive(parameterTypes); } return parameterTypes; } template static function_set getFunction(std::string name, TR (*udfFunc)(Args...), std::vector parameterTypes, common::LogicalTypeID returnType) { function_set definitions; if (returnType == common::LogicalTypeID::STRING) { UNREACHABLE_CODE; } validateType(returnType); scalar_func_exec_t scalarExecFunc = getScalarExecFunc(udfFunc, parameterTypes); definitions.push_back(std::make_unique(std::move(name), std::move(parameterTypes), returnType, std::move(scalarExecFunc))); return definitions; } template static function_set getFunction(std::string name, TR (*udfFunc)(Args...)) { return getFunction(std::move(name), udfFunc, getParameterTypes(), getParameterType()); } template static function_set getVectorizedFunction(std::string name, scalar_func_exec_t execFunc) { function_set definitions; definitions.push_back(std::make_unique(std::move(name), getParameterTypes(), getParameterType(), std::move(execFunc))); return definitions; } static function_set getVectorizedFunction(std::string name, scalar_func_exec_t execFunc, std::vector parameterTypes, common::LogicalTypeID returnType) { function_set definitions; definitions.push_back(std::make_unique(std::move(name), std::move(parameterTypes), returnType, std::move(execFunc))); return definitions; } }; } // namespace function } // namespace lbug