Coverage Report

Created: 2026-08-15 03:16

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