be/src/exprs/function/functions_logical.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 | | // This file is copied from |
18 | | // https://github.com/ClickHouse/ClickHouse/blob/master/src/Functions/FunctionsLogical.h |
19 | | // and modified by Doris |
20 | | |
21 | | #pragma once |
22 | | |
23 | | #include <fmt/format.h> |
24 | | #include <stddef.h> |
25 | | |
26 | | #include <algorithm> |
27 | | #include <boost/iterator/iterator_facade.hpp> |
28 | | #include <memory> |
29 | | |
30 | | #include "common/status.h" |
31 | | #include "core/block/column_numbers.h" |
32 | | #include "core/data_type/data_type.h" |
33 | | #include "core/types.h" |
34 | | #include "exprs/function/function.h" |
35 | | |
36 | | namespace doris { |
37 | | class FunctionContext; |
38 | | |
39 | | class Block; |
40 | | } // namespace doris |
41 | | |
42 | | /** Logical functions AND, OR and NOT support three-valued (or ternary) logic |
43 | | * https://en.wikibooks.org/wiki/Structured_Query_Language/NULLs_and_the_Three_Valued_Logic |
44 | | * |
45 | | * Functions NOT rely on "default implementation for NULLs": |
46 | | * - if any of the arguments is of Nullable type, the return value type is Nullable |
47 | | * - if any of the arguments is NULL, the return value is NULL |
48 | | * |
49 | | * Functions AND and OR provide their own special implementations for ternary logic |
50 | | */ |
51 | | |
52 | | namespace doris { |
53 | | namespace FunctionsLogicalDetail { |
54 | | namespace Ternary { |
55 | | using ResultType = UInt8; |
56 | | |
57 | | static constexpr UInt8 False = 0; |
58 | | static constexpr UInt8 True = -1; |
59 | | static constexpr UInt8 Null = 1; |
60 | | |
61 | | template <typename T> |
62 | | ResultType make_value(T value) { |
63 | | return value != 0 ? Ternary::True : Ternary::False; |
64 | | } |
65 | | |
66 | | template <typename T> |
67 | | ResultType make_value(T value, bool is_null) { |
68 | | if (is_null) return Ternary::Null; |
69 | | return make_value<T>(value); |
70 | | } |
71 | | } // namespace Ternary |
72 | | |
73 | | struct AndImpl { |
74 | | using ResultType = UInt8; |
75 | | |
76 | 17 | static inline constexpr ResultType apply(UInt8 a, UInt8 b) { return a & b; } |
77 | 17 | static inline constexpr ResultType apply_null(UInt8 a, UInt8 l_null, UInt8 b, UInt8 r_null) { |
78 | | // (<> && false) is false, (true && NULL) is NULL |
79 | 17 | return (l_null & r_null) | (r_null & (l_null ^ a)) | (l_null & (r_null ^ b)); |
80 | 17 | } |
81 | 1.38k | static inline constexpr bool special_implementation_for_nulls() { return true; } |
82 | | }; |
83 | | |
84 | | struct OrImpl { |
85 | | using ResultType = UInt8; |
86 | | |
87 | 6 | static inline constexpr ResultType apply(UInt8 a, UInt8 b) { return a | b; } |
88 | 6 | static inline constexpr ResultType apply_null(UInt8 a, UInt8 l_null, UInt8 b, UInt8 r_null) { |
89 | | // (<> || true) is true, (false || NULL) is NULL |
90 | 6 | return (l_null & r_null) | (r_null & (r_null ^ a)) | (l_null & (l_null ^ b)); |
91 | 6 | } |
92 | 2.80k | static inline constexpr bool special_implementation_for_nulls() { return true; } |
93 | | }; |
94 | | |
95 | | struct XorImpl { |
96 | | using ResultType = UInt8; |
97 | | |
98 | 9 | static inline constexpr ResultType apply(UInt8 a, UInt8 b) { return a ^ b; } |
99 | | // select null xor true , null xor false , false xor null , true xor null ; |
100 | 3 | static inline constexpr bool special_implementation_for_nulls() { return false; } |
101 | | }; |
102 | | |
103 | | template <PrimitiveType A> |
104 | | struct NotImpl { |
105 | | static constexpr PrimitiveType ResultType = TYPE_BOOLEAN; |
106 | | |
107 | 799k | static inline UInt8 apply(typename PrimitiveTypeTraits<A>::CppType a) { return !a; } |
108 | | }; |
109 | | |
110 | | template <typename Impl, typename Name> |
111 | | class FunctionAnyArityLogical : public IFunction { |
112 | | public: |
113 | | static constexpr auto name = Name::name; |
114 | 2.38k | static FunctionPtr create() { return std::make_shared<FunctionAnyArityLogical>(); }_ZN5doris22FunctionsLogicalDetail23FunctionAnyArityLogicalINS0_7AndImplENS_7NameAndEE6createEv Line | Count | Source | 114 | 914 | static FunctionPtr create() { return std::make_shared<FunctionAnyArityLogical>(); } |
_ZN5doris22FunctionsLogicalDetail23FunctionAnyArityLogicalINS0_6OrImplENS_6NameOrEE6createEv Line | Count | Source | 114 | 1.46k | static FunctionPtr create() { return std::make_shared<FunctionAnyArityLogical>(); } |
_ZN5doris22FunctionsLogicalDetail23FunctionAnyArityLogicalINS0_7XorImplENS_7NameXorEE6createEv Line | Count | Source | 114 | 10 | static FunctionPtr create() { return std::make_shared<FunctionAnyArityLogical>(); } |
|
115 | | |
116 | | public: |
117 | 3 | String get_name() const override { return name; }_ZNK5doris22FunctionsLogicalDetail23FunctionAnyArityLogicalINS0_7AndImplENS_7NameAndEE8get_nameB5cxx11Ev Line | Count | Source | 117 | 1 | String get_name() const override { return name; } |
_ZNK5doris22FunctionsLogicalDetail23FunctionAnyArityLogicalINS0_6OrImplENS_6NameOrEE8get_nameB5cxx11Ev Line | Count | Source | 117 | 1 | String get_name() const override { return name; } |
_ZNK5doris22FunctionsLogicalDetail23FunctionAnyArityLogicalINS0_7XorImplENS_7NameXorEE8get_nameB5cxx11Ev Line | Count | Source | 117 | 1 | String get_name() const override { return name; } |
|
118 | | |
119 | 2.36k | size_t get_number_of_arguments() const override { return 2; }_ZNK5doris22FunctionsLogicalDetail23FunctionAnyArityLogicalINS0_7AndImplENS_7NameAndEE23get_number_of_argumentsEv Line | Count | Source | 119 | 905 | size_t get_number_of_arguments() const override { return 2; } |
_ZNK5doris22FunctionsLogicalDetail23FunctionAnyArityLogicalINS0_6OrImplENS_6NameOrEE23get_number_of_argumentsEv Line | Count | Source | 119 | 1.45k | size_t get_number_of_arguments() const override { return 2; } |
_ZNK5doris22FunctionsLogicalDetail23FunctionAnyArityLogicalINS0_7XorImplENS_7NameXorEE23get_number_of_argumentsEv Line | Count | Source | 119 | 1 | size_t get_number_of_arguments() const override { return 2; } |
|
120 | | |
121 | 2.38k | bool use_default_implementation_for_nulls() const override { |
122 | 2.38k | return !Impl::special_implementation_for_nulls(); |
123 | 2.38k | } _ZNK5doris22FunctionsLogicalDetail23FunctionAnyArityLogicalINS0_7AndImplENS_7NameAndEE36use_default_implementation_for_nullsEv Line | Count | Source | 121 | 921 | bool use_default_implementation_for_nulls() const override { | 122 | 921 | return !Impl::special_implementation_for_nulls(); | 123 | 921 | } |
_ZNK5doris22FunctionsLogicalDetail23FunctionAnyArityLogicalINS0_6OrImplENS_6NameOrEE36use_default_implementation_for_nullsEv Line | Count | Source | 121 | 1.46k | bool use_default_implementation_for_nulls() const override { | 122 | 1.46k | return !Impl::special_implementation_for_nulls(); | 123 | 1.46k | } |
_ZNK5doris22FunctionsLogicalDetail23FunctionAnyArityLogicalINS0_7XorImplENS_7NameXorEE36use_default_implementation_for_nullsEv Line | Count | Source | 121 | 3 | bool use_default_implementation_for_nulls() const override { | 122 | 3 | return !Impl::special_implementation_for_nulls(); | 123 | 3 | } |
|
124 | | |
125 | | /// Get result types by argument types. If the function does not apply to these arguments, throw an exception. |
126 | | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override; |
127 | | |
128 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
129 | | uint32_t result, size_t input_rows_count) const override; |
130 | | }; |
131 | | |
132 | | template <template <PrimitiveType> class Impl, typename Name> |
133 | | class FunctionUnaryLogical : public IFunction { |
134 | | public: |
135 | | static constexpr auto name = Name::name; |
136 | 529 | static FunctionPtr create() { return std::make_shared<FunctionUnaryLogical>(); } |
137 | | |
138 | 1 | String get_name() const override { return name; } |
139 | | |
140 | 520 | size_t get_number_of_arguments() const override { return 1; } |
141 | | |
142 | | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override; |
143 | | |
144 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
145 | | uint32_t result, size_t input_rows_count) const override; |
146 | | }; |
147 | | |
148 | | } // namespace FunctionsLogicalDetail |
149 | | |
150 | | struct NameAnd { |
151 | | static constexpr auto name = "and"; |
152 | | }; |
153 | | struct NameOr { |
154 | | static constexpr auto name = "or"; |
155 | | }; |
156 | | struct NameNot { |
157 | | static constexpr auto name = "not"; |
158 | | }; |
159 | | |
160 | | struct NameXor { |
161 | | static constexpr auto name = "xor"; |
162 | | }; |
163 | | |
164 | | using FunctionAnd = |
165 | | FunctionsLogicalDetail::FunctionAnyArityLogical<FunctionsLogicalDetail::AndImpl, NameAnd>; |
166 | | using FunctionOr = |
167 | | FunctionsLogicalDetail::FunctionAnyArityLogical<FunctionsLogicalDetail::OrImpl, NameOr>; |
168 | | using FunctionNot = |
169 | | FunctionsLogicalDetail::FunctionUnaryLogical<FunctionsLogicalDetail::NotImpl, NameNot>; |
170 | | |
171 | | using FunctionXor = |
172 | | FunctionsLogicalDetail::FunctionAnyArityLogical<FunctionsLogicalDetail::XorImpl, NameXor>; |
173 | | } // namespace doris |