// Licensed to the Apache Software Foundation (ASF) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information // regarding copyright ownership. The ASF licenses this file // to you under the Apache License, Version 2.0 (the // "License"); you may not use this file except in compliance // with the License. You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, // software distributed under the License is distributed on an // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. See the License for the // specific language governing permissions and limitations // under the License. #include #include #include // IWYU pragma: no_include #include #include #include #include #include #include #include "common/status.h" #include "vec/aggregate_functions/aggregate_function.h" #include "vec/columns/column.h" #include "vec/columns/column_string.h" #include "vec/columns/column_vector.h" #include "vec/columns/columns_number.h" #include "vec/core/types.h" #include "vec/data_types/data_type.h" #include "vec/data_types/data_type_decimal.h" #include "vec/data_types/data_type_nullable.h" #include "vec/data_types/data_type_number.h" #include "vec/data_types/data_type_string.h" #include "vec/data_types/number_traits.h" #include "vec/functions/function_binary_arithmetic.h" #include "vec/functions/function_const.h" #include "vec/functions/function_math_unary.h" #include "vec/functions/function_math_unary_to_null_type.h" #include "vec/functions/function_string.h" #include "vec/functions/function_totype.h" #include "vec/functions/function_unary_arithmetic.h" #include "vec/functions/round.h" #include "vec/functions/simple_function_factory.h" namespace doris { namespace vectorized { struct LnImpl; struct Log10Impl; struct Log2Impl; } // namespace vectorized } // namespace doris namespace doris::vectorized { struct AcosName { static constexpr auto name = "acos"; }; using FunctionAcos = FunctionMathUnary>; struct AsinName { static constexpr auto name = "asin"; }; using FunctionAsin = FunctionMathUnary>; struct AtanName { static constexpr auto name = "atan"; }; using FunctionAtan = FunctionMathUnary>; struct CosName { static constexpr auto name = "cos"; }; using FunctionCos = FunctionMathUnary>; struct EImpl { static constexpr auto name = "e"; static constexpr double value = 2.7182818284590452353602874713526624977572470; }; using FunctionE = FunctionMathConstFloat64; struct PiImpl { static constexpr auto name = "pi"; static constexpr double value = 3.1415926535897932384626433832795028841971693; }; using FunctionPi = FunctionMathConstFloat64; struct ExpName { static constexpr auto name = "exp"; }; using FunctionExp = FunctionMathUnary>; #define LOG_FUNCTION_IMPL(CLASS, NAME, FUNC) \ struct CLASS##Impl { \ using Type = DataTypeFloat64; \ using RetType = Float64; \ static constexpr auto name = #NAME; \ template \ static void execute(const T* src, U* dst, UInt8& null_flag) { \ null_flag = src[0] <= 0; \ dst[0] = static_cast(FUNC((double)src[0])); \ } \ }; \ using Function##CLASS = FunctionMathUnaryToNullType; LOG_FUNCTION_IMPL(Log10, log10, std::log10); LOG_FUNCTION_IMPL(Log2, log2, std::log2); LOG_FUNCTION_IMPL(Ln, ln, std::log); struct LogName { static constexpr auto name = "log"; }; template struct LogImpl { using ResultType = Float64; using Traits = NumberTraits::BinaryOperatorTraits; static const constexpr bool allow_decimal = false; static constexpr double EPSILON = 1e-9; template static void apply(const typename Traits::ArrayA& a, B b, typename ColumnVector::Container& c, typename Traits::ArrayNull& null_map) { size_t size = c.size(); UInt8 is_null = b <= 0; memset(null_map.data(), is_null, size); if (!is_null) { for (size_t i = 0; i < size; i++) { if (a[i] <= 0 || std::fabs(a[i] - 1.0) < EPSILON) { null_map[i] = 1; } else { c[i] = static_cast(std::log(static_cast(b)) / std::log(static_cast(a[i]))); } } } } template static inline Result apply(A a, B b, UInt8& is_null) { is_null = a <= 0 || b <= 0 || std::fabs(a - 1.0) < EPSILON; return static_cast(std::log(static_cast(b)) / std::log(static_cast(a))); } }; using FunctionLog = FunctionBinaryArithmetic; template struct SignImpl { using ResultType = Int8; static inline ResultType apply(A a) { if constexpr (IsDecimalNumber || std::is_floating_point_v) return static_cast(a < A(0) ? -1 : a == A(0) ? 0 : 1); else if constexpr (std::is_signed_v) return static_cast(a < 0 ? -1 : a == 0 ? 0 : 1); else if constexpr (std::is_unsigned_v) return static_cast(a == 0 ? 0 : 1); } }; struct NameSign { static constexpr auto name = "sign"; }; using FunctionSign = FunctionUnaryArithmetic; template struct AbsImpl { using ResultType = std::conditional_t, A, typename NumberTraits::ResultOfAbs::Type>; static inline ResultType apply(A a) { if constexpr (IsDecimalNumber) return a < 0 ? A(-a) : a; else if constexpr (std::is_integral_v && std::is_signed_v) return a < 0 ? static_cast(~a) + 1 : a; else if constexpr (std::is_integral_v && std::is_unsigned_v) return static_cast(a); else if constexpr (std::is_floating_point_v) return static_cast(std::abs(a)); } }; struct NameAbs { static constexpr auto name = "abs"; }; using FunctionAbs = FunctionUnaryArithmetic; template struct NegativeImpl { using ResultType = A; static inline ResultType apply(A a) { return -a; } }; struct NameNegative { static constexpr auto name = "negative"; }; using FunctionNegative = FunctionUnaryArithmetic; template struct PositiveImpl { using ResultType = A; static inline ResultType apply(A a) { return static_cast(a); } }; struct NamePositive { static constexpr auto name = "positive"; }; using FunctionPositive = FunctionUnaryArithmetic; struct SinName { static constexpr auto name = "sin"; }; using FunctionSin = FunctionMathUnary>; struct SqrtName { static constexpr auto name = "sqrt"; }; using FunctionSqrt = FunctionMathUnary>; struct CbrtName { static constexpr auto name = "cbrt"; }; using FunctionCbrt = FunctionMathUnary>; struct TanName { static constexpr auto name = "tan"; }; using FunctionTan = FunctionMathUnary>; template struct RadiansImpl { using ResultType = A; static inline ResultType apply(A a) { return static_cast(a * PiImpl::value / 180.0); } }; struct NameRadians { static constexpr auto name = "radians"; }; using FunctionRadians = FunctionUnaryArithmetic; template struct DegreesImpl { using ResultType = A; static inline ResultType apply(A a) { return static_cast(a * 180.0 / PiImpl::value); } }; struct NameDegrees { static constexpr auto name = "degrees"; }; using FunctionDegrees = FunctionUnaryArithmetic; struct NameBin { static constexpr auto name = "bin"; }; struct BinImpl { using ReturnType = DataTypeString; static constexpr auto TYPE_INDEX = TypeIndex::Int64; using Type = Int64; using ReturnColumnType = ColumnString; static std::string bin_impl(Int64 value) { uint64_t n = static_cast(value); const size_t max_bits = sizeof(uint64_t) * 8; char result[max_bits]; uint32_t index = max_bits; do { result[--index] = '0' + (n & 1); } while (n >>= 1); return std::string(result + index, max_bits - index); } static Status vector(const ColumnInt64::Container& data, ColumnString::Chars& res_data, ColumnString::Offsets& res_offsets) { res_offsets.resize(data.size()); size_t input_size = res_offsets.size(); for (size_t i = 0; i < input_size; ++i) { StringOP::push_value_string(bin_impl(data[i]), i, res_data, res_offsets); } return Status::OK(); } }; using FunctionBin = FunctionUnaryToType; template struct PowImpl { using ResultType = double; static const constexpr bool allow_decimal = false; template static inline double apply(A a, B b) { /// Next everywhere, static_cast - so that there is no wrong result in expressions of the form Int64 c = UInt32(a) * Int32(-1). return std::pow((double)a, (double)b); } }; struct PowName { static constexpr auto name = "pow"; }; using FunctionPow = FunctionBinaryArithmetic; struct TruncateName { static constexpr auto name = "truncate"; }; struct CeilName { static constexpr auto name = "ceil"; }; struct FloorName { static constexpr auto name = "floor"; }; struct RoundName { static constexpr auto name = "round"; }; struct RoundBankersName { static constexpr auto name = "round_bankers"; }; /// round(double,int32)-->double /// key_str:roundFloat64Int32 template struct DoubleRoundTwoImpl { static constexpr auto name = Name::name; static DataTypes get_variadic_argument_types() { return {std::make_shared(), std::make_shared()}; } }; template struct DoubleRoundOneImpl { static constexpr auto name = Name::name; static DataTypes get_variadic_argument_types() { return {std::make_shared()}; } }; template struct DecimalRoundTwoImpl { static constexpr auto name = Name::name; static DataTypes get_variadic_argument_types() { return {std::make_shared>(9, 0), std::make_shared()}; } }; template struct DecimalRoundOneImpl { static constexpr auto name = Name::name; static DataTypes get_variadic_argument_types() { return {std::make_shared>(9, 0)}; } }; // TODO: Now math may cause one thread compile time too long, because the function in math // so mush. Split it to speed up compile time in the future void register_function_math(SimpleFunctionFactory& factory) { #define REGISTER_ROUND_FUNCTIONS(IMPL) \ factory.register_function< \ FunctionRounding, RoundingMode::Round, TieBreakingMode::Auto>>(); \ factory.register_function< \ FunctionRounding, RoundingMode::Floor, TieBreakingMode::Auto>>(); \ factory.register_function< \ FunctionRounding, RoundingMode::Ceil, TieBreakingMode::Auto>>(); \ factory.register_function< \ FunctionRounding, RoundingMode::Trunc, TieBreakingMode::Auto>>(); \ factory.register_function, RoundingMode::Round, \ TieBreakingMode::Bankers>>(); REGISTER_ROUND_FUNCTIONS(DecimalRoundOneImpl) REGISTER_ROUND_FUNCTIONS(DecimalRoundTwoImpl) REGISTER_ROUND_FUNCTIONS(DoubleRoundOneImpl) REGISTER_ROUND_FUNCTIONS(DoubleRoundTwoImpl) factory.register_alias("round", "dround"); factory.register_function(); factory.register_function(); factory.register_function(); factory.register_function(); factory.register_alias("ceil", "dceil"); factory.register_alias("ceil", "ceiling"); factory.register_function(); factory.register_function(); factory.register_alias("ln", "dlog1"); factory.register_function(); factory.register_function(); factory.register_function(); factory.register_alias("log10", "dlog10"); factory.register_function(); factory.register_function(); factory.register_function(); factory.register_function(); factory.register_function(); factory.register_function(); factory.register_function(); factory.register_alias("sqrt", "dsqrt"); factory.register_function(); factory.register_function(); factory.register_alias("floor", "dfloor"); factory.register_function(); factory.register_alias("pow", "power"); factory.register_alias("pow", "dpow"); factory.register_alias("pow", "fpow"); factory.register_function(); factory.register_alias("exp", "dexp"); factory.register_function(); factory.register_function(); factory.register_function(); } } // namespace doris::vectorized