be/src/exprs/aggregate/aggregate_function_bool_union.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #pragma once |
19 | | |
20 | | #include "core/assert_cast.h" |
21 | | #include "core/column/column_nullable.h" |
22 | | #include "core/column/column_vector.h" |
23 | | #include "core/data_type/data_type.h" |
24 | | #include "core/data_type/data_type_nullable.h" |
25 | | #include "core/data_type/data_type_number.h" |
26 | | #include "core/data_type/define_primitive_type.h" |
27 | | #include "core/data_type/primitive_type.h" |
28 | | #include "core/types.h" |
29 | | #include "exprs/aggregate/aggregate_function.h" |
30 | | #include "exprs/aggregate/aggregate_function_bit.h" |
31 | | |
32 | | namespace doris { |
33 | | |
34 | | struct AggregateFunctionBoolXorData { |
35 | | static constexpr auto name = "bool_xor"; |
36 | | |
37 | 42 | void add(bool x) { |
38 | 42 | if (x && _st < 2) { |
39 | 22 | ++_st; |
40 | 22 | } |
41 | 42 | } |
42 | | |
43 | 12 | void merge(const AggregateFunctionBoolXorData& rhs) { |
44 | 12 | if (_st == 0) { |
45 | 8 | _st = rhs._st; |
46 | 8 | } else if (_st == 1) { |
47 | 3 | _st = (rhs._st > 0) ? 2 : 1; |
48 | 3 | } |
49 | 12 | } |
50 | | |
51 | 14 | void write(BufferWritable& buf) const { buf.write_binary(_st); } |
52 | | |
53 | 12 | void read(BufferReadable& buf) { buf.read_binary(_st); } |
54 | | |
55 | 2 | void reset() { _st = 0; } |
56 | | |
57 | 12 | bool get() const { return _st == 1; } |
58 | | |
59 | | private: |
60 | | // represents the current XOR state |
61 | | // '0': there are no true values currently |
62 | | // '1': exactly one true value has appeared |
63 | | // '2': two true values have already appeared and will not change thereafter |
64 | | uint8_t _st = 0; |
65 | | }; |
66 | | |
67 | | template <typename BoolFunc> |
68 | | class AggregateFuntionBoolUnion final |
69 | | : public IAggregateFunctionDataHelper<BoolFunc, AggregateFuntionBoolUnion<BoolFunc>>, |
70 | | NullableAggregateFunction, |
71 | | UnaryExpression { |
72 | | public: |
73 | | explicit AggregateFuntionBoolUnion(const DataTypes& argument_types_) |
74 | 2 | : IAggregateFunctionDataHelper<BoolFunc, AggregateFuntionBoolUnion<BoolFunc>>( |
75 | 2 | argument_types_) {} |
76 | | |
77 | 2 | String get_name() const override { return BoolFunc::name; } |
78 | | |
79 | 2 | DataTypePtr get_return_type() const override { return std::make_shared<DataTypeBool>(); } |
80 | | |
81 | | void add(AggregateDataPtr __restrict place, const IColumn** columns, ssize_t row_num, |
82 | 42 | Arena&) const override { |
83 | 42 | this->data(place).add( |
84 | 42 | assert_cast<const ColumnUInt8&, TypeCheckOnRelease::DISABLE>(*columns[0]) |
85 | 42 | .get_element(row_num)); |
86 | 42 | } |
87 | | |
88 | 2 | void reset(AggregateDataPtr __restrict place) const override { this->data(place).reset(); } |
89 | | |
90 | | void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs, |
91 | 12 | Arena&) const override { |
92 | 12 | this->data(place).merge(this->data(rhs)); |
93 | 12 | } |
94 | | |
95 | 14 | void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override { |
96 | 14 | this->data(place).write(buf); |
97 | 14 | } |
98 | | |
99 | | void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf, |
100 | 12 | Arena&) const override { |
101 | 12 | this->data(place).read(buf); |
102 | 12 | } |
103 | | |
104 | 12 | void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override { |
105 | 12 | assert_cast<ColumnUInt8&>(to).insert_value(this->data(place).get()); |
106 | 12 | } |
107 | | }; |
108 | | } // namespace doris |