Coverage Report

Created: 2026-04-14 20:14

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