Coverage Report

Created: 2026-08-18 16:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/vdirect_in_predicate.h
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
#pragma once
19
20
#include <mutex>
21
#include <utility>
22
#include <vector>
23
24
#include "common/logging.h"
25
#include "common/status.h"
26
#include "core/field.h"
27
#include "core/types.h"
28
#include "exprs/expr_zonemap_filter.h"
29
#include "exprs/hybrid_set.h"
30
#include "exprs/vexpr.h"
31
#include "exprs/vin_predicate.h"
32
#include "exprs/vliteral.h"
33
#include "exprs/vslot_ref.h"
34
35
namespace doris {
36
37
class VDirectInPredicate final : public VExpr {
38
    ENABLE_FACTORY_CREATOR(VDirectInPredicate);
39
40
    struct PruningState {
41
        std::once_flag materialize_once;
42
        Status materialization_status;
43
        bool zonemap_materialized = false;
44
        bool seg_filter_contains_nan = false;
45
        std::vector<Field> seg_filter_values;
46
        Field seg_filter_min;
47
        Field seg_filter_max;
48
    };
49
50
public:
51
    // `hybrid_set_values_match_child_type` tells whether values in `filter` can be interpreted with
52
    // the child expression type. Parquet/ORC dictionary-filter rewrites evaluate the original
53
    // logical predicate against dictionary entries and then rewrite it to matched physical
54
    // dictionary codes, for example `col IN ('a', 'b')` becomes `dict_code IN (0, 1)`. In that
55
    // shape the HybridSet stores TYPE_INT dictionary codes while the child slot still has the
56
    // original logical type such as STRING. Callers must pass false to disable zonemap
57
    // materialization and slot-IN rewrite that would otherwise rebuild child-typed literals from
58
    // dictionary codes.
59
    VDirectInPredicate(const TExprNode& node, const std::shared_ptr<HybridSetBase>& filter,
60
                       bool hybrid_set_values_match_child_type = true)
61
118
            : VExpr(node),
62
118
              _filter(filter),
63
118
              _hybrid_set_values_match_child_type(hybrid_set_values_match_child_type),
64
118
              _expr_name("direct_in_predicate") {}
65
122
    ~VDirectInPredicate() override = default;
66
67
#ifdef BE_TEST
68
    VDirectInPredicate() = default;
69
#endif
70
71
    Status prepare(RuntimeState* state, const RowDescriptor& row_desc,
72
15
                   VExprContext* context) override {
73
15
        RETURN_IF_ERROR_OR_PREPARED(VExpr::prepare(state, row_desc, context));
74
15
        RETURN_IF_ERROR(_materialize_for_zonemap_filter());
75
15
        _prepare_finished = true;
76
15
        return Status::OK();
77
15
    }
78
79
    Status open(RuntimeState* state, VExprContext* context,
80
14
                FunctionContext::FunctionStateScope scope) override {
81
14
        DCHECK(_prepare_finished);
82
14
        RETURN_IF_ERROR(VExpr::open(state, context, scope));
83
14
        _open_finished = true;
84
14
        return Status::OK();
85
14
    }
86
87
    Status execute_column_impl(VExprContext* context, const Block* block, const Selector* selector,
88
11
                               size_t count, ColumnPtr& result_column) const override {
89
11
        return _do_execute(context, block, nullptr, selector, count, result_column, nullptr);
90
11
    }
91
92
    Status execute_runtime_filter(VExprContext* context, const Block* block,
93
                                  const uint8_t* __restrict filter, size_t count,
94
6
                                  ColumnPtr& result_column, ColumnPtr* arg_column) const override {
95
6
        return _do_execute(context, block, filter, nullptr, count, result_column, arg_column);
96
6
    }
97
98
14
    const std::string& expr_name() const override { return _expr_name; }
99
100
68
    std::shared_ptr<HybridSetBase> get_set_func() const override { return _filter; }
101
102
5
    ZoneMapFilterResult evaluate_zonemap_filter(const ZoneMapEvalContext& ctx) const override {
103
5
        return expr_zonemap::eval_in_zonemap(
104
5
                ctx, get_child(0), false, _pruning_state->seg_filter_values,
105
5
                _pruning_state->seg_filter_contains_nan, _pruning_state->seg_filter_min,
106
5
                _pruning_state->seg_filter_max);
107
5
    }
108
109
27
    bool can_evaluate_zonemap_filter() const override {
110
27
        return _pruning_state->zonemap_materialized &&
111
27
               std::dynamic_pointer_cast<VSlotRef>(get_child(0)) != nullptr;
112
27
    }
113
114
    ZoneMapFilterResult evaluate_dictionary_filter(
115
3
            const DictionaryEvalContext& ctx) const override {
116
3
        return expr_zonemap::eval_in_dictionary(ctx, get_child(0), false,
117
3
                                                _pruning_state->seg_filter_values);
118
3
    }
119
120
17
    bool can_evaluate_dictionary_filter() const override {
121
17
        return _pruning_state->zonemap_materialized &&
122
17
               std::dynamic_pointer_cast<VSlotRef>(get_child(0)) != nullptr;
123
17
    }
124
125
    bool can_execute_on_raw_fixed_values(const DataTypePtr& data_type,
126
52
                                         int column_id) const override {
127
52
        if (!_hybrid_set_values_match_child_type || data_type == nullptr || _filter == nullptr ||
128
52
            get_num_children() != 1) {
129
0
            return false;
130
0
        }
131
52
        const auto slot = std::dynamic_pointer_cast<VSlotRef>(get_child(0));
132
52
        if (slot == nullptr || slot->column_id() != column_id) {
133
0
            return false;
134
0
        }
135
52
        const auto raw_type = remove_nullable(data_type);
136
52
        if (!remove_nullable(slot->data_type())->equals(*raw_type)) {
137
0
            return false;
138
0
        }
139
52
        return _raw_fixed_value_size(raw_type->get_primitive_type()) != 0;
140
52
    }
141
142
    Status execute_on_raw_fixed_values(const uint8_t* values, size_t num_values, size_t value_width,
143
                                       const DataTypePtr& data_type, int column_id,
144
7
                                       uint8_t* matches) const override {
145
7
        if (!can_execute_on_raw_fixed_values(data_type, column_id)) {
146
0
            return Status::NotSupported(
147
0
                    "Direct IN predicate cannot evaluate raw fixed-width values");
148
0
        }
149
7
        DORIS_CHECK(values != nullptr || num_values == 0);
150
7
        DORIS_CHECK(matches != nullptr || num_values == 0);
151
7
        const size_t expected_width =
152
7
                _raw_fixed_value_size(remove_nullable(data_type)->get_primitive_type());
153
7
        if (value_width != expected_width) {
154
0
            return Status::Corruption("Raw direct IN width {} does not match expected {}",
155
0
                                      value_width, expected_width);
156
0
        }
157
        // Dispatch once per decoder batch so large runtime-filter sets retain the typed HybridSet
158
        // loop instead of paying a virtual lookup for every physical value.
159
7
        _filter->find_batch_raw_fixed(values, num_values, value_width, matches);
160
7
        return Status::OK();
161
7
    }
162
163
    bool can_execute_on_raw_binary_values(const DataTypePtr& data_type,
164
9
                                          int column_id) const override {
165
9
        if (!_hybrid_set_values_match_child_type || data_type == nullptr || _filter == nullptr ||
166
9
            get_num_children() != 1) {
167
0
            return false;
168
0
        }
169
9
        const auto slot = std::dynamic_pointer_cast<VSlotRef>(get_child(0));
170
9
        if (slot == nullptr || slot->column_id() != column_id || slot->data_type() == nullptr) {
171
0
            return false;
172
0
        }
173
9
        return is_string_type(remove_nullable(data_type)->get_primitive_type()) &&
174
9
               is_string_type(remove_nullable(slot->data_type())->get_primitive_type());
175
9
    }
176
177
    Status execute_on_raw_binary_values(const StringRef* values, size_t num_values,
178
                                        const DataTypePtr& data_type, int column_id,
179
1
                                        uint8_t* matches) const override {
180
1
        if (!can_execute_on_raw_binary_values(data_type, column_id)) {
181
0
            return Status::NotSupported("Direct IN predicate cannot evaluate raw binary values");
182
0
        }
183
1
        DORIS_CHECK(values != nullptr || num_values == 0);
184
1
        DORIS_CHECK(matches != nullptr || num_values == 0);
185
        // Probe immutable decoder slices directly; constructing ColumnString first would copy
186
        // every rejected payload and defeat predicate-only late materialization.
187
1
        _filter->find_batch_raw_binary(values, num_values, matches);
188
1
        return Status::OK();
189
1
    }
190
191
3
    Status clone_node(VExprSPtr* cloned_expr) const override {
192
3
        DORIS_CHECK(cloned_expr != nullptr);
193
3
        auto cloned = VDirectInPredicate::create_shared(clone_texpr_node(), _filter,
194
3
                                                        _hybrid_set_values_match_child_type);
195
        // Runtime-filter sets are immutable after publication, and file-local rewrites preserve
196
        // the predicate's logical child type, so every split clone must reuse this materialization.
197
3
        cloned->_pruning_state = _pruning_state;
198
3
        *cloned_expr = std::move(cloned);
199
3
        return Status::OK();
200
3
    }
201
202
9
    bool get_slot_in_expr(VExprSPtr& new_root) const {
203
9
        if (!_hybrid_set_values_match_child_type) {
204
1
            return false;
205
1
        }
206
8
        if (!get_child(0)->is_slot_ref()) {
207
0
            return false;
208
0
        }
209
210
8
        auto* slot_ref = assert_cast<VSlotRef*>(get_child(0).get());
211
8
        auto slot_data_type = remove_nullable(slot_ref->data_type());
212
8
        {
213
8
            TTypeDesc type_desc = create_type_desc(PrimitiveType::TYPE_BOOLEAN);
214
8
            TExprNode node;
215
8
            node.__set_type(type_desc);
216
8
            node.__set_node_type(TExprNodeType::IN_PRED);
217
8
            node.in_predicate.__set_is_not_in(false);
218
8
            node.__set_opcode(TExprOpcode::FILTER_IN);
219
            // VdirectInPredicate assume is_nullable = false.
220
8
            node.__set_is_nullable(false);
221
8
            new_root = VInPredicate::create_shared(node);
222
8
        }
223
8
        {
224
            // add slot
225
8
            new_root->add_child(children().at(0));
226
8
        }
227
8
        {
228
8
            auto iter = get_set_func()->begin();
229
3.09k
            while (iter->has_next()) {
230
3.08k
                DCHECK(iter->get_value() != nullptr);
231
3.08k
                const void* value = iter->get_value();
232
233
3.08k
                TExprNode node = expr_zonemap::create_texpr_node_from_hybrid_set_value(
234
3.08k
                        value, slot_data_type->get_primitive_type(),
235
3.08k
                        slot_data_type->get_precision(), slot_data_type->get_scale());
236
3.08k
                new_root->add_child(VLiteral::create_shared(node));
237
3.08k
                iter->next();
238
3.08k
            }
239
8
        }
240
8
        return true;
241
8
    }
242
243
4
    uint64_t get_digest(uint64_t seed) const override {
244
4
        seed = _children[0]->get_digest(seed);
245
4
        if (seed) {
246
4
            return _filter->get_digest(seed);
247
4
        }
248
0
        return seed;
249
4
    }
250
251
private:
252
59
    static size_t _raw_fixed_value_size(PrimitiveType primitive_type) {
253
59
        switch (primitive_type) {
254
0
#define RETURN_RAW_FIXED_SIZE(TYPE) \
255
56
    case TYPE:                      \
256
56
        return sizeof(typename PrimitiveTypeTraits<TYPE>::CppType)
257
1
            RETURN_RAW_FIXED_SIZE(TYPE_BOOLEAN);
258
8
            RETURN_RAW_FIXED_SIZE(TYPE_TINYINT);
259
1
            RETURN_RAW_FIXED_SIZE(TYPE_SMALLINT);
260
16
            RETURN_RAW_FIXED_SIZE(TYPE_INT);
261
1
            RETURN_RAW_FIXED_SIZE(TYPE_BIGINT);
262
1
            RETURN_RAW_FIXED_SIZE(TYPE_LARGEINT);
263
4
            RETURN_RAW_FIXED_SIZE(TYPE_FLOAT);
264
4
            RETURN_RAW_FIXED_SIZE(TYPE_DOUBLE);
265
1
            RETURN_RAW_FIXED_SIZE(TYPE_DATE);
266
1
            RETURN_RAW_FIXED_SIZE(TYPE_DATETIME);
267
1
            RETURN_RAW_FIXED_SIZE(TYPE_DATEV2);
268
1
            RETURN_RAW_FIXED_SIZE(TYPE_DATETIMEV2);
269
1
            RETURN_RAW_FIXED_SIZE(TYPE_TIMESTAMPTZ);
270
1
            RETURN_RAW_FIXED_SIZE(TYPE_TIMEV2);
271
1
            RETURN_RAW_FIXED_SIZE(TYPE_DECIMAL32);
272
8
            RETURN_RAW_FIXED_SIZE(TYPE_DECIMAL64);
273
1
            RETURN_RAW_FIXED_SIZE(TYPE_DECIMALV2);
274
1
            RETURN_RAW_FIXED_SIZE(TYPE_DECIMAL128I);
275
1
            RETURN_RAW_FIXED_SIZE(TYPE_DECIMAL256);
276
1
            RETURN_RAW_FIXED_SIZE(TYPE_IPV4);
277
1
            RETURN_RAW_FIXED_SIZE(TYPE_IPV6);
278
0
#undef RETURN_RAW_FIXED_SIZE
279
3
        default:
280
3
            return 0;
281
59
        }
282
59
    }
283
284
    Status _do_execute(VExprContext* context, const Block* block, const uint8_t* __restrict filter,
285
                       const Selector* selector, size_t count, ColumnPtr& result_column,
286
17
                       ColumnPtr* arg_column) const {
287
17
        DCHECK(_open_finished || block == nullptr);
288
17
        DCHECK(!(filter != nullptr && selector != nullptr))
289
0
                << "filter and selector can not be both set";
290
17
        ColumnPtr argument_column;
291
17
        RETURN_IF_ERROR(
292
17
                _children[0]->execute_column(context, block, selector, count, argument_column));
293
17
        argument_column = argument_column->convert_to_full_column_if_const();
294
295
17
        if (arg_column != nullptr) {
296
6
            *arg_column = argument_column;
297
6
        }
298
299
17
        size_t sz = argument_column->size();
300
17
        auto res_data_column = ColumnUInt8::create(sz);
301
17
        res_data_column->resize(sz);
302
303
17
        if (const auto* nullable = check_and_get_column<ColumnNullable>(argument_column.get())) {
304
16
            auto column_nested = nullable->get_nested_column_ptr();
305
16
            const auto& null_map = nullable->get_null_map_data();
306
16
            _filter->find_batch_nullable(*column_nested, sz, null_map, res_data_column->get_data(),
307
16
                                         filter);
308
16
        } else {
309
1
            _filter->find_batch(*argument_column, sz, res_data_column->get_data(), filter);
310
1
        }
311
312
17
        DCHECK(!_data_type->is_nullable());
313
17
        result_column = std::move(res_data_column);
314
17
        return Status::OK();
315
17
    }
316
317
20
    Status _materialize_for_zonemap_filter() {
318
20
        const auto pruning_state = _pruning_state;
319
20
        std::call_once(pruning_state->materialize_once, [&] {
320
20
            if (!_hybrid_set_values_match_child_type) {
321
1
                return;
322
1
            }
323
19
            DORIS_CHECK(_filter != nullptr);
324
19
            auto& filter = *_filter;
325
19
            const auto& data_type = remove_nullable(get_child(0)->data_type());
326
19
            expr_zonemap::InZonemapMaterializedSet materialized;
327
19
            pruning_state->materialization_status =
328
19
                    expr_zonemap::materialize_hybrid_set_for_zonemap_filter(filter, data_type,
329
19
                                                                            &materialized);
330
19
            if (!pruning_state->materialization_status.ok()) {
331
0
                return;
332
0
            }
333
19
            pruning_state->seg_filter_values = std::move(materialized.values);
334
19
            pruning_state->seg_filter_contains_nan = materialized.contains_nan;
335
19
            pruning_state->seg_filter_min = std::move(materialized.min_value);
336
19
            pruning_state->seg_filter_max = std::move(materialized.max_value);
337
19
            pruning_state->zonemap_materialized = true;
338
19
        });
339
20
        return pruning_state->materialization_status;
340
20
    }
341
342
    std::shared_ptr<HybridSetBase> _filter;
343
    // Dictionary-filter rewrites may store physical dictionary codes in the HybridSet while the
344
    // child slot keeps the original logical type. Such values must not be materialized as child-type
345
    // literals for zonemap pruning or slot-IN rewrite.
346
    bool _hybrid_set_values_match_child_type = true;
347
    std::string _expr_name;
348
    std::shared_ptr<PruningState> _pruning_state = std::make_shared<PruningState>();
349
};
350
351
} // namespace doris