Coverage Report

Created: 2026-05-18 19:20

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 <limits>
24
#include <memory>
25
#include <unordered_set>
26
#include <vector>
27
28
#include "core/arena.h"
29
#include "core/block/column_with_type_and_name.h"
30
#include "core/block/columns_with_type_and_name.h"
31
#include "core/column/column.h"
32
#include "core/column/column_string.h"
33
#include "core/data_type/data_type.h"
34
#include "core/data_type/primitive_type.h"
35
#include "core/data_type_serde/data_type_serde.h"
36
#include "core/string_ref.h"
37
#include "core/value/jsonb_value.h"
38
#include "runtime/descriptors.h"
39
#include "storage/tablet/tablet_schema.h"
40
#include "util/jsonb_document.h"
41
#include "util/jsonb_stream.h"
42
#include "util/jsonb_writer.h"
43
44
namespace doris {
45
46
void JsonbSerializeUtil::block_to_jsonb(const TabletSchema& schema, const Block& block,
47
                                        ColumnString& dst, int num_cols,
48
                                        const DataTypeSerDeSPtrs& serdes,
49
4
                                        const std::unordered_set<int32_t>& row_store_cids) {
50
4
    block_to_jsonb(schema, block, dst, num_cols, serdes, row_store_cids, 0, block.rows());
51
4
}
52
53
void JsonbSerializeUtil::block_to_jsonb(const TabletSchema& schema, const Block& block,
54
                                        ColumnString& dst, int num_cols,
55
                                        const DataTypeSerDeSPtrs& serdes,
56
                                        const std::unordered_set<int32_t>& row_store_cids,
57
4
                                        size_t row_pos, size_t num_rows) {
58
4
    static_cast<void>(block_to_jsonb(schema, block, dst, num_cols, serdes, row_store_cids, row_pos,
59
4
                                     num_rows, std::numeric_limits<size_t>::max()));
60
4
}
61
62
size_t JsonbSerializeUtil::block_to_jsonb(const TabletSchema& schema, const Block& block,
63
                                          ColumnString& dst, int num_cols,
64
                                          const DataTypeSerDeSPtrs& serdes,
65
                                          const std::unordered_set<int32_t>& row_store_cids,
66
4
                                          size_t row_pos, size_t num_rows, size_t max_bytes) {
67
4
    Arena arena;
68
4
    assert(num_cols <= block.columns());
69
4
    assert(row_pos + num_rows <= block.rows());
70
4
    DataTypeSerDe::FormatOptions options;
71
4
    auto tz = cctz::utc_time_zone();
72
4
    options.timezone = &tz;
73
4
    size_t written_rows = 0;
74
1.03k
    for (size_t i = row_pos; i < row_pos + num_rows; ++i) {
75
1.03k
        JsonbWriterT<JsonbOutStream> jsonb_writer;
76
1.03k
        jsonb_writer.writeStartObject();
77
10.2k
        for (int j = 0; j < num_cols; ++j) {
78
9.22k
            const auto& column = block.get_by_position(j).column;
79
9.22k
            const auto& tablet_column = *schema.columns()[j];
80
            // ignore row store columns
81
9.22k
            if (tablet_column.is_row_store_column()) {
82
0
                continue;
83
0
            }
84
            // TODO improve performance for checking column in group
85
9.22k
            if (row_store_cids.empty() || row_store_cids.contains(tablet_column.unique_id())) {
86
9.22k
                serdes[j]->write_one_cell_to_jsonb(*column, jsonb_writer, arena,
87
9.22k
                                                   tablet_column.unique_id(), i, options);
88
9.22k
            }
89
9.22k
        }
90
1.03k
        jsonb_writer.writeEndObject();
91
1.03k
        dst.insert_data(jsonb_writer.getOutput()->getBuffer(), jsonb_writer.getOutput()->getSize());
92
1.03k
        ++written_rows;
93
1.03k
        if (dst.byte_size() >= max_bytes) {
94
0
            break;
95
0
        }
96
1.03k
    }
97
4
    return written_rows;
98
4
}
99
100
// batch rows
101
Status JsonbSerializeUtil::jsonb_to_block(
102
        const DataTypeSerDeSPtrs& serdes, const ColumnString& jsonb_column,
103
        const std::unordered_map<uint32_t, uint32_t>& col_id_to_idx, Block& dst,
104
        const std::vector<std::string>& default_values,
105
4
        const std::unordered_set<int>& include_cids) {
106
1.03k
    for (int i = 0; i < jsonb_column.size(); ++i) {
107
1.03k
        StringRef jsonb_data = jsonb_column.get_data_at(i);
108
1.03k
        RETURN_IF_ERROR(jsonb_to_block(serdes, jsonb_data.data, jsonb_data.size, col_id_to_idx, dst,
109
1.03k
                                       default_values, include_cids));
110
1.03k
    }
111
4
    return Status::OK();
112
4
}
113
114
// single row
115
Status JsonbSerializeUtil::jsonb_to_block(
116
        const DataTypeSerDeSPtrs& serdes, const char* data, size_t size,
117
        const std::unordered_map<uint32_t, uint32_t>& col_id_to_idx, Block& dst,
118
        const std::vector<std::string>& default_values,
119
1.03k
        const std::unordered_set<int>& include_cids) {
120
1.03k
    const JsonbDocument* pdoc = nullptr;
121
1.03k
    RETURN_IF_ERROR(JsonbDocument::checkAndCreateDocument(data, size, &pdoc));
122
1.03k
    const JsonbDocument& doc = *pdoc;
123
1.03k
    size_t num_rows = dst.rows();
124
1.03k
    size_t filled_columns = 0;
125
10.2k
    for (auto it = doc->begin(); it != doc->end(); ++it) {
126
9.22k
        auto col_it = col_id_to_idx.find(it->getKeyId());
127
9.22k
        if (col_it != col_id_to_idx.end() &&
128
9.22k
            (include_cids.empty() || include_cids.contains(it->getKeyId()))) {
129
9.22k
            MutableColumnPtr dst_column =
130
9.22k
                    dst.get_by_position(col_it->second).column->assume_mutable();
131
9.22k
            serdes[col_it->second]->read_one_cell_from_jsonb(*dst_column, it->value());
132
9.22k
            ++filled_columns;
133
9.22k
        }
134
9.22k
    }
135
1.03k
    if (filled_columns >= dst.columns()) {
136
1.03k
        return Status::OK();
137
1.03k
    }
138
0
    auto fill_column = [&](Block& dst, int pos, size_t old_num_rows) {
139
0
        MutableColumnPtr dst_column = dst.get_by_position(pos).column->assume_mutable();
140
0
        if (dst_column->size() < old_num_rows + 1) {
141
0
            DCHECK(dst_column->size() == old_num_rows);
142
0
            if (default_values[pos].empty()) {
143
0
                dst_column->insert_default();
144
0
            } else {
145
0
                Slice value(default_values[pos].data(), default_values[pos].size());
146
0
                DataTypeSerDe::FormatOptions opt;
147
0
                opt.converted_from_string = true;
148
0
                RETURN_IF_ERROR(
149
0
                        serdes[pos]->deserialize_one_cell_from_json(*dst_column, value, opt));
150
0
            }
151
0
        }
152
0
        DCHECK(dst_column->size() == num_rows + 1);
153
0
        return Status::OK();
154
0
    };
155
    // fill missing column
156
0
    if (!include_cids.empty()) {
157
0
        for (auto cid : include_cids) {
158
0
            auto col_it = col_id_to_idx.find(cid);
159
0
            if (col_it == col_id_to_idx.end()) {
160
0
                continue;
161
0
            }
162
0
            RETURN_IF_ERROR(fill_column(dst, col_it->second, num_rows));
163
0
        }
164
0
    } else {
165
0
        for (int i = 0; i < dst.columns(); ++i) {
166
0
            RETURN_IF_ERROR(fill_column(dst, i, num_rows));
167
0
        }
168
0
    }
169
0
    return Status::OK();
170
0
}
171
172
} // namespace doris