Coverage Report

Created: 2026-06-11 19:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/core/block/block.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/Core/Block.cpp
19
// and modified by Doris
20
21
#include "core/block/block.h"
22
23
#include <fmt/format.h>
24
#include <gen_cpp/data.pb.h>
25
#include <glog/logging.h>
26
#include <snappy.h>
27
#include <streamvbyte.h>
28
29
#include <algorithm>
30
#include <cassert>
31
#include <iomanip>
32
#include <limits>
33
#include <ranges>
34
35
#include "agent/be_exec_version_manager.h"
36
#include "common/compiler_util.h" // IWYU pragma: keep
37
#include "common/logging.h"
38
#include "common/status.h"
39
#include "core/assert_cast.h"
40
#include "core/column/column.h"
41
#include "core/column/column_const.h"
42
#include "core/column/column_nothing.h"
43
#include "core/column/column_nullable.h"
44
#include "core/column/column_vector.h"
45
#include "core/data_type/data_type_factory.hpp"
46
#include "core/data_type/data_type_nullable.h"
47
#include "core/data_type_serde/data_type_serde.h"
48
#include "runtime/descriptors.h"
49
#include "runtime/runtime_profile.h"
50
#include "runtime/thread_context.h"
51
#include "util/block_compression.h"
52
#include "util/faststring.h"
53
#include "util/simd/bits.h"
54
#include "util/slice.h"
55
56
class SipHash;
57
58
namespace doris::segment_v2 {
59
enum CompressionTypePB : int;
60
} // namespace doris::segment_v2
61
namespace doris {
62
template <typename T>
63
void clear_blocks(moodycamel::ConcurrentQueue<T>& blocks,
64
2
                  RuntimeProfile::Counter* memory_used_counter = nullptr) {
65
2
    T block;
66
6
    while (blocks.try_dequeue(block)) {
67
4
        if (memory_used_counter) {
68
4
            if constexpr (std::is_same_v<T, Block>) {
69
2
                memory_used_counter->update(-block.allocated_bytes());
70
2
            } else {
71
2
                memory_used_counter->update(-block->allocated_bytes());
72
2
            }
73
4
        }
74
4
    }
75
2
}
_ZN5doris12clear_blocksINS_5BlockEEEvRN10moodycamel15ConcurrentQueueIT_NS2_28ConcurrentQueueDefaultTraitsEEEPNS_14RuntimeProfile7CounterE
Line
Count
Source
64
1
                  RuntimeProfile::Counter* memory_used_counter = nullptr) {
65
1
    T block;
66
3
    while (blocks.try_dequeue(block)) {
67
2
        if (memory_used_counter) {
68
2
            if constexpr (std::is_same_v<T, Block>) {
69
2
                memory_used_counter->update(-block.allocated_bytes());
70
            } else {
71
                memory_used_counter->update(-block->allocated_bytes());
72
            }
73
2
        }
74
2
    }
75
1
}
_ZN5doris12clear_blocksISt10unique_ptrINS_5BlockESt14default_deleteIS2_EEEEvRN10moodycamel15ConcurrentQueueIT_NS6_28ConcurrentQueueDefaultTraitsEEEPNS_14RuntimeProfile7CounterE
Line
Count
Source
64
1
                  RuntimeProfile::Counter* memory_used_counter = nullptr) {
65
1
    T block;
66
3
    while (blocks.try_dequeue(block)) {
67
2
        if (memory_used_counter) {
68
            if constexpr (std::is_same_v<T, Block>) {
69
                memory_used_counter->update(-block.allocated_bytes());
70
2
            } else {
71
2
                memory_used_counter->update(-block->allocated_bytes());
72
2
            }
73
2
        }
74
2
    }
75
1
}
76
77
template void clear_blocks<Block>(moodycamel::ConcurrentQueue<Block>&,
78
                                  RuntimeProfile::Counter* memory_used_counter);
79
template void clear_blocks<BlockUPtr>(moodycamel::ConcurrentQueue<BlockUPtr>&,
80
                                      RuntimeProfile::Counter* memory_used_counter);
