Coverage Report

Created: 2026-08-04 06:47

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