Coverage Report

Created: 2026-03-15 17:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/function_decode_varchar.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 <fmt/core.h>
19
20
#include <cstddef>
21
#include <limits>
22
#include <type_traits>
23
24
#include "common/exception.h"
25
#include "common/status.h"
26
#include "core/column/column_const.h"
27
#include "core/column/column_vector.h"
28
#include "core/data_type/data_type.h"
29
#include "core/data_type/data_type_number.h"
30
#include "core/data_type/data_type_string.h"
31
#include "core/data_type/primitive_type.h"
32
#include "core/types.h"
33
#include "exprs/function/function.h"
34
#include "exprs/function/function_helpers.h"
35
#include "exprs/function/simple_function_factory.h"
36
#include "util/simd/reverse_copy_bytes.h"
37
38
namespace doris {
39
40
template <PrimitiveType IntegerPType>
41
class FunctionDecodeAsVarchar : public IFunction {
42
public:
43
    using IntegerType = typename PrimitiveTypeTraits<IntegerPType>::CppType;
44
    static constexpr auto name = "decode_as_varchar";
45
68
    static FunctionPtr create() { return std::make_shared<FunctionDecodeAsVarchar>(); }
_ZN5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE4EE6createEv
Line
Count
Source
45
6
    static FunctionPtr create() { return std::make_shared<FunctionDecodeAsVarchar>(); }
_ZN5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE5EE6createEv
Line
Count
Source
45
10
    static FunctionPtr create() { return std::make_shared<FunctionDecodeAsVarchar>(); }
_ZN5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE6EE6createEv
Line
Count
Source
45
18
    static FunctionPtr create() { return std::make_shared<FunctionDecodeAsVarchar>(); }
_ZN5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE7EE6createEv
Line
Count
Source
45
34
    static FunctionPtr create() { return std::make_shared<FunctionDecodeAsVarchar>(); }
46
47
0
    String get_name() const override { return name; }
Unexecuted instantiation: _ZNK5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE4EE8get_nameB5cxx11Ev
Unexecuted instantiation: _ZNK5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE5EE8get_nameB5cxx11Ev
Unexecuted instantiation: _ZNK5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE6EE8get_nameB5cxx11Ev
Unexecuted instantiation: _ZNK5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE7EE8get_nameB5cxx11Ev
48
49
0
    size_t get_number_of_arguments() const override { return 1; }
Unexecuted instantiation: _ZNK5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE4EE23get_number_of_argumentsEv
Unexecuted instantiation: _ZNK5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE5EE23get_number_of_argumentsEv
Unexecuted instantiation: _ZNK5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE6EE23get_number_of_argumentsEv
Unexecuted instantiation: _ZNK5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE7EE23get_number_of_argumentsEv
50
51
64
    bool is_variadic() const override { return true; }
_ZNK5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE4EE11is_variadicEv
Line
Count
Source
51
5
    bool is_variadic() const override { return true; }
_ZNK5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE5EE11is_variadicEv
Line
Count
Source
51
9
    bool is_variadic() const override { return true; }
_ZNK5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE6EE11is_variadicEv
Line
Count
Source
51
17
    bool is_variadic() const override { return true; }
_ZNK5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE7EE11is_variadicEv
Line
Count
Source
51
33
    bool is_variadic() const override { return true; }
52
53
4
    DataTypes get_variadic_argument_types_impl() const override {
54
4
        return {std::make_shared<typename PrimitiveTypeTraits<IntegerPType>::DataType>()};
55
4
    }
_ZNK5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE4EE32get_variadic_argument_types_implEv
Line
Count
Source
53
1
    DataTypes get_variadic_argument_types_impl() const override {
54
1
        return {std::make_shared<typename PrimitiveTypeTraits<IntegerPType>::DataType>()};
55
1
    }
_ZNK5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE5EE32get_variadic_argument_types_implEv
Line
Count
Source
53
1
    DataTypes get_variadic_argument_types_impl() const override {
54
1
        return {std::make_shared<typename PrimitiveTypeTraits<IntegerPType>::DataType>()};
55
1
    }
_ZNK5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE6EE32get_variadic_argument_types_implEv
Line
Count
Source
53
1
    DataTypes get_variadic_argument_types_impl() const override {
54
1
        return {std::make_shared<typename PrimitiveTypeTraits<IntegerPType>::DataType>()};
55
1
    }
_ZNK5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE7EE32get_variadic_argument_types_implEv
Line
Count
Source
53
1
    DataTypes get_variadic_argument_types_impl() const override {
54
1
        return {std::make_shared<typename PrimitiveTypeTraits<IntegerPType>::DataType>()};
55
1
    }
56
57
60
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
58
60
        if (arguments.size() != 1) {
59
0
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
60
0
                                   "Function {} requires 1 arguments, got {}", name,
61
0
                                   arguments.size());
62
0
        }
63
64
60
        return std::make_shared<DataTypeString>();
65
60
    }