81
82
namespace {
83
84
// The no-clone fast path is only safe when the whole column tree is uniquely
85
// owned. A composite column with shared children still needs COW detachment.
86
32.5k
bool is_recursively_exclusive(const IColumn& column) {
87
32.5k
    if (!column.is_exclusive()) {
88
13
        return false;
89
13
    }
90
91
32.5k
    bool exclusive = true;
92
32.5k
    IColumn::ColumnCallback callback = [&](IColumn::WrappedPtr& subcolumn) {
93
27.7k
        if (!exclusive) {
94
0
            return;
95
0
        }
96
27.7k
        const ColumnPtr& subcolumn_ptr = const_cast<const IColumn::WrappedPtr&>(subcolumn);
97
27.7k
        DCHECK(subcolumn_ptr);
98
27.7k
        exclusive = is_recursively_exclusive(*subcolumn_ptr);
99
27.7k
    };
100
    // `for_each_subcolumn` only exposes a mutable callback type. This callback
101
    // only reads the wrapped pointers and never calls the non-const accessors.
102
32.5k
    const_cast<IColumn&>(column).for_each_subcolumn(callback);
103
32.5k
    return exclusive;
104
32.5k
}
105
106
// Acquire one live Block slot transactionally. Shared columns are detached while
107
// the original slot is still intact, so a clone failure cannot leave Block with
108
// a moved-from/null column. Exclusive column trees keep the stealing fast path.
109
4.79k
MutableColumnPtr scoped_mutate_column(ColumnPtr& column, const DataTypePtr& type) {
110
4.79k
    DCHECK(type);
111
4.79k
    if (!column) {
112
1
        return type->create_column();
113
1
    }
114
115
4.79k
    MutableColumnPtr mutable_column;
116
4.79k
    if (is_recursively_exclusive(*column)) {
117
4.77k
        mutable_column = std::move(*column).mutate();
118
4.77k
    } else {
119
13
        mutable_column = IColumn::mutate(column);
120
13
    }
121
4.79k
    column = nullptr;
122
4.79k
    return mutable_column;
123
4.79k
}
124
125
} // namespace
126
127
4.36k
Block::Block(std::initializer_list<ColumnWithTypeAndName> il) : data {il} {}
128
129
257k
Block::Block(ColumnsWithTypeAndName data_) : data {std::move(data_)} {}
130
131
56.0k
Block::Block(const std::vector<SlotDescriptor*>& slots, size_t block_size) {
132
271k
    for (auto* const slot_desc : slots) {
133
271k
        auto column_ptr = slot_desc->get_empty_mutable_column();
134
271k
        column_ptr->reserve(block_size);
135
271k
        insert(ColumnWithTypeAndName(std::move(column_ptr), slot_desc->get_data_type_ptr(),
136
271k
                                     slot_desc->col_name()));
137
271k
    }
138
56.0k
}
139
140
1
Block::Block(const std::vector<SlotDescriptor>& slots, size_t block_size) {
141
1
    std::vector<SlotDescriptor*> slot_ptrs(slots.size());
142
3
    for (size_t i = 0; i < slots.size(); ++i) {
143
        // Slots remain unmodified and are used to read column information; const_cast can be employed.
144
        // used in src/exec/rowid_fetcher.cpp
145
2
        slot_ptrs[i] = const_cast<SlotDescriptor*>(&slots[i]);
146
2
    }
147
1
    *this = Block(slot_ptrs, block_size);
148
1
}
149
150
Status Block::deserialize(const PBlock& pblock, size_t* uncompressed_bytes,
151
994
                          int64_t* decompress_time) {
152
994
    swap(Block());
153
994
    int be_exec_version = pblock.has_be_exec_version() ? pblock.be_exec_version() : 0;
154
994
    RETURN_IF_ERROR(BeExecVersionManager::check_be_exec_version(be_exec_version));
155
156
994
    const char* buf = nullptr;
157
994
    std::string compression_scratch;
158
994
    if (pblock.compressed()) {
159
        // Decompress
160
483
        SCOPED_RAW_TIMER(decompress_time);
161
483
        const char* compressed_data = pblock.column_values().c_str();
162
483
        size_t compressed_size = pblock.column_values().size();
163
483
        size_t uncompressed_size = 0;
164
483
        if (pblock.has_compression_type() && pblock.has_uncompressed_size()) {
165
483
            BlockCompressionCodec* codec;
166
483
            RETURN_IF_ERROR(get_block_compression_codec(pblock.compression_type(), &codec));
167
483
            uncompressed_size = pblock.uncompressed_size();
168
            // Should also use allocator to allocate memory here.
169
483
            compression_scratch.resize(uncompressed_size);
170
483
            Slice decompressed_slice(compression_scratch);
171
483
            RETURN_IF_ERROR(codec->decompress(Slice(compressed_data, compressed_size),
172
483
                                              &decompressed_slice));
173
483
            DCHECK(uncompressed_size == decompressed_slice.size);
174
483
        } else {
175
0
            bool success = snappy::GetUncompressedLength(compressed_data, compressed_size,
176
0
                                                         &uncompressed_size);
177
0
            DCHECK(success) << "snappy::GetUncompressedLength failed";
178
0
            compression_scratch.resize(uncompressed_size);
179
0
            success = snappy::RawUncompress(compressed_data, compressed_size,
180
0
                                            compression_scratch.data());
181
0
            DCHECK(success) << "snappy::RawUncompress failed";
182
0
        }
183
483
        *uncompressed_bytes = uncompressed_size;
184
483
        buf = compression_scratch.data();
185
511
    } else {
186
511
        buf = pblock.column_values().data();
187
511
    }
188
189
1.57k
    for (const auto& pcol_meta : pblock.column_metas()) {
190
1.57k
        DataTypePtr type = DataTypeFactory::instance().create_data_type(pcol_meta);
191
1.57k
        MutableColumnPtr data_column = type->create_column();
192
        // Here will try to allocate large memory, should return error if failed.
193
1.57k
        RETURN_IF_CATCH_EXCEPTION(
194
1.57k
                buf = type->deserialize(buf, &data_column, pblock.be_exec_version()));
195
1.57k
        data.emplace_back(data_column->get_ptr(), type, pcol_meta.name());
196
1.57k
    }
197
198
994
    return Status::OK();
199
994
}
200
201
13.4k
void Block::reserve(size_t count) {
202
13.4k
    data.reserve(count);
203
13.4k
}
204
205
4
void Block::insert(size_t position, const ColumnWithTypeAndName& elem) {
206
4
    if (position > data.size()) {
207
1
        throw Exception(ErrorCode::INTERNAL_ERROR,
208
1
                        "invalid input position, position={}, data.size={}, names={}", position,
209
1
                        data.size(), dump_names());
210
1
    }
211
212
3
    data.emplace(data.begin() + position, elem);
213
3
}
214
215
3
void Block::insert(size_t position, ColumnWithTypeAndName&& elem) {
216
3
    if (position > data.size()) {
217
1
        throw Exception(ErrorCode::INTERNAL_ERROR,
218
1
                        "invalid input position, position={}, data.size={}, names={}", position,
219
1
                        data.size(), dump_names());
220
1
    }
221
222
2
    data.emplace(data.begin() + position, std::move(elem));
223
2
}
224
225
8.09k
void Block::clear_names() {
226
177k
    for (auto& entry : data) {
227
177k
        entry.name.clear();
228
177k
    }
229
8.09k
}
230
231
14.5k
void Block::insert(const ColumnWithTypeAndName& elem) {
232
14.5k
    data.emplace_back(elem);
233
14.5k
}
234
235
1.00M
void Block::insert(ColumnWithTypeAndName&& elem) {
236
1.00M
    data.emplace_back(std::move(elem));
237
1.00M
}
238
239
3
void Block::erase(const std::set<size_t>& positions) {
240
3
    for (unsigned long position : std::ranges::reverse_view(positions)) {
241
2
        erase(position);
242
2
    }
243
3
}
244
245
712
void Block::erase_tail(size_t start) {
246
712
    DCHECK(start <= data.size()) << fmt::format(
247
0
            "Position out of bound in Block::erase(), max position = {}", data.size());
248
712
    data.erase(data.begin() + start, data.end());
249
712
}
250
251
124k
void Block::erase(size_t position) {
252
124k
    DCHECK(!data.empty()) << "Block is empty";
253
124k
    DCHECK_LT(position, data.size()) << fmt::format(
254
0
            "Position out of bound in Block::erase(), max position = {}", data.size() - 1);
255
256
124k
    erase_impl(position);
257
124k
}
258
259
124k
void Block::erase_impl(size_t position) {
260
124k
    data.erase(data.begin() + position);
261
124k
}
262
263
76.0k
ColumnWithTypeAndName& Block::safe_get_by_position(size_t position) {
264
76.0k
    if (position >= data.size()) {
265
0
        throw Exception(ErrorCode::INTERNAL_ERROR,
266
0
                        "invalid input position, position={}, data.size={}, names={}", position,
267
0
                        data.size(), dump_names());
268
0
    }
269
76.0k
    return data[position];
270
76.0k
}
271
272
109
const ColumnWithTypeAndName& Block::safe_get_by_position(size_t position) const {
273
109
    if (position >= data.size()) {
274
0
        throw Exception(ErrorCode::INTERNAL_ERROR,
275
0
                        "invalid input position, position={}, data.size={}, names={}", position,
276
0
                        data.size(), dump_names());
277
0
    }
278
109
    return data[position];
279
109
}
280
281
86
int Block::get_position_by_name(const std::string& name) const {
282
1.03k
    for (int i = 0; i < data.size(); i++) {
283
1.03k
        if (data[i].name == name) {
284
85
            return i;
285
85
        }
286
1.03k
    }
287
1
    return -1;
288
86
}
289
290
5
void Block::check_number_of_rows(bool allow_null_columns) const {
291
5
    ssize_t rows = -1;
292
9
    for (const auto& elem : data) {
293
9
        if (!elem.column && allow_null_columns) {
294
2
            continue;
295
2
        }
296
297
7
        if (!elem.column) {
298
1
            throw Exception(ErrorCode::INTERNAL_ERROR,
299
1
                            "Column {} in block is nullptr, in method check_number_of_rows.",
300
1
                            elem.name);
301
1
        }
302
303
6
        ssize_t size = elem.column->size();
304
305
6
        if (rows == -1) {
306
5
            rows = size;
307
5
        } else if (rows != size) {
308
1
            throw Exception(ErrorCode::INTERNAL_ERROR, "Sizes of columns doesn't match, block={}",
309
1
                            dump_structure());
310
1
        }
311
6
    }
312
5
}
313
314
3.10M
Status Block::check_type_and_column() const {
315
3.10M
#ifndef NDEBUG
316
3.10M
    for (const auto& elem : data) {
317
211k
        if (!elem.column) {
318
0
            continue;
319
0
        }
320
211k
        if (!elem.type) {
321
0
            continue;
322
0
        }
323
324
        // ColumnNothing is a special column type, it is used to represent a column that
325
        // is not materialized, so we don't need to check it.
326
211k
        if (check_and_get_column<ColumnNothing>(elem.column.get())) {
327
0
            continue;
328
0
        }
329
330
211k
        const auto& type = elem.type;
331
211k
        const auto& column = elem.column;
332
333
211k
        RETURN_IF_ERROR(column->column_self_check());
334
211k
        auto st = type->check_column(*column);
335
211k
        if (!st.ok()) {
336
1
            return Status::InternalError(
337
1
                    "Column {} in block is not compatible with its column type :{}, data type :{}, "
338
1
                    "error: {}",
339
1
                    elem.name, column->get_name(), type->get_name(), st.msg());
340
1
        }
341
211k
    }
342
3.10M
#endif
343
3.10M
    return Status::OK();
344
3.10M
}
345
346
50.9M
size_t Block::rows() const {
347
50.9M
    for (const auto& elem : data) {
348
46.4M
        if (elem.column) {
349
46.4M
            return elem.column->size();
350
46.4M
        }
351
46.4M
    }
352
353
4.50M
    return 0;
354
50.9M
}
355
356
6
void Block::set_num_rows(size_t length) {
357
6
    if (rows() > length) {
358
4
        for (auto& elem : data) {
359
4
            if (elem.column) {
360
4
                elem.column = elem.column->shrink(length);
361
4
            }
362
4
        }
363
4
    }
364
6
}
365
366
1
void Block::skip_num_rows(int64_t& length) {
367
1
    auto origin_rows = rows();
368
1
    if (origin_rows <= length) {
369
0
        clear();
370
0
        length -= origin_rows;
371
1
    } else {
372
1
        for (auto& elem : data) {
373
1
            if (elem.column) {
374
1
                elem.column = elem.column->cut(length, origin_rows - length);
375
1
            }
376
1
        }
377
1
    }
378
1
}
379
380
12.8k
size_t Block::bytes() const {
381
12.8k
    size_t res = 0;
382
27.2k
    for (const auto& elem : data) {
383
27.2k
        if (!elem.column) {
384
0
            std::stringstream ss;
385
0
            for (const auto& e : data) {
386
0
                ss << e.name + " ";
387
0
            }
388
0
            throw Exception(ErrorCode::INTERNAL_ERROR,
389
0
                            "Column {} in block is nullptr, in method bytes. All Columns are {}",
390
0
                            elem.name, ss.str());
391
0
        }
392
27.2k
        res += elem.column->byte_size();
393
27.2k
    }
394
395
12.8k
    return res;
396
12.8k
}
397
398
194k
size_t Block::allocated_bytes() const {
399
194k
    size_t res = 0;
400
380k
    for (const auto& elem : data) {
401
380k
        if (!elem.column) {
402
            // Sometimes if expr failed, then there will be a nullptr
403
            // column left in the block.
404
1
            continue;
405
1
        }
406
380k
        res += elem.column->allocated_bytes();
407
380k
    }
408
409
194k
    return res;
410
194k
}
411
412
8
std::string Block::dump_names() const {
413
8
    std::string out;
414
23
    for (auto it = data.begin(); it != data.end(); ++it) {
415
15
        if (it != data.begin()) {
416
7
            out += ", ";
417
7
        }
418
15
        out += it->name;
419
15
    }
420
8
    return out;
421
8
}
422
423
7
std::string Block::dump_types() const {
424
7
    std::string out;
425
21
    for (auto it = data.begin(); it != data.end(); ++it) {
426
14
        if (it != data.begin()) {
427
7
            out += ", ";
428
7
        }
429
14
        out += it->type->get_name();
430
14
    }
431
7
    return out;
432
7
}
433
434
31
std::string Block::dump_data_json(size_t begin, size_t row_limit, bool allow_null_mismatch) const {
435
31
    std::stringstream ss;
436
437
31
    std::vector<std::string> headers;
438
31
    headers.reserve(columns());
439
46
    for (const auto& it : data) {
440
        // fmt::format is from the {fmt} library, you might be using std::format in C++20
441
        // If not, you can build the string with a stringstream as a fallback.
442
46
        headers.push_back(fmt::format("{}({})", it.name, it.type->get_name()));
443
46
    }
444
445
31
    size_t start_row = std::min(begin, rows());
446
31
    size_t end_row = std::min(rows(), begin + row_limit);
447
448
31
    auto format_options = DataTypeSerDe::get_default_format_options();
449
31
    auto time_zone = cctz::utc_time_zone();
450
31
    format_options.timezone = &time_zone;
451
452
31
    ss << "[";
453
3.59k
    for (size_t row_num = start_row; row_num < end_row; ++row_num) {
454
3.56k
        if (row_num > start_row) {
455
3.53k
            ss << ",";
456
3.53k
        }
457
3.56k
        ss << "{";
458
8.33k
        for (size_t i = 0; i < columns(); ++i) {
459
4.77k
            if (i > 0) {
460
1.21k
                ss << ",";
461
1.21k
            }
462
4.77k
            ss << "\"" << headers[i] << "\":";
463
4.77k
            std::string s;
464
465
            // This value-extraction logic is preserved from your original function
466
            // to maintain consistency, especially for handling nullability mismatches.
467
4.77k
            if (data[i].column && data[i].type->is_nullable() && !data[i].column->is_nullable()) {
468
                // This branch handles a specific internal representation of nullable columns.
469
                // The original code would assert here if allow_null_mismatch is false.
470
0
                assert(allow_null_mismatch);
471
0
                s = assert_cast<const DataTypeNullable*>(data[i].type.get())
472
0
                            ->get_nested_type()
473
0
                            ->to_string(*data[i].column, row_num, format_options);
474
4.77k
            } else {
475
                // This is the standard path. The to_string method is expected to correctly
476
                // handle all cases, including when the column is null (e.g., by returning "NULL").
477
4.77k
                s = data[i].to_string(row_num, format_options);
478
4.77k
            }
479
4.77k
            ss << "\"" << s << "\"";
480
4.77k
        }
481
3.56k
        ss << "}";
482
3.56k
    }
483
31
    ss << "]";
484
31
    return ss.str();
485
31
}
486
487
858
std::string Block::dump_data(size_t begin, size_t row_limit, bool allow_null_mismatch) const {
488
858
    std::vector<std::string> headers;
489
858
    std::vector<int> headers_size;
490
2.10k
    for (const auto& it : data) {
491
2.10k
        std::string s = fmt::format("{}({})", it.name, it.type->get_name());
492
2.10k
        headers_size.push_back(s.size() > 15 ? (int)s.size() : 15);
493
2.10k
        headers.emplace_back(s);
494
2.10k
    }
495
496
858
    std::stringstream out;
497
    // header upper line
498
2.16k
    auto line = [&]() {
499
8.07k
        for (size_t i = 0; i < columns(); ++i) {
500
5.91k
            out << std::setfill('-') << std::setw(1) << "+" << std::setw(headers_size[i]) << "-";
501
5.91k
        }
502
2.16k
        out << std::setw(1) << "+" << std::endl;
503
2.16k
    };
504
858
    line();
505
    // header text
506
2.96k
    for (size_t i = 0; i < columns(); ++i) {
507
2.10k
        out << std::setfill(' ') << std::setw(1) << "|" << std::left << std::setw(headers_size[i])
508
2.10k
            << headers[i];
509
2.10k
    }
510
858
    out << std::setw(1) << "|" << std::endl;
511
    // header bottom line
512
858
    line();
513
858
    if (rows() == 0) {
514
414
        return out.str();
515
414
    }
516
517
444
    auto format_options = DataTypeSerDe::get_default_format_options();
518
444
    auto time_zone = cctz::utc_time_zone();
519
444
    format_options.timezone = &time_zone;
520
521
    // content
522
12.2k
    for (size_t row_num = begin; row_num < rows() && row_num < row_limit + begin; ++row_num) {
523
32.4k
        for (size_t i = 0; i < columns(); ++i) {
524
20.6k
            if (!data[i].column || data[i].column->empty()) {
525
0
                out << std::setfill(' ') << std::setw(1) << "|" << std::setw(headers_size[i])
526
0
                    << std::right;
527
0
                continue;
528
0
            }
529
20.6k
            std::string s;
530
20.6k
            if (data[i].column) { // column may be const
531
                // for code inside `default_implementation_for_nulls`, there's could have: type = null, col != null
532
20.6k
                if (data[i].type->is_nullable() && !data[i].column->is_nullable()) {
533
0
                    assert(allow_null_mismatch);
534
0
                    s = assert_cast<const DataTypeNullable*>(data[i].type.get())
535
0
                                ->get_nested_type()
536
0
                                ->to_string(*data[i].column, row_num, format_options);
537
20.6k
                } else {
538
20.6k
                    s = data[i].to_string(row_num, format_options);
539
20.6k
                }
540
20.6k
            }
541
20.6k
            if (s.length() > headers_size[i]) {
542
2.12k
                s = s.substr(0, headers_size[i] - 3) + "...";
543
2.12k
            }
544
20.6k
            out << std::setfill(' ') << std::setw(1) << "|" << std::setw(headers_size[i])
545
20.6k
                << std::right << s;
546
20.6k
        }
547
11.7k
        out << std::setw(1) << "|" << std::endl;
548
11.7k
    }
549
    // bottom line
550
444
    line();
551
444
    if (row_limit < rows()) {
552
112
        out << rows() << " rows in block, only show first " << row_limit << " rows." << std::endl;
553
112
    }
554
444
    return out.str();
555
444
}
556
557
1
std::string Block::dump_one_line(size_t row, int column_end) const {
558
1
    assert(column_end <= columns());
559
1
    fmt::memory_buffer line;
560
561
1
    auto format_options = DataTypeSerDe::get_default_format_options();
562
1
    auto time_zone = cctz::utc_time_zone();
563
1
    format_options.timezone = &time_zone;
564
565
3
    for (int i = 0; i < column_end; ++i) {
566
2
        if (LIKELY(i != 0)) {
567
            // TODO: need more effective function of to string. now the impl is slow
568
1
            fmt::format_to(line, " {}", data[i].to_string(row, format_options));
569
1
        } else {
570
1
            fmt::format_to(line, "{}", data[i].to_string(row, format_options));
571
1
        }
572
2
    }
573
1
    return fmt::to_string(line);
574
1
}
575
576
47
std::string Block::dump_structure() const {
577
47
    std::string out;
578
382
    for (auto it = data.begin(); it != data.end(); ++it) {
579
335
        if (it != data.begin()) {
580
288
            out += ", \n";
581
288
        }
582
335
        out += it->dump_structure();
583
335
    }
584
47
    return out;
585
47
}
586
587
48.9k
Block Block::clone_empty() const {
588
48.9k
    Block res;
589
95.8k
    for (const auto& elem : data) {
590
95.8k
        res.insert(elem.clone_empty());
591
95.8k
    }
592
48.9k
    return res;
593
48.9k
}
594
595
32
MutableColumns Block::clone_empty_columns() const {
596
32
    size_t num_columns = data.size();
597
32
    MutableColumns columns(num_columns);
598
142
    for (size_t i = 0; i < num_columns; ++i) {
599
110
        columns[i] = data[i].column ? data[i].column->clone_empty() : data[i].type->create_column();
600
110
    }
601
32
    return columns;
602
32
}
603
604
26.1k
Columns Block::get_columns() const {
605
26.1k
    size_t num_columns = data.size();
606
26.1k
    Columns columns(num_columns);
607
113k
    for (size_t i = 0; i < num_columns; ++i) {
608
87.7k
        columns[i] = data[i].column->convert_to_full_column_if_const();
609
87.7k
    }
610
26.1k
    return columns;
611
26.1k
}
612
613
559
Columns Block::get_columns_and_convert() {
614
559
    size_t num_columns = data.size();
615
559
    Columns columns(num_columns);
616
1.18k
    for (size_t i = 0; i < num_columns; ++i) {
617
622
        data[i].column = data[i].column->convert_to_full_column_if_const();
618
622
        columns[i] = data[i].column;
619
622
    }
620
559
    return columns;
621
559
}
622
623
2.47k
Block::ScopedMutableColumns::ScopedMutableColumns(Block& block) : _block(&block) {
624
2.47k
    const size_t num_columns = block.data.size();
625
2.47k
    _columns.resize(num_columns);
626
2.47k
    size_t acquired_columns = 0;
627
2.47k
    try {
628
7.17k
        for (; acquired_columns < num_columns; ++acquired_columns) {
629
4.69k
            auto& column_with_type_and_name = block.data[acquired_columns];
630
4.69k
            _columns[acquired_columns] = scoped_mutate_column(column_with_type_and_name.column,
631
4.69k
                                                              column_with_type_and_name.type);
632
4.69k
        }
633
2.47k
    } catch (...) {
634
4
        for (size_t i = 0; i < acquired_columns; ++i) {
635
2
            block.data[i].column = std::move(_columns[i]);
636
2
        }
637
2
        _block = nullptr;
638
2
        throw;
639
2
    }
640
2.47k
}
641
642
2.47k
Block::ScopedMutableColumns::~ScopedMutableColumns() {
643
2.47k
    restore();
644
2.47k
}
645
646
Block::ScopedMutableColumns::ScopedMutableColumns(ScopedMutableColumns&& other) noexcept
647
0
        : _block(std::exchange(other._block, nullptr)), _columns(std::move(other._columns)) {}
