Files
doris/be/src/vec/functions/multiply.cpp
TengJianPing 2bdfaac609 [fix](ubsan) fix ubsan errors (#19658)
ixu ubsan errors:

doris/be/src/util/string_parser.hpp:275:58: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int'

doris/be/src/vec/functions/functions_comparison.h:214:51: runtime error: addition of unsigned offset to 0x7fea6c6b7010 overflowed to 0x7fea6c6b700c

doris/be/src/vec/functions/multiply.cpp:67:50: runtime error: signed integer overflow: 1295699415680000000 * 0x0000000000015401d0a4cd4890a77700 cannot be represented in type '__int128

doris/be/src/vec/aggregate_functions/aggregate_function_percentile_approx.h:445:73: runtime error: addition of unsigned offset to 0x7feca3343d10 overflowed to 0x7feca3343d08 

doris/be/src/exec/schema_scanner/schema_tables_scanner.cpp:330:24: run
2023-05-17 09:32:03 +08:00

96 lines
3.7 KiB
C++

// 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.
// This file is copied from
// https://github.com/ClickHouse/ClickHouse/blob/master/src/Functions/Multiply.cpp
// and modified by Doris
#include <stddef.h>
#include <utility>
#include "gutil/integral_types.h"
#include "runtime/decimalv2_value.h"
#include "vec/columns/columns_number.h"
#include "vec/common/arithmetic_overflow.h"
#include "vec/core/types.h"
#include "vec/data_types/number_traits.h"
#include "vec/functions/function_binary_arithmetic.h"
#include "vec/functions/simple_function_factory.h"
namespace doris::vectorized {
template <typename A, typename B>
struct MultiplyImpl {
using ResultType = typename NumberTraits::ResultOfAdditionMultiplication<A, B>::Type;
static const constexpr bool allow_decimal = true;
template <typename Result = ResultType>
static inline Result apply(A a, B b) {
return static_cast<Result>(a) * b;
}
template <typename Result = DecimalV2Value>
static inline DecimalV2Value apply(const DecimalV2Value& a, const DecimalV2Value& b) {
return a * b;
}
static void vector_vector(const ColumnDecimal128::Container::value_type* __restrict a,
const ColumnDecimal128::Container::value_type* __restrict b,
ColumnDecimal128::Container::value_type* c, size_t size) {
int8 sgn[size];
for (int i = 0; i < size; i++) {
sgn[i] = ((DecimalV2Value(a[i]).value() > 0) && (DecimalV2Value(b[i]).value() > 0)) ||
((DecimalV2Value(a[i]).value() < 0) &&
(DecimalV2Value(b[i]).value() < 0))
? 1
: ((DecimalV2Value(a[i]).value() == 0) || (DecimalV2Value(b[i]).value() == 0))
? 0
: -1;
}
for (int i = 0; i < size; i++) {
int128_t i128_mul_result;
if (common::mul_overflow(DecimalV2Value(a[i]).value(), DecimalV2Value(b[i]).value(),
i128_mul_result)) {
VLOG_DEBUG << "Decimal multiply overflow";
c[i] = (sgn[i] == -1) ? -DecimalV2Value::MAX_DECIMAL_VALUE
: DecimalV2Value::MAX_DECIMAL_VALUE;
} else {
c[i] = (i128_mul_result - sgn[i]) / DecimalV2Value::ONE_BILLION + sgn[i];
}
}
}
/// Apply operation and check overflow. It's used for Decimal operations. @returns true if overflowed, false otherwise.
template <typename Result = ResultType>
static inline bool apply(A a, B b, Result& c) {
return common::mul_overflow(static_cast<Result>(a), b, c);
}
};
struct NameMultiply {
static constexpr auto name = "multiply";
};
using FunctionMultiply = FunctionBinaryArithmetic<MultiplyImpl, NameMultiply, false>;
void register_function_multiply(SimpleFunctionFactory& factory) {
factory.register_function<FunctionMultiply>();
}
} // namespace doris::vectorized