0AyanamiRei commented on code in PR #53595: URL: https://github.com/apache/doris/pull/53595#discussion_r2233970204
########## be/src/vec/aggregate_functions/aggregate_function_geomean.h: ########## @@ -0,0 +1,162 @@ +// 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 "vec/aggregate_functions/aggregate_function.h" +#include "vec/core/field.h" +#include "vec/core/types.h" +#include "vec/data_types/data_type.h" +#include "vec/data_types/data_type_decimal.h" +#include "vec/io/io_helper.h" + +namespace doris::vectorized { +#include "common/compile_check_begin.h" + +// geomean = (x1 * x2 * ... * xn)^(1/n) +// Supports negative numbers , zero and NULL based on mathematical definition: +// - If any value is 0, result is 0. +// - If value is NULL, ignore it. +// - If number of negative values is odd and n is even, result is undefined (complex). +// - Otherwise, result is sign * exp(sum(log(|x|)) / n), where sign is -1 for odd negatives, 1 otherwise. + +template <PrimitiveType T> +struct AggregateFunctionGeomeanData { + using DataType = typename PrimitiveTypeTraits<T>::ColumnItemType; + bool has_zero = false; + Int32 count_negative = 0; + Int32 count_positive = 0; + Float64 sum_log_abs = 0.0; + + void write(BufferWritable& buf) const { + buf.write_binary(count_positive); + buf.write_binary(count_negative); + buf.write_binary(has_zero); + buf.write_binary(sum_log_abs); + } + + void read(BufferReadable& buf) { + buf.read_binary(count_positive); + buf.read_binary(count_negative); + buf.read_binary(has_zero); + buf.read_binary(sum_log_abs); + } + + void reset() { + count_positive = 0; + count_negative = 0; + has_zero = false; + sum_log_abs = 0.0; + } + + void merge(const AggregateFunctionGeomeanData& rhs) { + if (has_zero || rhs.has_zero || (rhs.count_negative + rhs.count_positive) == 0) { Review Comment: I once considered pass the `has_zero` to this.has_zero here, but I found that `merge` has no return value, which is different from the `mapreduce` operation I imagined, so I removed it. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
