wyxxxcat commented on code in PR #39187: URL: https://github.com/apache/doris/pull/39187#discussion_r1779883236
########## be/src/vec/aggregate_functions/aggregate_function_regr_sxx_.h: ########## @@ -0,0 +1,274 @@ +// 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. + +#pragma once + +#include <boost/iterator/iterator_facade.hpp> +#include <cmath> +#include <cstddef> +#include <memory> + +#include "olap/olap_common.h" +#include "runtime/decimalv2_value.h" +#include "vec/aggregate_functions/aggregate_function.h" +#include "vec/columns/column.h" +#include "vec/columns/column_nullable.h" +#include "vec/common/assert_cast.h" +#include "vec/core/field.h" +#include "vec/core/types.h" +#include "vec/data_types/data_type_decimal.h" +#include "vec/data_types/data_type_number.h" +#include "vec/io/io_helper.h" +namespace doris { +namespace vectorized { +class Arena; +class BufferReadable; +class BufferWritable; +template <typename T> +class ColumnDecimal; +template <typename> +class ColumnVector; +} // namespace vectorized +} // namespace doris + +namespace doris::vectorized { + +template <typename T> +struct AggregateFunctionRegrSxxData { + UInt64 count = 0; + double square_of_sum_x {}; + double sum_x {}; + void write(BufferWritable& buf) const { + write_binary(square_of_sum_x, buf); + write_binary(sum_x, buf); + write_binary(count, buf); + } + + void read(BufferReadable& buf) { + read_binary(square_of_sum_x, buf); + read_binary(sum_x, buf); + read_binary(count, buf); + } + + void reset() { + square_of_sum_x = {}; + sum_x = {}; + count = 0; + } + + double get_regr_sxx_result() const { return square_of_sum_x - (sum_x * sum_x) / count; } + + void merge(const AggregateFunctionRegrSxxData& rhs) { + if (rhs.count == 0) { + return; + } + square_of_sum_x += rhs.square_of_sum_x; + sum_x += rhs.sum_x; + count += rhs.count; + } + + void add(const IColumn* column_y, const IColumn* column_x, size_t row_num) { +#ifdef __clang__ +#pragma clang fp reassociate(on) +#endif + const auto& sources_x = + assert_cast<const ColumnVector<T>&, TypeCheckOnRelease::DISABLE>(*column_x); + const auto& value = sources_x.get_data()[row_num]; + sum_x += value; + square_of_sum_x += value * value; + count += 1; + } +}; +template <typename T> +struct AggregateFunctionRegrSxyData { + using ResultType = T; + UInt64 count = 0; + double sum_x {}; + double sum_y {}; + double sum_of_x_mul_y {}; + void write(BufferWritable& buf) const { + write_binary(sum_x, buf); + write_binary(sum_y, buf); + write_binary(sum_of_x_mul_y, buf); + write_binary(count, buf); + } + + void read(BufferReadable& buf) { + read_binary(sum_x, buf); + read_binary(sum_y, buf); + read_binary(sum_of_x_mul_y, buf); + read_binary(count, buf); + } + + void reset() { + count = 0; + sum_x = {}; + sum_y = {}; + sum_of_x_mul_y = {}; + } + + double get_regr_sxy_result() const { return sum_of_x_mul_y - (sum_x * sum_y) / count; } Review Comment: The result of a query for an empty table is a null value -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org