Coverage Report

Created: 2026-08-06 15:40

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
8.22k
            : VExpr(node),
61
8.22k
              _filter(filter),
62
8.22k
              _hybrid_set_values_match_child_type(hybrid_set_values_match_child_type),
63
8.22k
              _expr_name("direct_in_predicate") {}
64
8.23k
    ~VDirectInPredicate() override = default;
65
66
#ifdef BE_TEST
67
    VDirectInPredicate() = default;
68
#endif
69
70
    Status prepare(RuntimeState* state, const RowDescriptor& row_desc,
71
3.77k
                   VExprContext* context) override {
72
3.77k
        RETURN_IF_ERROR_OR_PREPARED(VExpr::prepare(state, row_desc, context));
73
3.77k
        RETURN_IF_ERROR(_materialize_for_zonemap_filter());
74
3.77k
        _prepare_finished = true;
75
3.77k
        return Status::OK();
76
3.77k
    }
77
78
    Status open(RuntimeState* state, VExprContext* context,
79
7.33k
                FunctionContext::FunctionStateScope scope) override {
80
7.33k
        DCHECK(_prepare_finished);
81
7.33k
        RETURN_IF_ERROR(VExpr::open(state, context, scope));
82
7.33k
        _open_finished = true;
83
7.33k
        return Status::OK();
84
7.33k
    }
85
86
    Status execute_column_impl(VExprContext* context, const Block* block, const Selector* selector,
87
30
                               size_t count, ColumnPtr& result_column) const override {
88
30
        return _do_execute(context, block, nullptr, selector, count, result_column, nullptr);
89
30
    }
90
91
    Status execute_runtime_filter(VExprContext* context, const Block* block,
92
                                  const uint8_t* __restrict filter, size_t count,
93
8.45k
                                  ColumnPtr& result_column, ColumnPtr* arg_column) const override {
94
8.45k
        return _do_execute(context, block, filter, nullptr, count, result_column, arg_column);
95
8.45k
    }
96
97
3.77k
    const std::string& expr_name() const override { return _expr_name; }
98
99
2.49k
    std::shared_ptr<HybridSetBase> get_set_func() const override { return _filter; }
100
101
3.38k
    ZoneMapFilterResult evaluate_zonemap_filter(const ZoneMapEvalContext& ctx) const override {
102
3.38k
        return expr_zonemap::eval_in_zonemap(
103
3.38k
                ctx, get_child(0), false, _pruning_state->seg_filter_values,
104
3.38k
                _pruning_state->seg_filter_min, _pruning_state->seg_filter_max);
105
3.38k
    }
106
107
5.89k
    bool can_evaluate_zonemap_filter() const override {
108
5.89k
        return _pruning_state->zonemap_materialized &&
109
5.89k
               std::dynamic_pointer_cast<VSlotRef>(get_child(0)) != nullptr;
110
5.89k
    }
111
112
    ZoneMapFilterResult evaluate_dictionary_filter(
113
11
            const DictionaryEvalContext& ctx) const override {
114
11
        return expr_zonemap::eval_in_dictionary(ctx, get_child(0), false,
115
11
                                                _pruning_state->seg_filter_values);
116
11
    }
117
118
655
    bool can_evaluate_dictionary_filter() const override {
119
655
        return _pruning_state->zonemap_materialized &&
120
655
               std::dynamic_pointer_cast<VSlotRef>(get_child(0)) != nullptr;
121
655
    }
