Coverage Report

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