_ZNK5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE4EE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE
Line
Count
Source
57
4
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
58
4
        if (arguments.size() != 1) {
59
0
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
60
0
                                   "Function {} requires 1 arguments, got {}", name,
61
0
                                   arguments.size());
62
0
        }
63
64
4
        return std::make_shared<DataTypeString>();
65
4
    }
_ZNK5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE5EE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE
Line
Count
Source
57
8
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
58
8
        if (arguments.size() != 1) {
59
0
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
60
0
                                   "Function {} requires 1 arguments, got {}", name,
61
0
                                   arguments.size());
62
0
        }
63
64
8
        return std::make_shared<DataTypeString>();
65
8
    }
_ZNK5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE6EE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE
Line
Count
Source
57
16
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
58
16
        if (arguments.size() != 1) {
59
0
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
60
0
                                   "Function {} requires 1 arguments, got {}", name,
61
0
                                   arguments.size());
62
0
        }
63
64
16
        return std::make_shared<DataTypeString>();
65
16
    }
_ZNK5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE7EE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE
Line
Count
Source
57
32
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
58
32
        if (arguments.size() != 1) {
59
0
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
60
0
                                   "Function {} requires 1 arguments, got {}", name,
61
0
                                   arguments.size());
62
0
        }
63
64
32
        return std::make_shared<DataTypeString>();
65
32
    }
66
67
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
68
60
                        uint32_t result, size_t input_rows_count) const override {
69
60
        const auto* col_source =
70
60
                assert_cast<const typename PrimitiveTypeTraits<IntegerPType>::ColumnType*>(
71
60
                        block.get_by_position(arguments[0]).column.get());
72
73
60
        auto col_res = ColumnString::create();
74
75
60
        ColumnString::Chars& col_res_data = col_res->get_chars();
76
60
        ColumnString::Offsets& col_res_offset = col_res->get_offsets();
77
60
        col_res_data.resize(input_rows_count * sizeof(IntegerType));
78
60
        col_res_offset.resize(input_rows_count);
79
80
245k
        for (Int32 i = 0; i < input_rows_count; ++i) {
81
245k
            IntegerType value = col_source->get_element(i);
82
245k
            const auto* const __restrict ui8_ptr = reinterpret_cast<const UInt8*>(&value);
83
245k
            UInt32 str_size = static_cast<UInt32>(*ui8_ptr) & 0x7F;
84
85
245k
            if (str_size >= sizeof(IntegerType)) {
86
0
                const auto& type_ptr = block.get_by_position(arguments[0]).type;
87
0
                throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
88
0
                                       "Invalid input of function {}, input type {} value {}, "
89
0
                                       "string size {}, should not be larger than {}",
90
0
                                       name, type_ptr->get_name(), value, str_size,
91
0
                                       sizeof(IntegerType));
92
0
            }
93
94
            // col_res_offset[-1] is valid for PaddedPODArray, will get 0
95
245k
            col_res_offset[i] = col_res_offset[i - 1] + str_size;
96
245k
            value <<= 1;
97
98
245k
            simd::reverse_copy_bytes(col_res_data.data() + col_res_offset[i - 1], str_size,
99
245k
                                     ui8_ptr + sizeof(IntegerType) - str_size, str_size);
100
245k
        }
101
60
        col_res_data.resize(col_res_offset[col_res_offset.size() - 1]);
102
103
60
        block.get_by_position(result).column = std::move(col_res);
104
105
60
        return Status::OK();
106
60
    }