122
123
    bool can_execute_on_raw_fixed_values(const DataTypePtr& data_type,
124
38.5k
                                         int column_id) const override {
125
38.5k
        if (!_hybrid_set_values_match_child_type || data_type == nullptr || _filter == nullptr ||
126
38.5k
            get_num_children() != 1) {
127
0
            return false;
128
0
        }
129
38.5k
        const auto slot = std::dynamic_pointer_cast<VSlotRef>(get_child(0));
130
38.5k
        if (slot == nullptr || slot->column_id() != column_id) {
131
0
            return false;
132
0
        }
133
38.5k
        const auto raw_type = remove_nullable(data_type);
134
38.5k
        if (!remove_nullable(slot->data_type())->equals(*raw_type)) {
135
0
            return false;
136
0
        }
137
38.5k
        return _raw_fixed_value_size(raw_type->get_primitive_type()) != 0;
138
38.5k
    }
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
6.44k
                                       uint8_t* matches) const override {
143
6.44k
        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
6.44k
        DORIS_CHECK(values != nullptr || num_values == 0);
148
6.44k
        DORIS_CHECK(matches != nullptr || num_values == 0);
149
6.44k
        const size_t expected_width =
150
6.44k
                _raw_fixed_value_size(remove_nullable(data_type)->get_primitive_type());
151
6.44k
        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
6.44k
        _filter->find_batch_raw_fixed(values, num_values, value_width, matches);
158
6.44k
        return Status::OK();
159
6.44k
    }
160
161
    bool can_execute_on_raw_binary_values(const DataTypePtr& data_type,
162
225
                                          int column_id) const override {
163
225
        if (!_hybrid_set_values_match_child_type || data_type == nullptr || _filter == nullptr ||
164
225
            get_num_children() != 1) {
165
0
            return false;
166
0
        }
167
225
        const auto slot = std::dynamic_pointer_cast<VSlotRef>(get_child(0));
168
225
        if (slot == nullptr || slot->column_id() != column_id || slot->data_type() == nullptr) {
169
0
            return false;
170
0
        }
171
225
        return is_string_type(remove_nullable(data_type)->get_primitive_type()) &&
172
225
               is_string_type(remove_nullable(slot->data_type())->get_primitive_type());
173
225
    }
174
175
    Status execute_on_raw_binary_values(const StringRef* values, size_t num_values,
176
                                        const DataTypePtr& data_type, int column_id,
177
37
                                        uint8_t* matches) const override {
178
37
        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
37
        DORIS_CHECK(values != nullptr || num_values == 0);
182
37
        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
37
        _filter->find_batch_raw_binary(values, num_values, matches);
186
37
        return Status::OK();
187
37
    }
