Coverage Report

Created: 2026-04-11 13:34

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 "common/status.h"
21
#include "exprs/hybrid_set.h"
22
#include "exprs/vexpr.h"
23
#include "exprs/vin_predicate.h"
24
#include "exprs/vliteral.h"
25
#include "exprs/vslot_ref.h"
26
27
namespace doris {
28
29
class VDirectInPredicate final : public VExpr {
30
    ENABLE_FACTORY_CREATOR(VDirectInPredicate);
31
32
public:
33
    VDirectInPredicate(const TExprNode& node, const std::shared_ptr<HybridSetBase>& filter)
34
14.8k
            : VExpr(node), _filter(filter), _expr_name("direct_in_predicate") {}
35
14.8k
    ~VDirectInPredicate() override = default;
36
37
#ifdef BE_TEST
38
    VDirectInPredicate() = default;
39
#endif
40
41
    Status prepare(RuntimeState* state, const RowDescriptor& row_desc,
42
14.7k
                   VExprContext* context) override {
43
14.7k
        RETURN_IF_ERROR_OR_PREPARED(VExpr::prepare(state, row_desc, context));
44
14.7k
        _prepare_finished = true;
45
14.7k
        return Status::OK();
46
14.7k
    }
47
48
    Status open(RuntimeState* state, VExprContext* context,
49
21.1k
                FunctionContext::FunctionStateScope scope) override {
50
21.1k
        DCHECK(_prepare_finished);
51
21.1k
        RETURN_IF_ERROR(VExpr::open(state, context, scope));
52
21.1k
        _open_finished = true;
53
21.1k
        return Status::OK();
54
21.1k
    }
55
56
    Status execute_column(VExprContext* context, const Block* block, Selector* selector,
57
903
                          size_t count, ColumnPtr& result_column) const override {
58
903
        return _do_execute(context, block, nullptr, selector, count, result_column, nullptr);
59
903
    }
60
61
    Status execute_runtime_filter(VExprContext* context, const Block* block,
62
                                  const uint8_t* __restrict filter, size_t count,
63
12.3k
                                  ColumnPtr& result_column, ColumnPtr* arg_column) const override {
64
12.3k
        return _do_execute(context, block, filter, nullptr, count, result_column, arg_column);
65
12.3k
    }
66
67
14.6k
    const std::string& expr_name() const override { return _expr_name; }
68
69
15.0k
    std::shared_ptr<HybridSetBase> get_set_func() const override { return _filter; }
70
71
244
    bool get_slot_in_expr(VExprSPtr& new_root) const {
72
244
        if (!get_child(0)->is_slot_ref()) {
73
0
            return false;
74
0
        }
75
76
244
        auto* slot_ref = assert_cast<VSlotRef*>(get_child(0).get());
77
244
        auto slot_data_type = remove_nullable(slot_ref->data_type());
78
244
        {
79
244
            TTypeDesc type_desc = create_type_desc(PrimitiveType::TYPE_BOOLEAN);
80
244
            TExprNode node;
81
244
            node.__set_type(type_desc);
82
244
            node.__set_node_type(TExprNodeType::IN_PRED);
83
244
            node.in_predicate.__set_is_not_in(false);
84
244
            node.__set_opcode(TExprOpcode::FILTER_IN);
85
            // VdirectInPredicate assume is_nullable = false.
86
244
            node.__set_is_nullable(false);
87
244
            new_root = VInPredicate::create_shared(node);
88
244
        }
89
244
        {
90
            // add slot
91
244
            new_root->add_child(children().at(0));
92
244
        }
93
244
        {
94
244
            auto iter = get_set_func()->begin();
95
1.44k
            while (iter->has_next()) {
96
1.19k
                DCHECK(iter->get_value() != nullptr);
97
1.19k
                const void* value = iter->get_value();
98
99
1.19k
                TExprNode node = create_texpr_node_from(value, slot_data_type->get_primitive_type(),
100
1.19k
                                                        slot_data_type->get_precision(),
101
1.19k
                                                        slot_data_type->get_scale());
102
1.19k
                new_root->add_child(VLiteral::create_shared(node));
103
1.19k
                iter->next();
104
1.19k
            }
105
244
        }
106
244
        return true;
107
244
    }
108
109
14.3k
    uint64_t get_digest(uint64_t seed) const override {
110
14.3k
        seed = _children[0]->get_digest(seed);
111
14.3k
        if (seed) {
112
14.3k
            return _filter->get_digest(seed);
113
14.3k
        }
114
18.4E
        return seed;
115
14.3k
    }
116
117
private:
118
    Status _do_execute(VExprContext* context, const Block* block, const uint8_t* __restrict filter,
119
                       Selector* selector, size_t count, ColumnPtr& result_column,
120
13.2k
                       ColumnPtr* arg_column) const {
121
13.2k
        DCHECK(_open_finished || block == nullptr);
122
13.2k
        DCHECK(!(filter != nullptr && selector != nullptr))
123
0
                << "filter and selector can not be both set";
124
13.2k
        ColumnPtr argument_column;
125
13.2k
        RETURN_IF_ERROR(
126
13.2k
                _children[0]->execute_column(context, block, selector, count, argument_column));
127
13.2k
        argument_column = argument_column->convert_to_full_column_if_const();
128
129
13.2k
        if (arg_column != nullptr) {
130
12.3k
            *arg_column = argument_column;
131
12.3k
        }
132
133
13.2k
        size_t sz = argument_column->size();
134
13.2k
        auto res_data_column = ColumnUInt8::create(sz);
135
13.2k
        res_data_column->resize(sz);
136
137
13.2k
        if (argument_column->is_nullable()) {
138
13.2k
            auto column_nested = static_cast<const ColumnNullable*>(argument_column.get())
139
13.2k
                                         ->get_nested_column_ptr();
140
13.2k
            const auto& null_map =
141
13.2k
                    static_cast<const ColumnNullable*>(argument_column.get())->get_null_map_data();
142
13.2k
            _filter->find_batch_nullable(*column_nested, sz, null_map, res_data_column->get_data(),
143
13.2k
                                         filter);
144
13.2k
        } else {
145
25
            _filter->find_batch(*argument_column, sz, res_data_column->get_data(), filter);
146
25
        }
147
148
        DCHECK(!_data_type->is_nullable());
149
13.2k
        result_column = std::move(res_data_column);
150
13.2k
        return Status::OK();
151
13.2k
    }
152
153
    std::shared_ptr<HybridSetBase> _filter;
154
    std::string _expr_name;
155
};
156
157
} // namespace doris