Copilot commented on code in PR #57545: URL: https://github.com/apache/doris/pull/57545#discussion_r2483839569
########## 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: [nitpick] The formula has unnecessary parentheses around `(dCount)` which reduces readability. Consider simplifying to: `std::sqrt((sumOfSquares - (sum * sum) / dCount) / (dCount * (dCount - 1.0)))`. ```suggestion std::sqrt((sumOfSquares - (sum * sum) / dCount) / (dCount * (dCount - 1.0))); ``` ########## be/src/vec/aggregate_functions/aggregate_function_sem.cpp: ########## @@ -0,0 +1,48 @@ +// 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/AggregateFunctions/AggregateFunctionAvg.cpp +// and modified by Doris + +#include "vec/aggregate_functions/aggregate_function_sem.h" + +#include "runtime/define_primitive_type.h" +#include "vec/aggregate_functions/aggregate_function_simple_factory.h" +#include "vec/aggregate_functions/helpers.h" +#include "vec/core/field.h" + +namespace doris::vectorized { +#include "common/compile_check_begin.h" + +template <PrimitiveType T> +using AggregateFuncSem = AggregateFunctionSem<T, AggregateFunctionSemData>; + +void register_aggregate_function_sem(AggregateFunctionSimpleFactory& factory) { + AggregateFunctionCreator creator = [&](const std::string& name, const DataTypes& types, + const bool result_is_nullable, + const AggregateFunctionAttr& attr) { + return creator_with_type_list<TYPE_TINYINT, TYPE_SMALLINT, TYPE_INT, TYPE_BIGINT, + TYPE_LARGEINT, TYPE_FLOAT, + TYPE_DOUBLE>::creator<AggregateFuncSem>(name, types, Review Comment: [nitpick] The formatting is inconsistent - `TYPE_FLOAT` is on a new line while other types are grouped differently. Consider aligning all types consistently, either all on one line or with consistent grouping, matching the pattern used in similar functions. ```suggestion return creator_with_type_list<TYPE_TINYINT, TYPE_SMALLINT, TYPE_INT, TYPE_BIGINT, TYPE_LARGEINT, TYPE_FLOAT, TYPE_DOUBLE>::creator<AggregateFuncSem>(name, types, ``` ########## 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))); + return result; + } +}; + +template <PrimitiveType T, typename Data> +class AggregateFunctionSem final + : public IAggregateFunctionDataHelper<Data, AggregateFunctionSem<T, Data>>, + UnaryExpression, + NullableAggregateFunction { Review Comment: The class declaration incorrectly inherits from `UnaryExpression` and `NullableAggregateFunction` as base classes. These should be inherited with `public` access specifier, or they should be tag structs (empty marker types) that don't require public inheritance. Looking at similar implementations like `AggregateFunctionAvg`, the pattern should use commas between base classes without access specifiers since these are tag types. ```suggestion UnaryExpression, NullableAggregateFunction { ``` ########## regression-test/suites/query_p0/sql_functions/aggregate_functions/test_aggregate_all_functions2.groovy: ########## @@ -124,6 +124,29 @@ suite("test_aggregate_all_functions2") { exception "requires a boolean or numeric argument" } + qt_sem_double """SELECT sem(k8) FROM baseall;""" // 66813.32469 + qt_sem_float """SELECT sem(k9) FROM baseall;""" // 5350.392064 + qt_sem_largeint """SELECT sem(k13) FROM baseall;""" // 16604079329999999393723317663188385792 + qt_sem_bigint """SELECT sem(k4) FROM baseall;""" // 1272946277000000000 + qt_sem_int """SELECT sem(k3) FROM baseall;""" // 296380900.8 + qt_sem_smallint """SELECT sem(k2) FROM baseall;""" // 4529.188786 + qt_sem_tinyint """SELECT sem(k1) FROM baseall;""" // 1.154700538 + qt_sem_bool """SELECT sem(k0) FROM baseall;""" // 0.1333333333 + Review Comment: There is trailing whitespace on this blank line. Remove it to maintain consistent code style. ```suggestion ``` ########## 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), + FunctionSignature.ret(DoubleType.INSTANCE).args(FloatType.INSTANCE), + FunctionSignature.ret(DoubleType.INSTANCE).args(LargeIntType.INSTANCE), + FunctionSignature.ret(DoubleType.INSTANCE).args(BigIntType.INSTANCE), + FunctionSignature.ret(DoubleType.INSTANCE).args(IntegerType.INSTANCE), + FunctionSignature.ret(DoubleType.INSTANCE).args(SmallIntType.INSTANCE), + FunctionSignature.ret(DoubleType.INSTANCE).args(TinyIntType.INSTANCE)); + + /** + * constructor with 1 argument. + */ + public Sem(Expression child) { + this(false, false, child); + } + + /** + * constructor with 1 argument. Review Comment: The comment for the second constructor is identical to the first constructor's comment but doesn't mention the `distinct` parameter. Update the comment to: `constructor with distinct flag and 1 argument.` ```suggestion * constructor with distinct flag and 1 argument. ``` ########## regression-test/suites/query_p0/sql_functions/aggregate_functions/test_aggregate_all_functions2.groovy: ########## @@ -124,6 +124,29 @@ suite("test_aggregate_all_functions2") { exception "requires a boolean or numeric argument" } + qt_sem_double """SELECT sem(k8) FROM baseall;""" // 66813.32469 + qt_sem_float """SELECT sem(k9) FROM baseall;""" // 5350.392064 + qt_sem_largeint """SELECT sem(k13) FROM baseall;""" // 16604079329999999393723317663188385792 + qt_sem_bigint """SELECT sem(k4) FROM baseall;""" // 1272946277000000000 + qt_sem_int """SELECT sem(k3) FROM baseall;""" // 296380900.8 + qt_sem_smallint """SELECT sem(k2) FROM baseall;""" // 4529.188786 + qt_sem_tinyint """SELECT sem(k1) FROM baseall;""" // 1.154700538 + qt_sem_bool """SELECT sem(k0) FROM baseall;""" // 0.1333333333 + + qt_sem_distinct_double """SELECT sem(DISTINCT k8) FROM baseall;""" + + qt_sem_window_double """SELECT sem(k8) OVER(PARTITION BY k6) FROM baseall ORDER BY k1;""" + qt_sem_window_largeint """SELECT sem(k13) OVER(PARTITION BY k10) FROM baseall ORDER BY k1;""" + qt_sem_window_int """SELECT sem(k3) OVER(PARTITION BY k6) FROM baseall ORDER BY k1;""" + + qt_sem_null_double """SELECT sem(k8) FROM baseall WHERE k8 IS NULL OR k8 IS NOT NULL;""" + + test { + sql """SELECT sem(k10) FROM baseall;""" + exception "sem requires a numeric, boolean or string parameter" + } + + Review Comment: [nitpick] There are two consecutive blank lines (lines 148-149). Reduce to a single blank line for consistent spacing. ```suggestion ``` -- 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]
