ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

ClickHouse 聚合函数 实践

2021-08-02 19:04:45  阅读:262  来源: 互联网

标签:聚合 函数 sum static ClickHouse AggregateFunctionSum type 模板


ClickHouse 学习中,如果有问题,请在下方讨论。

 

为了比较快的了解聚合函数的相关架构,我们选择比较简单的聚合函数。常见比较简单的聚合函数有max/min/sum/average等,我们拿sum为例.

例如: 我们有个SQL 语句 select sum(a) from table;  如果看做是一个简单的求和问题,那么我们就会遍历所有输入的数据,并进行求和计算。 类似

伪码:

long Sum(data){
  let sum = 0;   //遍历列中的数据,并进行计算。   for (auto _data: data) sum += _data;   return sum;
}

    那么在AggregateFunctionSum是如何实现这种抽象的呢. 带着这个疑问,我们找到AggregateFunctionSum.h 文件.

    这个类模板 继承冲击力比较强,需要做一下作者在这类继承关系上的思路。 

/// Counts the sum of the numbers.
template <typename T, typename TResult, typename Data, AggregateFunctionSumType Type>
class AggregateFunctionSum final : public IAggregateFunctionDataHelper<Data, AggregateFunctionSum<T, TResult, Data, Type>>

  以下是推断逻辑:

  某个聚合函数的实际组成是由 函数 + 数据构成 (这里是继承关系)。

      1. 这里类比 AggregateFunctionSum : IAggregateFunctionDataHelpler(从类名称上,继承了Function和 Data的信息,后面会继续进行分解)

      2. IAggregateFunctionDataHelper 本模板类中定义的Data相关的 成员及成员函数,除去Data相关的就是  IAggregateFunctionHelper 的模板类。

      3. IAggregateFunctionHelper 是个Function的Helper类模板,类模板参数中的Derived中应该是一个Function. (AggregateFunctionSum<T,TResult,Data,Type>).

      4. IAggregateFunctionHelper 应该在继承IAggregateFunction的所有成员后,还需要加入一些增强(Helper)的成员。 这里我们看到的是 addFree ,静态方法,因为毕竟IAggregateFunctionHelpler是个Interface(因为其没有实现IAggregateFunction里面所有的pure virtual function,所以不能实例化)。其中也override了一些IAggregateFunction的一些 pure virtual function.  addFree方法 的确是可以通过 static_cast<const Derived &>(*that).add(place, columns, row_column, arena).

  

  5.通过Debug,我们可以看到,一般聚合函数的调用是在这里进行的。(Aggregator.cpp).

      6.我们注册的函数会根据参数类型动态生成对应的聚合函数模板类。 
/** Create an aggregate function with a numeric type in the template parameter, depending on the type of the argument.
  */
template <template <typename> class AggregateFunctionTemplate, typename... TArgs>
static IAggregateFunction * createWithNumericType(const IDataType & argument_type, TArgs && ... args)

  ---未完待续

  

      

#include <iostream>

using namespace std;

class Base{
protected:
    static void f(){cout << "invoke static f method" << endl;}
};

class Derived: public Base{
    public:
    using func_type = void (*)();
    void derivedFunc(){
        this->f();
     //为函数指针赋值 使用&和不使用&结果竟然是一样的。语法兼容性有点强。 func_type x = &f; func_type y = f; y(); x(); } }; int main(){ Derived d; d.derivedFunc(); }

---结果

➜ C++Basic ./accessStaticMethodViaThisPointer

函数指针 赋值 还是太灵活。
invoke static f method
invoke static f method
invoke static f method

 

  

标签:聚合,函数,sum,static,ClickHouse,AggregateFunctionSum,type,模板
来源: https://www.cnblogs.com/wyc2021/p/15091207.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有