be/src/exprs/function/function_bit_count.cpp
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 | | #include <bit> |
19 | | #include <bitset> |
20 | | #include <cstdint> |
21 | | #include <type_traits> |
22 | | |
23 | | #include "common/exception.h" |
24 | | #include "common/status.h" |
25 | | #include "core/data_type/define_primitive_type.h" |
26 | | #include "core/types.h" |
27 | | #include "exprs/function/function_unary_arithmetic.h" |
28 | | #include "exprs/function/simple_function_factory.h" |
29 | | |
30 | | namespace doris { |
31 | | #include "common/compile_check_begin.h" |
32 | | struct NameBitCount { |
33 | | static constexpr auto name = "bit_count"; |
34 | | }; |
35 | | |
36 | | template <typename T> |
37 | | struct BitCountImpl { |
38 | | // No unsigned type in Java. So we need signed number as return type |
39 | | // Int8_MAX = 127 |
40 | | static constexpr PrimitiveType ResultType = sizeof(T) * 8 >= 128 ? TYPE_SMALLINT : TYPE_TINYINT; |
41 | | using DataType = typename PrimitiveTypeTraits<ResultType>::DataType; |
42 | 0 | static inline typename PrimitiveTypeTraits<ResultType>::CppType apply(T a) { |
43 | | if constexpr (std::is_same_v<T, Int128> || std::is_same_v<T, Int64> || |
44 | | std::is_same_v<T, Int32> || std::is_same_v<T, Int16> || |
45 | 0 | std::is_same_v<T, Int8>) { |
46 | | // ResultType already check the length |
47 | 0 | return cast_set<typename PrimitiveTypeTraits<ResultType>::CppType, int, false>( |
48 | 0 | std::popcount(static_cast<std::make_unsigned_t<T>>(a))); |
49 | | } else { |
50 | | throw Exception(ErrorCode::INVALID_ARGUMENT, |
51 | | "bit_count only support using INTEGER as operator"); |
52 | | } |
53 | 0 | } Unexecuted instantiation: _ZN5doris12BitCountImplIaE5applyEa Unexecuted instantiation: _ZN5doris12BitCountImplIsE5applyEs Unexecuted instantiation: _ZN5doris12BitCountImplIiE5applyEi Unexecuted instantiation: _ZN5doris12BitCountImplIlE5applyEl Unexecuted instantiation: _ZN5doris12BitCountImplInE5applyEn |
54 | | }; |
55 | | |
56 | | using FunctionBitCountTinyInt = |
57 | | FunctionUnaryArithmetic<BitCountImpl<Int8>, NameBitCount, TYPE_TINYINT>; |
58 | | using FunctionBitCountSmallInt = |
59 | | FunctionUnaryArithmetic<BitCountImpl<Int16>, NameBitCount, TYPE_SMALLINT>; |
60 | | using FunctionBitCountInt = FunctionUnaryArithmetic<BitCountImpl<Int32>, NameBitCount, TYPE_INT>; |
61 | | using FunctionBitCountBigInt = |
62 | | FunctionUnaryArithmetic<BitCountImpl<Int64>, NameBitCount, TYPE_BIGINT>; |
63 | | using FunctionBitCountLargeInt = |
64 | | FunctionUnaryArithmetic<BitCountImpl<Int128>, NameBitCount, TYPE_LARGEINT>; |
65 | | |
66 | 7 | void register_function_bit_count(SimpleFunctionFactory& factory) { |
67 | 7 | factory.register_function<FunctionBitCountTinyInt>(); |
68 | 7 | factory.register_function<FunctionBitCountSmallInt>(); |
69 | 7 | factory.register_function<FunctionBitCountInt>(); |
70 | 7 | factory.register_function<FunctionBitCountBigInt>(); |
71 | 7 | factory.register_function<FunctionBitCountLargeInt>(); |
72 | 7 | } |
73 | | |
74 | | } // namespace doris |