Coverage Report

Created: 2026-03-12 16:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/jsonb/serialize.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
18
#include "util/jsonb/serialize.h"
19
20
#include <assert.h>
21
22
#include <algorithm>
23
#include <memory>
24
#include <unordered_set>
25
#include <vector>
26
27
#include "core/arena.h"
28
#include "core/block/column_with_type_and_name.h"
29
#include "core/block/columns_with_type_and_name.h"
30
#include "core/column/column.h"
31
#include "core/column/column_string.h"
32
#include "core/data_type/data_type.h"
33
#include "core/data_type/primitive_type.h"
34
#include "core/data_type_serde/data_type_serde.h"
35
#include "core/string_ref.h"
36
#include "core/value/jsonb_value.h"
37
#include "runtime/descriptors.h"
38
#include "storage/tablet/tablet_schema.h"
39
#include "util/jsonb_document.h"
40
#include "util/jsonb_stream.h"
41
#include "util/jsonb_writer.h"
42
43
namespace doris {
44
45
void JsonbSerializeUtil::block_to_jsonb(const TabletSchema& schema, const Block& block,
46
                                        ColumnString& dst, int num_cols,
47
                                        const DataTypeSerDeSPtrs& serdes,
48
1.48k
                                        const std::unordered_set<int32_t>& row_store_cids) {
49
1.48k
    auto num_rows = block.rows();
50
1.48k
    Arena arena;
51
1.48k
    assert(num_cols <= block.columns());
52
1.48k
    DataTypeSerDe::FormatOptions options;
53
1.48k
    auto tz = cctz::utc_time_zone();
54
1.48k
    options.timezone = &tz;
55
1.91M
    for (int i = 0; i < num_rows; ++i) {
56
1.91M
        JsonbWriterT<JsonbOutStream> jsonb_writer;
57
1.91M
        jsonb_writer.writeStartObject();
58
39.3M
        for (int j = 0; j < num_cols; ++j) {
59
37.4M
            const auto& column = block.get_by_position(j).column;
60
37.4M
            const auto& tablet_column = *schema.columns()[j];
61
            // ignore row store columns
62
37.4M
            if (tablet_column.is_row_store_column()) {
63
1.91M
                continue;
64
1.91M
            }
65
            // TODO improve performance for checking column in group
66
35.5M
            if (row_store_cids.empty() || row_store_cids.contains(tablet_column.unique_id())) {
67
24.0M
                serdes[j]->write_one_cell_to_jsonb(*column, jsonb_writer, arena,
68
24.0M
                                                   tablet_column.unique_id(), i, options);
69
24.0M
            }
70
35.5M
        }
71
1.91M
        jsonb_writer.writeEndObject();
72
1.91M
        dst.insert_data(jsonb_writer.getOutput()->getBuffer(), jsonb_writer.getOutput()->getSize());
73
1.91M
    }
74
1.48k
}
75
76
// batch rows
77
Status JsonbSerializeUtil::jsonb_to_block(
78
        const DataTypeSerDeSPtrs& serdes, const ColumnString& jsonb_column,
79
        const std::unordered_map<uint32_t, uint32_t>& col_id_to_idx, Block& dst,
80
        const std::vector<std::string>& default_values,
81
340
        const std::unordered_set<int>& include_cids) {
82
2.59k
    for (int i = 0; i < jsonb_column.size(); ++i) {
83
2.25k
        StringRef jsonb_data = jsonb_column.get_data_at(i);
84
2.25k
        RETURN_IF_ERROR(jsonb_to_block(serdes, jsonb_data.data, jsonb_data.size, col_id_to_idx, dst,
85
2.25k
                                       default_values, include_cids));
86
2.25k
    }
87
340
    return Status::OK();
88
340
}
89
90
// single row
91
Status JsonbSerializeUtil::jsonb_to_block(
92
        const DataTypeSerDeSPtrs& serdes, const char* data, size_t size,
93
        const std::unordered_map<uint32_t, uint32_t>& col_id_to_idx, Block& dst,
94
        const std::vector<std::string>& default_values,
95
6.96k
        const std::unordered_set<int>& include_cids) {
96
6.96k
    const JsonbDocument* pdoc = nullptr;
97
6.96k
    RETURN_IF_ERROR(JsonbDocument::checkAndCreateDocument(data, size, &pdoc));
98
6.96k
    const JsonbDocument& doc = *pdoc;
99
6.96k
    size_t num_rows = dst.rows();
100
6.96k
    size_t filled_columns = 0;
101
117k
    for (auto it = doc->begin(); it != doc->end(); ++it) {
102
110k
        auto col_it = col_id_to_idx.find(it->getKeyId());
103
110k
        if (col_it != col_id_to_idx.end() &&
104
110k
            (include_cids.empty() || include_cids.contains(it->getKeyId()))) {
105
95.4k
            MutableColumnPtr dst_column =
106
95.4k
                    dst.get_by_position(col_it->second).column->assume_mutable();
107
95.4k
            serdes[col_it->second]->read_one_cell_from_jsonb(*dst_column, it->value());
108
95.4k
            ++filled_columns;
109
95.4k
        }
110
110k
    }
111
6.96k
    if (filled_columns >= dst.columns()) {
112
5.75k
        return Status::OK();
113
5.75k
    }
114
16.6k
    auto fill_column = [&](Block& dst, int pos, size_t old_num_rows) {
115
16.6k
        MutableColumnPtr dst_column = dst.get_by_position(pos).column->assume_mutable();
116
16.6k
        if (dst_column->size() < old_num_rows + 1) {
117
1.22k
            DCHECK(dst_column->size() == old_num_rows);
118
1.22k
            if (default_values[pos].empty()) {
119
1.18k
                dst_column->insert_default();
120
1.18k
            } else {
121
39
                Slice value(default_values[pos].data(), default_values[pos].size());
122
39
                DataTypeSerDe::FormatOptions opt;
123
39
                opt.converted_from_string = true;
124
39
                RETURN_IF_ERROR(
125
39
                        serdes[pos]->deserialize_one_cell_from_json(*dst_column, value, opt));
126
39
            }
127
1.22k
        }
128
16.6k
        DCHECK(dst_column->size() == num_rows + 1);
129
16.6k
        return Status::OK();
130
16.6k
    };
131
    // fill missing column
132
1.20k
    if (!include_cids.empty()) {
133
18
        for (auto cid : include_cids) {
134
18
            auto col_it = col_id_to_idx.find(cid);
135
18
            if (col_it == col_id_to_idx.end()) {
136
2
                continue;
137
2
            }
138
16
            RETURN_IF_ERROR(fill_column(dst, col_it->second, num_rows));
139
16
        }
140
1.19k
    } else {
141
17.8k
        for (int i = 0; i < dst.columns(); ++i) {
142
16.6k
            RETURN_IF_ERROR(fill_column(dst, i, num_rows));
143
16.6k
        }
144
1.19k
    }
145
1.20k
    return Status::OK();
146
1.20k
}
147
148
} // namespace doris