648
649
Block::ScopedMutableColumns& Block::ScopedMutableColumns::operator=(
650
0
        ScopedMutableColumns&& other) noexcept {
651
0
    if (this != &other) {
652
0
        restore();
653
0
        _block = std::exchange(other._block, nullptr);
654
0
        _columns = std::move(other._columns);
655
0
    }
656
0
    return *this;
657
0
}
658
659
2
const DataTypePtr& Block::ScopedMutableColumns::get_datatype_by_position(size_t position) const {
660
2
    DCHECK(_block != nullptr);
661
2
    return _block->get_by_position(position).type;
662
2
}
663
664
2
const std::string& Block::ScopedMutableColumns::get_name_by_position(size_t position) const {
665
2
    DCHECK(_block != nullptr);
666
2
    return _block->get_by_position(position).name;
667
2
}
668
669
816
MutableColumns Block::ScopedMutableColumns::release() {
670
816
    DCHECK(_block != nullptr);
671
816
    _block = nullptr;
672
816
    return std::move(_columns);
673
816
}
674
675
2.95k
void Block::ScopedMutableColumns::restore() {
676
2.95k
    if (_block != nullptr) {
677
1.65k
        _block->set_columns(std::move(_columns));
678
1.65k
        _block = nullptr;
679
1.65k
    }
680
2.95k
}
681
682
Block::ScopedMutableColumn::ScopedMutableColumn(Block& block, size_t position)
683
98
        : _block(&block), _position(position) {
684
98
    DCHECK_LT(_position, _block->data.size());
685
98
    auto& column_with_type_and_name = _block->data[_position];
686
98
    DCHECK(column_with_type_and_name.type);
687
98
    _column =
688
98
            scoped_mutate_column(column_with_type_and_name.column, column_with_type_and_name.type);
689
98
}
690
691
97
Block::ScopedMutableColumn::~ScopedMutableColumn() {
692
97
    restore();
693
97
}
694
695
Block::ScopedMutableColumn::ScopedMutableColumn(ScopedMutableColumn&& other) noexcept
696
0
        : _block(std::exchange(other._block, nullptr)),
