Coverage Report

Created: 2026-09-23 12:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/jsonb_utils.h
Line
Count
Source
1
/*
2
 *  Copyright (c) 2014, Facebook, Inc.
3
 *  All rights reserved.
4
 *
5
 *  This source code is licensed under the BSD-style license found in the
6
 *  LICENSE file in the root directory of this source tree. An additional grant
7
 *  of patent rights can be found in the PATENTS file in the same directory.
8
 *
9
 */
10
11
/*
12
 * This header file defines miscellaneous utility classes.
13
 *
14
 * @author Tian Xia <tianx@fb.com>
15
 *
16
 *  this file is copied from 
17
 * https://github.com/facebook/mysql-5.6/blob/fb-mysql-5.6.35/fbson/FbsonUtil.h
18
 * and modified by Doris
19
 */
20
21
#ifndef JSONB_JSONBUTIL_H
22
#define JSONB_JSONBUTIL_H
23
24
#include "common/status.h"
25
#include "util/jsonb_document.h"
26
#include "util/jsonb_stream.h"
27
28
namespace doris {
29
30
39.3k
#define OUT_BUF_SIZE 1024
31
32
/*
33
 * JsonbToJson converts an JsonbValue object to a JSON string.
34
 */
35
class JsonbToJson {
36
public:
37
39.3k
    JsonbToJson() : os_(buffer_, OUT_BUF_SIZE) {}
38
39
    // get json string
40
37.8k
    std::string to_json_string(const char* data, size_t size) {
41
37.8k
        const JsonbDocument* pdoc;
42
37.8k
        THROW_IF_ERROR(doris::JsonbDocument::checkAndCreateDocument(data, size, &pdoc));
43
37.8k
        return to_json_string(pdoc->getValue());
44
37.8k
    }
45
46
39.3k
    std::string to_json_string(const JsonbValue* val) {
47
39.3k
        os_.clear();
48
39.3k
        os_.seekp(0);
49
50
39.3k
        if (val) {
51
39.3k
            intern_json(val);
52
39.3k
        }
53
54
39.3k
        os_.put(0);
55
56
39.3k
        std::string json_string {os_.getBuffer()};
57
39.3k
        return json_string;
58
39.3k
    }
59
60
37.8k
    static std::string jsonb_to_json_string(const char* data, size_t size) {
61
37.8k
        JsonbToJson jsonb_to_json;
62
37.8k
        return jsonb_to_json.to_json_string(data, size);
63
37.8k
    }
64
65
private:
66
    // recursively convert JsonbValue
67
111k
    void intern_json(const JsonbValue* val) {
68
111k
        switch (val->type) {
69
3.21k
        case JsonbType::T_Null: {
70
3.21k
            os_.write("null", 4);
71
3.21k
            break;
72
0
        }
73
3.20k
        case JsonbType::T_True: {
74
3.20k
            os_.write("true", 4);
75
3.20k
            break;
76
0
        }
77
3.06k
        case JsonbType::T_False: {
78
3.06k
            os_.write("false", 5);
79
3.06k
            break;
80
0
        }
81
12.6k
        case JsonbType::T_Int8: {
82
12.6k
            os_.write(val->unpack<JsonbInt8Val>()->val());
83
12.6k
            break;
84
0
        }
85
13.0k
        case JsonbType::T_Int16: {
86
13.0k
            os_.write(val->unpack<JsonbInt16Val>()->val());
87
13.0k
            break;
88
0
        }
89
1.96k
        case JsonbType::T_Int32: {
90
1.96k
            os_.write(val->unpack<JsonbInt32Val>()->val());
91
1.96k
            break;
92
0
        }
93
2.58k
        case JsonbType::T_Int64: {
94
2.58k
            os_.write(val->unpack<JsonbInt64Val>()->val());
95
2.58k
            break;
96
0
        }
97
6.74k
        case JsonbType::T_Double: {
98
6.74k
            os_.write(val->unpack<JsonbDoubleVal>()->val());
99
6.74k
            break;
100
0
        }
101
2
        case JsonbType::T_Float: {
102
2
            os_.write(val->unpack<JsonbFloatVal>()->val());
103
2
            break;
104
0
        }
105
2.07k
        case JsonbType::T_Int128: {
106
2.07k
            os_.write(val->unpack<JsonbInt128Val>()->val());
107
2.07k
            break;
108
0
        }
109
31.4k
        case JsonbType::T_String: {
110
            // getBlobLen() is the stored payload length. length() would drop a
111
            // trailing NUL that belongs to the value, so it cannot be used here;
112
            // the writer stores strings by their exact length and never pads.
113
31.4k
            string_to_json(val->unpack<JsonbStringVal>()->getBlob(),
114
31.4k
                           val->unpack<JsonbStringVal>()->getBlobLen());
115
31.4k
            break;
116
0
        }
117
0
        case JsonbType::T_Binary: {
118
0
            os_.write("\"<BINARY>", 9);
119
0
            os_.write(val->unpack<JsonbBinaryVal>()->getBlob(),
120
0
                      val->unpack<JsonbBinaryVal>()->getBlobLen());
121
0
            os_.write("<BINARY>\"", 9);
122
0
            break;
123
0
        }
124
19.3k
        case JsonbType::T_Object: {
125
19.3k
            object_to_json(val->unpack<ObjectVal>());
126
19.3k
            break;
127
0
        }
128
11.6k
        case JsonbType::T_Array: {
129
11.6k
            array_to_json(val->unpack<ArrayVal>());
130
11.6k
            break;
131
0
        }
132
30
        case JsonbType::T_Decimal32: {
133
30
            const auto* decimal_val = val->unpack<JsonbDecimal32>();
134
30
            decimal_to_json(Decimal32 {decimal_val->val()}, decimal_val->precision,
135
30
                            decimal_val->scale);
136
30
            break;
137
0
        }
138
31
        case JsonbType::T_Decimal64: {
139
31
            const auto* decimal_val = val->unpack<JsonbDecimal64>();
140
31
            decimal_to_json(Decimal64 {decimal_val->val()}, decimal_val->precision,
141
31
                            decimal_val->scale);
142
31
            break;
143
0
        }
144
40
        case JsonbType::T_Decimal128: {
145
40
            const auto* decimal_val = val->unpack<JsonbDecimal128>();
146
40
            decimal_to_json(Decimal128V3 {decimal_val->val()}, decimal_val->precision,
147
40
                            decimal_val->scale);
148
40
            break;
149
0
        }
150
1
        case JsonbType::T_Decimal256: {
151
1
            const auto* decimal_val = val->unpack<JsonbDecimal256>();
152
1
            decimal_to_json(Decimal256 {decimal_val->val()}, decimal_val->precision,
153
1
                            decimal_val->scale);
154
1
            break;
155
0
        }
156
0
        default:
157
0
            break;
158
111k
        }
159
111k
    }
160
161
1.78k
    void string_to_json(const char* str, size_t len) {
162
1.78k
        os_.put('"');
163
1.78k
        if (nullptr == str) {
164
3
            os_.put('"');
165
3
            return;
166
3
        }
167
1.77k
        char char_buffer[16];
168
        // A JSON string may legally contain U+0000, so the loop must be bounded by
169
        // the length only; the NUL itself is escaped as \u0000 by the default branch.
170
2.05M
        for (const char* ptr = str; ptr != str + len; ++ptr) {
171
2.05M
            if ((unsigned char)*ptr > 31 && *ptr != '\"' && *ptr != '\\') {
172
2.05M
                os_.put(*ptr);
173
2.05M
            } else {
174
13
                os_.put('\\');
175
13
                unsigned char token;
176
13
                switch (token = *ptr) {
177
0
                case '\\':
178
0
                    os_.put('\\');
179
0
                    break;
180
1
                case '\"':
181
1
                    os_.put('\"');
182
1
                    break;
183
1
                case '\b':
184
1
                    os_.put('b');
185
1
                    break;
186
1
                case '\f':
187
1
                    os_.put('f');
188
1
                    break;
189
1
                case '\n':
190
1
                    os_.put('n');
191
1
                    break;
192
1
                case '\r':
193
1
                    os_.put('r');
194
1
                    break;
195
1
                case '\t':
196
1
                    os_.put('t');
197
1
                    break;
198
7
                default: {
199
7
                    int char_num = snprintf(char_buffer, sizeof(char_buffer), "u%04x", token);
200
7
                    os_.write(char_buffer, char_num);
201
7
                    break;
202
0
                }
203
13
                }
204
13
            }
205
2.05M
        }
206
1.77k
        os_.put('"');
207
1.77k
    }
208
209
    // convert object
210
19.3k
    void object_to_json(const ObjectVal* val) {
211
19.3k
        os_.put('{');
212
213
19.3k
        auto iter = val->begin();
214
19.3k
        auto iter_fence = val->end();
215
216
57.5k
        while (iter < iter_fence) {
217
            // write key
218
38.1k
            if (iter->klen()) {
219
35.9k
                string_to_json(iter->getKeyStr(), iter->klen());
220
35.9k
            } else {
221
                // NOTE: we use sMaxKeyId to represent an empty key. see jsonb_writer.h
222
2.20k
                if (iter->getKeyId() == JsonbKeyValue::sMaxKeyId) {
223
2.20k
                    string_to_json(nullptr, 0);
224
2.20k
                } else {
225
0
                    os_.write(iter->getKeyId());
226
0
                }
227
2.20k
            }
228
38.1k
            os_.put(':');
229
230
            // convert value
231
38.1k
            intern_json(iter->value());
232
233
38.1k
            ++iter;
234
38.1k
            if (iter != iter_fence) {
235
20.4k
                os_.put(',');
236
20.4k
            }
237
38.1k
        }
238
239
19.3k
        assert(iter == iter_fence);
240
241
19.3k
        os_.put('}');
242
19.3k
    }
243
244
    // convert array to json
245
11.6k
    void array_to_json(const ArrayVal* val) {
246
11.6k
        os_.put('[');
247
248
11.6k
        auto iter = val->begin();
249
11.6k
        auto iter_fence = val->end();
250
251
45.1k
        while (iter != iter_fence) {
252
            // convert value
253
33.5k
            intern_json((const JsonbValue*)iter);
254
33.5k
            ++iter;
255
33.5k
            if (iter != iter_fence) {
256
23.3k
                os_.put(',');
257
23.3k
            }
258
33.5k
        }
259
260
11.6k
        assert(iter == iter_fence);
261
262
11.6k
        os_.put(']');
263
11.6k
    }
264
265
    template <JsonbDecimalType T>
266
    void decimal_to_json(const T& value, const uint32_t precision, const uint32_t scale);
267
268
    JsonbOutStream os_;
269
    char buffer_[OUT_BUF_SIZE];
270
};
271
272
} // namespace doris
273
274
#endif // JSONB_JSONBUTIL_H