Coverage Report

Created: 2026-11-07 04:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/exprs/function/random.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/format.h>
19
#include <glog/logging.h>
20
21
#include <algorithm>
22
#include <boost/iterator/iterator_facade.hpp>
23
#include <cstdint>
24
#include <cstdlib>
25
#include <memory>
26
#include <random>
27
#include <utility>
28
29
#include "common/status.h"
30
#include "core/block/block.h"
31
#include "core/block/column_numbers.h"
32
#include "core/column/column.h"
33
#include "core/column/column_vector.h"
34
#include "core/data_type/data_type_number.h"
35
#include "core/types.h"
36
#include "exprs/aggregate/aggregate_function.h"
37
#include "exprs/function/function.h"
38
#include "exprs/function/simple_function_factory.h"
39
#include "exprs/function_context.h"
40
41
namespace doris {
42
43
#include "common/compile_check_begin.h"
44
class Random : public IFunction {
45
public:
46
    static constexpr auto name = "random";
47
48
6
    static FunctionPtr create() { return std::make_shared<Random>(); }
49
50
0
    String get_name() const override { return name; }
51
52
4
    bool use_default_implementation_for_constants() const override { return false; }
53
54
0
    size_t get_number_of_arguments() const override { return 0; }
55
56
5
    bool is_variadic() const override { return true; }
57
58
4
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
59
4
        if (arguments.size() == 2) {
60
2
            return std::make_shared<DataTypeInt64>();
61
2
        }
62
2
        return std::make_shared<DataTypeFloat64>();
63
4
    }
64
65
6
    Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override {
66
6
        std::shared_ptr<State> state = std::make_shared<State>();
67
6
        context->set_function_state(scope, state);
68
6
        if (scope == FunctionContext::THREAD_LOCAL) {
69
3
            if (context->get_num_args() == 1) {
70
                // This is a call to RandSeed, initialize the seed. The seed is a constant, but a
71
                // constant expression such as an arithmetic one is not evaluated in open. Then the
72
                // generator is seeded with the first row in execute.
73
1
                if (context->is_col_constant(0)) {
74
0
                    state->generator.seed(get_seed(*context->get_constant_col(0)->column_ptr));
75
1
                } else {
76
1
                    state->seed_in_execute = true;
77
1
                }
78
2
            } else { // zero args, or the range bounds
79
2
                state->generator.seed(std::random_device()());
80
2
            }
81
3
        }
82
83
6
        return Status::OK();
84
6
    }
85
86
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
87
4
                        uint32_t result, size_t input_rows_count) const override {
88
4
        if (arguments.size() == 2) {
89
2
            return _execute_int_range(context, block, arguments, result, input_rows_count);
90
2
        }
91
2
        return _execute_float(context, block, arguments, result, input_rows_count);
92
4
    }
93
94
private:
95
    struct State {
96
        std::mt19937_64 generator;
97
        bool seed_in_execute = false;
98
    };
99
100
1
    static uint32_t get_seed(const IColumn& seed_column) {
101
1
        return seed_column.is_null_at(0) ? 0 : (uint32_t)seed_column[0].get<TYPE_BIGINT>();
102
1
    }
103
104
    static Status _execute_int_range(FunctionContext* context, Block& block,
105
                                     const ColumnNumbers& arguments, uint32_t result,
106
2
                                     size_t input_rows_count) {
107
2
        auto res_column = ColumnInt64::create(input_rows_count);
108
2
        auto& res_data = res_column->get_data();
109
110
2
        auto* state = reinterpret_cast<State*>(
111
2
                context->get_function_state(FunctionContext::THREAD_LOCAL));
112
2
        DCHECK(state != nullptr);
113
2
        if (input_rows_count == 0) {
114
0
            block.replace_by_position(result, std::move(res_column));
115
0
            return Status::OK();
116
0
        }
117
118
        // The bounds are constants checked in FE, so the first row holds their values. A constant
119
        // expression such as an arithmetic one is not a ColumnConst.
120
2
        Int64 min = block.get_by_position(arguments[0]).column->get_int(0);
121
2
        Int64 max = block.get_by_position(arguments[1]).column->get_int(0);
122
2
        if (min >= max) {
123
1
            return Status::InvalidArgument(fmt::format(
124
1
                    "random's lower bound should less than upper bound, but got [{}, {})", min,
125
1
                    max));
126
1
        }
127
128
1
        std::uniform_int_distribution<int64_t> distribution(min, max);
129
4
        for (int i = 0; i < input_rows_count; i++) {
130
3
            res_data[i] = distribution(state->generator);
131
3
        }
132
133
1
        block.replace_by_position(result, std::move(res_column));
134
1
        return Status::OK();
135
2
    }
136
137
    static Status _execute_float(FunctionContext* context, Block& block,
138
                                 const ColumnNumbers& arguments, uint32_t result,
139
2
                                 size_t input_rows_count) {
140
2
        static const double min = 0.0;
141
2
        static const double max = 1.0;
142
2
        auto res_column = ColumnFloat64::create(input_rows_count);
143
2
        auto& res_data = res_column->get_data();
144
145
2
        auto* state = reinterpret_cast<State*>(
146
2
                context->get_function_state(FunctionContext::THREAD_LOCAL));
147
2
        DCHECK(state != nullptr);
148
2
        if (state->seed_in_execute && input_rows_count > 0) {
149
1
            state->generator.seed(get_seed(*block.get_by_position(arguments[0]).column));
150
1
            state->seed_in_execute = false;
151
1
        }
152
153
2
        std::uniform_real_distribution<double> distribution(min, max);
154
5
        for (int i = 0; i < input_rows_count; i++) {
155
3
            res_data[i] = distribution(state->generator);
156
3
        }
157
158
2
        block.replace_by_position(result, std::move(res_column));
159
2
        return Status::OK();
160
2
    }
161
};
162
163
1
void register_function_random(SimpleFunctionFactory& factory) {
164
1
    factory.register_function<Random>();
165
1
    factory.register_alias(Random::name, "rand");
166
1
}
167
#include "common/compile_check_end.h"
168
} // namespace doris