Coverage Report

Created: 2026-04-13 11:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/uniform.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 <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 "core/assert_cast.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" // IWYU pragma: keep
35
#include "core/data_type/primitive_type.h"
36
#include "core/types.h"
37
#include "exprs/aggregate/aggregate_function.h"
38
#include "exprs/function/function.h"
39
#include "exprs/function/simple_function_factory.h"
40
#include "exprs/function_context.h"
41
42
namespace doris {
43
44
// Integer uniform implementation
45
struct UniformIntImpl {
46
12
    static DataTypes get_variadic_argument_types() {
47
12
        return {std::make_shared<DataTypeInt64>(), std::make_shared<DataTypeInt64>(),
48
12
                std::make_shared<DataTypeInt64>()};
49
12
    }
50
51
4
    static DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) {
52
4
        return std::make_shared<DataTypeInt64>();
53
4
    }
54
55
    static Status execute_impl(FunctionContext* context, Block& block,
56
                               const ColumnNumbers& arguments, uint32_t result,
57
7
                               size_t input_rows_count) {
58
7
        auto res_column = ColumnInt64::create(input_rows_count);
59
7
        auto& res_data = static_cast<ColumnInt64&>(*res_column).get_data();
60
61
        // Get min and max values (constants)
62
7
        const auto& left =
63
7
                assert_cast<const ColumnConst&>(*block.get_by_position(arguments[0]).column)
64
7
                        .get_data_column();
65
7
        const auto& right =
66
7
                assert_cast<const ColumnConst&>(*block.get_by_position(arguments[1]).column)
67
7
                        .get_data_column();
68
7
        Int64 min = assert_cast<const ColumnInt64&>(left).get_element(0);
69
7
        Int64 max = assert_cast<const ColumnInt64&>(right).get_element(0);
70
71
7
        if (min >= max) {
72
1
            return Status::InvalidArgument(
73
1
                    "uniform's min should be less than max, but got [{}, {})", min, max);
74
1
        }
75
76
        // Get gen column (seed values)
77
6
        const auto& gen_column = block.get_by_position(arguments[2]).column;
78
79
39
        for (int i = 0; i < input_rows_count; i++) {
80
            // Use gen value as seed for each row
81
33
            auto seed = (*gen_column)[i].get<TYPE_BIGINT>();
82
33
            std::mt19937_64 generator(seed);
83
33
            std::uniform_int_distribution<int64_t> distribution(min, max);
84
33
            res_data[i] = distribution(generator);
85
33
        }
86
87
6
        block.replace_by_position(result, std::move(res_column));
88
6
        return Status::OK();
89
7
    }
90
};
91
92
// Double uniform implementation
93
struct UniformDoubleImpl {
94
8
    static DataTypes get_variadic_argument_types() {
95
8
        return {std::make_shared<DataTypeFloat64>(), std::make_shared<DataTypeFloat64>(),
96
8
                std::make_shared<DataTypeInt64>()};
97
8
    }
98
99
0
    static DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) {
100
0
        return std::make_shared<DataTypeFloat64>();
101
0
    }
102
103
    static Status execute_impl(FunctionContext* context, Block& block,
104
                               const ColumnNumbers& arguments, uint32_t result,
105
0
                               size_t input_rows_count) {
106
0
        auto res_column = ColumnFloat64::create(input_rows_count);
107
0
        auto& res_data = static_cast<ColumnFloat64&>(*res_column).get_data();
108
109
        // Get min and max values (constants)
110
0
        const auto& left =
111
0
                assert_cast<const ColumnConst&>(*block.get_by_position(arguments[0]).column)
112
0
                        .get_data_column();
113
0
        const auto& right =
114
0
                assert_cast<const ColumnConst&>(*block.get_by_position(arguments[1]).column)
115
0
                        .get_data_column();
116
0
        double min = assert_cast<const ColumnFloat64&>(left).get_element(0);
117
0
        double max = assert_cast<const ColumnFloat64&>(right).get_element(0);
118
119
0
        if (min >= max) {
120
0
            return Status::InvalidArgument(
121
0
                    "uniform's min should be less than max, but got [{}, {})", min, max);
122
0
        }
123
124
        // Get gen column (seed values)
125
0
        const auto& gen_column = block.get_by_position(arguments[2]).column;
126
127
0
        for (int i = 0; i < input_rows_count; i++) {
128
            // Use gen value as seed for each row
129
0
            auto seed = (*gen_column)[i].get<TYPE_BIGINT>();
130
0
            std::mt19937_64 generator(seed);
131
0
            std::uniform_real_distribution<double> distribution(min, max);
132
0
            res_data[i] = distribution(generator);
133
0
        }
134
135
0
        block.replace_by_position(result, std::move(res_column));
136
0
        return Status::OK();
137
0
    }
