Coverage Report

Created: 2024-11-20 15:53

/root/doris/be/src/runtime/runtime_predicate.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 "runtime/runtime_predicate.h"
19
20
#include <stdint.h>
21
22
#include <memory>
23
24
#include "common/compiler_util.h" // IWYU pragma: keep
25
#include "olap/accept_null_predicate.h"
26
#include "olap/column_predicate.h"
27
#include "olap/predicate_creator.h"
28
29
namespace doris::vectorized {
30
31
0
std::string get_time_value(const Field& field) {
32
0
    using ValueType = typename PrimitiveTypeTraits<TYPE_TIMEV2>::CppType;
33
0
    ValueType value = field.get<ValueType>();
34
0
    return cast_to_string<TYPE_TIMEV2, ValueType>(value, 0);
35
0
}
36
37
Status RuntimePredicate::init(PrimitiveType type, bool nulls_first, bool is_asc,
38
0
                              const std::string& col_name) {
39
0
    std::unique_lock<std::shared_mutex> wlock(_rwlock);
40
41
0
    if (_inited) {
42
0
        return Status::OK();
43
0
    }
44
45
0
    _nulls_first = nulls_first;
46
0
    _is_asc = is_asc;
47
    // For ASC  sort, create runtime predicate col_name <= max_top_value
48
    // since values that > min_top_value are large than any value in current topn values
49
    // For DESC sort, create runtime predicate col_name >= min_top_value
50
    // since values that < min_top_value are less than any value in current topn values
51
0
    _pred_constructor = is_asc ? create_comparison_predicate<PredicateType::LE>
52
0
                               : create_comparison_predicate<PredicateType::GE>;
53
0
    _col_name = col_name;
54
55
    // set get value function
56
0
    switch (type) {
57
0
    case PrimitiveType::TYPE_BOOLEAN: {
58
0
        _get_value_fn = get_normal_value<TYPE_BOOLEAN>;
59
0
        break;
60
0
    }
61
0
    case PrimitiveType::TYPE_TINYINT: {
62
0
        _get_value_fn = get_normal_value<TYPE_TINYINT>;
63
0
        break;
64
0
    }
65
0
    case PrimitiveType::TYPE_SMALLINT: {
66
0
        _get_value_fn = get_normal_value<TYPE_SMALLINT>;
67
0
        break;
68
0
    }
69
0
    case PrimitiveType::TYPE_INT: {
70
0
        _get_value_fn = get_normal_value<TYPE_INT>;
71
0
        break;
72
0
    }
73
0
    case PrimitiveType::TYPE_BIGINT: {
74
0
        _get_value_fn = get_normal_value<TYPE_BIGINT>;
75
0
        break;
76
0
    }
77
0
    case PrimitiveType::TYPE_LARGEINT: {
78
0
        _get_value_fn = get_normal_value<TYPE_LARGEINT>;
79
0
        break;
80
0
    }
81
0
    case PrimitiveType::TYPE_CHAR:
82
0
    case PrimitiveType::TYPE_VARCHAR:
83
0
    case PrimitiveType::TYPE_STRING: {
84
0
        _get_value_fn = [](const Field& field) { return field.get<String>(); };
85
0
        break;
86
0
    }
87
0
    case PrimitiveType::TYPE_DATEV2: {
88
0
        _get_value_fn = get_normal_value<TYPE_DATEV2>;
89
0
        break;
90
0
    }
91
0
    case PrimitiveType::TYPE_DATETIMEV2: {
92
0
        _get_value_fn = get_normal_value<TYPE_DATETIMEV2>;
93
0
        break;
94
0
    }
95
0
    case PrimitiveType::TYPE_DATE: {
96
0
        _get_value_fn = get_date_value;
97
0
        break;
98
0
    }
99
0
    case PrimitiveType::TYPE_DATETIME: {
100
0
        _get_value_fn = get_datetime_value;
101
0
        break;
102
0
    }
103
0
    case PrimitiveType::TYPE_TIMEV2: {
104
0
        _get_value_fn = get_time_value;
105
0
        break;
106
0
    }
107
0
    case PrimitiveType::TYPE_DECIMAL32: {
108
0
        _get_value_fn = get_decimal_value<TYPE_DECIMAL32>;
109
0
        break;
110
0
    }
111
0
    case PrimitiveType::TYPE_DECIMAL64: {
112
0
        _get_value_fn = get_decimal_value<TYPE_DECIMAL64>;
113
0
        break;
114
0
    }
115
0
    case PrimitiveType::TYPE_DECIMALV2: {
116
0
        _get_value_fn = get_decimalv2_value;
117
0
        break;
118
0
    }
119
0
    case PrimitiveType::TYPE_DECIMAL128I: {
120
0
        _get_value_fn = get_decimal_value<TYPE_DECIMAL128I>;
121
0
        break;
122
0
    }
123
0
    case PrimitiveType::TYPE_DECIMAL256: {
124
0
        _get_value_fn = get_decimal_value<TYPE_DECIMAL256>;
125
0
        break;
126
0
    }
127
0
    case PrimitiveType::TYPE_IPV4: {
128
0
        _get_value_fn = get_normal_value<TYPE_IPV4>;
129
0
        break;
130
0
    }
131
0
    case PrimitiveType::TYPE_IPV6: {
132
0
        _get_value_fn = get_normal_value<TYPE_IPV6>;
133
0
        break;
134
0
    }
135
0
    default:
136
0
        return Status::InvalidArgument("unsupported runtime predicate type {}", type);
137
0
    }
138
139
0
    _inited = true;
140
0
    return Status::OK();
141
0
}
142
143
0
Status RuntimePredicate::update(const Field& value) {
144
0
    std::unique_lock<std::shared_mutex> wlock(_rwlock);
145
    // skip null value
146
0
    if (value.is_null() || !_inited || !_tablet_schema) {
147
0
        return Status::OK();
148
0
    }
149
150
0
    bool updated = false;
151
152
0
    if (UNLIKELY(_orderby_extrem.is_null())) {
153
0
        _orderby_extrem = value;
154
0
        updated = true;
155
0
    } else {
156
0
        if ((_is_asc && value < _orderby_extrem) || (!_is_asc && value > _orderby_extrem)) {
157
0
            _orderby_extrem = value;
158
0
            updated = true;
159
0
        }
160
0
    }
161
162
0
    if (!updated) {
163
0
        return Status::OK();
164
0
    }
165
166
0
    std::unique_ptr<ColumnPredicate> pred {
167
0
            _pred_constructor(_tablet_schema->column(_col_name), _predicate->column_id(),
168
0
                              _get_value_fn(_orderby_extrem), false, &_predicate_arena)};
169
    // For NULLS FIRST, wrap a AcceptNullPredicate to return true for NULL
170
    // since ORDER BY ASC/DESC should get NULL first but pred returns NULL
171
    // and NULL in where predicate will be treated as FALSE
172
0
    if (_nulls_first) {
173
0
        pred = AcceptNullPredicate::create_unique(pred.release());
174
0
    }
175
176
0
    ((SharedPredicate*)_predicate.get())->set_nested(pred.release());
177
178
0
    return Status::OK();
179
0
}
180
181
} // namespace doris::vectorized