Coverage Report

Created: 2026-09-09 16:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/segment/column_writer.h
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
#pragma once
19
20
#include <gen_cpp/AgentService_types.h>
21
#include <gen_cpp/olap_file.pb.h>
22
#include <gen_cpp/segment_v2.pb.h>
23
#include <stddef.h>
24
#include <stdint.h>
25
26
#include <algorithm>
27
#include <memory> // for unique_ptr
28
#include <ostream>
29
#include <span>
30
#include <string>
31
#include <unordered_map>
32
#include <utility>
33
#include <vector>
34
35
#include "common/status.h" // for Status
36
#include "storage/index/ann/ann_index_writer.h"
37
#include "storage/index/bloom_filter/bloom_filter.h"
38
#include "storage/index/inverted/inverted_index_writer.h"
39
#include "storage/segment/common.h"
40
#include "storage/segment/options.h"
41
#include "storage/segment/variant/nested_group_provider.h"
42
#include "storage/segment/variant/variant_statistics.h"
43
#include "storage/tablet/tablet_schema.h" // for TabletColumnPtr
44
#include "storage/types.h"                // for field_type_size
45
#include "util/bitmap.h"                  // for BitmapChange
46
#include "util/slice.h"                   // for OwnedSlice
47
48
namespace doris {
49
50
class BlockCompressionCodec;
51
class TabletColumn;
52
class TabletIndex;
53
struct RowsetWriterContext;
54
struct VariantColumnData;
55
56
namespace io {
57
class FileWriter;
58
}
59
60
namespace segment_v2 {
61
62
enum class VariantWriterInputFormat : uint8_t {
63
    UNSET,
64
    V2,
65
};
66
67
struct ColumnWriterOptions {
68
    // input and output parameter:
69
    // - input: column_id/unique_id/type/length/encoding/compression/is_nullable members
70
    // - output: encoding/indexes/dict_page members
71
    ColumnMetaPB* meta = nullptr;
72
    size_t data_page_size = STORAGE_PAGE_SIZE_DEFAULT_VALUE;
73
    size_t dict_page_size = STORAGE_DICT_PAGE_SIZE_DEFAULT_VALUE;
74
    // store compressed page only when space saving is above the threshold.
75
    // space saving = 1 - compressed_size / uncompressed_size
76
    double compression_min_space_saving = 0.1;
77
    bool need_zone_map = false;
78
    bool need_bloom_filter = false;
79
    bool is_ngram_bf_index = false;
80
    bool need_inverted_index = false;
81
    bool need_ann_index = false;
82
    uint8_t gram_size;
83
    uint16_t gram_bf_size;
84
    BloomFilterOptions bf_options;
85
    std::vector<const TabletIndex*> inverted_indexes;
86
    IndexFileWriter* index_file_writer = nullptr;
87
    // The owning segment serves a direct load (stream/broker load,
88
    // DataWriteType::TYPE_DIRECT) rather than compaction / schema change. Set
89
    // once by the segment writer and propagated to variant subcolumn writers;
90
    // forwarded to every created IndexColumnWriter via set_direct_load() so
91
    // SNII can select its direct-load PRX zstd level without plumbing
92
    // DataWriteType itself down here.
93
    bool is_direct_load = false;
94
95
    SegmentFooterPB* footer = nullptr;
96
    io::FileWriter* file_writer = nullptr;
97
    CompressionTypePB compression_type = UNKNOWN_COMPRESSION;
98
    RowsetWriterContext* rowset_ctx = nullptr;
99
    // For collect segment statistics for compaction
100
    std::vector<RowsetReaderSharedPtr> input_rs_readers;
101
    const TabletIndex* ann_index = nullptr;
102
103
    // Storage format of the owning tablet (V2 or V3). Set once by the segment writer
104
    // (from TabletMeta::storage_format()) and propagated down to aux child writers
105
    // (null / array-length / map-length), struct subcolumn writers and variant subcolumn
106
    // writers. All encoding-default decisions consult this via resolve_default_encoding().
107
    // Also forwarded to BinaryDictPageBuilder via PageBuilderOptions::binary_plain_encoding.
108
    TabletStorageFormatPB storage_format = TabletStorageFormatPB::TABLET_STORAGE_FORMAT_V2;
109
110
0
    std::string to_string() const {
111
0
        std::stringstream ss;
112
0
        ss << std::boolalpha << "meta=" << meta->DebugString()
113
0
           << ", data_page_size=" << data_page_size << ", dict_page_size=" << dict_page_size
114
0
           << ", compression_min_space_saving = " << compression_min_space_saving
115
0
           << ", need_zone_map=" << need_zone_map << ", need_bloom_filter" << need_bloom_filter;
116
0
        return ss.str();
117
0
    }
118
};
119
120
class EncodingInfo;
121
class NullBitmapBuilder;
122
class OrdinalIndexWriter;
123
class PageBuilder;
124
class BloomFilterIndexWriter;
125
class ZoneMapIndexWriter;
126
class VariantColumnWriterImpl;
127
class VariantShredder;
128
class VariantPathBuilder;
129
class ColumnWriter;
130
131
class ColumnWriter {
132
public:
133
    static Status create(const ColumnWriterOptions& opts, const TabletColumn* column,
134
                         io::FileWriter* file_writer, std::unique_ptr<ColumnWriter>* writer);
135
    static Status create_struct_writer(const ColumnWriterOptions& opts, const TabletColumn* column,
136
                                       io::FileWriter* file_writer,
137
                                       std::unique_ptr<ColumnWriter>* writer);
138
    static Status create_array_writer(const ColumnWriterOptions& opts, const TabletColumn* column,
139
                                      io::FileWriter* file_writer,
140
                                      std::unique_ptr<ColumnWriter>* writer);
141
    static Status create_map_writer(const ColumnWriterOptions& opts, const TabletColumn* column,
142
                                    io::FileWriter* file_writer,
143
                                    std::unique_ptr<ColumnWriter>* writer);
144
145
    static Status create_variant_writer(const ColumnWriterOptions& opts, const TabletColumn* column,
146
                                        io::FileWriter* file_writer,
147
                                        std::unique_ptr<ColumnWriter>* writer);
148
149
    static Status create_agg_state_writer(const ColumnWriterOptions& opts,
150
                                          const TabletColumn* column, io::FileWriter* file_writer,
151
                                          std::unique_ptr<ColumnWriter>* writer);
152
153
    explicit ColumnWriter(TabletColumnPtr column, bool is_nullable, ColumnMetaPB* meta);
154
155
1.05M
    virtual ~ColumnWriter() = default;
156
157
    virtual Status init() = 0;
158
159
    template <typename CellType>
160
    Status append(const CellType& cell) {
161
        if (_is_nullable) {
162
            uint8_t nullmap = 0;
163
            BitmapChange(&nullmap, 0, cell.is_null());
164
            return append_nullable(&nullmap, cell.cell_ptr(), 1);
165
        } else {
166
            auto* cel_ptr = cell.cell_ptr();
167
            return append_data((const uint8_t**)&cel_ptr, 1);
168
        }
169
    }
170
171
    // Now we only support append one by one, we should support append
172
    // multi rows in one call
173
    Status append(bool is_null, void* data) {
174
        uint8_t nullmap = 0;
175
        BitmapChange(&nullmap, 0, is_null);
176
        return append_nullable(&nullmap, data, 1);
177
    }
178
179
    Status append(const uint8_t* nullmap, const void* data, size_t num_rows);
180
181
    Status append_nullable(const uint8_t* nullmap, const void* data, size_t num_rows);
182
183
    // use only in vectorized load
184
    virtual Status append_nullable(const uint8_t* null_map, const uint8_t** data, size_t num_rows);
185
186
    virtual Status append_nulls(size_t num_rows) = 0;
187
188
    virtual Status finish_current_page() = 0;
189
190
    virtual uint64_t estimate_buffer_size() = 0;
191
192
    // finish append data
193
    virtual Status finish() = 0;
194
195
    // write all data into file
196
    virtual Status write_data() = 0;
197
198
    virtual Status write_ordinal_index() = 0;
199
200
    virtual Status write_zone_map() = 0;
201
202
    virtual Status write_inverted_index() = 0;
203
204
670k
    virtual Status write_ann_index() { return Status::OK(); }
205
206
    virtual Status write_bloom_filter_index() = 0;
207
208
    virtual ordinal_t get_next_rowid() const = 0;
209
210
    virtual uint64_t get_raw_data_bytes() const = 0;
211
    virtual uint64_t get_total_uncompressed_data_pages_bytes() const = 0;
212
    virtual uint64_t get_total_compressed_data_pages_bytes() const = 0;
213
214
    // used for append not null data.
215
    virtual Status append_data(const uint8_t** ptr, size_t num_rows) = 0;
216
217
4.79M
    bool is_nullable() const { return _is_nullable; }
218
219
1.99M
    const TabletColumn* get_column() const { return _column.get(); }
220
221
    // Per-row in-memory cell footprint of this writer's column, used to step
222
    // the input pointer across rows in append_*/null-run loops.
223
2.87M
    size_t cell_size() const { return field_type_size(_column->type()); }
224
225
710k
    ColumnMetaPB* get_column_meta() const { return _column_meta; }
226
227
protected:
228
    DataTypePtr _data_type;
229
230
private:
231
    TabletColumnPtr _column;
232
    bool _is_nullable;
233
    ColumnMetaPB* _column_meta;
234
    std::vector<uint8_t> _null_bitmap;
235
};
236
237
class FlushPageCallback {
238
public:
239
66.7k
    virtual ~FlushPageCallback() = default;
240
0
    virtual void put_extra_info_in_page(DataPageFooterPB* footer) {}
241
};
242
243
// Encode one column's data into some memory slice.
244
// Because some columns would be stored in a file, we should wait
245
// until all columns has been finished, and then data can be written
246
// to file
247
class ScalarColumnWriter : public ColumnWriter {
248
public:
249
    ScalarColumnWriter(const ColumnWriterOptions& opts, TabletColumnPtr column,
250
                       io::FileWriter* file_writer);
251
252
    ~ScalarColumnWriter() override;
253
254
    Status init() override;
255
256
    Status append_nulls(size_t num_rows) override;
257
258
    Status finish_current_page() override;
259
260
    uint64_t estimate_buffer_size() override;
261
262
    // finish append data
263
    Status finish() override;
264
265
    Status write_data() override;
266
    Status write_ordinal_index() override;
267
    Status write_zone_map() override;
268
    Status write_inverted_index() override;
269
    Status write_bloom_filter_index() override;
270
159k
    ordinal_t get_next_rowid() const override { return _next_rowid; }
271
272
820k
    uint64_t get_raw_data_bytes() const override { return _raw_data_bytes; }
273
274
820k
    uint64_t get_total_uncompressed_data_pages_bytes() const override {
275
820k
        return _total_uncompressed_data_pages_size;
276
820k
    }
277
278
820k
    uint64_t get_total_compressed_data_pages_bytes() const override {
279
820k
        return _total_compressed_data_pages_size;
280
820k
    }
281
282
66.7k
    void register_flush_page_callback(FlushPageCallback* flush_page_callback) {
283
66.7k
        _new_page_callback = flush_page_callback;
284
66.7k
    }
285
    Status append_data(const uint8_t** ptr, size_t num_rows) override;
286
    Status append_nullable(const uint8_t* null_map, const uint8_t** ptr, size_t num_rows) override;
287
288
    // used for append not null data. When page is full, will append data not reach num_rows.
289
    Status append_data_in_current_page(const uint8_t** ptr, size_t* num_written);
290
291
2.67M
    Status append_data_in_current_page(const uint8_t* ptr, size_t* num_written) {
292
2.67M
        RETURN_IF_CATCH_EXCEPTION(
293
2.67M
                { return _internal_append_data_in_current_page(ptr, num_written); });
294
2.67M
    }
295
    friend class ArrayColumnWriter;
296
    friend class OffsetColumnWriter;
297
298
private:
299
    Status _internal_append_data_in_current_page(const uint8_t* ptr, size_t* num_written);
300
301
private:
302
    struct NullRun {
303
        bool is_null;
304
        uint32_t len;
305
    };
306
307
    std::vector<NullRun> _null_run_buffer;
308
    std::unique_ptr<PageBuilder> _page_builder;
309
310
    std::unique_ptr<NullBitmapBuilder> _null_bitmap_builder;
311
312
    ColumnWriterOptions _opts;
313
314
    const EncodingInfo* _encoding_info = nullptr;
315
316
    ordinal_t _next_rowid = 0;
317
318
    // All Pages will be organized into a linked list
319
    struct Page {
320
        // the data vector may contain:
321
        //     1. one OwnedSlice if the page body is compressed
322
        //     2. one OwnedSlice if the page body is not compressed and doesn't have nullmap
323
        //     3. two OwnedSlice if the page body is not compressed and has nullmap
324
        // use vector for easier management for lifetime of OwnedSlice
325
        std::vector<OwnedSlice> data;
326
        PageFooterPB footer;
327
    };
328
329
1.02M
    void _push_back_page(std::unique_ptr<Page> page) {
330
1.95M
        for (auto& data_slice : page->data) {
331
1.95M
            _data_size += data_slice.slice().size;
332
1.95M
        }
333
        // estimate (page footer + footer size + checksum) took 20 bytes
334
1.02M
        _data_size += 20;
335
        // add page to pages' tail
336
1.02M
        _pages.emplace_back(std::move(page));
337
1.02M
    }
338
339
    Status _write_data_page(Page* page);
340
341
private:
342
    io::FileWriter* _file_writer = nullptr;
343
    // total size of data page list
344
    uint64_t _data_size;
345
346
    uint64_t _raw_data_bytes {0};
347
    uint64_t _total_uncompressed_data_pages_size {0};
348
    uint64_t _total_compressed_data_pages_size {0};
349
350
    // cached generated pages,
351
    std::vector<std::unique_ptr<Page>> _pages;
352
    ordinal_t _first_rowid = 0;
353
354
    BlockCompressionCodec* _compress_codec;
355
356
    std::unique_ptr<OrdinalIndexWriter> _ordinal_index_builder;
357
    std::unique_ptr<ZoneMapIndexWriter> _zone_map_index_builder;
358
    std::vector<std::unique_ptr<IndexColumnWriter>> _inverted_index_builders;
359
    std::unique_ptr<BloomFilterIndexWriter> _bloom_filter_index_builder;
360
361
    // call before flush data page.
362
    FlushPageCallback* _new_page_callback = nullptr;
363
};
364
365
// offsetColumnWriter is used column which has offset column, like array, map.
366
//  column type is only uint64 and should response for whole column value [start, end], end will set
367
//  in footer.next_array_item_ordinal which in finish_cur_page() callback put_extra_info_in_page()
368
class OffsetColumnWriter final : public ScalarColumnWriter, FlushPageCallback {
369
public:
370
    OffsetColumnWriter(const ColumnWriterOptions& opts, TabletColumnPtr column,
371
                       io::FileWriter* file_writer);
372
373
    ~OffsetColumnWriter() override;
374
375
    Status init() override;
376
377
    Status append_data(const uint8_t** ptr, size_t num_rows) override;
378
379
private:
380
    void put_extra_info_in_page(DataPageFooterPB* footer) override;
381
382
    uint64_t _next_offset;
383
};
384
385
class StructColumnWriter final : public ColumnWriter {
386
public:
387
    explicit StructColumnWriter(const ColumnWriterOptions& opts, TabletColumnPtr column,
388
                                ScalarColumnWriter* null_writer,
389
                                std::vector<std::unique_ptr<ColumnWriter>>& sub_column_writers);
390
2.91k
    ~StructColumnWriter() override = default;
391
392
    Status init() override;
393
394
    Status append_nullable(const uint8_t* null_map, const uint8_t** data, size_t num_rows) override;
395
    Status append_data(const uint8_t** ptr, size_t num_rows) override;
396
397
    uint64_t estimate_buffer_size() override;
398
399
    Status finish() override;
400
    Status write_data() override;
401
    Status write_ordinal_index() override;
402
    Status append_nulls(size_t num_rows) override;
403
404
    Status finish_current_page() override;
405
406
2.21k
    Status write_zone_map() override {
407
2.21k
        if (_opts.need_zone_map) {
408
0
            return Status::NotSupported("struct not support zone map");
409
0
        }
410
2.21k
        return Status::OK();
411
2.21k
    }
412
413
    Status write_inverted_index() override;
414
2.21k
    Status write_bloom_filter_index() override {
415
2.21k
        if (_opts.need_bloom_filter) {
416
0
            return Status::NotSupported("struct not support bloom filter index");
417
0
        }
418
2.21k
        return Status::OK();
419
2.21k
    }
420
421
3.31k
    ordinal_t get_next_rowid() const override { return _sub_column_writers[0]->get_next_rowid(); }
422
423
2.91k
    uint64_t get_raw_data_bytes() const override {
424
2.91k
        return _get_total_data_pages_bytes(&ColumnWriter::get_raw_data_bytes);
425
2.91k
    }
426
427
2.91k
    uint64_t get_total_uncompressed_data_pages_bytes() const override {
428
2.91k
        return _get_total_data_pages_bytes(&ColumnWriter::get_total_uncompressed_data_pages_bytes);
429
2.91k
    }
430
431
2.92k
    uint64_t get_total_compressed_data_pages_bytes() const override {
432
2.92k
        return _get_total_data_pages_bytes(&ColumnWriter::get_total_compressed_data_pages_bytes);
433
2.92k
    }
434
435
private:
436
    template <typename Func>
437
8.75k
    uint64_t _get_total_data_pages_bytes(Func func) const {
438
8.75k
        uint64_t size = is_nullable() ? std::invoke(func, _null_writer.get()) : 0;
439
55.3k
        for (const auto& writer : _sub_column_writers) {
440
55.3k
            size += std::invoke(func, writer.get());
441
55.3k
        }
442
8.75k
        return size;
443
8.75k
    }
444
445
private:
446
    size_t _num_sub_column_writers;
447
    std::unique_ptr<ScalarColumnWriter> _null_writer;
448
    std::vector<std::unique_ptr<ColumnWriter>> _sub_column_writers;
449
    ColumnWriterOptions _opts;
450
};
451
452
class ArrayColumnWriter final : public ColumnWriter {
453
public:
454
    explicit ArrayColumnWriter(const ColumnWriterOptions& opts, TabletColumnPtr column,
455
                               OffsetColumnWriter* offset_writer, ScalarColumnWriter* null_writer,
456
                               std::unique_ptr<ColumnWriter> item_writer);
457
42.7k
    ~ArrayColumnWriter() override = default;
458
459
    Status init() override;
460
461
    Status append_data(const uint8_t** ptr, size_t num_rows) override;
462
463
    uint64_t estimate_buffer_size() override;
464
465
    Status finish() override;
466
    Status write_data() override;
467
    Status write_ordinal_index() override;
468
    Status append_nulls(size_t num_rows) override;
469
    Status append_nullable(const uint8_t* null_map, const uint8_t** ptr, size_t num_rows) override;
470
471
    Status finish_current_page() override;
472
473
40.0k
    Status write_zone_map() override {
474
40.0k
        if (_opts.need_zone_map) {
475
0
            return Status::NotSupported("array not support zone map");
476
0
        }
477
40.0k
        return Status::OK();
478
40.0k
    }
479
480
    Status write_inverted_index() override;
481
    Status write_ann_index() override;
482
40.0k
    Status write_bloom_filter_index() override {
483
40.0k
        if (_opts.need_bloom_filter) {
484
0
            return Status::NotSupported("array not support bloom filter index");
485
0
        }
486
40.0k
        return Status::OK();
487
40.0k
    }
488
44.5k
    ordinal_t get_next_rowid() const override { return _offset_writer->get_next_rowid(); }
489
490
42.0k
    uint64_t get_raw_data_bytes() const override {
491
42.0k
        return _get_total_data_pages_bytes(&ColumnWriter::get_raw_data_bytes);
492
42.0k
    }
493
494
42.0k
    uint64_t get_total_uncompressed_data_pages_bytes() const override {
495
42.0k
        return _get_total_data_pages_bytes(&ColumnWriter::get_total_uncompressed_data_pages_bytes);
496
42.0k
    }
497
498
42.0k
    uint64_t get_total_compressed_data_pages_bytes() const override {
499
42.0k
        return _get_total_data_pages_bytes(&ColumnWriter::get_total_compressed_data_pages_bytes);
500
42.0k
    }
501
502
private:
503
    template <typename Func>
504
126k
    uint64_t _get_total_data_pages_bytes(Func func) const {
505
126k
        uint64_t size = std::invoke(func, _offset_writer.get());
506
126k
        if (is_nullable()) {
507
77.6k
            size += std::invoke(func, _null_writer.get());
508
77.6k
        }
509
126k
        size += std::invoke(func, _item_writer.get());
510
126k
        return size;
511
126k
    }
512
513
private:
514
    Status write_null_column(size_t num_rows, bool is_null); // 写入num_rows个null标记
515
42.3k
    bool has_empty_items() const { return _item_writer->get_next_rowid() == 0; }
516
517
private:
518
    std::unique_ptr<OffsetColumnWriter> _offset_writer;
519
    std::unique_ptr<ScalarColumnWriter> _null_writer;
520
    std::unique_ptr<ColumnWriter> _item_writer;
521
    std::unique_ptr<IndexColumnWriter> _inverted_index_writer;
522
    std::unique_ptr<AnnIndexColumnWriter> _ann_index_writer;
523
    ColumnWriterOptions _opts;
524
};
525
526
class MapColumnWriter final : public ColumnWriter {
527
public:
528
    explicit MapColumnWriter(const ColumnWriterOptions& opts, TabletColumnPtr column,
529
                             ScalarColumnWriter* null_writer, OffsetColumnWriter* offsets_writer,
530
                             std::vector<std::unique_ptr<ColumnWriter>>& _kv_writers);
531
532
23.9k
    ~MapColumnWriter() override = default;
533
534
    Status init() override;
535
536
    Status append_data(const uint8_t** ptr, size_t num_rows) override;
537
    Status append_nullable(const uint8_t* null_map, const uint8_t** ptr, size_t num_rows) override;
538
    uint64_t estimate_buffer_size() override;
539
540
    Status finish() override;
541
    Status write_data() override;
542
    Status write_ordinal_index() override;
543
    Status write_inverted_index() override;
544
    Status append_nulls(size_t num_rows) override;
545
546
    Status finish_current_page() override;
547
548
9.94k
    Status write_zone_map() override {
549
9.94k
        if (_opts.need_zone_map) {
550
0
            return Status::NotSupported("map not support zone map");
551
0
        }
552
9.94k
        return Status::OK();
553
9.94k
    }
554
555
9.94k
    Status write_bloom_filter_index() override {
556
9.94k
        if (_opts.need_bloom_filter) {
557
0
            return Status::NotSupported("map not support bloom filter index");
558
0
        }
559
9.94k
        return Status::OK();
560
9.94k
    }
561
562
    // according key writer to get next rowid
563
24.9k
    ordinal_t get_next_rowid() const override { return _offsets_writer->get_next_rowid(); }
564
565
10.4k
    uint64_t get_raw_data_bytes() const override {
566
10.4k
        return _get_total_data_pages_bytes(&ColumnWriter::get_raw_data_bytes);
567
10.4k
    }
568
569
10.4k
    uint64_t get_total_uncompressed_data_pages_bytes() const override {
570
10.4k
        return _get_total_data_pages_bytes(&ColumnWriter::get_total_uncompressed_data_pages_bytes);
571
10.4k
    }
572
573
10.4k
    uint64_t get_total_compressed_data_pages_bytes() const override {
574
10.4k
        return _get_total_data_pages_bytes(&ColumnWriter::get_total_compressed_data_pages_bytes);
575
10.4k
    }
576
577
private:
578
    template <typename Func>
579
31.3k
    uint64_t _get_total_data_pages_bytes(Func func) const {
580
31.3k
        uint64_t size = std::invoke(func, _offsets_writer.get());
581
31.3k
        if (is_nullable()) {
582
25.4k
            size += std::invoke(func, _null_writer.get());
583
25.4k
        }
584
62.7k
        for (const auto& writer : _kv_writers) {
585
62.7k
            size += std::invoke(func, writer.get());
586
62.7k
        }
587
31.3k
        return size;
588
31.3k
    }
589
590
private:
591
    std::vector<std::unique_ptr<ColumnWriter>> _kv_writers;
592
    // we need null writer to make sure a row is null or not
593
    std::unique_ptr<ScalarColumnWriter> _null_writer;
594
    std::unique_ptr<OffsetColumnWriter> _offsets_writer;
595
    std::unique_ptr<IndexColumnWriter> _index_builder;
596
    ColumnWriterOptions _opts;
597
};
598
599
// used for compaction to write sub variant column
600
class VariantSubcolumnWriter : public ColumnWriter {
601
public:
602
    explicit VariantSubcolumnWriter(const ColumnWriterOptions& opts, TabletColumnPtr column);
603
604
    ~VariantSubcolumnWriter() override;
605
606
    Status init() override;
607
608
    Status append_data(const uint8_t** ptr, size_t num_rows) override;
609
610
    uint64_t estimate_buffer_size() override;
611
612
    Status finish() override;
613
    Status write_data() override;
614
    Status write_ordinal_index() override;
615
616
    Status write_zone_map() override;
617
618
    Status write_inverted_index() override;
619
    Status write_bloom_filter_index() override;
620
2
    ordinal_t get_next_rowid() const override { return _next_rowid; }
621
622
14
    uint64_t get_raw_data_bytes() const override {
623
14
        return 0; // TODO
624
14
    }
625
626
14
    uint64_t get_total_uncompressed_data_pages_bytes() const override {
627
14
        return 0; // TODO
628
14
    }
629
630
14
    uint64_t get_total_compressed_data_pages_bytes() const override {
631
14
        return 0; // TODO
632
14
    }
633
634
0
    Status append_nulls(size_t num_rows) override {
635
0
        return Status::NotSupported("variant writer can not append_nulls");
636
0
    }
637
    Status append_nullable(const uint8_t* null_map, const uint8_t** ptr, size_t num_rows) override;
638
639
0
    Status finish_current_page() override {
640
0
        return Status::NotSupported("variant writer has no data, can not finish_current_page");
641
0
    }
642
643
0
    size_t get_non_null_size() const { return none_null_size; }
644
645
    Status finalize();
646
647
private:
648
    Status _append(const uint8_t* null_map, const uint8_t** ptr, size_t num_rows);
649
    Status _append_v2(const VariantColumnData& column, size_t num_rows,
650
                      std::span<const uint8_t> outer_nulls);
651
    Status _ensure_input_format(const VariantColumnData& column);
652
    Status _initialize_v2_builder();
653
    bool is_finalized() const;
654
    bool _is_finalized = false;
655
    ordinal_t _next_rowid = 0;
656
    size_t none_null_size = 0;
657
    VariantWriterInputFormat _input_format = VariantWriterInputFormat::UNSET;
658
    std::unique_ptr<VariantPathBuilder> _v2_builder;
659
    size_t _num_rows = 0;
660
    ColumnWriterOptions _opts;
661
    std::unique_ptr<ColumnWriter> _writer;
662
    TabletIndexes _indexes;
663
664
    std::unique_ptr<NestedGroupWriteProvider> _nested_group_provider;
665
    VariantStatistics _statistics;
666
};
667
668
class VariantColumnWriter : public ColumnWriter {
669
public:
670
    explicit VariantColumnWriter(const ColumnWriterOptions& opts, TabletColumnPtr column);
671
672
5.30k
    ~VariantColumnWriter() override = default;
673
674
    Status init() override;
675
676
    Status append_data(const uint8_t** ptr, size_t num_rows) override;
677
678
    uint64_t estimate_buffer_size() override;
679
680
    Status finish() override;
681
    Status write_data() override;
682
    Status write_ordinal_index() override;
683
684
    Status write_zone_map() override;
685
686
    Status write_inverted_index() override;
687
    Status write_bloom_filter_index() override;
688
1
    ordinal_t get_next_rowid() const override { return _next_rowid; }
689
690
5.26k
    uint64_t get_raw_data_bytes() const override {
691
5.26k
        return 0; // TODO
692
5.26k
    }
693
694
5.26k
    uint64_t get_total_uncompressed_data_pages_bytes() const override {
695
5.26k
        return 0; // TODO
696
5.26k
    }
697
698
5.26k
    uint64_t get_total_compressed_data_pages_bytes() const override {
699
5.26k
        return 0; // TODO
700
5.26k
    }
701
702
0
    Status append_nulls(size_t num_rows) override {
703
0
        return Status::NotSupported("variant writer can not append_nulls");
704
0
    }
705
    Status append_nullable(const uint8_t* null_map, const uint8_t** ptr, size_t num_rows) override;
706
707
0
    Status finish_current_page() override {
708
0
        return Status::NotSupported("variant writer has no data, can not finish_current_page");
709
0
    }
710
711
    VariantColumnWriterImpl* impl_for_test() const { return _impl.get(); }
712
713
private:
714
    std::unique_ptr<VariantColumnWriterImpl> _impl;
715
    ordinal_t _next_rowid = 0;
716
};
717
718
} // namespace segment_v2
719
} // namespace doris