_ZNK5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE4EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
Line
Count
Source
68
4
                        uint32_t result, size_t input_rows_count) const override {
69
4
        const auto* col_source =
70
4
                assert_cast<const typename PrimitiveTypeTraits<IntegerPType>::ColumnType*>(
71
4
                        block.get_by_position(arguments[0]).column.get());
72
73
4
        auto col_res = ColumnString::create();
74
75
4
        ColumnString::Chars& col_res_data = col_res->get_chars();
76
4
        ColumnString::Offsets& col_res_offset = col_res->get_offsets();
77
4
        col_res_data.resize(input_rows_count * sizeof(IntegerType));
78
4
        col_res_offset.resize(input_rows_count);
79
80
16.3k
        for (Int32 i = 0; i < input_rows_count; ++i) {
81
16.3k
            IntegerType value = col_source->get_element(i);
82
16.3k
            const auto* const __restrict ui8_ptr = reinterpret_cast<const UInt8*>(&value);
83
16.3k
            UInt32 str_size = static_cast<UInt32>(*ui8_ptr) & 0x7F;
84
85
16.3k
            if (str_size >= sizeof(IntegerType)) {
86
0
                const auto& type_ptr = block.get_by_position(arguments[0]).type;
87
0
                throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
88
0
                                       "Invalid input of function {}, input type {} value {}, "
89
0
                                       "string size {}, should not be larger than {}",
90
0
                                       name, type_ptr->get_name(), value, str_size,
91
0
                                       sizeof(IntegerType));
92
0
            }
93
94
            // col_res_offset[-1] is valid for PaddedPODArray, will get 0
95
16.3k
            col_res_offset[i] = col_res_offset[i - 1] + str_size;
96
16.3k
            value <<= 1;
97
98
16.3k
            simd::reverse_copy_bytes(col_res_data.data() + col_res_offset[i - 1], str_size,
99
16.3k
                                     ui8_ptr + sizeof(IntegerType) - str_size, str_size);
100
16.3k
        }
101
4
        col_res_data.resize(col_res_offset[col_res_offset.size() - 1]);
102
103
4
        block.get_by_position(result).column = std::move(col_res);
104
105
4
        return Status::OK();
106
4
    }
_ZNK5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE5EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
Line
Count
Source
68
8
                        uint32_t result, size_t input_rows_count) const override {
69
8
        const auto* col_source =
70
8
                assert_cast<const typename PrimitiveTypeTraits<IntegerPType>::ColumnType*>(
71
8
                        block.get_by_position(arguments[0]).column.get());
72
73
8
        auto col_res = ColumnString::create();
74
75
8
        ColumnString::Chars& col_res_data = col_res->get_chars();
76
8
        ColumnString::Offsets& col_res_offset = col_res->get_offsets();
77
8
        col_res_data.resize(input_rows_count * sizeof(IntegerType));
78
8
        col_res_offset.resize(input_rows_count);
79
80
32.7k
        for (Int32 i = 0; i < input_rows_count; ++i) {
81
32.7k
            IntegerType value = col_source->get_element(i);
82
32.7k
            const auto* const __restrict ui8_ptr = reinterpret_cast<const UInt8*>(&value);
83
32.7k
            UInt32 str_size = static_cast<UInt32>(*ui8_ptr) & 0x7F;
84
85
32.7k
            if (str_size >= sizeof(IntegerType)) {
86
0
                const auto& type_ptr = block.get_by_position(arguments[0]).type;
87
0
                throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
88
0
                                       "Invalid input of function {}, input type {} value {}, "
89
0
                                       "string size {}, should not be larger than {}",
90
0
                                       name, type_ptr->get_name(), value, str_size,
91
0
                                       sizeof(IntegerType));
92
0
            }
93
94
            // col_res_offset[-1] is valid for PaddedPODArray, will get 0
95
32.7k
            col_res_offset[i] = col_res_offset[i - 1] + str_size;
96
32.7k
            value <<= 1;
97
98
32.7k
            simd::reverse_copy_bytes(col_res_data.data() + col_res_offset[i - 1], str_size,
99
32.7k
                                     ui8_ptr + sizeof(IntegerType) - str_size, str_size);
100
32.7k
        }
101
8
        col_res_data.resize(col_res_offset[col_res_offset.size() - 1]);
102
103
8
        block.get_by_position(result).column = std::move(col_res);
104
105
8
        return Status::OK();
106
8
    }
