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 | | struct NameBitCount { |
32 | | static constexpr auto name = "bit_count"; |
33 | | }; |
34 | | |
35 | | template <typename T> |
36 | | struct BitCountImpl { |
37 | | // No unsigned type in Java. So we need signed number as return type |
38 | | // Int8_MAX = 127 |
39 | | static constexpr PrimitiveType ResultType = sizeof(T) * 8 >= 128 ? TYPE_SMALLINT : TYPE_TINYINT; |
40 | | using DataType = typename PrimitiveTypeTraits<ResultType>::DataType; |
41 | 33 | static inline typename PrimitiveTypeTraits<ResultType>::CppType apply(T a) { |
42 | | if constexpr (std::is_same_v<T, Int128> || std::is_same_v<T, Int64> || |
43 | | std::is_same_v<T, Int32> || std::is_same_v<T, Int16> || |
44 | 33 | std::is_same_v<T, Int8>) { |
45 | | // ResultType already check the length |
46 | 33 | return cast_set<typename PrimitiveTypeTraits<ResultType>::CppType, int, false>( |
47 | 33 | std::popcount(static_cast<std::make_unsigned_t<T>>(a))); |
48 | | } else { |
49 | | throw Exception(ErrorCode::INVALID_ARGUMENT, |
50 | | "bit_count only support using INTEGER as operator"); |
51 | | } |
52 | 33 | } _ZN5doris12BitCountImplIaE5applyEa Line | Count | Source | 41 | 4 | static inline typename PrimitiveTypeTraits<ResultType>::CppType apply(T a) { | 42 | | if constexpr (std::is_same_v<T, Int128> || std::is_same_v<T, Int64> || | 43 | | std::is_same_v<T, Int32> || std::is_same_v<T, Int16> || | 44 | 4 | std::is_same_v<T, Int8>) { | 45 | | // ResultType already check the length | 46 | 4 | return cast_set<typename PrimitiveTypeTraits<ResultType>::CppType, int, false>( | 47 | 4 | std::popcount(static_cast<std::make_unsigned_t<T>>(a))); | 48 | | } else { | 49 | | throw Exception(ErrorCode::INVALID_ARGUMENT, | 50 | | "bit_count only support using INTEGER as operator"); | 51 | | } | 52 | 4 | } |
_ZN5doris12BitCountImplIsE5applyEs Line | Count | Source | 41 | 1 | static inline typename PrimitiveTypeTraits<ResultType>::CppType apply(T a) { | 42 | | if constexpr (std::is_same_v<T, Int128> || std::is_same_v<T, Int64> || | 43 | | std::is_same_v<T, Int32> || std::is_same_v<T, Int16> || | 44 | 1 | std::is_same_v<T, Int8>) { | 45 | | // ResultType already check the length | 46 | 1 | return cast_set<typename PrimitiveTypeTraits<ResultType>::CppType, int, false>( | 47 | 1 | std::popcount(static_cast<std::make_unsigned_t<T>>(a))); | 48 | | } else { | 49 | | throw Exception(ErrorCode::INVALID_ARGUMENT, | 50 | | "bit_count only support using INTEGER as operator"); | 51 | | } | 52 | 1 | } |
_ZN5doris12BitCountImplIiE5applyEi Line | Count | Source | 41 | 2 | static inline typename PrimitiveTypeTraits<ResultType>::CppType apply(T a) { | 42 | | if constexpr (std::is_same_v<T, Int128> || std::is_same_v<T, Int64> || | 43 | | std::is_same_v<T, Int32> || std::is_same_v<T, Int16> || | 44 | 2 | std::is_same_v<T, Int8>) { | 45 | | // ResultType already check the length | 46 | 2 | return cast_set<typename PrimitiveTypeTraits<ResultType>::CppType, int, false>( | 47 | 2 | std::popcount(static_cast<std::make_unsigned_t<T>>(a))); | 48 | | } else { | 49 | | throw Exception(ErrorCode::INVALID_ARGUMENT, | 50 | | "bit_count only support using INTEGER as operator"); | 51 | | } | 52 | 2 | } |
_ZN5doris12BitCountImplIlE5applyEl Line | Count | Source | 41 | 26 | static inline typename PrimitiveTypeTraits<ResultType>::CppType apply(T a) { | 42 | | if constexpr (std::is_same_v<T, Int128> || std::is_same_v<T, Int64> || | 43 | | std::is_same_v<T, Int32> || std::is_same_v<T, Int16> || | 44 | 26 | std::is_same_v<T, Int8>) { | 45 | | // ResultType already check the length | 46 | 26 | return cast_set<typename PrimitiveTypeTraits<ResultType>::CppType, int, false>( | 47 | 26 | std::popcount(static_cast<std::make_unsigned_t<T>>(a))); | 48 | | } else { | 49 | | throw Exception(ErrorCode::INVALID_ARGUMENT, | 50 | | "bit_count only support using INTEGER as operator"); | 51 | | } | 52 | 26 | } |
Unexecuted instantiation: _ZN5doris12BitCountImplInE5applyEn |
53 | | }; |
54 | | |
55 | | using FunctionBitCountTinyInt = |
56 | | FunctionUnaryArithmetic<BitCountImpl<Int8>, NameBitCount, TYPE_TINYINT>; |
57 | | using FunctionBitCountSmallInt = |
58 | | FunctionUnaryArithmetic<BitCountImpl<Int16>, NameBitCount, TYPE_SMALLINT>; |
59 | | using FunctionBitCountInt = FunctionUnaryArithmetic<BitCountImpl<Int32>, NameBitCount, TYPE_INT>; |
60 | | using FunctionBitCountBigInt = |
61 | | FunctionUnaryArithmetic<BitCountImpl<Int64>, NameBitCount, TYPE_BIGINT>; |
62 | | using FunctionBitCountLargeInt = |
63 | | FunctionUnaryArithmetic<BitCountImpl<Int128>, NameBitCount, TYPE_LARGEINT>; |
64 | | |
65 | 8 | void register_function_bit_count(SimpleFunctionFactory& factory) { |
66 | 8 | factory.register_function<FunctionBitCountTinyInt>(); |
67 | 8 | factory.register_function<FunctionBitCountSmallInt>(); |
68 | 8 | factory.register_function<FunctionBitCountInt>(); |
69 | 8 | factory.register_function<FunctionBitCountBigInt>(); |
70 | 8 | factory.register_function<FunctionBitCountLargeInt>(); |
71 | 8 | } |
72 | | |
73 | | } // namespace doris |