zclllyybb commented on code in PR #57545: URL: https://github.com/apache/doris/pull/57545#discussion_r2483837454
########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/Sem.java: ########## @@ -0,0 +1,111 @@ +// 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. + +package org.apache.doris.nereids.trees.expressions.functions.agg; + +import org.apache.doris.catalog.FunctionSignature; +import org.apache.doris.nereids.exceptions.AnalysisException; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature; +import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression; +import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor; +import org.apache.doris.nereids.types.BigIntType; +import org.apache.doris.nereids.types.DataType; +import org.apache.doris.nereids.types.DoubleType; +import org.apache.doris.nereids.types.FloatType; +import org.apache.doris.nereids.types.IntegerType; +import org.apache.doris.nereids.types.LargeIntType; +import org.apache.doris.nereids.types.SmallIntType; +import org.apache.doris.nereids.types.TinyIntType; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; + +import java.util.List; + +/** + * AggregateFunction 'sem'. + */ + +public class Sem extends NullableAggregateFunction + implements UnaryExpression, ExplicitlyCastableSignature { + + public static final List<FunctionSignature> SIGNATURES = ImmutableList.of( + FunctionSignature.ret(DoubleType.INSTANCE).args(DoubleType.INSTANCE), Review Comment: since the result is double, could we accept double only? ########## be/src/vec/aggregate_functions/aggregate_function_sem.h: ########## @@ -0,0 +1,130 @@ +// 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 <math.h> + +#include "runtime/decimalv2_value.h" +#include "runtime/define_primitive_type.h" +#include "runtime/primitive_type.h" +#include "vec/aggregate_functions/aggregate_function.h" +#include "vec/columns/column.h" +#include "vec/common/assert_cast.h" +#include "vec/core/types.h" +#include "vec/data_types/data_type.h" +#include "vec/data_types/data_type_decimal.h" + +namespace doris::vectorized { +#include "common/compile_check_begin.h" + +class Arena; +class BufferReadable; +class BufferWritable; + +struct AggregateFunctionSemData { + double sum {}; + double sumOfSquares {}; Review Comment: ```suggestion double sum_of_squares {}; ``` ########## be/src/vec/aggregate_functions/aggregate_function_sem.h: ########## @@ -0,0 +1,130 @@ +// 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 <math.h> + +#include "runtime/decimalv2_value.h" +#include "runtime/define_primitive_type.h" +#include "runtime/primitive_type.h" +#include "vec/aggregate_functions/aggregate_function.h" +#include "vec/columns/column.h" +#include "vec/common/assert_cast.h" +#include "vec/core/types.h" +#include "vec/data_types/data_type.h" +#include "vec/data_types/data_type_decimal.h" + +namespace doris::vectorized { +#include "common/compile_check_begin.h" + +class Arena; +class BufferReadable; +class BufferWritable; + +struct AggregateFunctionSemData { + double sum {}; + double sumOfSquares {}; + UInt64 count = 0; + + void add(const double& value) { + sum += value; + sumOfSquares += value * value; + count++; + } + + void merge(const AggregateFunctionSemData& rhs) { + sum += rhs.sum; + sumOfSquares += rhs.sumOfSquares; + count += rhs.count; + } + + void write(BufferWritable& buf) const { + buf.write_binary(sum); + buf.write_binary(sumOfSquares); + buf.write_binary(count); + } + + void read(BufferReadable& buf) { + buf.read_binary(sum); + buf.read_binary(sumOfSquares); + buf.read_binary(count); + } + + void reset() { + sum = {}; + sumOfSquares = {}; + count = 0; + } + + double result() const { + if (count <= 1) { + return 0; + } + double dCount = static_cast<double>(count); + double result = + std::sqrt((sumOfSquares - (sum * sum) / dCount) / ((dCount) * (dCount - 1.0))); Review Comment: did you test this impl and Welford's algorithm? Is Welford's more stable? ########## regression-test/suites/query_p0/sql_functions/aggregate_functions/test_aggregate_all_functions2.groovy: ########## Review Comment: add a test of literal -- 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]