_ZNK5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE6EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
Line
Count
Source
68
16
                        uint32_t result, size_t input_rows_count) const override {
69
16
        const auto* col_source =
70
16
                assert_cast<const typename PrimitiveTypeTraits<IntegerPType>::ColumnType*>(
71
16
                        block.get_by_position(arguments[0]).column.get());
72
73
16
        auto col_res = ColumnString::create();
74
75
16
        ColumnString::Chars& col_res_data = col_res->get_chars();
76
16
        ColumnString::Offsets& col_res_offset = col_res->get_offsets();
77
16
        col_res_data.resize(input_rows_count * sizeof(IntegerType));
78
16
        col_res_offset.resize(input_rows_count);
79
80
65.5k
        for (Int32 i = 0; i < input_rows_count; ++i) {
81
65.5k
            IntegerType value = col_source->get_element(i);
82
65.5k
            const auto* const __restrict ui8_ptr = reinterpret_cast<const UInt8*>(&value);
83
65.5k
            UInt32 str_size = static_cast<UInt32>(*ui8_ptr) & 0x7F;
84
85
65.5k
            if (str_size >= sizeof(IntegerType)) {
86
0
                const auto& type_ptr = block.get_by_position(arguments[0]).type;
87
0
                throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
88
0
                                       "Invalid input of function {}, input type {} value {}, "
89
0
                                       "string size {}, should not be larger than {}",
90
0
                                       name, type_ptr->get_name(), value, str_size,
91
0
                                       sizeof(IntegerType));
92
0
            }
93
94
            // col_res_offset[-1] is valid for PaddedPODArray, will get 0
95
65.5k
            col_res_offset[i] = col_res_offset[i - 1] + str_size;
96
65.5k
            value <<= 1;
97
98
65.5k
            simd::reverse_copy_bytes(col_res_data.data() + col_res_offset[i - 1], str_size,
99
65.5k
                                     ui8_ptr + sizeof(IntegerType) - str_size, str_size);
100
65.5k
        }
101
16
        col_res_data.resize(col_res_offset[col_res_offset.size() - 1]);
102
103
16
        block.get_by_position(result).column = std::move(col_res);
104
105
16
        return Status::OK();
106
16
    }
_ZNK5doris23FunctionDecodeAsVarcharILNS_13PrimitiveTypeE7EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
Line
Count
Source
68
32
                        uint32_t result, size_t input_rows_count) const override {
69
32
        const auto* col_source =
70
32
                assert_cast<const typename PrimitiveTypeTraits<IntegerPType>::ColumnType*>(
71
32
                        block.get_by_position(arguments[0]).column.get());
72
73
32
        auto col_res = ColumnString::create();
74
75
32
        ColumnString::Chars& col_res_data = col_res->get_chars();
76
32
        ColumnString::Offsets& col_res_offset = col_res->get_offsets();
77
32
        col_res_data.resize(input_rows_count * sizeof(IntegerType));
78
32
        col_res_offset.resize(input_rows_count);
79
80
131k
        for (Int32 i = 0; i < input_rows_count; ++i) {
81
131k
            IntegerType value = col_source->get_element(i);
82
131k
            const auto* const __restrict ui8_ptr = reinterpret_cast<const UInt8*>(&value);
83
131k
            UInt32 str_size = static_cast<UInt32>(*ui8_ptr) & 0x7F;
84
85
131k
            if (str_size >= sizeof(IntegerType)) {
86
0
                const auto& type_ptr = block.get_by_position(arguments[0]).type;
87
0
                throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
88
0
                                       "Invalid input of function {}, input type {} value {}, "
89
0
                                       "string size {}, should not be larger than {}",
90
0
                                       name, type_ptr->get_name(), value, str_size,
91
0
                                       sizeof(IntegerType));
92
0
            }
93
94
            // col_res_offset[-1] is valid for PaddedPODArray, will get 0
95
131k
            col_res_offset[i] = col_res_offset[i - 1] + str_size;
96
131k
            value <<= 1;
97
98
131k
            simd::reverse_copy_bytes(col_res_data.data() + col_res_offset[i - 1], str_size,
99
131k
                                     ui8_ptr + sizeof(IntegerType) - str_size, str_size);
100
131k
        }
101
32
        col_res_data.resize(col_res_offset[col_res_offset.size() - 1]);
102
103
32
        block.get_by_position(result).column = std::move(col_res);
104
105
32
        return Status::OK();
106
32
    }
107
};
108
109
1
void register_function_decode_as_varchar(SimpleFunctionFactory& factory) {
110
1
    factory.register_function<FunctionDecodeAsVarchar<TYPE_SMALLINT>>();
111
1
    factory.register_function<FunctionDecodeAsVarchar<TYPE_INT>>();
112
1
    factory.register_function<FunctionDecodeAsVarchar<TYPE_BIGINT>>();
113
1
    factory.register_function<FunctionDecodeAsVarchar<TYPE_LARGEINT>>();
114
1
}
115
116
} // namespace doris