188
189
5.82k
    Status clone_node(VExprSPtr* cloned_expr) const override {
190
5.82k
        DORIS_CHECK(cloned_expr != nullptr);
191
5.82k
        auto cloned = VDirectInPredicate::create_shared(clone_texpr_node(), _filter,
192
5.82k
                                                        _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
5.82k
        cloned->_pruning_state = _pruning_state;
196
5.82k
        *cloned_expr = std::move(cloned);
197
5.82k
        return Status::OK();
198
5.82k
    }
199
200
140
    bool get_slot_in_expr(VExprSPtr& new_root) const {
201
140
        if (!_hybrid_set_values_match_child_type) {
202
1
            return false;
203
1
        }
204
139
        if (!get_child(0)->is_slot_ref()) {
205
0
            return false;
206
0
        }
207
208
139
        auto* slot_ref = assert_cast<VSlotRef*>(get_child(0).get());
209
139
        auto slot_data_type = remove_nullable(slot_ref->data_type());
210
139
        {
211
139
            TTypeDesc type_desc = create_type_desc(PrimitiveType::TYPE_BOOLEAN);
212
139
            TExprNode node;
213
139
            node.__set_type(type_desc);
214
139
            node.__set_node_type(TExprNodeType::IN_PRED);
215
139
            node.in_predicate.__set_is_not_in(false);
216
139
            node.__set_opcode(TExprOpcode::FILTER_IN);
217
            // VdirectInPredicate assume is_nullable = false.
218
139
            node.__set_is_nullable(false);
219
139
            new_root = VInPredicate::create_shared(node);
220
139
        }
221
139
        {
222
            // add slot
223
139
            new_root->add_child(children().at(0));
224
139
        }
225
139
        {
226
139
            auto iter = get_set_func()->begin();
227
3.82k
            while (iter->has_next()) {
228
3.68k
                DCHECK(iter->get_value() != nullptr);
229
3.68k
                const void* value = iter->get_value();
230
231
3.68k
                TExprNode node = expr_zonemap::create_texpr_node_from_hybrid_set_value(
232
3.68k
                        value, slot_data_type->get_primitive_type(),
233
3.68k
                        slot_data_type->get_precision(), slot_data_type->get_scale());
234
3.68k
                new_root->add_child(VLiteral::create_shared(node));
235
3.68k
                iter->next();
236
3.68k
            }
237
139
        }
238
139
        return true;
239
139
    }
240
241
3.51k
    uint64_t get_digest(uint64_t seed) const override {
242
3.51k
        seed = _children[0]->get_digest(seed);
243
3.51k
        if (seed) {
244
3.51k
            return _filter->get_digest(seed);
245
3.51k
        }
246
3
        return seed;
247
3.51k
    }
248
249
private:
250
44.9k
    static size_t _raw_fixed_value_size(PrimitiveType primitive_type) {
251
44.9k
        switch (primitive_type) {
252
0
#define RETURN_RAW_FIXED_SIZE(TYPE) \
253
44.9k
    case TYPE:                      \
254
44.9k
        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
44.4k
            RETURN_RAW_FIXED_SIZE(TYPE_INT);
259
85
            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
29
            RETURN_RAW_FIXED_SIZE(TYPE_DATEV2);
266
225
            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
50
            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
46
        default:
278
46
            return 0;
279
44.9k
        }
280
44.9k
    }
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
8.48k
                       ColumnPtr* arg_column) const {
285
8.48k
        DCHECK(_open_finished || block == nullptr);
286
18.4E
        DCHECK(!(filter != nullptr && selector != nullptr))
287
18.4E
                << "filter and selector can not be both set";
288
8.48k
        ColumnPtr argument_column;
289
8.48k
        RETURN_IF_ERROR(
290
8.48k
                _children[0]->execute_column(context, block, selector, count, argument_column));
291
8.48k
        argument_column = argument_column->convert_to_full_column_if_const();
292
293
8.48k
        if (arg_column != nullptr) {
294
8.45k
            *arg_column = argument_column;
295
8.45k
        }
296
297
8.48k
        size_t sz = argument_column->size();
298
8.48k
        auto res_data_column = ColumnUInt8::create(sz);
299
8.48k
        res_data_column->resize(sz);
300
301
8.48k
        if (const auto* nullable = check_and_get_column<ColumnNullable>(argument_column.get())) {
302
8.29k
            auto column_nested = nullable->get_nested_column_ptr();
303
8.29k
            const auto& null_map = nullable->get_null_map_data();
304
8.29k
            _filter->find_batch_nullable(*column_nested, sz, null_map, res_data_column->get_data(),
305
8.29k
                                         filter);
306
8.29k
        } else {
307
190
            _filter->find_batch(*argument_column, sz, res_data_column->get_data(), filter);
308
190
        }
309
310
8.48k
        DCHECK(!_data_type->is_nullable());
311
8.48k
        result_column = std::move(res_data_column);
312
8.48k
        return Status::OK();
313
8.48k
    }
314
315
3.77k
    Status _materialize_for_zonemap_filter() {
316
3.77k
        const auto pruning_state = _pruning_state;
317
3.77k
        std::call_once(pruning_state->materialize_once, [&] {
318
2.32k
            if (!_hybrid_set_values_match_child_type) {
319
1
                return;
320
1
            }
321
2.32k
            DORIS_CHECK(_filter != nullptr);
322
2.32k
            auto& filter = *_filter;
323
2.32k
            const auto& data_type = remove_nullable(get_child(0)->data_type());
324
2.32k
            expr_zonemap::InZonemapMaterializedSet materialized;
325
2.32k
            pruning_state->materialization_status =
326
2.32k
                    expr_zonemap::materialize_hybrid_set_for_zonemap_filter(filter, data_type,
327
2.32k
                                                                            &materialized);
328
2.32k
            if (!pruning_state->materialization_status.ok()) {
329
0
                return;
330
0
            }
331
2.32k
            pruning_state->seg_filter_values = std::move(materialized.values);
332
2.32k
            pruning_state->seg_filter_min = std::move(materialized.min_value);
333
2.32k
            pruning_state->seg_filter_max = std::move(materialized.max_value);
334
2.32k
            pruning_state->zonemap_materialized = true;
335
2.32k
        });
336
3.77k
        return pruning_state->materialization_status;
337
3.77k
    }
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