be/src/exprs/function/function_dict_get.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 | | |
20 | | #include "common/logging.h" |
21 | | #include "common/status.h" |
22 | | #include "core/column/column.h" |
23 | | #include "core/data_type/data_type_decimal.h" |
24 | | #include "core/data_type/data_type_number.h" // IWYU pragma: keep |
25 | | #include "core/types.h" |
26 | | #include "exprs/function/dictionary.h" |
27 | | #include "exprs/function/dictionary_factory.h" |
28 | | #include "exprs/function/function.h" |
29 | | #include "exprs/function/simple_function_factory.h" |
30 | | |
31 | | namespace doris { |
32 | | |
33 | | struct DictGetState { |
34 | | std::shared_ptr<const IDictionary> dict; |
35 | | ///TODO: |
36 | | // 1. we do not need to check dict every time(shoud only check in open) |
37 | | // 2. for some dict, will init some struct each time, we should cache it |
38 | | }; |
39 | | |
40 | | class FunctionDictGet : public IFunction { |
41 | | public: |
42 | | static constexpr auto name = "dict_get"; |
43 | 80 | static FunctionPtr create() { return std::make_shared<FunctionDictGet>(); } |
44 | 1 | String get_name() const override { return name; } |
45 | | |
46 | 1 | bool is_variadic() const override { return false; } |
47 | 0 | size_t get_number_of_arguments() const override { return 3; } |
48 | | |
49 | 8 | DataTypes get_variadic_argument_types_impl() const override { return {}; } |
50 | | |
51 | 0 | DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const override { |
52 | 0 | return std::make_shared<DataTypeDecimal128>(); |
53 | 0 | } |
54 | | |
55 | 71 | bool skip_return_type_check() const override { return true; } |
56 | | |
57 | 73 | bool use_default_implementation_for_nulls() const override { return false; } |
58 | | |
59 | 233 | Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override { |
60 | 233 | if (scope == FunctionContext::THREAD_LOCAL) { |
61 | 162 | return Status::OK(); |
62 | 162 | } |
63 | 71 | std::shared_ptr<DictGetState> state = std::make_shared<DictGetState>(); |
64 | 71 | context->set_function_state(scope, state); |
65 | 71 | DCHECK(context->get_num_args() == 3); |
66 | 71 | auto dict_fn = context->dict_function(); |
67 | 71 | if (!dict_fn) { |
68 | 1 | throw doris::Exception(ErrorCode::INVALID_ARGUMENT, "not set dict_function"); |
69 | 1 | } |
70 | 70 | auto dict = ExecEnv::GetInstance()->dict_factory()->get(dict_fn->dictionary_id, |
71 | 70 | dict_fn->version_id); |
72 | 70 | if (!dict) { |
73 | 1 | std::string dict_name = |
74 | 1 | context->get_constant_col(0)->column_ptr->get_data_at(0).to_string(); |
75 | 1 | throw doris::Exception(ErrorCode::INVALID_ARGUMENT, |
76 | 1 | "can not find dict name : {} , dict_id : {} , version_id : {} ", |
77 | 1 | dict_name, dict_fn->dictionary_id, dict_fn->version_id); |
78 | 1 | } |
79 | 69 | state->dict = dict; |
80 | 69 | return Status::OK(); |
81 | 70 | } |
82 | | |
83 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
84 | 73 | uint32_t result, size_t input_rows_count) const override { |
85 | 73 | auto* dict_state = reinterpret_cast<DictGetState*>( |
86 | 73 | context->get_function_state(FunctionContext::FRAGMENT_LOCAL)); |
87 | 73 | if (!dict_state) { |
88 | 0 | return Status::RuntimeError("funciton context for function '{}' must have dict_state;", |
89 | 0 | get_name()); |
90 | 0 | } |
91 | | |
92 | 73 | const auto dict = dict_state->dict; |
93 | | |
94 | 73 | const std::string attribute_name = |
95 | 73 | block.get_by_position(arguments[1]).column->get_data_at(0).to_string(); |
96 | 73 | const DataTypePtr attribute_type = dict->get_attribute_type(attribute_name); |
97 | | |
98 | 73 | const ColumnPtr key_column = |
99 | 73 | block.get_by_position(arguments[2]).column->convert_to_full_column_if_const(); |
100 | 73 | const DataTypePtr key_type = block.get_by_position(arguments[2]).type; |
101 | | |
102 | | // key_type is not nullable, but key_column may be nullable |
103 | | // wiil check key_column in dict::getColumn |
104 | 73 | auto res = dict->get_column(attribute_name, attribute_type, key_column, |
105 | 73 | remove_nullable(key_type)); |
106 | | |
107 | 73 | block.replace_by_position(result, std::move(res)); |
108 | | |
109 | 73 | return Status::OK(); |
110 | 73 | } |
111 | | }; |
112 | | |
113 | 8 | void register_function_dict_get(SimpleFunctionFactory& factory) { |
114 | 8 | factory.register_function<FunctionDictGet>(); |
115 | 8 | } |
116 | | |
117 | | } // namespace doris |