From a6bc9cbe539a1234ebeecd8c39a65308713513c7 Mon Sep 17 00:00:00 2001 From: HappenLee Date: Thu, 24 Feb 2022 11:06:58 +0800 Subject: [PATCH] [Function] Refactor the function code of log (#8199) 1. Support return null when input is invalid 2. Del the unless code in vec function Co-authored-by: lihaopeng --- be/src/exprs/math_functions.cpp | 12 +- .../functions/function_binary_arithmetic.h | 7 + .../functions/function_math_binary_float64.h | 248 ------------------ be/src/vec/functions/function_math_unary.h | 1 + .../function_math_unary_to_null_type.h | 122 +++++++++ be/src/vec/functions/math.cpp | 127 +++++---- be/src/vec/functions/modulo.cpp | 2 +- be/src/vec/io/io_helper.h | 1 + be/test/vec/function/function_math_test.cpp | 11 +- be/test/vec/function/function_test_util.h | 1 - gensrc/script/doris_builtins_functions.py | 8 +- 11 files changed, 226 insertions(+), 314 deletions(-) delete mode 100644 be/src/vec/functions/function_math_binary_float64.h create mode 100644 be/src/vec/functions/function_math_unary_to_null_type.h diff --git a/be/src/exprs/math_functions.cpp b/be/src/exprs/math_functions.cpp index 708fc425d9..13fe610971 100644 --- a/be/src/exprs/math_functions.cpp +++ b/be/src/exprs/math_functions.cpp @@ -161,6 +161,12 @@ SmallIntVal MathFunctions::abs(FunctionContext* ctx, const doris_udf::TinyIntVal return SmallIntVal(std::abs(int16_t(val.val))); } +#define LOG_MATH_FN(NAME, RET_TYPE, INPUT_TYPE, FN) \ + RET_TYPE MathFunctions::NAME(FunctionContext* ctx, const INPUT_TYPE& v) { \ + if (v.is_null || v.val <= 0) return RET_TYPE::null(); \ + return RET_TYPE(FN(v.val)); \ + } + // Generates a UDF that always calls FN() on the input val and returns it. #define ONE_ARG_MATH_FN(NAME, RET_TYPE, INPUT_TYPE, FN) \ RET_TYPE MathFunctions::NAME(FunctionContext* ctx, const INPUT_TYPE& v) { \ @@ -179,9 +185,9 @@ ONE_ARG_MATH_FN(atan, DoubleVal, DoubleVal, std::atan); ONE_ARG_MATH_FN(sqrt, DoubleVal, DoubleVal, std::sqrt); ONE_ARG_MATH_FN(ceil, BigIntVal, DoubleVal, std::ceil); ONE_ARG_MATH_FN(floor, BigIntVal, DoubleVal, std::floor); -ONE_ARG_MATH_FN(ln, DoubleVal, DoubleVal, std::log); -ONE_ARG_MATH_FN(log10, DoubleVal, DoubleVal, std::log10); ONE_ARG_MATH_FN(exp, DoubleVal, DoubleVal, std::exp); +LOG_MATH_FN(ln, DoubleVal, DoubleVal, std::log); +LOG_MATH_FN(log10, DoubleVal, DoubleVal, std::log10); TinyIntVal MathFunctions::sign(FunctionContext* ctx, const DoubleVal& v) { if (v.is_null) { @@ -227,7 +233,7 @@ DoubleVal MathFunctions::truncate(FunctionContext* ctx, const DoubleVal& v, cons } DoubleVal MathFunctions::log2(FunctionContext* ctx, const DoubleVal& v) { - if (v.is_null) { + if (v.is_null || v.val <= 0.0) { return DoubleVal::null(); } return DoubleVal(std::log(v.val) / std::log(2.0)); diff --git a/be/src/vec/functions/function_binary_arithmetic.h b/be/src/vec/functions/function_binary_arithmetic.h index f987f90eeb..da1cabbc4a 100644 --- a/be/src/vec/functions/function_binary_arithmetic.h +++ b/be/src/vec/functions/function_binary_arithmetic.h @@ -461,6 +461,8 @@ template