This is an automated email from the ASF dual-hosted git repository.

zhangstar333 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new e45a629903c [Refactor](agg-func) Remove some nonsense template 
interlayers (#43777)
e45a629903c is described below

commit e45a629903c2a52abc6f14dfe6073a45a8fa1ebb
Author: zclllhhjj <zhaochan...@selectdb.com>
AuthorDate: Mon Nov 18 10:31:35 2024 +0800

    [Refactor](agg-func) Remove some nonsense template interlayers (#43777)
    
    ### What problem does this PR solve?
    
    Issue Number: close #xxx
    
    Related PR: #xxx
    
    Problem Summary:
    
    In this pr, we significantly improved code readability.
---
 .../aggregate_function_covar.cpp                   |  25 ++---
 .../aggregate_functions/aggregate_function_covar.h | 117 ++++-----------------
 .../aggregate_function_reader_first_last.h         |  56 +++++-----
 .../aggregate_function_stddev.cpp                  |  36 +++----
 .../aggregate_function_stddev.h                    |  99 +++++------------
 5 files changed, 107 insertions(+), 226 deletions(-)

diff --git a/be/src/vec/aggregate_functions/aggregate_function_covar.cpp 
b/be/src/vec/aggregate_functions/aggregate_function_covar.cpp
index 71d09f61de4..4c5fe132195 100644
--- a/be/src/vec/aggregate_functions/aggregate_function_covar.cpp
+++ b/be/src/vec/aggregate_functions/aggregate_function_covar.cpp
@@ -29,20 +29,15 @@
 
 namespace doris::vectorized {
 
-template <template <typename, bool> class AggregateFunctionTemplate,
-          template <typename> class NameData, template <typename, typename> 
class Data,
-          bool is_nullable = false>
+template <template <typename> class Function, template <typename> class Data>
 AggregateFunctionPtr create_function_single_value(const String& name,
                                                   const DataTypes& 
argument_types,
-                                                  const bool 
result_is_nullable,
-                                                  bool custom_nullable) {
+                                                  const bool 
result_is_nullable) {
     WhichDataType which(remove_nullable(argument_types[0]));
-#define DISPATCH(TYPE)                                                         
                \
-    if (which.idx == TypeIndex::TYPE)                                          
                \
-        return creator_without_type::create<                                   
                \
-                AggregateFunctionTemplate<NameData<Data<TYPE, 
BaseData<TYPE>>>, is_nullable>>( \
-                custom_nullable ? remove_nullable(argument_types) : 
argument_types,            \
-                result_is_nullable);
+#define DISPATCH(TYPE)                                                         
   \
+    if (which.idx == TypeIndex::TYPE)                                          
   \
+        return 
creator_without_type::create<Function<Data<TYPE>>>(argument_types, \
+                                                                  
result_is_nullable);
     FOR_NUMERIC_TYPES(DISPATCH)
 #undef DISPATCH
 
@@ -55,16 +50,16 @@ AggregateFunctionPtr 
create_aggregate_function_covariance_samp(const std::string
                                                                const 
DataTypes& argument_types,
                                                                const bool 
result_is_nullable,
                                                                const 
AggregateFunctionAttr& attr) {
-    return create_function_single_value<AggregateFunctionSamp, CovarSampName, 
SampData>(
-            name, argument_types, result_is_nullable, NOTNULLABLE);
+    return create_function_single_value<AggregateFunctionSampCovariance, 
SampData>(
+            name, argument_types, result_is_nullable);
 }
 
 AggregateFunctionPtr create_aggregate_function_covariance_pop(const 
std::string& name,
                                                               const DataTypes& 
argument_types,
                                                               const bool 
result_is_nullable,
                                                               const 
AggregateFunctionAttr& attr) {
-    return create_function_single_value<AggregateFunctionPop, CovarName, 
PopData>(
-            name, argument_types, result_is_nullable, NOTNULLABLE);
+    return create_function_single_value<AggregateFunctionSampCovariance, 
PopData>(
+            name, argument_types, result_is_nullable);
 }
 
 void register_aggregate_function_covar_pop(AggregateFunctionSimpleFactory& 
factory) {
diff --git a/be/src/vec/aggregate_functions/aggregate_function_covar.h 
b/be/src/vec/aggregate_functions/aggregate_function_covar.h
index e6ebec70285..b01d1ba11c8 100644
--- a/be/src/vec/aggregate_functions/aggregate_function_covar.h
+++ b/be/src/vec/aggregate_functions/aggregate_function_covar.h
@@ -19,35 +19,22 @@
 
 #include <glog/logging.h>
 
-#include "agent/be_exec_version_manager.h"
-#define POP true
-#define NOTPOP false
-#define NULLABLE true
-#define NOTNULLABLE false
-
-#include <stddef.h>
-#include <stdint.h>
-
-#include <algorithm>
 #include <boost/iterator/iterator_facade.hpp>
-#include <cmath>
+#include <cstddef>
+#include <cstdint>
 #include <memory>
-#include <type_traits>
 
-#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 {
+namespace doris::vectorized {
+
 class Arena;
 class BufferReadable;
 class BufferWritable;
@@ -55,15 +42,12 @@ template <typename T>
 class ColumnDecimal;
 template <typename>
 class ColumnVector;
-} // namespace vectorized
-} // namespace doris
-
-namespace doris::vectorized {
 
 template <typename T>
 struct BaseData {
-    BaseData() : sum_x(0.0), sum_y(0.0), sum_xy(0.0), count(0) {}
+    BaseData() = default;
     virtual ~BaseData() = default;
+    static DataTypePtr get_return_type() { return 
std::make_shared<DataTypeFloat64>(); }
 
     void write(BufferWritable& buf) const {
         write_binary(sum_x, buf);
@@ -122,23 +106,26 @@ struct BaseData {
         count += 1;
     }
 
-    double sum_x;
-    double sum_y;
-    double sum_xy;
-    int64_t count;
+    double sum_x {};
+    double sum_y {};
+    double sum_xy {};
+    int64_t count {};
 };
 
-template <typename T, typename Data>
-struct PopData : Data {
+template <typename T>
+struct PopData : BaseData<T> {
+    static const char* name() { return "covar"; }
+
     void insert_result_into(IColumn& to) const {
         auto& col = assert_cast<ColumnFloat64&>(to);
         col.get_data().push_back(this->get_pop_result());
     }
-    static DataTypePtr get_return_type() { return 
std::make_shared<DataTypeNumber<Float64>>(); }
 };
 
-template <typename T, typename Data>
-struct SampData : Data {
+template <typename T>
+struct SampData : BaseData<T> {
+    static const char* name() { return "covar_samp"; }
+
     void insert_result_into(IColumn& to) const {
         auto& col = assert_cast<ColumnFloat64&>(to);
         if (this->count == 1 || this->count == 0) {
@@ -147,27 +134,14 @@ struct SampData : Data {
             col.get_data().push_back(this->get_samp_result());
         }
     }
-    static DataTypePtr get_return_type() { return 
std::make_shared<DataTypeNumber<Float64>>(); }
 };
 
 template <typename Data>
-struct CovarName : Data {
-    static const char* name() { return "covar"; }
-};
-
-template <typename Data>
-struct CovarSampName : Data {
-    static const char* name() { return "covar_samp"; }
-};
-
-template <bool is_pop, typename Data, bool is_nullable>
 class AggregateFunctionSampCovariance
-        : public IAggregateFunctionDataHelper<
-                  Data, AggregateFunctionSampCovariance<is_pop, Data, 
is_nullable>> {
+        : public IAggregateFunctionDataHelper<Data, 
AggregateFunctionSampCovariance<Data>> {
 public:
     AggregateFunctionSampCovariance(const DataTypes& argument_types_)
-            : IAggregateFunctionDataHelper<
-                      Data, AggregateFunctionSampCovariance<is_pop, Data, 
is_nullable>>(
+            : IAggregateFunctionDataHelper<Data, 
AggregateFunctionSampCovariance<Data>>(
                       argument_types_) {}
 
     String get_name() const override { return Data::name(); }
@@ -176,39 +150,7 @@ public:
 
     void add(AggregateDataPtr __restrict place, const IColumn** columns, 
ssize_t row_num,
              Arena*) const override {
-        if constexpr (is_pop) {
-            this->data(place).add(columns[0], columns[1], row_num);
-        } else {
-            if constexpr (is_nullable) { //this if check could remove with old 
function
-                // nullable means at least one child is null.
-                // so here, maybe JUST ONE OF ups is null. so nullptr perhaps 
in ..._x or ..._y!
-                const auto* nullable_column_x = 
check_and_get_column<ColumnNullable>(columns[0]);
-                const auto* nullable_column_y = 
check_and_get_column<ColumnNullable>(columns[1]);
-
-                if (nullable_column_x && nullable_column_y) { // both nullable
-                    if (!nullable_column_x->is_null_at(row_num) &&
-                        !nullable_column_y->is_null_at(row_num)) {
-                        
this->data(place).add(&nullable_column_x->get_nested_column(),
-                                              
&nullable_column_y->get_nested_column(), row_num);
-                    }
-                } else if (nullable_column_x) { // x nullable
-                    if (!nullable_column_x->is_null_at(row_num)) {
-                        
this->data(place).add(&nullable_column_x->get_nested_column(), columns[1],
-                                              row_num);
-                    }
-                } else if (nullable_column_y) { // y nullable
-                    if (!nullable_column_y->is_null_at(row_num)) {
-                        this->data(place).add(columns[0], 
&nullable_column_y->get_nested_column(),
-                                              row_num);
-                    }
-                } else {
-                    throw Exception(ErrorCode::INTERNAL_ERROR,
-                                    "Nullable function {} get non-nullable 
columns!", get_name());
-                }
-            } else {
-                this->data(place).add(columns[0], columns[1], row_num);
-            }
-        }
+        this->data(place).add(columns[0], columns[1], row_num);
     }
 
     void reset(AggregateDataPtr __restrict place) const override { 
this->data(place).reset(); }
@@ -232,19 +174,4 @@ public:
     }
 };
 
-template <typename Data, bool is_nullable>
-class AggregateFunctionSamp final
-        : public AggregateFunctionSampCovariance<NOTPOP, Data, is_nullable> {
-public:
-    AggregateFunctionSamp(const DataTypes& argument_types_)
-            : AggregateFunctionSampCovariance<NOTPOP, Data, 
is_nullable>(argument_types_) {}
-};
-
-template <typename Data, bool is_nullable>
-class AggregateFunctionPop final : public AggregateFunctionSampCovariance<POP, 
Data, is_nullable> {
-public:
-    AggregateFunctionPop(const DataTypes& argument_types_)
-            : AggregateFunctionSampCovariance<POP, Data, 
is_nullable>(argument_types_) {}
-};
-
-} // namespace doris::vectorized
\ No newline at end of file
+} // namespace doris::vectorized
diff --git 
a/be/src/vec/aggregate_functions/aggregate_function_reader_first_last.h 
b/be/src/vec/aggregate_functions/aggregate_function_reader_first_last.h
index 1a6ac288583..066ef2a2579 100644
--- a/be/src/vec/aggregate_functions/aggregate_function_reader_first_last.h
+++ b/be/src/vec/aggregate_functions/aggregate_function_reader_first_last.h
@@ -17,9 +17,7 @@
 
 #pragma once
 
-#include "factory_helpers.h"
 #include "vec/aggregate_functions/aggregate_function.h"
-#include "vec/aggregate_functions/helpers.h"
 #include "vec/columns/column_array.h"
 #include "vec/columns/column_map.h"
 #include "vec/columns/column_nullable.h"
@@ -28,10 +26,8 @@
 #include "vec/columns/column_vector.h"
 #include "vec/data_types/data_type_decimal.h"
 #include "vec/data_types/data_type_nullable.h"
-#include "vec/data_types/data_type_number.h"
 #include "vec/data_types/data_type_string.h"
 #include "vec/functions/function.h"
-#include "vec/io/io_helper.h"
 
 namespace doris::vectorized {
 
@@ -51,7 +47,7 @@ public:
 
     void insert_into(IColumn& to) const {
         if constexpr (arg_is_nullable) {
-            auto* col = assert_cast<const ColumnNullable*, 
TypeCheckOnRelease::DISABLE>(_ptr);
+            const auto* col = assert_cast<const ColumnNullable*, 
TypeCheckOnRelease::DISABLE>(_ptr);
             assert_cast<ColVecType&, 
TypeCheckOnRelease::DISABLE>(to).insert_from(
                     col->get_nested_column(), _offset);
         } else {
@@ -89,7 +85,8 @@ public:
         // because the address have meaningless, only need it to check is 
nullptr
         this->_ptr = (IColumn*)0x00000001;
         if constexpr (arg_is_nullable) {
-            auto* col = assert_cast<const ColumnNullable*, 
TypeCheckOnRelease::DISABLE>(column);
+            const auto* col =
+                    assert_cast<const ColumnNullable*, 
TypeCheckOnRelease::DISABLE>(column);
             if (col->is_null_at(row)) {
                 this->reset();
                 return;
@@ -149,8 +146,9 @@ protected:
     bool _has_value = false;
 };
 
-template <typename Data>
-struct ReaderFunctionFirstData : Data {
+template <typename ColVecType, bool result_is_nullable, bool arg_is_nullable, 
bool is_copy>
+struct ReaderFunctionFirstData
+        : ReaderFirstAndLastData<ColVecType, result_is_nullable, 
arg_is_nullable, is_copy> {
     void add(int64_t row, const IColumn** columns) {
         if (this->has_set_value()) {
             return;
@@ -160,13 +158,15 @@ struct ReaderFunctionFirstData : Data {
     static const char* name() { return "first_value"; }
 };
 
-template <typename Data>
-struct ReaderFunctionFirstNonNullData : Data {
+template <typename ColVecType, bool result_is_nullable, bool arg_is_nullable, 
bool is_copy>
+struct ReaderFunctionFirstNonNullData
+        : ReaderFirstAndLastData<ColVecType, result_is_nullable, 
arg_is_nullable, is_copy> {
     void add(int64_t row, const IColumn** columns) {
         if (this->has_set_value()) {
             return;
         }
-        if constexpr (Data::nullable) {
+        if constexpr (ReaderFirstAndLastData<ColVecType, result_is_nullable, 
arg_is_nullable,
+                                             is_copy>::nullable) {
             const auto* nullable_column =
                     assert_cast<const ColumnNullable*, 
TypeCheckOnRelease::DISABLE>(columns[0]);
             if (nullable_column->is_null_at(row)) {
@@ -178,16 +178,19 @@ struct ReaderFunctionFirstNonNullData : Data {
     static const char* name() { return "first_non_null_value"; }
 };
 
-template <typename Data>
-struct ReaderFunctionLastData : Data {
+template <typename ColVecType, bool result_is_nullable, bool arg_is_nullable, 
bool is_copy>
+struct ReaderFunctionLastData
+        : ReaderFirstAndLastData<ColVecType, result_is_nullable, 
arg_is_nullable, is_copy> {
     void add(int64_t row, const IColumn** columns) { this->set_value(columns, 
row); }
     static const char* name() { return "last_value"; }
 };
 
-template <typename Data>
-struct ReaderFunctionLastNonNullData : Data {
+template <typename ColVecType, bool result_is_nullable, bool arg_is_nullable, 
bool is_copy>
+struct ReaderFunctionLastNonNullData
+        : ReaderFirstAndLastData<ColVecType, result_is_nullable, 
arg_is_nullable, is_copy> {
     void add(int64_t row, const IColumn** columns) {
-        if constexpr (Data::nullable) {
+        if constexpr (ReaderFirstAndLastData<ColVecType, result_is_nullable, 
arg_is_nullable,
+                                             is_copy>::nullable) {
             const auto* nullable_column =
                     assert_cast<const ColumnNullable*, 
TypeCheckOnRelease::DISABLE>(columns[0]);
             if (nullable_column->is_null_at(row)) {
@@ -256,17 +259,18 @@ private:
     DataTypePtr _argument_type;
 };
 
-template <template <typename> class AggregateFunctionTemplate, template 
<typename> class Impl,
-          bool result_is_nullable, bool arg_is_nullable, bool is_copy = false>
+template <template <typename, bool, bool, bool> class FunctionData, bool 
result_is_nullable,
+          bool arg_is_nullable, bool is_copy = false>
 AggregateFunctionPtr create_function_single_value(const String& name,
                                                   const DataTypes& 
argument_types) {
     auto type = remove_nullable(argument_types[0]);
     WhichDataType which(*type);
 
-#define DISPATCH(TYPE, COLUMN_TYPE)                                            
        \
-    if (which.idx == TypeIndex::TYPE)                                          
        \
-        return 
std::make_shared<AggregateFunctionTemplate<Impl<ReaderFirstAndLastData< \
-                COLUMN_TYPE, result_is_nullable, arg_is_nullable, 
is_copy>>>>(argument_types);
+#define DISPATCH(TYPE, COLUMN_TYPE)                                            
            \
+    if (which.idx == TypeIndex::TYPE)                                          
            \
+        return std::make_shared<ReaderFunctionData<                            
            \
+                FunctionData<COLUMN_TYPE, result_is_nullable, arg_is_nullable, 
is_copy>>>( \
+                argument_types);
     TYPE_TO_COLUMN_TYPE(DISPATCH)
 #undef DISPATCH
 
@@ -285,9 +289,9 @@ AggregateFunctionPtr create_function_single_value(const 
String& name,
         std::visit(                                                            
                \
                 [&](auto result_is_nullable, auto arg_is_nullable) {           
                \
                     res = AggregateFunctionPtr(                                
                \
-                            create_function_single_value<ReaderFunctionData, 
FUNCTION_DATA,    \
-                                                         result_is_nullable, 
arg_is_nullable,  \
-                                                         is_copy>(name, 
argument_types));      \
+                            create_function_single_value<FUNCTION_DATA, 
result_is_nullable,    \
+                                                         arg_is_nullable, 
is_copy>(            \
+                                    name, argument_types));                    
                \
                 },                                                             
                \
                 make_bool_variant(result_is_nullable), 
make_bool_variant(arg_is_nullable));    \
         if (!res) {                                                            
                \
@@ -303,4 +307,6 @@ 
CREATE_READER_FUNCTION_WITH_NAME_AND_DATA(create_aggregate_function_first_non_nu
 CREATE_READER_FUNCTION_WITH_NAME_AND_DATA(create_aggregate_function_last, 
ReaderFunctionLastData);
 
CREATE_READER_FUNCTION_WITH_NAME_AND_DATA(create_aggregate_function_last_non_null_value,
                                           ReaderFunctionLastNonNullData);
+#undef CREATE_READER_FUNCTION_WITH_NAME_AND_DATA
+
 } // namespace doris::vectorized
\ No newline at end of file
diff --git a/be/src/vec/aggregate_functions/aggregate_function_stddev.cpp 
b/be/src/vec/aggregate_functions/aggregate_function_stddev.cpp
index f9fe2dca748..5a76c3b836d 100644
--- a/be/src/vec/aggregate_functions/aggregate_function_stddev.cpp
+++ b/be/src/vec/aggregate_functions/aggregate_function_stddev.cpp
@@ -29,20 +29,16 @@
 
 namespace doris::vectorized {
 
-template <template <typename, bool> class AggregateFunctionTemplate,
-          template <typename> class NameData, template <typename, typename> 
class Data,
-          bool is_stddev, bool is_nullable = false>
+template <template <typename> class Function, typename Name,
+          template <typename, typename, bool> class Data, bool is_stddev>
 AggregateFunctionPtr create_function_single_value(const String& name,
                                                   const DataTypes& 
argument_types,
-                                                  const bool 
result_is_nullable,
-                                                  bool custom_nullable) {
+                                                  const bool 
result_is_nullable) {
     WhichDataType which(remove_nullable(argument_types[0]));
 #define DISPATCH(TYPE)                                                         
     \
     if (which.idx == TypeIndex::TYPE)                                          
     \
-        return creator_without_type::create<AggregateFunctionTemplate<         
     \
-                NameData<Data<TYPE, BaseData<TYPE, is_stddev>>>, 
is_nullable>>(     \
-                custom_nullable ? remove_nullable(argument_types) : 
argument_types, \
-                result_is_nullable);
+        return creator_without_type::create<Function<Data<TYPE, Name, 
is_stddev>>>( \
+                argument_types, result_is_nullable);
     FOR_NUMERIC_TYPES(DISPATCH)
 #undef DISPATCH
 
@@ -55,41 +51,39 @@ AggregateFunctionPtr 
create_aggregate_function_variance_samp(const std::string&
                                                              const DataTypes& 
argument_types,
                                                              const bool 
result_is_nullable,
                                                              const 
AggregateFunctionAttr& attr) {
-    return create_function_single_value<AggregateFunctionSamp, 
VarianceSampName, SampData, false>(
-            name, argument_types, result_is_nullable, false);
+    return create_function_single_value<AggregateFunctionSampVariance, 
VarianceSampName, SampData,
+                                        false>(name, argument_types, 
result_is_nullable);
 }
 
-template <bool is_stddev>
 AggregateFunctionPtr create_aggregate_function_variance_pop(const std::string& 
name,
                                                             const DataTypes& 
argument_types,
                                                             const bool 
result_is_nullable,
                                                             const 
AggregateFunctionAttr& attr) {
-    return create_function_single_value<AggregateFunctionPop, VarianceName, 
PopData, is_stddev>(
-            name, argument_types, result_is_nullable, false);
+    return create_function_single_value<AggregateFunctionSampVariance, 
VarianceName, PopData,
+                                        false>(name, argument_types, 
result_is_nullable);
 }
 
-template <bool is_stddev>
 AggregateFunctionPtr create_aggregate_function_stddev_pop(const std::string& 
name,
                                                           const DataTypes& 
argument_types,
                                                           const bool 
result_is_nullable,
                                                           const 
AggregateFunctionAttr& attr) {
-    return create_function_single_value<AggregateFunctionPop, StddevName, 
PopData, is_stddev>(
-            name, argument_types, result_is_nullable, false);
+    return create_function_single_value<AggregateFunctionSampVariance, 
StddevName, PopData, true>(
+            name, argument_types, result_is_nullable);
 }
 
 AggregateFunctionPtr create_aggregate_function_stddev_samp(const std::string& 
name,
                                                            const DataTypes& 
argument_types,
                                                            const bool 
result_is_nullable,
                                                            const 
AggregateFunctionAttr& attr) {
-    return create_function_single_value<AggregateFunctionSamp, StddevSampName, 
SampData, true>(
-            name, argument_types, result_is_nullable, false);
+    return create_function_single_value<AggregateFunctionSampVariance, 
StddevSampName, SampData,
+                                        true>(name, argument_types, 
result_is_nullable);
 }
 
 void 
register_aggregate_function_stddev_variance_pop(AggregateFunctionSimpleFactory& 
factory) {
-    factory.register_function_both("variance", 
create_aggregate_function_variance_pop<false>);
+    factory.register_function_both("variance", 
create_aggregate_function_variance_pop);
     factory.register_alias("variance", "var_pop");
     factory.register_alias("variance", "variance_pop");
-    factory.register_function_both("stddev", 
create_aggregate_function_stddev_pop<true>);
+    factory.register_function_both("stddev", 
create_aggregate_function_stddev_pop);
     factory.register_alias("stddev", "stddev_pop");
 }
 
diff --git a/be/src/vec/aggregate_functions/aggregate_function_stddev.h 
b/be/src/vec/aggregate_functions/aggregate_function_stddev.h
index bcd35b13149..9b3cd190991 100644
--- a/be/src/vec/aggregate_functions/aggregate_function_stddev.h
+++ b/be/src/vec/aggregate_functions/aggregate_function_stddev.h
@@ -17,15 +17,13 @@
 
 #pragma once
 
-#include <stddef.h>
-#include <stdint.h>
-
 #include <boost/iterator/iterator_facade.hpp>
 #include <cmath>
+#include <cstddef>
+#include <cstdint>
 #include <memory>
 #include <type_traits>
 
-#include "agent/be_exec_version_manager.h"
 #include "vec/aggregate_functions/aggregate_function.h"
 #include "vec/columns/column.h"
 #include "vec/columns/column_nullable.h"
@@ -35,8 +33,8 @@
 #include "vec/data_types/data_type_number.h"
 #include "vec/io/io_helper.h"
 
-namespace doris {
-namespace vectorized {
+namespace doris::vectorized {
+
 class Arena;
 class BufferReadable;
 class BufferWritable;
@@ -44,14 +42,10 @@ template <typename T>
 class ColumnDecimal;
 template <typename>
 class ColumnVector;
-} // namespace vectorized
-} // namespace doris
-
-namespace doris::vectorized {
 
 template <typename T, bool is_stddev>
 struct BaseData {
-    BaseData() : mean(0.0), m2(0.0), count(0) {}
+    BaseData() = default;
     virtual ~BaseData() = default;
 
     void write(BufferWritable& buf) const {
@@ -126,13 +120,13 @@ struct BaseData {
         count += 1;
     }
 
-    double mean;
-    double m2;
-    int64_t count;
+    double mean {};
+    double m2 {};
+    int64_t count {};
 };
 
-template <typename T, typename Data>
-struct PopData : Data {
+template <typename T, typename Name, bool is_stddev>
+struct PopData : BaseData<T, is_stddev>, Name {
     using ColVecResult =
             std::conditional_t<IsDecimalNumber<T>, 
ColumnDecimal<Decimal128V2>, ColumnFloat64>;
     void insert_result_into(IColumn& to) const {
@@ -151,28 +145,8 @@ struct PopData : Data {
 // because the operations involve squaring,
 // which can easily exceed the range of the Decimal type.
 
-template <typename Data>
-struct StddevName : Data {
-    static const char* name() { return "stddev"; }
-};
-
-template <typename Data>
-struct VarianceName : Data {
-    static const char* name() { return "variance"; }
-};
-
-template <typename Data>
-struct VarianceSampName : Data {
-    static const char* name() { return "variance_samp"; }
-};
-
-template <typename Data>
-struct StddevSampName : Data {
-    static const char* name() { return "stddev_samp"; }
-};
-
-template <typename T, typename Data>
-struct SampData : Data {
+template <typename T, typename Name, bool is_stddev>
+struct SampData : BaseData<T, is_stddev>, Name {
     using ColVecResult =
             std::conditional_t<IsDecimalNumber<T>, 
ColumnDecimal<Decimal128V2>, ColumnFloat64>;
     void insert_result_into(IColumn& to) const {
@@ -191,14 +165,25 @@ struct SampData : Data {
     static DataTypePtr get_return_type() { return 
std::make_shared<DataTypeNumber<Float64>>(); }
 };
 
-template <bool is_pop, typename Data, bool is_nullable>
+struct StddevName {
+    static const char* name() { return "stddev"; }
+};
+struct VarianceName {
+    static const char* name() { return "variance"; }
+};
+struct VarianceSampName {
+    static const char* name() { return "variance_samp"; }
+};
+struct StddevSampName {
+    static const char* name() { return "stddev_samp"; }
+};
+
+template <typename Data>
 class AggregateFunctionSampVariance
-        : public IAggregateFunctionDataHelper<
-                  Data, AggregateFunctionSampVariance<is_pop, Data, 
is_nullable>> {
+        : public IAggregateFunctionDataHelper<Data, 
AggregateFunctionSampVariance<Data>> {
 public:
     AggregateFunctionSampVariance(const DataTypes& argument_types_)
-            : IAggregateFunctionDataHelper<
-                      Data, AggregateFunctionSampVariance<is_pop, Data, 
is_nullable>>(
+            : IAggregateFunctionDataHelper<Data, 
AggregateFunctionSampVariance<Data>>(
                       argument_types_) {}
 
     String get_name() const override { return Data::name(); }
@@ -207,18 +192,7 @@ public:
 
     void add(AggregateDataPtr __restrict place, const IColumn** columns, 
ssize_t row_num,
              Arena*) const override {
-        if constexpr (is_pop) {
-            this->data(place).add(columns[0], row_num);
-        } else {
-            if constexpr (is_nullable) { //this if check could remove with old 
function
-                const auto* nullable_column = 
check_and_get_column<ColumnNullable>(columns[0]);
-                if (!nullable_column->is_null_at(row_num)) {
-                    
this->data(place).add(&nullable_column->get_nested_column(), row_num);
-                }
-            } else {
-                this->data(place).add(columns[0], row_num);
-            }
-        }
+        this->data(place).add(columns[0], row_num);
     }
 
     void reset(AggregateDataPtr __restrict place) const override { 
this->data(place).reset(); }
@@ -242,19 +216,4 @@ public:
     }
 };
 
-template <typename Data, bool is_nullable>
-class AggregateFunctionSamp final : public 
AggregateFunctionSampVariance<false, Data, is_nullable> {
-public:
-    AggregateFunctionSamp(const DataTypes& argument_types_)
-            : AggregateFunctionSampVariance<false, Data, 
is_nullable>(argument_types_) {}
-};
-
-//pop function have use AggregateFunctionNullBase function, so needn't 
processing null values
-template <typename Data, bool is_nullable>
-class AggregateFunctionPop final : public AggregateFunctionSampVariance<true, 
Data, is_nullable> {
-public:
-    AggregateFunctionPop(const DataTypes& argument_types_)
-            : AggregateFunctionSampVariance<true, Data, 
is_nullable>(argument_types_) {}
-};
-
 } // namespace doris::vectorized


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to