be/src/core/column/column.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 | | // This file is copied from |
18 | | // https://github.com/ClickHouse/ClickHouse/blob/master/src/Columns/IColumn.cpp |
19 | | // and modified by Doris |
20 | | |
21 | | #include "core/column/column.h" |
22 | | |
23 | | #include "core/column/column_const.h" |
24 | | #include "core/column/column_nullable.h" |
25 | | #include "core/data_type/data_type.h" |
26 | | #include "exec/sort/sort_block.h" |
27 | | #include "util/simd/bits.h" |
28 | | |
29 | | namespace doris { |
30 | | |
31 | 3.18k | std::string IColumn::dump_structure() const { |
32 | 3.18k | std::stringstream res; |
33 | 3.18k | res << get_name() << "(size = " << size(); |
34 | | |
35 | 3.18k | ColumnCallback callback = [&](const IColumn& subcolumn) { |
36 | 2.81k | res << ", " << subcolumn.dump_structure(); |
37 | 2.81k | }; |
38 | | |
39 | 3.18k | for_each_subcolumn(callback); |
40 | | |
41 | 3.18k | res << ")"; |
42 | 3.18k | return res.str(); |
43 | 3.18k | } |
44 | | |
45 | 418k | int IColumn::count_const_column() const { |
46 | 418k | int count = is_column_const(*this) ? 1 : 0; |
47 | 418k | ColumnCallback callback = [&](const IColumn& subcolumn) { |
48 | 199k | count += subcolumn.count_const_column(); |
49 | 199k | }; |
50 | 418k | for_each_subcolumn(callback); |
51 | 418k | return count; |
52 | 418k | } |
53 | | |
54 | 219k | bool IColumn::const_nested_check() const { |
55 | 219k | auto const_cnt = count_const_column(); |
56 | 219k | if (const_cnt == 0) { |
57 | 219k | return true; |
58 | 219k | } |
59 | | // A const column is not allowed to be nested; it may only appear as the outermost (top-level) column. |
60 | 20 | return const_cnt == 1 && is_column_const(*this); |
61 | 219k | } |
62 | | |
63 | 320k | bool IColumn::column_boolean_check() const { |
64 | 320k | if (const auto* col_nullable = check_and_get_column<ColumnNullable>(*this)) { |
65 | | // for column nullable, we need to skip null values check |
66 | 98.3k | const auto& nested_col = col_nullable->get_nested_column(); |
67 | 98.3k | const auto& null_map = col_nullable->get_null_map_data(); |
68 | 98.3k | Filter not_null_filter; |
69 | 98.3k | not_null_filter.reserve(nested_col.size()); |
70 | 98.3k | size_t result_size_hint = 0; |
71 | 1.36M | for (size_t i = 0; i < null_map.size(); ++i) { |
72 | 1.26M | not_null_filter.push_back(null_map[i] == 0); |
73 | 1.26M | if (null_map[i] == 0) { |
74 | 1.19M | ++result_size_hint; |
75 | 1.19M | } |
76 | 1.26M | } |
77 | 98.3k | if (result_size_hint == nested_col.size()) { |
78 | | // Filtering an all-non-null column preserves every row, so copying nested payloads |
79 | | // only for validation can exhaust memory for large complex columns. |
80 | 50.7k | return nested_col.column_boolean_check(); |
81 | 50.7k | } |
82 | 47.6k | auto nested_col_skip_null = nested_col.filter(not_null_filter, result_size_hint); |
83 | 47.6k | return nested_col_skip_null->column_boolean_check(); |
84 | 98.3k | } |
85 | | |
86 | 221k | auto check_boolean_is_zero_or_one = [&](const IColumn& subcolumn) { |
87 | 221k | if (const auto* column_boolean = check_and_get_column<ColumnBool>(subcolumn)) { |
88 | 74.6k | for (size_t i = 0; i < column_boolean->size(); ++i) { |
89 | 66.6k | auto val = column_boolean->get_element(i); |
90 | 66.6k | if (val != 0 && val != 1) { |
91 | 2 | LOG_WARNING("column boolean check failed at index {} with value {}", i, val) |
92 | 2 | .tag("column structure", subcolumn.dump_structure()); |
93 | 2 | return false; |
94 | 2 | } |
95 | 66.6k | } |
96 | 8.01k | } |
97 | 221k | return true; |
98 | 221k | }; |
99 | | |
100 | 221k | bool is_valid = check_boolean_is_zero_or_one(*this); |
101 | 221k | ColumnCallback callback = [&](const IColumn& subcolumn) { |
102 | 2.48k | if (!subcolumn.column_boolean_check()) { |
103 | 0 | is_valid = false; |
104 | 0 | } |
105 | 2.48k | }; |
106 | 221k | for_each_subcolumn(callback); |
107 | 221k | return is_valid; |
108 | 320k | } |
109 | | |
110 | 418k | bool IColumn::null_map_check() const { |
111 | 418k | auto check_null_map_is_zero_or_one = [&](const IColumn& subcolumn) { |
112 | 418k | if (is_column_nullable(subcolumn)) { |
113 | 98.3k | const auto& nullable_col = assert_cast<const ColumnNullable&>(subcolumn); |
114 | 98.3k | const auto& null_map = nullable_col.get_null_map_data(); |
115 | 1.36M | for (size_t i = 0; i < null_map.size(); ++i) { |
116 | 1.26M | if (null_map[i] != 0 && null_map[i] != 1) { |
117 | 3 | LOG_WARNING("null map check failed at index {} with value {}", i, null_map[i]) |
118 | 3 | .tag("column structure", subcolumn.dump_structure()); |
119 | 3 | return false; |
120 | 3 | } |
121 | 1.26M | } |
122 | 98.3k | } |
123 | 418k | return true; |
124 | 418k | }; |
125 | | |
126 | 418k | bool is_valid = check_null_map_is_zero_or_one(*this); |
127 | 418k | ColumnCallback callback = [&](const IColumn& subcolumn) { |
128 | 199k | if (!subcolumn.null_map_check()) { |
129 | 2 | is_valid = false; |
130 | 2 | } |
131 | 199k | }; |
132 | 418k | for_each_subcolumn(callback); |
133 | 418k | return is_valid; |
134 | 418k | } |
135 | | |
136 | 219k | Status IColumn::column_self_check() const { |
137 | 219k | #ifndef NDEBUG |
138 | | // check const nested |
139 | 219k | if (!const_nested_check()) { |
140 | 1 | return Status::InternalError("const nested check failed for column: {} , {}", get_name(), |
141 | 1 | dump_structure()); |
142 | 1 | } |
143 | | // check null map |
144 | 219k | if (!null_map_check()) { |
145 | 1 | return Status::InternalError("null map check failed for column: {}", get_name()); |
146 | 1 | } |
147 | | // check boolean column |
148 | 219k | if (!column_boolean_check()) { |
149 | 0 | return Status::InternalError("boolean column check failed for column: {}", get_name()); |
150 | 0 | } |
151 | 219k | #endif |
152 | 219k | return Status::OK(); |
153 | 219k | } |
154 | | |
155 | 0 | void IColumn::insert_from(const IColumn& src, size_t n) { |
156 | 0 | insert(src[n]); |
157 | 0 | } |
158 | | |
159 | | void IColumn::sort_column(const ColumnSorter* sorter, EqualFlags& flags, |
160 | 0 | IColumn::Permutation& perms, EqualRange& range, bool last_column) const { |
161 | 0 | sorter->sort_column(static_cast<const IColumn&>(*this), flags, perms, range, last_column); |
162 | 0 | } |
163 | | |
164 | | void IColumn::compare_internal(size_t rhs_row_id, const IColumn& rhs, int nan_direction_hint, |
165 | | int direction, std::vector<uint8_t>& cmp_res, |
166 | 3 | uint8_t* __restrict filter) const { |
167 | 3 | auto sz = this->size(); |
168 | 3 | DCHECK(cmp_res.size() == sz); |
169 | 3 | size_t begin = simd::find_zero(cmp_res, 0); |
170 | 6 | while (begin < sz) { |
171 | 3 | size_t end = simd::find_one(cmp_res, begin + 1); |
172 | 6 | for (size_t row_id = begin; row_id < end; row_id++) { |
173 | 3 | int res = this->compare_at(row_id, rhs_row_id, rhs, nan_direction_hint); |
174 | 3 | if (res * direction < 0) { |
175 | 1 | filter[row_id] = 1; |
176 | 1 | cmp_res[row_id] = 1; |
177 | 2 | } else if (res * direction > 0) { |
178 | 1 | cmp_res[row_id] = 1; |
179 | 1 | } |
180 | 3 | } |
181 | 3 | begin = simd::find_zero(cmp_res, end + 1); |
182 | 3 | } |
183 | 3 | } |
184 | | |
185 | | void IColumn::serialize_with_nullable(StringRef* keys, size_t num_rows, const bool has_null, |
186 | 0 | const uint8_t* __restrict null_map) const { |
187 | 0 | if (has_null) { |
188 | 0 | for (size_t i = 0; i < num_rows; ++i) { |
189 | 0 | char* dest = const_cast<char*>(keys[i].data + keys[i].size); |
190 | 0 | if (null_map[i]) { |
191 | | // is null |
192 | 0 | *dest = true; |
193 | 0 | keys[i].size += sizeof(UInt8); |
194 | 0 | continue; |
195 | 0 | } |
196 | | // not null |
197 | 0 | *dest = false; |
198 | 0 | keys[i].size += sizeof(UInt8) + serialize_impl(dest + sizeof(UInt8), i); |
199 | 0 | } |
200 | 0 | } else { |
201 | 0 | for (size_t i = 0; i < num_rows; ++i) { |
202 | 0 | char* dest = const_cast<char*>(keys[i].data + keys[i].size); |
203 | 0 | *dest = false; |
204 | 0 | keys[i].size += sizeof(UInt8) + serialize_impl(dest + sizeof(UInt8), i); |
205 | 0 | } |
206 | 0 | } |
207 | 0 | } |
208 | | |
209 | | void IColumn::deserialize_with_nullable(StringRef* keys, const size_t num_rows, |
210 | 0 | PaddedPODArray<UInt8>& null_map) { |
211 | 0 | for (size_t i = 0; i != num_rows; ++i) { |
212 | 0 | UInt8 is_null = *reinterpret_cast<const UInt8*>(keys[i].data); |
213 | 0 | null_map.push_back(is_null); |
214 | 0 | keys[i].data += sizeof(UInt8); |
215 | 0 | keys[i].size -= sizeof(UInt8); |
216 | 0 | if (is_null) { |
217 | 0 | insert_default(); |
218 | 0 | continue; |
219 | 0 | } |
220 | 0 | auto sz = deserialize_impl(keys[i].data); |
221 | 0 | keys[i].data += sz; |
222 | 0 | keys[i].size -= sz; |
223 | 0 | } |
224 | 0 | } |
225 | | |
226 | 515k | bool is_column_nullable(const IColumn& column) { |
227 | 515k | return is_column<ColumnNullable>(column); |
228 | 515k | } |
229 | | |
230 | 73.8M | bool is_column_const(const IColumn& column) { |
231 | 73.8M | return is_column<ColumnConst>(column); |
232 | 73.8M | } |
233 | | |
234 | 829k | void IColumn::check_const_only_in_top_level() const { |
235 | 1.67M | ColumnCallback throw_if_const = [&](const IColumn& column) { |
236 | 1.67M | if (is_column_const(column)) { |
237 | 49 | throw doris::Exception(ErrorCode::INTERNAL_ERROR, |
238 | 49 | "const column is not allowed to be nested, but got {}", |
239 | 49 | column.get_name()); |
240 | 49 | } |
241 | 1.67M | }; |
242 | 829k | for_each_subcolumn(throw_if_const); |
243 | 829k | } |
244 | | |
245 | | } // namespace doris |