Coverage Report

Created: 2026-06-12 08:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/format/table/equality_delete.cpp
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
#include "format/table/equality_delete.h"
19
20
#include "core/column/column_nullable.h"
21
#include "exprs/create_predicate_function.h"
22
23
namespace doris {
24
25
std::unique_ptr<EqualityDeleteBase> EqualityDeleteBase::get_delete_impl(
26
1.70k
        const Block* delete_block, const std::vector<int>& delete_col_ids) {
27
1.70k
    DCHECK_EQ(delete_block->columns(), delete_col_ids.size());
28
1.70k
    if (delete_block->columns() == 1) {
29
776
        return std::make_unique<SimpleEqualityDelete>(delete_block, delete_col_ids);
30
928
    } else {
31
928
        return std::make_unique<MultiEqualityDelete>(delete_block, delete_col_ids);
32
928
    }
33
1.70k
}
34
35
776
Status SimpleEqualityDelete::_build_set() {
36
776
    COUNTER_UPDATE(num_delete_rows, _delete_block->rows());
37
776
    if (_delete_block->columns() != 1) [[unlikely]] {
38
0
        return Status::InternalError("Simple equality delete can be only applied with one column");
39
0
    }
40
776
    auto& column_and_type = _delete_block->get_by_position(0);
41
776
    auto delete_column_type = remove_nullable(column_and_type.type)->get_primitive_type();
42
776
    _hybrid_set.reset(create_set(delete_column_type, _delete_block->rows(), false));
43
776
    _hybrid_set->insert_fixed_len(column_and_type.column, 0);
44
776
    return Status::OK();
45
776
}
46
47
Status SimpleEqualityDelete::filter_data_block(
48
        Block* data_block, const std::unordered_map<std::string, uint32_t>* col_name_to_block_idx,
49
        const std::unordered_map<int, std::string>& id_to_block_column_name,
50
1.13k
        IColumn::Filter& filter) {
51
1.13k
    SCOPED_TIMER(equality_delete_time);
52
1.13k
    DCHECK(_delete_col_ids.size() == 1);
53
1.13k
    auto column_field_id = _delete_col_ids[0];
54
55
1.13k
    const auto& block_col_name = id_to_block_column_name.at(column_field_id);
56
1.13k
    auto block_idx = col_name_to_block_idx->at(block_col_name);
57
58
1.13k
    auto column_and_type = data_block->get_by_position(block_idx);
59
60
1.13k
    size_t rows = data_block->rows();
61
    //     _filter: 1 => in _hybrid_set; 0 => not in _hybrid_set
62
1.13k
    if (_single_filter == nullptr) {
63
776
        _single_filter = std::make_unique<IColumn::Filter>(rows, 0);
64
776
    } else {
65
        // reset the array capacity and fill all elements using the 0
66
354
        _single_filter->assign(rows, UInt8(0));
67
354
    }
68
1.13k
    auto column = column_and_type.column->convert_to_full_column_if_const();
69
1.13k
    if (const auto* nullable = check_and_get_column<ColumnNullable>(column.get())) {
70
1.13k
        const NullMap& null_map = nullable->get_null_map_data();
71
1.13k
        _hybrid_set->find_batch_nullable(*remove_nullable(column), rows, null_map, *_single_filter);
72
1.13k
        if (_hybrid_set->contain_null()) {
73
0
            auto* filter_data = _single_filter->data();
74
0
            for (size_t i = 0; i < rows; ++i) {
75
0
                filter_data[i] = filter_data[i] || null_map[i];
76
0
            }
77
0
        }
78
1.13k
    } else {
79
0
        _hybrid_set->find_batch(*column, rows, *_single_filter);
80
0
    }
81
    // should reverse _filter
82
1.13k
    auto* filter_data = filter.data();
83
2.52k
    for (size_t i = 0; i < rows; ++i) {
84
1.39k
        filter_data[i] &= !_single_filter->data()[i];
85
1.39k
    }
86
1.13k
    return Status::OK();
87
1.13k
}
88
89
928
Status MultiEqualityDelete::_build_set() {
90
928
    COUNTER_UPDATE(num_delete_rows, _delete_block->rows());
91
928
    size_t rows = _delete_block->rows();
92
928
    _delete_hashes.clear();
93
928
    _delete_hashes.resize(rows, 0);
94
1.88k
    for (ColumnPtr column : _delete_block->get_columns()) {
95
1.88k
        column->update_hashes_with_value(_delete_hashes.data(), nullptr);
96
1.88k
    }
97
3.60k
    for (size_t i = 0; i < rows; ++i) {
98
2.68k
        _delete_hash_map.insert({_delete_hashes[i], i});
99
2.68k
    }
100
928
    _data_column_index.resize(_delete_block->columns());
101
928
    return Status::OK();
102
928
}
103
104
Status MultiEqualityDelete::filter_data_block(
105
        Block* data_block, const std::unordered_map<std::string, uint32_t>* col_name_to_block_idx,
106
        const std::unordered_map<int, std::string>& id_to_block_column_name,
107
1.35k
        IColumn::Filter& filter) {
108
1.35k
    SCOPED_TIMER(equality_delete_time);
109
1.35k
    DCHECK_EQ(_delete_block->get_columns_with_type_and_name().size(), _delete_col_ids.size());
110
1.35k
    size_t column_index = 0;
111
112
4.09k
    for (size_t idx = 0; idx < _delete_block->get_columns_with_type_and_name().size(); ++idx) {
113
2.74k
        auto delete_col = _delete_block->get_columns_with_type_and_name()[idx];
114
2.74k
        auto delete_col_id = _delete_col_ids[idx];
115
116
2.74k
        DCHECK(id_to_block_column_name.contains(delete_col_id));
117
2.74k
        const auto& block_column_name = id_to_block_column_name.at(delete_col_id);
118
2.74k
        if (!col_name_to_block_idx->contains(block_column_name)) [[unlikely]] {
119
0
            return Status::InternalError("Column '{}' not found in data block: {}",
120
0
                                         block_column_name, data_block->dump_structure());
121
0
        }
122
2.74k
        auto column_and_type =
123
2.74k
                data_block->safe_get_by_position(col_name_to_block_idx->at(block_column_name));
124
2.74k
        if (!delete_col.type->equals(*column_and_type.type)) [[unlikely]] {
125
0
            return Status::InternalError(
126
0
                    "Not support type change in column '{}', src type: {}, target type: {}",
127
0
                    block_column_name, delete_col.type->get_name(),
128
0
                    column_and_type.type->get_name());
129
0
        }
130
2.74k
        _data_column_index[column_index++] = col_name_to_block_idx->at(block_column_name);
131
2.74k
    }
132
1.35k
    size_t rows = data_block->rows();
133
1.35k
    _data_hashes.clear();
134
1.35k
    _data_hashes.resize(rows, 0);
135
2.74k
    for (size_t index : _data_column_index) {
136
2.74k
        data_block->get_by_position(index).column->update_hashes_with_value(_data_hashes.data(),
137
2.74k
                                                                            nullptr);
138
2.74k
    }
139
1.35k
    auto* filter_data = filter.data();
140
3.01k
    for (size_t i = 0; i < rows; ++i) {
141
1.65k
        for (auto beg = _delete_hash_map.lower_bound(_data_hashes[i]),
142
1.65k
                  end = _delete_hash_map.upper_bound(_data_hashes[i]);
143
1.90k
             beg != end; ++beg) {
144
980
            if (filter[i] && _equal(data_block, i, beg->second)) {
145
736
                filter_data[i] = 0;
146
736
                break;
147
736
            }
148
980
        }
149
1.65k
    }
150
151
1.35k
    return Status::OK();
152
1.35k
}
153
154
bool MultiEqualityDelete::_equal(Block* data_block, size_t data_row_index,
155
736
                                 size_t delete_row_index) {
156
2.23k
    for (size_t i = 0; i < _delete_block->columns(); ++i) {
157
1.49k
        ColumnPtr data_col = data_block->get_by_position(_data_column_index[i]).column;
158
1.49k
        ColumnPtr delete_col = _delete_block->get_by_position(i).column;
159
1.49k
        if (data_col->compare_at(data_row_index, delete_row_index, *delete_col, -1) != 0) {
160
0
            return false;
161
0
        }
162
1.49k
    }
163
736
    return true;
164
736
}
165
166
} // namespace doris