Coverage Report

Created: 2026-03-11 12:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/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
4
                                        const std::unordered_set<int32_t>& row_store_cids) {
49
4
    auto num_rows = block.rows();
50
4
    Arena arena;
51
4
    assert(num_cols <= block.columns());
52
4
    DataTypeSerDe::FormatOptions options;
53
4
    auto tz = cctz::utc_time_zone();
54
4
    options.timezone = &tz;
55
1.03k
    for (int i = 0; i < num_rows; ++i) {
56
1.03k
        JsonbWriterT<JsonbOutStream> jsonb_writer;
57
1.03k
        jsonb_writer.writeStartObject();
58
10.2k
        for (int j = 0; j < num_cols; ++j) {
59
9.22k
            const auto& column = block.get_by_position(j).column;
60
9.22k
            const auto& tablet_column = *schema.columns()[j];
61
            // ignore row store columns
62
9.22k
            if (tablet_column.is_row_store_column()) {
63
0
                continue;
64
0
            }
65
            // TODO improve performance for checking column in group
66
9.22k
            if (row_store_cids.empty() || row_store_cids.contains(tablet_column.unique_id())) {
67
9.22k
                serdes[j]->write_one_cell_to_jsonb(*column, jsonb_writer, arena,
68
9.22k
                                                   tablet_column.unique_id(), i, options);
69
9.22k
            }
70
9.22k
        }
71
1.03k
        jsonb_writer.writeEndObject();
72
1.03k
        dst.insert_data(jsonb_writer.getOutput()->getBuffer(), jsonb_writer.getOutput()->getSize());
73
1.03k
    }
74
4
}
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
4
        const std::unordered_set<int>& include_cids) {
82
1.03k
    for (int i = 0; i < jsonb_column.size(); ++i) {
83
1.03k
        StringRef jsonb_data = jsonb_column.get_data_at(i);
84
1.03k
        RETURN_IF_ERROR(jsonb_to_block(serdes, jsonb_data.data, jsonb_data.size, col_id_to_idx, dst,
85
1.03k
                                       default_values, include_cids));
86
1.03k
    }
87
4
    return Status::OK();
88
4
}
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
1.03k
        const std::unordered_set<int>& include_cids) {
96
1.03k
    const JsonbDocument* pdoc = nullptr;
97
1.03k
    RETURN_IF_ERROR(JsonbDocument::checkAndCreateDocument(data, size, &pdoc));
98
1.03k
    const JsonbDocument& doc = *pdoc;
99
1.03k
    size_t num_rows = dst.rows();
100
1.03k
    size_t filled_columns = 0;
101
10.2k
    for (auto it = doc->begin(); it != doc->end(); ++it) {
102
9.22k
        auto col_it = col_id_to_idx.find(it->getKeyId());
103
9.22k
        if (col_it != col_id_to_idx.end() &&
104
9.22k
            (include_cids.empty() || include_cids.contains(it->getKeyId()))) {
105
9.22k
            MutableColumnPtr dst_column =
106
9.22k
                    dst.get_by_position(col_it->second).column->assume_mutable();
107
9.22k
            serdes[col_it->second]->read_one_cell_from_jsonb(*dst_column, it->value());
108
9.22k
            ++filled_columns;
109
9.22k
        }
110
9.22k
    }
111
1.03k
    if (filled_columns >= dst.columns()) {
112
1.03k
        return Status::OK();
113
1.03k
    }
114
0
    auto fill_column = [&](Block& dst, int pos, size_t old_num_rows) {
115
0
        MutableColumnPtr dst_column = dst.get_by_position(pos).column->assume_mutable();
116
0
        if (dst_column->size() < old_num_rows + 1) {
117
0
            DCHECK(dst_column->size() == old_num_rows);
118
0
            if (default_values[pos].empty()) {
119
0
                dst_column->insert_default();
120
0
            } else {
121
0
                Slice value(default_values[pos].data(), default_values[pos].size());
122
0
                DataTypeSerDe::FormatOptions opt;
123
0
                opt.converted_from_string = true;
124
0
                RETURN_IF_ERROR(
125
0
                        serdes[pos]->deserialize_one_cell_from_json(*dst_column, value, opt));
126
0
            }
127
0
        }
128
0
        DCHECK(dst_column->size() == num_rows + 1);
129
0
        return Status::OK();
130
0
    };
131
    // fill missing column
132
0
    if (!include_cids.empty()) {
133
0
        for (auto cid : include_cids) {
134
0
            auto col_it = col_id_to_idx.find(cid);
135
0
            if (col_it == col_id_to_idx.end()) {
136
0
                continue;
137
0
            }
138
0
            RETURN_IF_ERROR(fill_column(dst, col_it->second, num_rows));
139
0
        }
140
0
    } else {
141
0
        for (int i = 0; i < dst.columns(); ++i) {
142
0
            RETURN_IF_ERROR(fill_column(dst, i, num_rows));
143
0
        }
144
0
    }
145
0
    return Status::OK();
146
0
}
147
148
} // namespace doris