1#pragma once
2
3#include <IO/WriteHelpers.h>
4#include <IO/ReadHelpers.h>
5
6#include <DataTypes/DataTypesNumber.h>
7#include <Columns/ColumnVector.h>
8#include <Common/assert_cast.h>
9
10#include <AggregateFunctions/IAggregateFunction.h>
11
12
13namespace DB
14{
15
16
17template <typename T>
18struct AggregateFunctionGroupBitOrData
19{
20 T value = 0;
21 static const char * name() { return "groupBitOr"; }
22 void update(T x) { value |= x; }
23};
24
25template <typename T>
26struct AggregateFunctionGroupBitAndData
27{
28 T value = -1; /// Two's complement arithmetic, sign extension.
29 static const char * name() { return "groupBitAnd"; }
30 void update(T x) { value &= x; }
31};
32
33template <typename T>
34struct AggregateFunctionGroupBitXorData
35{
36 T value = 0;
37 static const char * name() { return "groupBitXor"; }
38 void update(T x) { value ^= x; }
39};
40
41
42/// Counts bitwise operation on numbers.
43template <typename T, typename Data>
44class AggregateFunctionBitwise final : public IAggregateFunctionDataHelper<Data, AggregateFunctionBitwise<T, Data>>
45{
46public:
47 AggregateFunctionBitwise(const DataTypePtr & type)
48 : IAggregateFunctionDataHelper<Data, AggregateFunctionBitwise<T, Data>>({type}, {}) {}
49
50 String getName() const override { return Data::name(); }
51
52 DataTypePtr getReturnType() const override
53 {
54 return std::make_shared<DataTypeNumber<T>>();
55 }
56
57 void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena *) const override
58 {
59 this->data(place).update(assert_cast<const ColumnVector<T> &>(*columns[0]).getData()[row_num]);
60 }
61
62 void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena *) const override
63 {
64 this->data(place).update(this->data(rhs).value);
65 }
66
67 void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
68 {
69 writeBinary(this->data(place).value, buf);
70 }
71
72 void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena *) const override
73 {
74 readBinary(this->data(place).value, buf);
75 }
76
77 void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
78 {
79 assert_cast<ColumnVector<T> &>(to).getData().push_back(this->data(place).value);
80 }
81};
82
83
84}
85