138
};
139
140
template <typename Impl>
141
class FunctionUniform : public IFunction {
142
public:
143
    static constexpr auto name = "uniform";
144
145
22
    static FunctionPtr create() { return std::make_shared<FunctionUniform<Impl>>(); }
_ZN5doris15FunctionUniformINS_14UniformIntImplEE6createEv
Line
Count
Source
145
13
    static FunctionPtr create() { return std::make_shared<FunctionUniform<Impl>>(); }
_ZN5doris15FunctionUniformINS_17UniformDoubleImplEE6createEv
Line
Count
Source
145
9
    static FunctionPtr create() { return std::make_shared<FunctionUniform<Impl>>(); }
146
2
    String get_name() const override { return name; }
_ZNK5doris15FunctionUniformINS_14UniformIntImplEE8get_nameB5cxx11Ev
Line
Count
Source
146
1
    String get_name() const override { return name; }
_ZNK5doris15FunctionUniformINS_17UniformDoubleImplEE8get_nameB5cxx11Ev
Line
Count
Source
146
1
    String get_name() const override { return name; }
147
148
4
    size_t get_number_of_arguments() const override {
149
4
        return get_variadic_argument_types_impl().size();
150
4
    }
_ZNK5doris15FunctionUniformINS_14UniformIntImplEE23get_number_of_argumentsEv
Line
Count
Source
148
4
    size_t get_number_of_arguments() const override {
149
4
        return get_variadic_argument_types_impl().size();
150
4
    }
Unexecuted instantiation: _ZNK5doris15FunctionUniformINS_17UniformDoubleImplEE23get_number_of_argumentsEv
151
152
4
    DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const override {
153
4
        return Impl::get_return_type_impl(arguments);
154
4
    }
_ZNK5doris15FunctionUniformINS_14UniformIntImplEE20get_return_type_implERKSt6vectorINS_21ColumnWithTypeAndNameESaIS4_EE
Line
Count
Source
152
4
    DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const override {
153
4
        return Impl::get_return_type_impl(arguments);
154
4
    }
Unexecuted instantiation: _ZNK5doris15FunctionUniformINS_17UniformDoubleImplEE20get_return_type_implERKSt6vectorINS_21ColumnWithTypeAndNameESaIS4_EE
155
156
20
    DataTypes get_variadic_argument_types_impl() const override {
157
20
        return Impl::get_variadic_argument_types();
158
20
    }
_ZNK5doris15FunctionUniformINS_14UniformIntImplEE32get_variadic_argument_types_implEv
Line
Count
Source
156
12
    DataTypes get_variadic_argument_types_impl() const override {
157
12
        return Impl::get_variadic_argument_types();
158
12
    }
_ZNK5doris15FunctionUniformINS_17UniformDoubleImplEE32get_variadic_argument_types_implEv
Line
Count
Source
156
8
    DataTypes get_variadic_argument_types_impl() const override {
157
8
        return Impl::get_variadic_argument_types();
158
8
    }
159
160
16
    Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override {
161
        // init_function_context do set_constant_cols for FRAGMENT_LOCAL scope
162
16
        if (scope == FunctionContext::FRAGMENT_LOCAL) {
163
4
            if (!context->is_col_constant(0)) {
164
0
                return Status::InvalidArgument(
165
0
                        "The first parameter (min) of uniform function must be literal");
166
0
            }
167
4
            if (!context->is_col_constant(1)) {
168
0
                return Status::InvalidArgument(
169
0
                        "The second parameter (max) of uniform function must be literal");
170
0
            }
171
4
        }
172
16
        return Status::OK();
173
16
    }
_ZN5doris15FunctionUniformINS_14UniformIntImplEE4openEPNS_15FunctionContextENS3_18FunctionStateScopeE
Line
Count
Source
160
16
    Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override {
161
        // init_function_context do set_constant_cols for FRAGMENT_LOCAL scope
162
16
        if (scope == FunctionContext::FRAGMENT_LOCAL) {
163
4
            if (!context->is_col_constant(0)) {
164
0
                return Status::InvalidArgument(
165
0
                        "The first parameter (min) of uniform function must be literal");
166
0
            }
167
4
            if (!context->is_col_constant(1)) {
168
0
                return Status::InvalidArgument(
169
0
                        "The second parameter (max) of uniform function must be literal");
170
0
            }
171
4
        }
172
16
        return Status::OK();
173
16
    }
Unexecuted instantiation: _ZN5doris15FunctionUniformINS_17UniformDoubleImplEE4openEPNS_15FunctionContextENS3_18FunctionStateScopeE
174
175
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
176
7
                        uint32_t result, size_t input_rows_count) const override {
177
7
        return Impl::execute_impl(context, block, arguments, result, input_rows_count);
178
7
    }
_ZNK5doris15FunctionUniformINS_14UniformIntImplEE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
Line
Count
Source
176
7
                        uint32_t result, size_t input_rows_count) const override {
177
7
        return Impl::execute_impl(context, block, arguments, result, input_rows_count);
178
7
    }
Unexecuted instantiation: _ZNK5doris15FunctionUniformINS_17UniformDoubleImplEE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
179
};
180
181
8
void register_function_uniform(SimpleFunctionFactory& factory) {
182
8
    factory.register_function<FunctionUniform<UniformIntImpl>>();
183
8
    factory.register_function<FunctionUniform<UniformDoubleImpl>>();
184
8
}
185
} // namespace doris