Coverage Report

Created: 2024-11-22 12:06

/root/doris/be/src/vec/jsonb/serialize.cpp
Line
Count
Source (jump to first uncovered line)
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 "vec/jsonb/serialize.h"
19
20
#include <assert.h>
21
22
#include <algorithm>
23
#include <memory>
24
#include <vector>
25
26
#include "olap/tablet_schema.h"
27
#include "runtime/descriptors.h"
28
#include "runtime/jsonb_value.h"
29
#include "runtime/primitive_type.h"
30
#include "runtime/types.h"
31
#include "util/jsonb_document.h"
32
#include "util/jsonb_stream.h"
33
#include "util/jsonb_writer.h"
34
#include "vec/columns/column.h"
35
#include "vec/columns/column_string.h"
36
#include "vec/common/arena.h"
37
#include "vec/common/string_ref.h"
38
#include "vec/core/column_with_type_and_name.h"
39
#include "vec/core/columns_with_type_and_name.h"
40
#include "vec/data_types/data_type.h"
41
#include "vec/data_types/serde/data_type_serde.h"
42
#include "vec/io/reader_buffer.h"
43
44
namespace doris::vectorized {
45
46
void JsonbSerializeUtil::block_to_jsonb(const TabletSchema& schema, const Block& block,
47
                                        ColumnString& dst, int num_cols,
48
4
                                        const DataTypeSerDeSPtrs& serdes) {
49
4
    auto num_rows = block.rows();
50
4
    Arena pool;
51
4
    assert(num_cols <= block.columns());
52
1.03k
    for (int i = 0; i < num_rows; ++i) {
53
1.03k
        JsonbWriterT<JsonbOutStream> jsonb_writer;
54
1.03k
        jsonb_writer.writeStartObject();
55
10.2k
        for (int j = 0; j < num_cols; ++j) {
56
9.22k
            const auto& column = block.get_by_position(j).column;
57
9.22k
            const auto& tablet_column = *schema.columns()[j];
58
9.22k
            if (tablet_column.is_row_store_column()) {
59
                // ignore dst row store column
60
0
                continue;
61
0
            }
62
9.22k
            serdes[j]->write_one_cell_to_jsonb(*column, jsonb_writer, &pool,
63
9.22k
                                               tablet_column.unique_id(), i);
64
9.22k
        }
65
1.03k
        jsonb_writer.writeEndObject();
66
1.03k
        dst.insert_data(jsonb_writer.getOutput()->getBuffer(), jsonb_writer.getOutput()->getSize());
67
1.03k
    }
68
4
}
69
70
// batch rows
71
void JsonbSerializeUtil::jsonb_to_block(const DataTypeSerDeSPtrs& serdes,
72
                                        const ColumnString& jsonb_column,
73
                                        const std::unordered_map<uint32_t, uint32_t>& col_id_to_idx,
74
                                        Block& dst,
75
4
                                        const std::vector<std::string>& default_values) {
76
1.03k
    for (int i = 0; i < jsonb_column.size(); ++i) {
77
1.03k
        StringRef jsonb_data = jsonb_column.get_data_at(i);
78
1.03k
        jsonb_to_block(serdes, jsonb_data.data, jsonb_data.size, col_id_to_idx, dst,
79
1.03k
                       default_values);
80
1.03k
    }
81
4
}
82
83
// single row
84
void JsonbSerializeUtil::jsonb_to_block(const DataTypeSerDeSPtrs& serdes, const char* data,
85
                                        size_t size,
86
                                        const std::unordered_map<uint32_t, uint32_t>& col_id_to_idx,
87
                                        Block& dst,
88
1.03k
                                        const std::vector<std::string>& default_values) {
89
1.03k
    auto pdoc = JsonbDocument::createDocument(data, size);
90
1.03k
    JsonbDocument& doc = *pdoc;
91
1.03k
    size_t num_rows = dst.rows();
92
1.03k
    size_t filled_columns = 0;
93
10.2k
    for (auto it = doc->begin(); it != doc->end(); ++it) {
94
9.22k
        auto col_it = col_id_to_idx.find(it->getKeyId());
95
9.22k
        if (col_it != col_id_to_idx.end()) {
96
9.22k
            MutableColumnPtr dst_column =
97
9.22k
                    dst.get_by_position(col_it->second).column->assume_mutable();
98
9.22k
            serdes[col_it->second]->read_one_cell_from_jsonb(*dst_column, it->value());
99
9.22k
            ++filled_columns;
100
9.22k
        }
101
9.22k
    }
102
1.03k
    if (filled_columns < dst.columns()) {
103
        // fill missing slot
104
0
        for (int i = 0; i < dst.columns(); ++i) {
105
0
            const auto& column_type_name = dst.get_by_position(i);
106
0
            MutableColumnPtr col = column_type_name.column->assume_mutable();
107
0
            if (col->size() < num_rows + 1) {
108
0
                DCHECK(col->size() == num_rows);
109
0
                if (default_values[i].empty()) {
110
0
                    col->insert_default();
111
0
                } else {
112
0
                    Slice value(default_values[i].data(), default_values[i].size());
113
0
                    DataTypeSerDe::FormatOptions opt;
114
0
                    opt.converted_from_string = true;
115
0
                    static_cast<void>(serdes[i]->deserialize_one_cell_from_json(*col, value, opt));
116
0
                }
117
0
            }
118
0
            DCHECK(col->size() == num_rows + 1);
119
0
        }
120
0
    }
121
1.03k
}
122
123
} // namespace doris::vectorized