Coverage Report

Created: 2026-03-13 09:58

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