be/src/exprs/function/function_utility.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 | | #include <stddef.h> |
18 | | |
19 | | #include <boost/iterator/iterator_facade.hpp> |
20 | | // IWYU pragma: no_include <bits/chrono.h> |
21 | | #include <algorithm> |
22 | | #include <chrono> // IWYU pragma: keep |
23 | | #include <memory> |
24 | | #include <string> |
25 | | #include <thread> |
26 | | #include <utility> |
27 | | |
28 | | #include "common/status.h" |
29 | | #include "core/assert_cast.h" |
30 | | #include "core/block/block.h" |
31 | | #include "core/block/column_numbers.h" |
32 | | #include "core/block/column_with_type_and_name.h" |
33 | | #include "core/block/columns_with_type_and_name.h" |
34 | | #include "core/column/column.h" |
35 | | #include "core/column/column_const.h" |
36 | | #include "core/column/column_nullable.h" |
37 | | #include "core/column/column_string.h" |
38 | | #include "core/column/column_vector.h" |
39 | | #include "core/data_type/data_type.h" |
40 | | #include "core/data_type/data_type_nullable.h" |
41 | | #include "core/data_type/data_type_number.h" |
42 | | #include "core/data_type/data_type_string.h" |
43 | | #include "core/types.h" |
44 | | #include "exprs/aggregate/aggregate_function.h" |
45 | | #include "exprs/function/function.h" |
46 | | #include "exprs/function/simple_function_factory.h" |
47 | | |
48 | | namespace doris { |
49 | | class FunctionContext; |
50 | | } // namespace doris |
51 | | |
52 | | namespace doris { |
53 | | |
54 | | class FunctionSleep : public IFunction { |
55 | | public: |
56 | | static constexpr auto name = "sleep"; |
57 | 29 | static FunctionPtr create() { return std::make_shared<FunctionSleep>(); } |
58 | | |
59 | 1 | String get_name() const override { return name; } |
60 | | |
61 | 20 | size_t get_number_of_arguments() const override { return 1; } |
62 | | |
63 | 20 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
64 | 20 | if (arguments[0]->is_nullable()) { |
65 | 0 | return make_nullable(std::make_shared<DataTypeUInt8>()); |
66 | 0 | } |
67 | 20 | return std::make_shared<DataTypeUInt8>(); |
68 | 20 | } |
69 | | |
70 | 46 | bool use_default_implementation_for_nulls() const override { return false; } |
71 | | |
72 | | // Sleep function should not be executed during open stage, this will makes fragment prepare |
73 | | // waiting too long, so we do not use default impl. |
74 | 72 | bool use_default_implementation_for_constants() const override { return false; } |
75 | | |
76 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
77 | 26 | uint32_t result, size_t input_rows_count) const override { |
78 | 26 | const auto& argument_column = |
79 | 26 | block.get_by_position(arguments[0]).column->convert_to_full_column_if_const(); |
80 | | |
81 | 26 | auto res_column = ColumnUInt8::create(input_rows_count, 0); |
82 | | |
83 | 26 | if (auto* nullable_column = check_and_get_column<ColumnNullable>(*argument_column)) { |
84 | 0 | auto null_map_column = ColumnUInt8::create(); |
85 | |
|
86 | 0 | auto nested_column = nullable_column->get_nested_column_ptr(); |
87 | 0 | auto data_column = assert_cast<const ColumnInt32*>(nested_column.get()); |
88 | |
|
89 | 0 | for (int i = 0; i < input_rows_count; i++) { |
90 | 0 | if (nullable_column->is_null_at(i)) { |
91 | 0 | null_map_column->insert(Field::create_field<TYPE_BOOLEAN>(1)); |
92 | 0 | } else { |
93 | 0 | int seconds = data_column->get_data()[i]; |
94 | 0 | std::this_thread::sleep_for(std::chrono::seconds(seconds)); |
95 | 0 | null_map_column->insert(Field::create_field<TYPE_BOOLEAN>(0)); |
96 | 0 | } |
97 | 0 | } |
98 | |
|
99 | 0 | block.replace_by_position(result, ColumnNullable::create(std::move(res_column), |
100 | 0 | std::move(null_map_column))); |
101 | 26 | } else { |
102 | 26 | auto data_column = assert_cast<const ColumnInt32*>(argument_column.get()); |
103 | | |
104 | 69 | for (int i = 0; i < input_rows_count; i++) { |
105 | 43 | int seconds = data_column->get_element(i); |
106 | 43 | std::this_thread::sleep_for(std::chrono::seconds(seconds)); |
107 | 43 | } |
108 | | |
109 | 26 | block.replace_by_position(result, std::move(res_column)); |
110 | 26 | } |
111 | 26 | return Status::OK(); |
112 | 26 | } |
113 | | }; |
114 | | |
115 | | class FunctionVersion : public IFunction { |
116 | | public: |
117 | | static constexpr auto name = "version"; |
118 | | |
119 | | static const std::string version; |
120 | | |
121 | 10 | static FunctionPtr create() { return std::make_shared<FunctionVersion>(); } |
122 | | |
123 | 1 | String get_name() const override { return name; } |
124 | | |
125 | 1 | size_t get_number_of_arguments() const override { return 0; } |
126 | | |
127 | 1 | DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const override { |
128 | 1 | return std::make_shared<DataTypeString>(); |
129 | 1 | } |
130 | | |
131 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
132 | 1 | uint32_t result, size_t input_rows_count) const override { |
133 | 1 | auto res_column = ColumnString::create(); |
134 | 1 | res_column->insert_data(version.c_str(), version.length()); |
135 | 1 | auto col_const = ColumnConst::create(std::move(res_column), input_rows_count); |
136 | 1 | block.replace_by_position(result, std::move(col_const)); |
137 | 1 | return Status::OK(); |
138 | 1 | } |
139 | | }; |
140 | | |
141 | | const std::string FunctionVersion::version = "5.7.99"; |
142 | | |
143 | 8 | void register_function_utility(SimpleFunctionFactory& factory) { |
144 | 8 | factory.register_function<FunctionSleep>(); |
145 | 8 | factory.register_function<FunctionVersion>(); |
146 | 8 | } |
147 | | |
148 | | } // namespace doris |