Coverage Report

Created: 2024-11-21 13:41

/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 <memory>
21
22
#include "common/compiler_util.h" // IWYU pragma: keep
23
#include "common/exception.h"
24
#include "common/status.h"
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
RuntimePredicate::RuntimePredicate(const TTopnFilterDesc& desc)
32
0
        : _nulls_first(desc.null_first), _is_asc(desc.is_asc) {
33
0
    DCHECK(!desc.target_node_id_to_target_expr.empty());
34
0
    for (auto p : desc.target_node_id_to_target_expr) {
35
0
        _contexts[p.first].expr = p.second;
36
0
    }
37
38
0
    PrimitiveType type = thrift_to_type(desc.target_node_id_to_target_expr.begin()
39
0
                                                ->second.nodes[0]
40
0
                                                .type.types[0]
41
0
                                                .scalar_type.type);
42
0
    if (!_init(type)) {
43
0
        std::stringstream ss;
44
0
        desc.target_node_id_to_target_expr.begin()->second.nodes[0].printTo(ss);
45
0
        throw Exception(ErrorCode::INTERNAL_ERROR, "meet invalid type, type={}, expr={}", int(type),
46
0
                        ss.str());
47
0
    }
48
49
    // For ASC  sort, create runtime predicate col_name <= max_top_value
50
    // since values that > min_top_value are large than any value in current topn values
51
    // For DESC sort, create runtime predicate col_name >= min_top_value
52
    // since values that < min_top_value are less than any value in current topn values
53
0
    _pred_constructor = _is_asc ? create_comparison_predicate<PredicateType::LE>
54
0
                                : create_comparison_predicate<PredicateType::GE>;
55
0
}
56
57
void RuntimePredicate::init_target(
58
0
        int32_t target_node_id, phmap::flat_hash_map<int, SlotDescriptor*> slot_id_to_slot_desc) {
59
0
    std::unique_lock<std::shared_mutex> wlock(_rwlock);
60
0
    check_target_node_id(target_node_id);
61
0
    if (target_is_slot(target_node_id)) {
62
0
        _contexts[target_node_id].col_name =
63
0
                slot_id_to_slot_desc[get_texpr(target_node_id).nodes[0].slot_ref.slot_id]
64
0
                        ->col_name();
65
0
    }
66
0
    _detected_target = true;
67
0
}
68
69
template <PrimitiveType type>
70
0
std::string get_normal_value(const Field& field) {
71
0
    using ValueType = typename PrimitiveTypeTraits<type>::CppType;
72
0
    return cast_to_string<type, ValueType>(field.get<ValueType>(), 0);
73
0
}
Unexecuted instantiation: _ZN5doris10vectorized16get_normal_valueILNS_13PrimitiveTypeE2EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
Unexecuted instantiation: _ZN5doris10vectorized16get_normal_valueILNS_13PrimitiveTypeE3EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
Unexecuted instantiation: _ZN5doris10vectorized16get_normal_valueILNS_13PrimitiveTypeE4EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
Unexecuted instantiation: _ZN5doris10vectorized16get_normal_valueILNS_13PrimitiveTypeE5EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
Unexecuted instantiation: _ZN5doris10vectorized16get_normal_valueILNS_13PrimitiveTypeE6EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
Unexecuted instantiation: _ZN5doris10vectorized16get_normal_valueILNS_13PrimitiveTypeE7EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
Unexecuted instantiation: _ZN5doris10vectorized16get_normal_valueILNS_13PrimitiveTypeE25EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
Unexecuted instantiation: _ZN5doris10vectorized16get_normal_valueILNS_13PrimitiveTypeE26EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
Unexecuted instantiation: _ZN5doris10vectorized16get_normal_valueILNS_13PrimitiveTypeE36EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
Unexecuted instantiation: _ZN5doris10vectorized16get_normal_valueILNS_13PrimitiveTypeE37EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
74
75
0
std::string get_date_value(const Field& field) {
76
0
    using ValueType = typename PrimitiveTypeTraits<TYPE_DATE>::CppType;
77
0
    ValueType value;
78
0
    Int64 v = field.get<Int64>();
79
0
    auto* p = (VecDateTimeValue*)&v;
80
0
    value.from_olap_date(p->to_olap_date());
81
0
    value.cast_to_date();
82
0
    return cast_to_string<TYPE_DATE, ValueType>(value, 0);
83
0
}
84
85
0
std::string get_datetime_value(const Field& field) {
86
0
    using ValueType = typename PrimitiveTypeTraits<TYPE_DATETIME>::CppType;
87
0
    ValueType value;
88
0
    Int64 v = field.get<Int64>();
89
0
    auto* p = (VecDateTimeValue*)&v;
90
0
    value.from_olap_datetime(p->to_olap_datetime());
91
0
    value.to_datetime();
92
0
    return cast_to_string<TYPE_DATETIME, ValueType>(value, 0);
93
0
}
94
95
0
std::string get_time_value(const Field& field) {
96
0
    using ValueType = typename PrimitiveTypeTraits<TYPE_TIMEV2>::CppType;
97
0
    ValueType value = field.get<ValueType>();
98
0
    return cast_to_string<TYPE_TIMEV2, ValueType>(value, 0);
99
0
}
100
101
0
std::string get_decimalv2_value(const Field& field) {
102
    // can NOT use PrimitiveTypeTraits<TYPE_DECIMALV2>::CppType since
103
    //   it is DecimalV2Value and Decimal128V2 can not convert to it implicitly
104
0
    using ValueType = Decimal128V2::NativeType;
105
0
    auto v = field.get<DecimalField<Decimal128V2>>();
106
    // use TYPE_DECIMAL128I instead of TYPE_DECIMALV2 since v.get_scale()
107
    //   is always 9 for DECIMALV2
108
0
    return cast_to_string<TYPE_DECIMAL128I, ValueType>(v.get_value(), v.get_scale());
109
0
}
110
111
template <PrimitiveType type>
112
0
std::string get_decimal_value(const Field& field) {
113
0
    using ValueType = typename PrimitiveTypeTraits<type>::CppType;
114
0
    auto v = field.get<DecimalField<ValueType>>();
115
0
    return cast_to_string<type, ValueType>(v.get_value(), v.get_scale());
116
0
}
Unexecuted instantiation: _ZN5doris10vectorized17get_decimal_valueILNS_13PrimitiveTypeE28EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
Unexecuted instantiation: _ZN5doris10vectorized17get_decimal_valueILNS_13PrimitiveTypeE29EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
Unexecuted instantiation: _ZN5doris10vectorized17get_decimal_valueILNS_13PrimitiveTypeE30EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
Unexecuted instantiation: _ZN5doris10vectorized17get_decimal_valueILNS_13PrimitiveTypeE35EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE
117
118
0
bool RuntimePredicate::_init(PrimitiveType type) {
119
    // set get value function
120
0
    switch (type) {
121
0
    case PrimitiveType::TYPE_BOOLEAN: {
122
0
        _get_value_fn = get_normal_value<TYPE_BOOLEAN>;
123
0
        break;
124
0
    }
125
0
    case PrimitiveType::TYPE_TINYINT: {
126
0
        _get_value_fn = get_normal_value<TYPE_TINYINT>;
127
0
        break;
128
0
    }
129
0
    case PrimitiveType::TYPE_SMALLINT: {
130
0
        _get_value_fn = get_normal_value<TYPE_SMALLINT>;
131
0
        break;
132
0
    }
133
0
    case PrimitiveType::TYPE_INT: {
134
0
        _get_value_fn = get_normal_value<TYPE_INT>;
135
0
        break;
136
0
    }
137
0
    case PrimitiveType::TYPE_BIGINT: {
138
0
        _get_value_fn = get_normal_value<TYPE_BIGINT>;
139
0
        break;
140
0
    }
141
0
    case PrimitiveType::TYPE_LARGEINT: {
142
0
        _get_value_fn = get_normal_value<TYPE_LARGEINT>;
143
0
        break;
144
0
    }
145
0
    case PrimitiveType::TYPE_CHAR:
146
0
    case PrimitiveType::TYPE_VARCHAR:
147
0
    case PrimitiveType::TYPE_STRING: {
148
0
        _get_value_fn = [](const Field& field) { return field.get<String>(); };
149
0
        break;
150
0
    }
151
0
    case PrimitiveType::TYPE_DATEV2: {
152
0
        _get_value_fn = get_normal_value<TYPE_DATEV2>;
153
0
        break;
154
0
    }
155
0
    case PrimitiveType::TYPE_DATETIMEV2: {
156
0
        _get_value_fn = get_normal_value<TYPE_DATETIMEV2>;
157
0
        break;
158
0
    }
159
0
    case PrimitiveType::TYPE_DATE: {
160
0
        _get_value_fn = get_date_value;
161
0
        break;
162
0
    }
163
0
    case PrimitiveType::TYPE_DATETIME: {
164
0
        _get_value_fn = get_datetime_value;
165
0
        break;
166
0
    }
167
0
    case PrimitiveType::TYPE_TIMEV2: {
168
0
        _get_value_fn = get_time_value;
169
0
        break;
170
0
    }
171
0
    case PrimitiveType::TYPE_DECIMAL32: {
172
0
        _get_value_fn = get_decimal_value<TYPE_DECIMAL32>;
173
0
        break;
174
0
    }
175
0
    case PrimitiveType::TYPE_DECIMAL64: {
176
0
        _get_value_fn = get_decimal_value<TYPE_DECIMAL64>;
177
0
        break;
178
0
    }
179
0
    case PrimitiveType::TYPE_DECIMALV2: {
180
0
        _get_value_fn = get_decimalv2_value;
181
0
        break;
182
0
    }
183
0
    case PrimitiveType::TYPE_DECIMAL128I: {
184
0
        _get_value_fn = get_decimal_value<TYPE_DECIMAL128I>;
185
0
        break;
186
0
    }
187
0
    case PrimitiveType::TYPE_DECIMAL256: {
188
0
        _get_value_fn = get_decimal_value<TYPE_DECIMAL256>;
189
0
        break;
190
0
    }
191
0
    case PrimitiveType::TYPE_IPV4: {
192
0
        _get_value_fn = get_normal_value<TYPE_IPV4>;
193
0
        break;
194
0
    }
195
0
    case PrimitiveType::TYPE_IPV6: {
196
0
        _get_value_fn = get_normal_value<TYPE_IPV6>;
197
0
        break;
198
0
    }
199
0
    default:
200
0
        return false;
201
0
    }
202
203
0
    return true;
204
0
}
205
206
0
Status RuntimePredicate::update(const Field& value) {
207
0
    std::unique_lock<std::shared_mutex> wlock(_rwlock);
208
    // skip null value
209
0
    if (value.is_null()) {
210
0
        return Status::OK();
211
0
    }
212
213
0
    bool updated = false;
214
215
0
    if (UNLIKELY(_orderby_extrem.is_null())) {
216
0
        _orderby_extrem = value;
217
0
        updated = true;
218
0
    } else {
219
0
        if ((_is_asc && value < _orderby_extrem) || (!_is_asc && value > _orderby_extrem)) {
220
0
            _orderby_extrem = value;
221
0
            updated = true;
222
0
        }
223
0
    }
224
225
0
    _has_value = true;
226
227
0
    if (!updated) {
228
0
        return Status::OK();
229
0
    }
230
0
    for (auto p : _contexts) {
231
0
        auto ctx = p.second;
232
0
        if (!ctx.tablet_schema) {
233
0
            continue;
234
0
        }
235
0
        const auto& column = *DORIS_TRY(ctx.tablet_schema->column(ctx.col_name));
236
0
        std::unique_ptr<ColumnPredicate> pred {_pred_constructor(column, ctx.predicate->column_id(),
237
0
                                                                 _get_value_fn(_orderby_extrem),
238
0
                                                                 false, &_predicate_arena)};
239
240
        // For NULLS FIRST, wrap a AcceptNullPredicate to return true for NULL
241
        // since ORDER BY ASC/DESC should get NULL first but pred returns NULL
242
        // and NULL in where predicate will be treated as FALSE
243
0
        if (_nulls_first) {
244
0
            pred = AcceptNullPredicate::create_unique(pred.release());
245
0
        }
246
247
0
        ((SharedPredicate*)ctx.predicate.get())->set_nested(pred.release());
248
0
    }
249
0
    return Status::OK();
250
0
}
251
252
} // namespace doris::vectorized