/root/doris/be/src/vec/functions/multiply.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // This file is copied from |
18 | | // https://github.com/ClickHouse/ClickHouse/blob/master/src/Functions/Multiply.cpp |
19 | | // and modified by Doris |
20 | | |
21 | | #include <stddef.h> |
22 | | |
23 | | #include <utility> |
24 | | |
25 | | #include "gutil/integral_types.h" |
26 | | #include "runtime/decimalv2_value.h" |
27 | | #include "vec/columns/columns_number.h" |
28 | | #include "vec/common/arithmetic_overflow.h" |
29 | | #include "vec/core/types.h" |
30 | | #include "vec/data_types/number_traits.h" |
31 | | #include "vec/functions/function_binary_arithmetic.h" |
32 | | #include "vec/functions/simple_function_factory.h" |
33 | | |
34 | | namespace doris::vectorized { |
35 | | |
36 | | template <typename A, typename B> |
37 | | struct MultiplyImpl { |
38 | | using ResultType = typename NumberTraits::ResultOfAdditionMultiplication<A, B>::Type; |
39 | | static const constexpr bool allow_decimal = true; |
40 | | |
41 | | template <typename Result = ResultType> |
42 | 0 | static inline Result apply(A a, B b) { |
43 | 0 | return static_cast<Result>(a) * b; |
44 | 0 | } Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIhhE5applyIiEET_hh Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIhaE5applyIsEET_ha Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIhsE5applyIiEET_hs Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIhiE5applyIlEET_hi Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIhlE5applyIlEET_hl Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIhnE5applyInEET_hn Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIhfE5applyIdEET_hf Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIhdE5applyIdEET_hd Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIiiE5applyIiEET_ii Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIllE5applyIlEET_ll Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplInnE5applyInEET_nn Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIN4wide7integerILm256EiEES4_E5applyIS4_EET_S4_S4_ Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplInnE5applyIN4wide7integerILm256EiEEEET_nn Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIahE5applyIsEET_ah Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIaaE5applyIsEET_aa Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIasE5applyIiEET_as Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIaiE5applyIlEET_ai Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIalE5applyIlEET_al Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIanE5applyInEET_an Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIafE5applyIdEET_af Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIadE5applyIdEET_ad Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIshE5applyIiEET_sh Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIsaE5applyIiEET_sa Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIssE5applyIiEET_ss Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIsiE5applyIlEET_si Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIslE5applyIlEET_sl Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIsnE5applyInEET_sn Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIsfE5applyIdEET_sf Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIsdE5applyIdEET_sd Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIihE5applyIlEET_ih Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIiaE5applyIlEET_ia Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIisE5applyIlEET_is Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIiiE5applyIlEET_ii Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIilE5applyIlEET_il Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIinE5applyInEET_in Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIifE5applyIdEET_if Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIidE5applyIdEET_id Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIlhE5applyIlEET_lh Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIlaE5applyIlEET_la Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIlsE5applyIlEET_ls Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIliE5applyIlEET_li Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIlnE5applyInEET_ln Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIlfE5applyIdEET_lf Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIldE5applyIdEET_ld Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplInhE5applyInEET_nh Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplInaE5applyInEET_na Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplInsE5applyInEET_ns Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIniE5applyInEET_ni Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplInlE5applyInEET_nl Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplInfE5applyIdEET_nf Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIndE5applyIdEET_nd Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIfhE5applyIdEET_fh Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIfaE5applyIdEET_fa Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIfsE5applyIdEET_fs Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIfiE5applyIdEET_fi Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIflE5applyIdEET_fl Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIfnE5applyIdEET_fn Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIffE5applyIdEET_ff Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIfdE5applyIdEET_fd Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIdhE5applyIdEET_dh Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIdaE5applyIdEET_da Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIdsE5applyIdEET_ds Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIdiE5applyIdEET_di Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIdlE5applyIdEET_dl Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIdnE5applyIdEET_dn Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIdfE5applyIdEET_df Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIddE5applyIdEET_dd |
45 | | |
46 | | template <typename Result = DecimalV2Value> |
47 | 0 | static inline DecimalV2Value apply(const DecimalV2Value& a, const DecimalV2Value& b) { |
48 | 0 | return a * b; |
49 | 0 | } Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIiiE5applyINS_14DecimalV2ValueEEES4_RKS4_S6_ Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIllE5applyINS_14DecimalV2ValueEEES4_RKS4_S6_ Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplInnE5applyINS_14DecimalV2ValueEEES4_RKS4_S6_ Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIN4wide7integerILm256EiEES4_E5applyINS_14DecimalV2ValueEEES7_RKS7_S9_ |
50 | | |
51 | | /* |
52 | | select 999999999999999999999999999 * 999999999999999999999999999; |
53 | | 999999999999999999999999998000000000.000000000000000001 54 digits |
54 | | */ |
55 | | template <bool check_overflow> |
56 | | static void vector_vector(const ColumnDecimal128V2::Container::value_type* __restrict a, |
57 | | const ColumnDecimal128V2::Container::value_type* __restrict b, |
58 | 0 | ColumnDecimal128V2::Container::value_type* c, size_t size) { |
59 | 0 | auto sng_uptr = std::unique_ptr<int8[]>(new int8[size]); |
60 | 0 | int8* sgn = sng_uptr.get(); |
61 | 0 | auto max = DecimalV2Value::get_max_decimal(); |
62 | 0 | auto min = DecimalV2Value::get_min_decimal(); |
63 | |
|
64 | 0 | for (int i = 0; i < size; i++) { |
65 | 0 | sgn[i] = ((DecimalV2Value(a[i]).value() > 0) && (DecimalV2Value(b[i]).value() > 0)) || |
66 | 0 | ((DecimalV2Value(a[i]).value() < 0) && |
67 | 0 | (DecimalV2Value(b[i]).value() < 0)) |
68 | 0 | ? 1 |
69 | 0 | : ((DecimalV2Value(a[i]).value() == 0) || (DecimalV2Value(b[i]).value() == 0)) |
70 | 0 | ? 0 |
71 | 0 | : -1; |
72 | 0 | } |
73 | |
|
74 | 0 | for (int i = 0; i < size; i++) { |
75 | 0 | if constexpr (check_overflow) { |
76 | 0 | int128_t i128_mul_result; |
77 | 0 | if (common::mul_overflow(DecimalV2Value(a[i]).value(), DecimalV2Value(b[i]).value(), |
78 | 0 | i128_mul_result)) { |
79 | 0 | THROW_DECIMAL_BINARY_OP_OVERFLOW_EXCEPTION( |
80 | 0 | DecimalV2Value(a[i]).to_string(), "multiply", |
81 | 0 | DecimalV2Value(b[i]).to_string(), |
82 | 0 | DecimalV2Value(i128_mul_result).to_string(), "decimalv2"); |
83 | 0 | } |
84 | 0 | c[i] = (i128_mul_result - sgn[i]) / DecimalV2Value::ONE_BILLION + sgn[i]; |
85 | 0 | if (c[i].value > max.value() || c[i].value < min.value()) { |
86 | 0 | THROW_DECIMAL_BINARY_OP_OVERFLOW_EXCEPTION( |
87 | 0 | DecimalV2Value(a[i]).to_string(), "multiply", |
88 | 0 | DecimalV2Value(b[i]).to_string(), |
89 | 0 | DecimalV2Value(i128_mul_result).to_string(), "decimalv2"); |
90 | 0 | } |
91 | 0 | } else { |
92 | 0 | c[i] = (DecimalV2Value(a[i]).value() * DecimalV2Value(b[i]).value() - sgn[i]) / |
93 | 0 | DecimalV2Value::ONE_BILLION + |
94 | 0 | sgn[i]; |
95 | 0 | } |
96 | 0 | } |
97 | 0 | } Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplInnE13vector_vectorILb1EEEvPKNS0_7DecimalInEES7_PS5_m Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplInnE13vector_vectorILb0EEEvPKNS0_7DecimalInEES7_PS5_m |
98 | | |
99 | | /// Apply operation and check overflow. It's used for Decimal operations. @returns true if overflowed, false otherwise. |
100 | | template <typename Result = ResultType> |
101 | 0 | static inline bool apply(A a, B b, Result& c) { |
102 | 0 | return common::mul_overflow(static_cast<Result>(a), b, c); |
103 | 0 | } Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIiiE5applyIiEEbiiRT_ Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIllE5applyIlEEbllRT_ Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplInnE5applyInEEbnnRT_ Unexecuted instantiation: _ZN5doris10vectorized12MultiplyImplIN4wide7integerILm256EiEES4_E5applyIS4_EEbS4_S4_RT_ |
104 | | }; |
105 | | |
106 | | struct NameMultiply { |
107 | | static constexpr auto name = "multiply"; |
108 | | }; |
109 | | using FunctionMultiply = FunctionBinaryArithmetic<MultiplyImpl, NameMultiply, false>; |
110 | | |
111 | 1 | void register_function_multiply(SimpleFunctionFactory& factory) { |
112 | 1 | factory.register_function<FunctionMultiply>(); |
113 | 1 | } |
114 | | |
115 | | } // namespace doris::vectorized |