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 | | #include "common/compile_check_begin.h" |
34 | | |
35 | | struct AggregateFunctionBoolXorData { |
36 | | static constexpr auto name = "bool_xor"; |
37 | | |
38 | 42 | void add(bool x) { |
39 | 42 | if (x && _st < 2) { |
40 | 22 | ++_st; |
41 | 22 | } |
42 | 42 | } |
43 | | |
44 | 12 | void merge(const AggregateFunctionBoolXorData& rhs) { |
45 | 12 | if (_st == 0) { |
46 | 8 | _st = rhs._st; |
47 | 8 | } else if (_st == 1) { |
48 | 3 | _st = (rhs._st > 0) ? 2 : 1; |
49 | 3 | } |
50 | 12 | } |
51 | | |
52 | 14 | void write(BufferWritable& buf) const { buf.write_binary(_st); } |
53 | | |
54 | 12 | void read(BufferReadable& buf) { buf.read_binary(_st); } |
55 | | |
56 | 2 | void reset() { _st = 0; } |
57 | | |
58 | 12 | bool get() const { return _st == 1; } |
59 | | |
60 | | private: |
61 | | // represents the current XOR state |
62 | | // '0': there are no true values currently |
63 | | // '1': exactly one true value has appeared |
64 | | // '2': two true values have already appeared and will not change thereafter |
65 | | uint8_t _st = 0; |
66 | | }; |
67 | | |
68 | | template <typename BoolFunc> |
69 | | class AggregateFuntionBoolUnion final |
70 | | : public IAggregateFunctionDataHelper<BoolFunc, AggregateFuntionBoolUnion<BoolFunc>>, |
71 | | NullableAggregateFunction, |
72 | | UnaryExpression { |
73 | | public: |
74 | | explicit AggregateFuntionBoolUnion(const DataTypes& argument_types_) |
75 | 2 | : IAggregateFunctionDataHelper<BoolFunc, AggregateFuntionBoolUnion<BoolFunc>>( |
76 | 2 | argument_types_) {} |
77 | | |
78 | 2 | String get_name() const override { return BoolFunc::name; } |
79 | | |
80 | 2 | DataTypePtr get_return_type() const override { return std::make_shared<DataTypeBool>(); } |
81 | | |
82 | | void add(AggregateDataPtr __restrict place, const IColumn** columns, ssize_t row_num, |
83 | 42 | Arena&) const override { |
84 | 42 | this->data(place).add( |
85 | 42 | assert_cast<const ColumnUInt8&, TypeCheckOnRelease::DISABLE>(*columns[0]) |
86 | 42 | .get_element(row_num)); |
87 | 42 | } |
88 | | |
89 | 2 | void reset(AggregateDataPtr __restrict place) const override { this->data(place).reset(); } |
90 | | |
91 | | void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs, |
92 | 12 | Arena&) const override { |
93 | 12 | this->data(place).merge(this->data(rhs)); |
94 | 12 | } |
95 | | |
96 | 14 | void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override { |
97 | 14 | this->data(place).write(buf); |
98 | 14 | } |
99 | | |
100 | | void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf, |
101 | 12 | Arena&) const override { |
102 | 12 | this->data(place).read(buf); |
103 | 12 | } |
104 | | |
105 | 12 | void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override { |
106 | 12 | assert_cast<ColumnUInt8&>(to).insert_value(this->data(place).get()); |
107 | 12 | } |
108 | | }; |
109 | | } // namespace doris |
110 | | |
111 | | #include "common/compile_check_end.h" |