697
0
          _position(other._position),
698
0
          _column(std::move(other._column)) {}
699
700
Block::ScopedMutableColumn& Block::ScopedMutableColumn::operator=(
701
0
        ScopedMutableColumn&& other) noexcept {
702
0
    if (this != &other) {
703
0
        restore();
704
0
        _block = std::exchange(other._block, nullptr);
705
0
        _position = other._position;
706
0
        _column = std::move(other._column);
707
0
    }
708
0
    return *this;
709
0
}
710
711
97
void Block::ScopedMutableColumn::restore() {
712
97
    if (_block != nullptr) {
713
97
        DCHECK_LT(_position, _block->data.size());
714
97
        _block->data[_position].column = std::move(_column);
715
97
        _block = nullptr;
716
97
    }
717
97
}
718
719
2.47k
Block::ScopedMutableColumns Block::mutate_columns_scoped() & {
720
2.47k
    return ScopedMutableColumns(*this);
721
2.47k
}
722
723
98
Block::ScopedMutableColumn Block::mutate_column_scoped(size_t position) & {
724
98
    return ScopedMutableColumn(*this, position);
725
98
}
726
727
817
ScopedMutableBlock::ScopedMutableBlock(Block* block) {
728
817
    DCHECK(block != nullptr);
729
817
    DataTypes data_types = block->get_data_types();
730
817
    std::vector<std::string> names = block->get_names();
731
817
    auto columns_guard = block->mutate_columns_scoped();
732
817
    _mutable_block.data_types() = std::move(data_types);
733
817
    _mutable_block.get_names() = std::move(names);
734
817
    _mutable_block.set_mutable_columns(columns_guard.release());
735
817
    _block = block;
736
817
}
737
738
145k
MutableColumns Block::mutate_columns() && {
739
145k
    size_t num_columns = data.size();
740
145k
    MutableColumns columns(num_columns);
741
433k
    for (size_t i = 0; i < num_columns; ++i) {
742
287k
        DCHECK(data[i].type);
743
287k
        columns[i] = data[i].column ? IColumn::mutate(std::move(data[i].column))
744
287k
                                    : data[i].type->create_column();
745
287k
    }
746
145k
    return columns;
747
145k
}
748
749
3.38k
void Block::set_columns(MutableColumns&& columns) {
750
3.38k
    DCHECK_GE(columns.size(), data.size())
751
0
            << fmt::format("Invalid size of columns, columns size: {}, data size: {}",
752
0
                           columns.size(), data.size());
753
3.38k
    size_t num_columns = data.size();
754
11.6k
    for (size_t i = 0; i < num_columns; ++i) {
755
8.23k
        data[i].column = std::move(columns[i]);
756
8.23k
    }
757
3.38k
}
758
759
49
Block Block::clone_without_columns(const std::vector<int>* column_offset) const {
760
49
    Block res;
761
762
49
    if (column_offset != nullptr) {
763
30
        size_t num_columns = column_offset->size();
764
170
        for (size_t i = 0; i < num_columns; ++i) {
765
140
            res.insert({nullptr, data[(*column_offset)[i]].type, data[(*column_offset)[i]].name});
766
140
        }
767
30
    } else {
768
19
        size_t num_columns = data.size();
769
53
        for (size_t i = 0; i < num_columns; ++i) {
770
34
            res.insert({nullptr, data[i].type, data[i].name});
771
34
        }
772
19
    }
773
49
    return res;
774
49
}
775
776
55.8k
const ColumnsWithTypeAndName& Block::get_columns_with_type_and_name() const {
777
55.8k
    return data;
778
55.8k
}
779
780
145k
std::vector<std::string> Block::get_names() const {
781
145k
    std::vector<std::string> res;
782
145k
    res.reserve(columns());
783
784
285k
    for (const auto& elem : data) {
785
285k
        res.push_back(elem.name);
786
285k
    }
787
788
145k
    return res;
789
145k
}
790
791
145k
DataTypes Block::get_data_types() const {
792
145k
    DataTypes res;
793
145k
    res.reserve(columns());
794
795
285k
    for (const auto& elem : data) {
796
285k
        res.push_back(elem.type);
797
285k
    }
798
799
145k
    return res;
800
145k
}
801
802
52.7k
void Block::clear() {
803
52.7k
    data.clear();
804
52.7k
}
805
806
1.52M
void Block::clear_column_data(int64_t column_size) {
807
1.52M
    SCOPED_SKIP_MEMORY_CHECK();
808
    // data.size() greater than column_size, means here have some
809
    // function exec result in block, need erase it here
810
1.52M
    if (column_size != -1 and data.size() > column_size) {
811
2.20k
        for (int64_t i = data.size() - 1; i >= column_size; --i) {
812
1.10k
            erase(i);
813
1.10k
        }
814
1.10k
    }
815
1.52M
    for (auto& d : data) {
816
51.3k
        if (d.column) {
817
51.3k
            if (d.column->is_exclusive()) {
818
51.1k
                d.column->assert_mutable()->clear();
819
51.1k
            } else {
820
212
                d.column = d.column->clone_empty();
821
212
            }
822
51.3k
        }
823
51.3k
    }
824
1.52M
}
825
826
46
void Block::clear_column_data(const std::vector<uint32_t>& columns_to_clear) {
827
46
    SCOPED_SKIP_MEMORY_CHECK();
828
79
    for (auto col : columns_to_clear) {
829
79
        DCHECK_LT(col, data.size());
830
79
        auto& column = data[col].column;
831
79
        if (column) {
832
79
            if (column->is_exclusive()) {
833
77
                column->assert_mutable()->clear();
834
77
            } else {
835
2
                column = column->clone_empty();
836
2
            }
837
79
        }
838
79
    }
839
46
}
840
841
void Block::clear_column_mem_not_keep(const std::vector<bool>& column_keep_flags,
842
48.0k
                                      bool need_keep_first) {
843
48.0k
    if (data.size() >= column_keep_flags.size()) {
844
48.0k
        auto origin_rows = rows();
845
142k
        for (size_t i = 0; i < column_keep_flags.size(); ++i) {
846
94.1k
            if (!column_keep_flags[i]) {
847
36.8k
                data[i].column = data[i].column->clone_empty();
848
36.8k
            }
849
94.1k
        }
850
851
48.0k
        if (need_keep_first && !column_keep_flags[0]) {
852
1
            auto first_column = data[0].column->clone_empty();
853
1
            first_column->resize(origin_rows);
854
1
            data[0].column = std::move(first_column);
855
1
        }
856
48.0k
    }
857
48.0k
}
858
859
1.32k
void Block::swap(Block& other) noexcept {
860
1.32k
    SCOPED_SKIP_MEMORY_CHECK();
861
1.32k
    data.swap(other.data);
862
1.32k
}
863
864
1.70k
void Block::swap(Block&& other) noexcept {
865
1.70k
    SCOPED_SKIP_MEMORY_CHECK();
866
1.70k
    data = std::move(other.data);
867
1.70k
}
868
869
3
void Block::shuffle_columns(const std::vector<int>& result_column_ids) {
870
3
    Container tmp_data;
871
3
    tmp_data.reserve(result_column_ids.size());
872
5
    for (const int result_column_id : result_column_ids) {
873
5
        tmp_data.push_back(data[result_column_id]);
874
5
    }
875
3
    data = std::move(tmp_data);
876
3
}
877
878
2
void Block::update_hash(SipHash& hash) const {
879
8
    for (size_t row_no = 0, num_rows = rows(); row_no < num_rows; ++row_no) {
880
12
        for (const auto& col : data) {
881
12
            col.column->update_hash_with_value(row_no, hash);
882
12
        }
883
6
    }
884
2
}
885
886
void Block::filter_block_internal(Block* block, const std::vector<uint32_t>& columns_to_filter,
887
519
                                  const IColumn::Filter& filter) {
888
519
    size_t count = filter.size() - simd::count_zero_num((int8_t*)filter.data(), filter.size());
889
1.34k
    for (const auto& col : columns_to_filter) {
890
1.34k
        auto& column = block->get_by_position(col).column;
891
1.34k
        if (column->size() == count) {
892
1.29k
            continue;
893
1.29k
        }
894
43
        if (count == 0) {
895
2
            if (column->is_exclusive()) {
896
0
                column->assert_mutable()->clear();
897
2
            } else {
898
2
                column = column->clone_empty();
899
2
            }
900
2
            continue;
901
2
        }
902
41
        if (column->is_exclusive()) {
903
            // COW: safe to mutate in-place since we have exclusive ownership
904
41
            const auto result_size = column->assert_mutable()->filter(filter);
905
41
            if (result_size != count) [[unlikely]] {
906
0
                throw Exception(ErrorCode::INTERNAL_ERROR,
907
0
                                "result_size not equal with filter_size, result_size={}, "
908
0
                                "filter_size={}",
909
0
                                result_size, count);
910
0
            }
911
41
        } else {
912
            // COW: must create a copy since column is shared
913
0
            column = column->filter(filter, count);
914
0
        }
915
41
    }
916
519
}
917
918
void Block::filter_block_internal(Block* block, const IColumn::Filter& filter,
919
1
                                  uint32_t column_to_keep) {
920
1
    std::vector<uint32_t> columns_to_filter;
921
1
    columns_to_filter.resize(column_to_keep);
922
3
    for (uint32_t i = 0; i < column_to_keep; ++i) {
923
2
        columns_to_filter[i] = i;
924
2
    }
925
1
    filter_block_internal(block, columns_to_filter, filter);
926
1
}
927
928
8
void Block::filter_block_internal(Block* block, const IColumn::Filter& filter) {
929
8
    const size_t count =
930
8
            filter.size() - simd::count_zero_num((int8_t*)filter.data(), filter.size());
931
24
    for (int i = 0; i < block->columns(); ++i) {
932
16
        auto& column = block->get_by_position(i).column;
933
16
        if (column->is_exclusive()) {
934
16
            column->assert_mutable()->filter(filter);
935
16
        } else {
936
0
            column = column->filter(filter, count);
937
0
        }
938
16
    }
939
8
}
940
941
Status Block::append_to_block_by_selector(MutableBlock* dst,
942
1
                                          const IColumn::Selector& selector) const {
943
1
    RETURN_IF_CATCH_EXCEPTION({
944
1
        DCHECK_EQ(data.size(), dst->mutable_columns().size());
945
1
        for (size_t i = 0; i < data.size(); i++) {
946
            // FIXME: this is a quickfix. we assume that only partition functions make there some
947
1
            if (!is_column_const(*data[i].column)) {
948
1
                data[i].column->append_data_by_selector(dst->mutable_columns()[i], selector);
949
1
            }
950
1
        }
951
1
    });
952
1
    return Status::OK();
953
1
}
954
955
Status Block::filter_block(Block* block, const std::vector<uint32_t>& columns_to_filter,
956
496
                           size_t filter_column_id, size_t column_to_keep) {
957
496
    const auto& filter_column = block->get_by_position(filter_column_id).column;
958
496
    if (const auto* nullable_column = check_and_get_column<ColumnNullable>(*filter_column)) {
959
1
        const auto& nested_column = nullable_column->get_nested_column_ptr();
960
961
1
        MutableColumnPtr mutable_holder =
962
1
                nested_column->use_count() == 1
963
1
                        ? nested_column->assert_mutable()
964
1
                        : nested_column->clone_resized(nested_column->size());
965
966
1
        auto* concrete_column = assert_cast<ColumnUInt8*>(mutable_holder.get());
967
1
        const auto* __restrict null_map = nullable_column->get_null_map_data().data();
968
1
        IColumn::Filter& filter = concrete_column->get_data();
969
1
        auto* __restrict filter_data = filter.data();
970
971
1
        const size_t size = filter.size();
972
4
        for (size_t i = 0; i < size; ++i) {
973
3
            filter_data[i] &= !null_map[i];
974
3
        }
975
1
        RETURN_IF_CATCH_EXCEPTION(filter_block_internal(block, columns_to_filter, filter));
976
495
    } else if (const auto* const_column = check_and_get_column<ColumnConst>(*filter_column)) {
977
2
        bool ret = const_column->get_bool(0);
978
2
        if (!ret) {
979
2
            for (const auto& col : columns_to_filter) {
980
2
                auto& column = block->get_by_position(col).column;
981
2
                if (column->is_exclusive()) {
982
2
                    column->assert_mutable()->clear();
983
2
                } else {
984
0
                    column = column->clone_empty();
985
0
                }
986
2
            }
987
1
        }
988
493
    } else {
989
493
        const IColumn::Filter& filter =
990
493
                assert_cast<const doris::ColumnUInt8&>(*filter_column).get_data();
991
493
        RETURN_IF_CATCH_EXCEPTION(filter_block_internal(block, columns_to_filter, filter));
992
493
    }
993
994
496
    erase_useless_column(block, column_to_keep);
995
496
    return Status::OK();
996
496
}
997
998
490
Status Block::filter_block(Block* block, size_t filter_column_id, size_t column_to_keep) {
999
490
    std::vector<uint32_t> columns_to_filter;
1000
490
    columns_to_filter.resize(column_to_keep);
1001
1.77k
    for (uint32_t i = 0; i < column_to_keep; ++i) {
1002
1.28k
        columns_to_filter[i] = i;
1003
1.28k
    }
1004
490
    return filter_block(block, columns_to_filter, filter_column_id, column_to_keep);
1005
490
}
1006
1007
Status Block::serialize(int be_exec_version, PBlock* pblock,
1008
                        /*std::string* compressed_buffer,*/ size_t* uncompressed_bytes,
1009
                        size_t* compressed_bytes, int64_t* compress_time,
1010
                        segment_v2::CompressionTypePB compression_type,
1011
2.73k
                        bool allow_transfer_large_data) const {
1012
2.73k
    RETURN_IF_ERROR(BeExecVersionManager::check_be_exec_version(be_exec_version));
1013
2.73k
    pblock->set_be_exec_version(be_exec_version);
1014
1015
    // calc uncompressed size for allocation
1016
2.73k
    size_t content_uncompressed_size = 0;
1017
3.42k
    for (const auto& c : *this) {
1018
3.42k
        PColumnMeta* pcm = pblock->add_column_metas();
1019
3.42k
        c.to_pb_column_meta(pcm);
1020
3.42k
        DCHECK(pcm->type() != PGenericType::UNKNOWN) << " forget to set pb type";
1021
        // get serialized size
1022
3.42k
        content_uncompressed_size +=
1023
3.42k
                c.type->get_uncompressed_serialized_bytes(*(c.column), pblock->be_exec_version());
1024
3.42k
    }
1025
1026
    // serialize data values
1027
    // when data type is HLL, content_uncompressed_size maybe larger than real size.
1028
2.73k
    std::string column_values;
1029
2.73k
    try {
1030
        // TODO: After support c++23, we should use resize_and_overwrite to replace resize
1031
2.73k
        column_values.resize(content_uncompressed_size);
1032
2.73k
    } catch (...) {
1033
0
        std::string msg = fmt::format("Try to alloc {} bytes for pblock column values failed.",
1034
0
                                      content_uncompressed_size);
1035
0
        LOG(WARNING) << msg;
1036
0
        return Status::BufferAllocFailed(msg);
1037
0
    }
1038
2.73k
    char* buf = column_values.data();
1039
1040
3.42k
    for (const auto& c : *this) {
1041
3.42k
        buf = c.type->serialize(*(c.column), buf, pblock->be_exec_version());
1042
3.42k
    }
1043
2.73k
    *uncompressed_bytes = content_uncompressed_size;
1044
2.73k
    const size_t serialize_bytes = buf - column_values.data() + STREAMVBYTE_PADDING;
1045
2.73k
    *compressed_bytes = serialize_bytes;
1046
2.73k
    column_values.resize(serialize_bytes);
1047
1048
    // compress
1049
2.73k
    if (compression_type != segment_v2::NO_COMPRESSION && content_uncompressed_size > 0) {
1050
626
        SCOPED_RAW_TIMER(compress_time);
1051
626
        pblock->set_compression_type(compression_type);
1052
626
        pblock->set_uncompressed_size(serialize_bytes);
1053
1054
626
        BlockCompressionCodec* codec;
1055
626
        RETURN_IF_ERROR(get_block_compression_codec(compression_type, &codec));
1056
1057
626
        faststring buf_compressed;
1058
626
        RETURN_IF_ERROR_OR_CATCH_EXCEPTION(
1059
626
                codec->compress(Slice(column_values.data(), serialize_bytes), &buf_compressed));
1060
626
        size_t compressed_size = buf_compressed.size();
1061
626
        if (LIKELY(compressed_size < serialize_bytes)) {
1062
            // TODO: rethink the logic here may copy again ?
1063
626
            pblock->set_column_values(buf_compressed.data(), buf_compressed.size());
1064
626
            pblock->set_compressed(true);
1065
626
            *compressed_bytes = compressed_size;
1066
626
        } else {
1067
0
            pblock->set_column_values(std::move(column_values));
1068
0
        }
1069
1070
626
        VLOG_ROW << "uncompressed size: " << content_uncompressed_size
1071
0
                 << ", compressed size: " << compressed_size;
1072
2.10k
    } else {
1073
2.10k
        pblock->set_column_values(std::move(column_values));
1074
2.10k
    }
1075
2.73k
    if (!allow_transfer_large_data && *compressed_bytes >= std::numeric_limits<int32_t>::max()) {
1076
0
        return Status::InternalError("The block is large than 2GB({}), can not send by Protobuf.",
1077
0
                                     *compressed_bytes);
1078
0
    }
1079
2.73k
    return Status::OK();
1080
2.73k
}
1081
1082
240k
size_t MutableBlock::rows() const {
1083
240k
    for (const auto& column : _columns) {
1084
144k
        if (column) {
1085
144k
            return column->size();
1086
144k
        }
1087
144k
    }
1088
1089
96.0k
    return 0;
1090
240k
}
1091
1092
0
void MutableBlock::swap(MutableBlock& another) noexcept {
1093
0
    SCOPED_SKIP_MEMORY_CHECK();
1094
0
    _columns.swap(another._columns);
1095
0
    _data_types.swap(another._data_types);
1096
0
    _names.swap(another._names);
1097
0
}
1098
1099
0
void MutableBlock::add_row(const Block* block, int row) {
1100
0
    const auto& block_data = block->get_columns_with_type_and_name();
1101
0
    for (size_t i = 0; i < _columns.size(); ++i) {
1102
0
        _columns[i]->insert_from(*block_data[i].column.get(), row);
1103
0
    }
1104
0
}
1105
1106
Status MutableBlock::add_rows(const Block* block, const uint32_t* row_begin,
1107
162
                              const uint32_t* row_end, const std::vector<int>* column_offset) {
1108
162
    RETURN_IF_CATCH_EXCEPTION({
1109
162
        DCHECK_LE(columns(), block->columns());
1110
162
        if (column_offset != nullptr) {
1111
162
            DCHECK_EQ(columns(), column_offset->size());
1112
162
        }
1113
162
        const auto& block_data = block->get_columns_with_type_and_name();
1114
162
        for (size_t i = 0; i < _columns.size(); ++i) {
1115
162
            const auto& src_col = column_offset ? block_data[(*column_offset)[i]] : block_data[i];
1116
162
            DCHECK_EQ(_data_types[i]->get_name(), src_col.type->get_name());
1117
162
            auto& dst = _columns[i];
1118
162
            const auto& src = *src_col.column.get();
1119
162
            DCHECK_GE(src.size(), row_end - row_begin);
1120
162
            dst->insert_indices_from(src, row_begin, row_end);
1121
162
        }
1122
162
    });
1123
161
    return Status::OK();
1124
162
}
1125
1126
126
Status MutableBlock::add_rows(const Block* block, size_t row_begin, size_t length) {
1127
126
    RETURN_IF_CATCH_EXCEPTION({
1128
126
        DCHECK_LE(columns(), block->columns());
1129
126
        const auto& block_data = block->get_columns_with_type_and_name();
1130
126
        for (size_t i = 0; i < _columns.size(); ++i) {
1131
126
            DCHECK_EQ(_data_types[i]->get_name(), block_data[i].type->get_name());
1132
126
            auto& dst = _columns[i];
1133
126
            const auto& src = *block_data[i].column.get();
1134
126
            dst->insert_range_from(src, row_begin, length);
1135
126
        }
1136
126
    });
1137
126
    return Status::OK();
1138
126
}
1139
1140
144k
Block MutableBlock::to_block(int start_column) {
1141
144k
    return to_block(start_column, (int)_columns.size());
1142
144k
}
1143
1144
144k
Block MutableBlock::to_block(int start_column, int end_column) {
1145
144k
    ColumnsWithTypeAndName columns_with_schema;
1146
144k
    columns_with_schema.reserve(end_column - start_column);
1147
428k
    for (size_t i = start_column; i < end_column; ++i) {
1148
283k
        columns_with_schema.emplace_back(std::move(_columns[i]), _data_types[i], _names[i]);
1149
283k
    }
1150
144k
    return {columns_with_schema};
1151
144k
}
1152
1153
1
std::string MutableBlock::dump_data_json(size_t row_limit) const {
1154
1
    std::stringstream ss;
1155
1
    std::vector<std::string> headers;
1156
1157
1
    headers.reserve(columns());
1158
2
    for (size_t i = 0; i < columns(); ++i) {
1159
1
        headers.push_back(_data_types[i]->get_name());
1160
1
    }
1161
1
    size_t num_rows_to_dump = std::min(rows(), row_limit);
1162
1
    ss << "[";
1163
1164
1
    auto format_options = DataTypeSerDe::get_default_format_options();
1165
1
    auto time_zone = cctz::utc_time_zone();
1166
1
    format_options.timezone = &time_zone;
1167
1168
4
    for (size_t row_num = 0; row_num < num_rows_to_dump; ++row_num) {
1169
3
        if (row_num > 0) {
1170
2
            ss << ",";
1171
2
        }
1172
3
        ss << "{";
1173
6
        for (size_t i = 0; i < columns(); ++i) {
1174
3
            if (i > 0) {
1175
0
                ss << ",";
1176
0
            }
1177
3
            ss << "\"" << headers[i] << "\":";
1178
3
            std::string s = _data_types[i]->to_string(*_columns[i].get(), row_num, format_options);
1179
3
            ss << "\"" << s << "\"";
1180
3
        }
1181
3
        ss << "}";
1182
3
    }
1183
1
    ss << "]";
1184
1
    return ss.str();
1185
1
}
1186
1187
1
std::string MutableBlock::dump_data(size_t row_limit) const {
1188
1
    std::vector<std::string> headers;
1189
1
    std::vector<int> headers_size;
1190
2
    for (size_t i = 0; i < columns(); ++i) {
1191
1
        std::string s = _data_types[i]->get_name();
1192
1
        headers_size.push_back(s.size() > 15 ? (int)s.size() : 15);
1193
1
        headers.emplace_back(s);
1194
1
    }
1195
1196
1
    std::stringstream out;
1197
    // header upper line
1198
3
    auto line = [&]() {
1199
6
        for (size_t i = 0; i < columns(); ++i) {
1200
3
            out << std::setfill('-') << std::setw(1) << "+" << std::setw(headers_size[i]) << "-";
1201
3
        }
1202
3
        out << std::setw(1) << "+" << std::endl;
1203
3
    };
1204
1
    line();
1205
    // header text
1206
2
    for (size_t i = 0; i < columns(); ++i) {
1207
1
        out << std::setfill(' ') << std::setw(1) << "|" << std::left << std::setw(headers_size[i])
1208
1
            << headers[i];
1209
1
    }
1210
1
    out << std::setw(1) << "|" << std::endl;
1211
    // header bottom line
1212
1
    line();
1213
1
    if (rows() == 0) {
1214
0
        return out.str();
1215
0
    }
1216
1217
1
    auto format_options = DataTypeSerDe::get_default_format_options();
1218
1
    auto time_zone = cctz::utc_time_zone();
1219
1
    format_options.timezone = &time_zone;
1220
1221
    // content
1222
4
    for (size_t row_num = 0; row_num < rows() && row_num < row_limit; ++row_num) {
1223
6
        for (size_t i = 0; i < columns(); ++i) {
1224
3
            if (_columns[i].get()->empty()) {
1225
0
                out << std::setfill(' ') << std::setw(1) << "|" << std::setw(headers_size[i])
1226
0
                    << std::right;
1227
0
                continue;
1228
0
            }
1229
3
            std::string s = _data_types[i]->to_string(*_columns[i].get(), row_num, format_options);
1230
3
            if (s.length() > headers_size[i]) {
1231
0
                s = s.substr(0, headers_size[i] - 3) + "...";
1232
0
            }
1233
3
            out << std::setfill(' ') << std::setw(1) << "|" << std::setw(headers_size[i])
1234
3
                << std::right << s;
1235
3
        }
1236
3
        out << std::setw(1) << "|" << std::endl;
1237
3
    }
1238
    // bottom line
1239
1
    line();
1240
1
    if (row_limit < rows()) {
1241
0
        out << rows() << " rows in block, only show first " << row_limit << " rows." << std::endl;
1242
0
    }
1243
1
    return out.str();
1244
1
}
1245
1246
48.0k
std::unique_ptr<Block> Block::create_same_struct_block(size_t size, bool is_reserve) const {
1247
48.0k
    auto temp_block = Block::create_unique();
1248
94.1k
    for (const auto& d : data) {
1249
94.1k
        auto column = d.type->create_column();
1250
94.1k
        if (is_reserve) {
1251
0
            column->reserve(size);
1252
94.1k
        } else {
1253
94.1k
            column->insert_many_defaults(size);
1254
94.1k
        }
1255
94.1k
        temp_block->insert({std::move(column), d.type, d.name});
1256
94.1k
    }
1257
48.0k
    return temp_block;
1258
48.0k
}
1259
1260
96.1k
size_t MutableBlock::allocated_bytes() const {
1261
96.1k
    size_t res = 0;
1262
188k
    for (const auto& col : _columns) {
1263
188k
        if (col) {
1264
188k
            res += col->allocated_bytes();
1265
188k
        }
1266
188k
    }
1267
1268
96.1k
    return res;
1269
96.1k
}
1270
1271
1
void MutableBlock::clear_column_data() noexcept {
1272
1
    SCOPED_SKIP_MEMORY_CHECK();
1273
1
    for (auto& col : _columns) {
1274
1
        if (col) {
1275
1
            col->clear();
1276
1
        }
1277
1
    }
1278
1
}
1279
1280
5
std::string MutableBlock::dump_names() const {
1281
5
    std::string out;
1282
16
    for (auto it = _names.begin(); it != _names.end(); ++it) {
1283
11
        if (it != _names.begin()) {
1284
6
            out += ", ";
1285
6
        }
1286
11
        out += *it;
1287
11
    }
1288
5
    return out;
1289
5
}
1290
} // namespace doris