forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathequality_delete.cpp
More file actions
166 lines (150 loc) · 7.08 KB
/
Copy pathequality_delete.cpp
File metadata and controls
166 lines (150 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "format/table/equality_delete.h"
#include "core/column/column_nullable.h"
#include "exprs/create_predicate_function.h"
namespace doris {
std::unique_ptr<EqualityDeleteBase> EqualityDeleteBase::get_delete_impl(
const Block* delete_block, const std::vector<int>& delete_col_ids) {
DCHECK_EQ(delete_block->columns(), delete_col_ids.size());
if (delete_block->columns() == 1) {
return std::make_unique<SimpleEqualityDelete>(delete_block, delete_col_ids);
} else {
return std::make_unique<MultiEqualityDelete>(delete_block, delete_col_ids);
}
}
Status SimpleEqualityDelete::_build_set() {
COUNTER_UPDATE(num_delete_rows, _delete_block->rows());
if (_delete_block->columns() != 1) [[unlikely]] {
return Status::InternalError("Simple equality delete can be only applied with one column");
}
auto& column_and_type = _delete_block->get_by_position(0);
auto delete_column_type = remove_nullable(column_and_type.type)->get_primitive_type();
_hybrid_set.reset(create_set(delete_column_type, _delete_block->rows(), false));
_hybrid_set->insert_fixed_len(column_and_type.column, 0);
return Status::OK();
}
Status SimpleEqualityDelete::filter_data_block(
Block* data_block, const std::unordered_map<std::string, uint32_t>* col_name_to_block_idx,
const std::unordered_map<int, std::string>& id_to_block_column_name,
IColumn::Filter& filter) {
SCOPED_TIMER(equality_delete_time);
DCHECK(_delete_col_ids.size() == 1);
auto column_field_id = _delete_col_ids[0];
const auto& block_col_name = id_to_block_column_name.at(column_field_id);
auto block_idx = col_name_to_block_idx->at(block_col_name);
auto column_and_type = data_block->get_by_position(block_idx);
size_t rows = data_block->rows();
// _filter: 1 => in _hybrid_set; 0 => not in _hybrid_set
if (_single_filter == nullptr) {
_single_filter = std::make_unique<IColumn::Filter>(rows, 0);
} else {
// reset the array capacity and fill all elements using the 0
_single_filter->assign(rows, UInt8(0));
}
auto column = column_and_type.column->convert_to_full_column_if_const();
if (const auto* nullable = check_and_get_column<ColumnNullable>(column.get())) {
const NullMap& null_map = nullable->get_null_map_data();
_hybrid_set->find_batch_nullable(*remove_nullable(column), rows, null_map, *_single_filter);
if (_hybrid_set->contain_null()) {
auto* filter_data = _single_filter->data();
for (size_t i = 0; i < rows; ++i) {
filter_data[i] = filter_data[i] || null_map[i];
}
}
} else {
_hybrid_set->find_batch(*column, rows, *_single_filter);
}
// should reverse _filter
auto* filter_data = filter.data();
for (size_t i = 0; i < rows; ++i) {
filter_data[i] &= !_single_filter->data()[i];
}
return Status::OK();
}
Status MultiEqualityDelete::_build_set() {
COUNTER_UPDATE(num_delete_rows, _delete_block->rows());
size_t rows = _delete_block->rows();
_delete_hashes.clear();
_delete_hashes.resize(rows, 0);
for (ColumnPtr column : _delete_block->get_columns()) {
column->update_hashes_with_value(_delete_hashes.data(), nullptr);
}
for (size_t i = 0; i < rows; ++i) {
_delete_hash_map.insert({_delete_hashes[i], i});
}
_data_column_index.resize(_delete_block->columns());
return Status::OK();
}
Status MultiEqualityDelete::filter_data_block(
Block* data_block, const std::unordered_map<std::string, uint32_t>* col_name_to_block_idx,
const std::unordered_map<int, std::string>& id_to_block_column_name,
IColumn::Filter& filter) {
SCOPED_TIMER(equality_delete_time);
DCHECK_EQ(_delete_block->get_columns_with_type_and_name().size(), _delete_col_ids.size());
size_t column_index = 0;
for (size_t idx = 0; idx < _delete_block->get_columns_with_type_and_name().size(); ++idx) {
auto delete_col = _delete_block->get_columns_with_type_and_name()[idx];
auto delete_col_id = _delete_col_ids[idx];
DCHECK(id_to_block_column_name.contains(delete_col_id));
const auto& block_column_name = id_to_block_column_name.at(delete_col_id);
if (!col_name_to_block_idx->contains(block_column_name)) [[unlikely]] {
return Status::InternalError("Column '{}' not found in data block: {}",
block_column_name, data_block->dump_structure());
}
auto column_and_type =
data_block->safe_get_by_position(col_name_to_block_idx->at(block_column_name));
if (!delete_col.type->equals(*column_and_type.type)) [[unlikely]] {
return Status::InternalError(
"Not support type change in column '{}', src type: {}, target type: {}",
block_column_name, delete_col.type->get_name(),
column_and_type.type->get_name());
}
_data_column_index[column_index++] = col_name_to_block_idx->at(block_column_name);
}
size_t rows = data_block->rows();
_data_hashes.clear();
_data_hashes.resize(rows, 0);
for (size_t index : _data_column_index) {
data_block->get_by_position(index).column->update_hashes_with_value(_data_hashes.data(),
nullptr);
}
auto* filter_data = filter.data();
for (size_t i = 0; i < rows; ++i) {
for (auto beg = _delete_hash_map.lower_bound(_data_hashes[i]),
end = _delete_hash_map.upper_bound(_data_hashes[i]);
beg != end; ++beg) {
if (filter[i] && _equal(data_block, i, beg->second)) {
filter_data[i] = 0;
break;
}
}
}
return Status::OK();
}
bool MultiEqualityDelete::_equal(Block* data_block, size_t data_row_index,
size_t delete_row_index) {
for (size_t i = 0; i < _delete_block->columns(); ++i) {
ColumnPtr data_col = data_block->get_by_position(_data_column_index[i]).column;
ColumnPtr delete_col = _delete_block->get_by_position(i).column;
if (data_col->compare_at(data_row_index, delete_row_index, *delete_col, -1) != 0) {
return false;
}
}
return true;
}
} // namespace doris