Coverage Report

Created: 2026-05-25 13:19

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/defer_op.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
5
                                        const std::unordered_set<int32_t>& row_store_cids) {
50
5
    auto num_rows = block.rows();
51
5
    Arena arena;
52
5
    assert(num_cols <= block.columns());
53
5
    DataTypeSerDe::FormatOptions options;
54
5
    auto tz = cctz::utc_time_zone();
55
5
    options.timezone = &tz;
56
1.03k
    for (int i = 0; i < num_rows; ++i) {
57
1.03k
        JsonbWriterT<JsonbOutStream> jsonb_writer;
58
1.03k
        jsonb_writer.writeStartObject();
59
10.2k
        for (int j = 0; j < num_cols; ++j) {
60
9.23k
            const auto& column = block.get_by_position(j).column;
61
9.23k
            const auto& tablet_column = *schema.columns()[j];
62
            // ignore row store columns
63
9.23k
            if (tablet_column.is_row_store_column()) {
64
0
                continue;
65
0
            }
66
            // TODO improve performance for checking column in group
67
9.23k
            if (row_store_cids.empty() || row_store_cids.contains(tablet_column.unique_id())) {
68
9.23k
                serdes[j]->write_one_cell_to_jsonb(*column, jsonb_writer, arena,
69
9.23k
                                                   tablet_column.unique_id(), i, options);
70
9.23k
            }
71
9.23k
        }
72
1.03k
        jsonb_writer.writeEndObject();
73
1.03k
        dst.insert_data(jsonb_writer.getOutput()->getBuffer(), jsonb_writer.getOutput()->getSize());
74
1.03k
    }
75
5
}
76
77
// batch rows
78
Status JsonbSerializeUtil::jsonb_to_block(
79
        const DataTypeSerDeSPtrs& serdes, const ColumnString& jsonb_column,
80
        const std::unordered_map<uint32_t, uint32_t>& col_id_to_idx, Block& dst,
81
        const std::vector<std::string>& default_values,
82
6
        const std::unordered_set<int>& include_cids) {
83
6
    auto dst_columns_guard = dst.mutate_columns_scoped();
84
6
    MutableColumns& dst_columns = dst_columns_guard.mutable_columns();
85
1.04k
    for (int i = 0; i < jsonb_column.size(); ++i) {
86
1.03k
        StringRef jsonb_data = jsonb_column.get_data_at(i);
87
1.03k
        RETURN_IF_ERROR(jsonb_to_columns(serdes, jsonb_data.data, jsonb_data.size, col_id_to_idx,
88
1.03k
                                         dst_columns, default_values, include_cids));
89
1.03k
    }
90
6
    return Status::OK();
91
6
}
92
93
Status JsonbSerializeUtil::jsonb_to_columns(
94
        const DataTypeSerDeSPtrs& serdes, const char* data, size_t size,
95
        const std::unordered_map<uint32_t, uint32_t>& col_id_to_idx, MutableColumns& dst_columns,
96
        const std::vector<std::string>& default_values,
97
1.03k
        const std::unordered_set<int>& include_cids) {
98
1.03k
    const JsonbDocument* pdoc = nullptr;
99
1.03k
    RETURN_IF_ERROR(JsonbDocument::checkAndCreateDocument(data, size, &pdoc));
100
1.03k
    const JsonbDocument& doc = *pdoc;
101
1.03k
    DCHECK(!dst_columns.empty());
102
1.03k
    size_t num_rows = dst_columns[0]->size();
103
1.03k
    size_t filled_columns = 0;
104
10.3k
    for (auto it = doc->begin(); it != doc->end(); ++it) {
105
9.33k
        auto col_it = col_id_to_idx.find(it->getKeyId());
106
9.33k
        if (col_it != col_id_to_idx.end() &&
107
9.33k
            (include_cids.empty() || include_cids.contains(it->getKeyId()))) {
108
9.33k
            auto& dst_column = dst_columns[col_it->second];
109
9.33k
            serdes[col_it->second]->read_one_cell_from_jsonb(*dst_column, it->value());
110
9.33k
            ++filled_columns;
111
9.33k
        }
112
9.33k
    }
113
1.03k
    if (filled_columns >= dst_columns.size()) {
114
1.03k
        return Status::OK();
115
1.03k
    }
116
0
    auto fill_column = [&](size_t pos, size_t old_num_rows) {
117
0
        auto& dst_column = dst_columns[pos];
118
0
        if (dst_column->size() < old_num_rows + 1) {
119
0
            DCHECK(dst_column->size() == old_num_rows);
120
0
            Status st = Status::OK();
121
0
            if (default_values[pos].empty()) {
122
0
                dst_column->insert_default();
123
0
            } else {
124
0
                Slice value(default_values[pos].data(), default_values[pos].size());
125
0
                DataTypeSerDe::FormatOptions opt;
126
0
                opt.converted_from_string = true;
127
0
                st = serdes[pos]->deserialize_one_cell_from_json(*dst_column, value, opt);
128
0
            }
129
0
            RETURN_IF_ERROR(st);
130
0
            DCHECK(dst_column->size() == num_rows + 1);
131
0
            return Status::OK();
132
0
        }
133
0
        DCHECK(dst_column->size() == num_rows + 1);
134
0
        return Status::OK();
135
0
    };
136
    // fill missing column
137
0
    if (!include_cids.empty()) {
138
0
        for (auto cid : include_cids) {
139
0
            auto col_it = col_id_to_idx.find(cid);
140
0
            if (col_it == col_id_to_idx.end()) {
141
0
                continue;
142
0
            }
143
0
            RETURN_IF_ERROR(fill_column(static_cast<size_t>(col_it->second), num_rows));
144
0
        }
145
0
    } else {
146
0
        for (size_t i = 0; i < dst_columns.size(); ++i) {
147
0
            RETURN_IF_ERROR(fill_column(i, num_rows));
148
0
        }
149
0
    }
150
0
    return Status::OK();
151
0
}
152
153
// single row
154
Status JsonbSerializeUtil::jsonb_to_block(
155
        const DataTypeSerDeSPtrs& serdes, const char* data, size_t size,
156
        const std::unordered_map<uint32_t, uint32_t>& col_id_to_idx, Block& dst,
157
        const std::vector<std::string>& default_values,
158
0
        const std::unordered_set<int>& include_cids) {
159
0
    auto dst_columns_guard = dst.mutate_columns_scoped();
160
0
    MutableColumns& dst_columns = dst_columns_guard.mutable_columns();
161
0
    return jsonb_to_columns(serdes, data, size, col_id_to_idx, dst_columns, default_values,
162
0
                            include_cids);
163
0
}
164
165
} // namespace doris