Coverage Report

Created: 2026-03-16 19:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/function_dict_get_many.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 <memory>
19
#include <vector>
20
21
#include "common/logging.h"
22
#include "common/status.h"
23
#include "core/column/column.h"
24
#include "core/column/column_array.h"
25
#include "core/column/column_struct.h"
26
#include "core/data_type/data_type_decimal.h"
27
#include "core/data_type/data_type_number.h" // IWYU pragma: keep
28
#include "core/data_type/data_type_struct.h"
29
#include "core/field.h"
30
#include "core/types.h"
31
#include "exprs/function/dictionary.h"
32
#include "exprs/function/dictionary_factory.h"
33
#include "exprs/function/function.h"
34
#include "exprs/function/simple_function_factory.h"
35
36
namespace doris {
37
38
struct DictGetState {
39
    std::shared_ptr<const IDictionary> dict;
40
    ///TODO:
41
    // 1. we do not need to check dict every time(shoud only check in open)
42
    // 2. for some dict, will init some struct each time, we should cache it
43
};
44
45
class FunctionDictGetMany : public IFunction {
46
public:
47
    static constexpr auto name = "dict_get_many";
48
4
    static FunctionPtr create() { return std::make_shared<FunctionDictGetMany>(); }
49
1
    String get_name() const override { return name; }
50
51
1
    bool is_variadic() const override { return false; }
52
0
    size_t get_number_of_arguments() const override { return 3; }
53
54
1
    DataTypes get_variadic_argument_types_impl() const override { return {}; }
55
56
0
    DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const override {
57
0
        return std::make_shared<DataTypeDecimal128>();
58
0
    }
59
60
2
    bool skip_return_type_check() const override { return true; }
61
62
0
    bool use_default_implementation_for_nulls() const override { return false; }
63
64
2
    Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override {
65
2
        if (scope == FunctionContext::THREAD_LOCAL) {
66
0
            return Status::OK();
67
0
        }
68
2
        std::shared_ptr<DictGetState> state = std::make_shared<DictGetState>();
69
2
        context->set_function_state(scope, state);
70
2
        DCHECK(context->get_num_args() == 3);
71
2
        auto dict_fn = context->dict_function();
72
2
        if (!dict_fn) {
73
1
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT, "not set dict_function");
74
1
        }
75
1
        auto dict = ExecEnv::GetInstance()->dict_factory()->get(dict_fn->dictionary_id,
76
1
                                                                dict_fn->version_id);
77
1
        if (!dict) {
78
1
            std::string dict_name =
79
1
                    context->get_constant_col(0)->column_ptr->get_data_at(0).to_string();
80
1
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
81
1
                                   "can not find dict name : {} , dict_id : {} , version_id : {}  ",
82
1
                                   dict_name, dict_fn->dictionary_id, dict_fn->version_id);
83
1
        }
84
0
        state->dict = dict;
85
0
        return Status::OK();
86
1
    }
87
88
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
89
0
                        uint32_t result, size_t input_rows_count) const override {
90
0
        auto* dict_state = reinterpret_cast<DictGetState*>(
91
0
                context->get_function_state(FunctionContext::FRAGMENT_LOCAL));
92
0
        if (!dict_state) {
93
0
            return Status::RuntimeError("funciton context for function '{}' must have dict_state;",
94
0
                                        get_name());
95
0
        }
96
97
        // dict get many(name, array<value names>, struct<key columns>) -> struct <value columns>
98
99
        // get value names
100
0
        const Array array_names =
101
0
                (*block.get_by_position(arguments[1]).column)[0].get<TYPE_ARRAY>();
102
0
        std::vector<std::string> attribute_names;
103
0
        for (auto field : array_names) {
104
0
            attribute_names.push_back(field.template get<TYPE_STRING>());
105
0
        }
106
0
        const auto dict = dict_state->dict;
107
108
0
        DataTypes attribute_types;
109
0
        for (auto attribute_name : attribute_names) {
110
0
            attribute_types.push_back(dict->get_attribute_type(attribute_name));
111
0
        }
112
113
        // get key columns (struct<key columns>)
114
0
        const ColumnPtr key_struct_column =
115
0
                block.get_by_position(arguments[2]).column->convert_to_full_column_if_const();
116
        // columns_copy is just copy ptr , not column
117
0
        auto key_columns = assert_cast<const ColumnStruct&>(*key_struct_column).get_columns_copy();
118
119
0
        DataTypes key_types = remove_nullable(
120
0
                assert_cast<const DataTypeStruct&>(*block.get_by_position(arguments[2]).type)
121
0
                        .get_elements());
122
123
0
        auto result_columns =
124
0
                dict->get_tuple_columns(attribute_names, attribute_types, key_columns, key_types);
125
126
0
        block.replace_by_position(result, ColumnStruct::create(result_columns));
127
128
0
        return Status::OK();
129
0
    }
130
};
131
132
1
void register_function_dict_get_many(SimpleFunctionFactory& factory) {
133
1
    factory.register_function<FunctionDictGetMany>();
134
1
}
135
136
} // namespace doris