Coverage Report

Created: 2026-07-30 05:26

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