/root/doris/be/src/vec/functions/random.cpp
Line | Count | Source (jump to first uncovered line) |
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 <glog/logging.h> |
19 | | |
20 | | #include <algorithm> |
21 | | #include <boost/iterator/iterator_facade.hpp> |
22 | | #include <cstdint> |
23 | | #include <cstdlib> |
24 | | #include <memory> |
25 | | #include <random> |
26 | | #include <utility> |
27 | | |
28 | | #include "common/status.h" |
29 | | #include "udf/udf.h" |
30 | | #include "vec/aggregate_functions/aggregate_function.h" |
31 | | #include "vec/columns/column.h" |
32 | | #include "vec/columns/column_vector.h" |
33 | | #include "vec/columns/columns_number.h" |
34 | | #include "vec/common/assert_cast.h" |
35 | | #include "vec/core/block.h" |
36 | | #include "vec/core/column_numbers.h" |
37 | | #include "vec/core/types.h" |
38 | | #include "vec/data_types/data_type_number.h" |
39 | | #include "vec/functions/function.h" |
40 | | #include "vec/functions/simple_function_factory.h" |
41 | | |
42 | | namespace doris::vectorized { |
43 | | class Random : public IFunction { |
44 | | public: |
45 | | static constexpr auto name = "random"; |
46 | | |
47 | 7 | static FunctionPtr create() { return std::make_shared<Random>(); } |
48 | | |
49 | 0 | String get_name() const override { return name; } |
50 | | |
51 | 12 | bool use_default_implementation_for_constants() const override { return false; } |
52 | | |
53 | 0 | size_t get_number_of_arguments() const override { return 0; } |
54 | | |
55 | 6 | bool is_variadic() const override { return true; } |
56 | | |
57 | 6 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
58 | 6 | return std::make_shared<DataTypeFloat64>(); |
59 | 6 | } |
60 | | |
61 | 12 | Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override { |
62 | 12 | std::shared_ptr<std::mt19937_64> generator(new std::mt19937_64()); |
63 | 12 | context->set_function_state(scope, generator); |
64 | 12 | if (scope == FunctionContext::THREAD_LOCAL) { |
65 | 6 | if (context->get_num_args() == 1) { |
66 | | // This is a call to RandSeed, initialize the seed |
67 | | // TODO: should we support non-constant seed? |
68 | 6 | if (!context->is_col_constant(0)) { |
69 | 0 | return Status::InvalidArgument("Seed argument to rand() must be constant."); |
70 | 0 | } |
71 | 6 | uint32_t seed = 0; |
72 | 6 | if (!context->get_constant_col(0)->column_ptr->is_null_at(0)) { |
73 | 5 | seed = context->get_constant_col(0)->column_ptr->get64(0); |
74 | 5 | } |
75 | 6 | generator->seed(seed); |
76 | 6 | } else { |
77 | 0 | generator->seed(std::random_device()()); |
78 | 0 | } |
79 | 6 | } |
80 | | |
81 | 12 | return Status::OK(); |
82 | 12 | } |
83 | | |
84 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
85 | 6 | size_t result, size_t input_rows_count) override { |
86 | 6 | static const double min = 0.0; |
87 | 6 | static const double max = 1.0; |
88 | 6 | auto res_column = ColumnFloat64::create(input_rows_count); |
89 | 6 | auto& res_data = assert_cast<ColumnFloat64&>(*res_column).get_data(); |
90 | | |
91 | 6 | std::mt19937_64* generator = reinterpret_cast<std::mt19937_64*>( |
92 | 6 | context->get_function_state(FunctionContext::THREAD_LOCAL)); |
93 | 6 | DCHECK(generator != nullptr); |
94 | | |
95 | 6 | std::uniform_real_distribution<double> distribution(min, max); |
96 | 12 | for (int i = 0; i < input_rows_count; i++) { |
97 | 6 | res_data[i] = distribution(*generator); |
98 | 6 | } |
99 | | |
100 | 6 | block.replace_by_position(result, std::move(res_column)); |
101 | 6 | return Status::OK(); |
102 | 6 | } |
103 | | |
104 | 12 | Status close(FunctionContext* context, FunctionContext::FunctionStateScope scope) override { |
105 | 12 | return Status::OK(); |
106 | 12 | } |
107 | | }; |
108 | | |
109 | 1 | void register_function_random(SimpleFunctionFactory& factory) { |
110 | 1 | factory.register_function<Random>(); |
111 | 1 | factory.register_alias(Random::name, "rand"); |
112 | 1 | } |
113 | | |
114 | | } // namespace doris::vectorized |