Coverage Report

Created: 2026-07-24 22:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/jsonb_document.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 defines JsonbDocument, JsonbKeyValue, and various value classes
13
 * which are derived from JsonbValue, and a forward iterator for container
14
 * values - essentially everything that is related to JSONB binary data
15
 * structures.
16
 *
17
 * Implementation notes:
18
 *
19
 * None of the classes in this header file can be instantiated directly (i.e.
20
 * you cannot create a JsonbKeyValue or JsonbValue object - all constructors
21
 * are declared non-public). We use the classes as wrappers on the packed JSONB
22
 * bytes (serialized), and cast the classes (types) to the underlying packed
23
 * byte array.
24
 *
25
 * For the same reason, we cannot define any JSONB value class to be virtual,
26
 * since we never call constructors, and will not instantiate vtbl and vptrs.
27
 *
28
 * Therefore, the classes are defined as packed structures (i.e. no data
29
 * alignment and padding), and the private member variables of the classes are
30
 * defined precisely in the same order as the JSONB spec. This ensures we
31
 * access the packed JSONB bytes correctly.
32
 *
33
 * The packed structures are highly optimized for in-place operations with low
34
 * overhead. The reads (and in-place writes) are performed directly on packed
35
 * bytes. There is no memory allocation at all at runtime.
36
 *
37
 * For updates/writes of values that will expand the original JSONB size, the
38
 * write will fail, and the caller needs to handle buffer increase.
39
 *
40
 * ** Iterator **
41
 * Both ObjectVal class and ArrayVal class have iterator type that you can use
42
 * to declare an iterator on a container object to go through the key-value
43
 * pairs or value list. The iterator has both non-const and const types.
44
 *
45
 * Note: iterators are forward direction only.
46
 *
47
 * ** Query **
48
 * Querying into containers is through the member functions find (for key/value
49
 * pairs) and get (for array elements), and is in streaming style. We don't
50
 * need to read/scan the whole JSONB packed bytes in order to return results.
51
 * Once the key/index is found, we will stop search.  You can use text to query
52
 * both objects and array (for array, text will be converted to integer index),
53
 * and use index to retrieve from array. Array index is 0-based.
54
 *
55
 * ** External dictionary **
56
 * During query processing, you can also pass a call-back function, so the
57
 * search will first try to check if the key string exists in the dictionary.
58
 * If so, search will be based on the id instead of the key string.
59
 * @author Tian Xia <tianx@fb.com>
60
 * 
61
 * this file is copied from 
62
 * https://github.com/facebook/mysql-5.6/blob/fb-mysql-5.6.35/fbson/FbsonDocument.h
63
 * and modified by Doris
64
 */
65
66
#ifndef JSONB_JSONBDOCUMENT_H
67
#define JSONB_JSONBDOCUMENT_H
68
69
#include <algorithm>
70
#include <array>
71
#include <cctype>
72
#include <charconv>
73
#include <cmath>
74
#include <cstddef>
75
#include <cstdint>
76
#include <limits>
77
#include <string>
78
#include <string_view>
79
#include <type_traits>
80
81
#include "common/compiler_util.h" // IWYU pragma: keep
82
#include "common/status.h"
83
#include "core/data_type/define_primitive_type.h"
84
#include "core/string_ref.h"
85
#include "core/types.h"
86
#include "util/string_util.h"
87
88
// #include "util/string_parser.hpp"
89
90
// Concept to check for supported decimal types
91
template <typename T>
92
concept JsonbDecimalType =
93
        std::same_as<T, doris::Decimal256> || std::same_as<T, doris::Decimal64> ||
94
        std::same_as<T, doris::Decimal128V3> || std::same_as<T, doris::Decimal32>;
95
96
namespace doris {
97
98
template <typename T>
99
constexpr bool is_pod_v = std::is_trivial_v<T> && std::is_standard_layout_v<T>;
100
101
struct JsonbStringVal;
102
struct ObjectVal;
103
struct ArrayVal;
104
struct JsonbBinaryVal;
105
struct ContainerVal;
106
107
template <JsonbDecimalType T>
108
struct JsonbDecimalVal;
109
110
using JsonbDecimal256 = JsonbDecimalVal<Decimal256>;
111
using JsonbDecimal128 = JsonbDecimalVal<Decimal128V3>;
112
using JsonbDecimal64 = JsonbDecimalVal<Decimal64>;
113
using JsonbDecimal32 = JsonbDecimalVal<Decimal32>;
114
115
template <typename T>
116
    requires std::is_integral_v<T> || std::is_floating_point_v<T>
117
struct NumberValT;
118
119
using JsonbInt8Val = NumberValT<int8_t>;
120
using JsonbInt16Val = NumberValT<int16_t>;
121
using JsonbInt32Val = NumberValT<int32_t>;
122
using JsonbInt64Val = NumberValT<int64_t>;
123
using JsonbInt128Val = NumberValT<int128_t>;
124
using JsonbDoubleVal = NumberValT<double>;
125
using JsonbFloatVal = NumberValT<float>;
126
127
template <typename T>
128
concept JsonbPodType = (std::same_as<T, JsonbStringVal> || std::same_as<T, ObjectVal> ||
129
                        std::same_as<T, ContainerVal> || std::same_as<T, ArrayVal> ||
130
                        std::same_as<T, JsonbBinaryVal> || std::same_as<T, JsonbDecimal32> ||
131
                        std::same_as<T, JsonbDecimal64> || std::same_as<T, JsonbDecimal128> ||
132
                        std::same_as<T, JsonbDecimal256> || std::same_as<T, JsonbDecimal32> ||
133
                        std::same_as<T, JsonbInt8Val> || std::same_as<T, JsonbInt16Val> ||
134
                        std::same_as<T, JsonbInt32Val> || std::same_as<T, JsonbInt64Val> ||
135
                        std::same_as<T, JsonbInt128Val> || std::same_as<T, JsonbFloatVal> ||
136
                        std::same_as<T, JsonbFloatVal> || std::same_as<T, JsonbDoubleVal>);
137
138
2.42M
#define JSONB_VER 1
139
140
using int128_t = __int128;
141
142
// forward declaration
143
struct JsonbValue;
144
145
class JsonbOutStream;
146
147
template <class OS_TYPE>
148
class JsonbWriterT;
149
150
using JsonbWriter = JsonbWriterT<JsonbOutStream>;
151
152
const int MaxNestingLevel = 100;
153
154
/*
155
 * JsonbType defines 10 primitive types and 2 container types, as described
156
 * below.
157
 * NOTE: Do NOT modify the existing values or their order in this enum.
158
 *      You may only append new entries at the end before `NUM_TYPES`.
159
 *      This enum will be used in serialized data and/or persisted data.
160
 *      Changing existing values may break backward compatibility
161
 *      with previously stored or transmitted data.
162
 *
163
 * primitive_value ::=
164
 *   0x00        //null value (0 byte)
165
 * | 0x01        //boolean true (0 byte)
166
 * | 0x02        //boolean false (0 byte)
167
 * | 0x03 int8   //char/int8 (1 byte)
168
 * | 0x04 int16  //int16 (2 bytes)
169
 * | 0x05 int32  //int32 (4 bytes)
170
 * | 0x06 int64  //int64 (8 bytes)
171
 * | 0x07 double //floating point (8 bytes)
172
 * | 0x08 string //variable length string
173
 * | 0x09 binary //variable length binary
174
 *
175
 * container ::=
176
 *   0x0A int32 key_value_list //object, int32 is the total bytes of the object
177
 * | 0x0B int32 value_list     //array, int32 is the total bytes of the array
178
 */
179
enum class JsonbType : char {
180
    T_Null = 0x00,
181
    T_True = 0x01,
182
    T_False = 0x02,
183
    T_Int8 = 0x03,
184
    T_Int16 = 0x04,
185
    T_Int32 = 0x05,
186
    T_Int64 = 0x06,
187
    T_Double = 0x07,
188
    T_String = 0x08,
189
    T_Binary = 0x09,
190
    T_Object = 0x0A,
191
    T_Array = 0x0B,
192
    T_Int128 = 0x0C,
193
    T_Float = 0x0D,
194
    T_Decimal32 = 0x0E,  // DecimalV3 only
195
    T_Decimal64 = 0x0F,  // DecimalV3 only
196
    T_Decimal128 = 0x10, // DecimalV3 only
197
    T_Decimal256 = 0x11, // DecimalV3 only
198
    NUM_TYPES,
199
};
200
201
22
inline PrimitiveType get_primitive_type_from_json_type(JsonbType json_type) {
202
22
    switch (json_type) {
203
2
    case JsonbType::T_Null:
204
2
        return TYPE_NULL;
205
2
    case JsonbType::T_True:
206
4
    case JsonbType::T_False:
207
4
        return TYPE_BOOLEAN;
208
0
    case JsonbType::T_Int8:
209
0
        return TYPE_TINYINT;
210
0
    case JsonbType::T_Int16:
211
0
        return TYPE_SMALLINT;
212
0
    case JsonbType::T_Int32:
213
0
        return TYPE_INT;
214
0
    case JsonbType::T_Int64:
215
0
        return TYPE_BIGINT;
216
0
    case JsonbType::T_Double:
217
0
        return TYPE_DOUBLE;
218
2
    case JsonbType::T_String:
219
2
        return TYPE_STRING;
220
0
    case JsonbType::T_Binary:
221
0
        return TYPE_BINARY;
222
0
    case JsonbType::T_Object:
223
0
        return TYPE_STRUCT;
224
2
    case JsonbType::T_Array:
225
2
        return TYPE_ARRAY;
226
2
    case JsonbType::T_Int128:
227
2
        return TYPE_LARGEINT;
228
2
    case JsonbType::T_Float:
229
2
        return TYPE_FLOAT;
230
2
    case JsonbType::T_Decimal32:
231
2
        return TYPE_DECIMAL32;
232
2
    case JsonbType::T_Decimal64:
233
2
        return TYPE_DECIMAL64;
234
2
    case JsonbType::T_Decimal128:
235
2
        return TYPE_DECIMAL128I;
236
2
    case JsonbType::T_Decimal256:
237
2
        return TYPE_DECIMAL256;
238
0
    default:
239
0
        throw Exception(ErrorCode::INTERNAL_ERROR, "Unsupported JsonbType: {}",
240
0
                        static_cast<int>(json_type));
241
22
    }
242
22
}
243
244
//for parse json path
245
constexpr char SCOPE = '$';
246
constexpr char BEGIN_MEMBER = '.';
247
constexpr char BEGIN_ARRAY = '[';
248
constexpr char END_ARRAY = ']';
249
constexpr char DOUBLE_QUOTE = '"';
250
constexpr char WILDCARD = '*';
251
constexpr char MINUS = '-';
252
constexpr char LAST[] = "last";
253
constexpr char ESCAPE = '\\';
254
constexpr unsigned int MEMBER_CODE = 0;
255
constexpr unsigned int ARRAY_CODE = 1;
256
257
/// A simple input stream class for the JSON path parser.
258
class Stream {
259
public:
260
    /// Creates an input stream reading from a character string.
261
    /// @param string  the input string
262
    /// @param length  the length of the input string
263
232
    Stream(const char* string, size_t length) : m_position(string), m_end(string + length) {}
264
265
    /// Returns a pointer to the current position in the stream.
266
228
    const char* position() const { return m_position; }
267
268
    /// Returns a pointer to the position just after the end of the stream.
269
0
    const char* end() const { return m_end; }
270
271
    /// Returns the number of bytes remaining in the stream.
272
4.53k
    size_t remaining() const {
273
4.53k
        assert(m_position <= m_end);
274
4.53k
        return m_end - m_position;
275
4.53k
    }
276
277
    /// Tells if the stream has been exhausted.
278
4.15k
    bool exhausted() const { return remaining() == 0; }
279
280
    /// Reads the next byte from the stream and moves the position forward.
281
216
    char read() {
282
216
        assert(!exhausted());
283
216
        return *m_position++;
284
216
    }
285
286
    /// Reads the next byte from the stream without moving the position forward.
287
2.26k
    char peek() const {
288
2.26k
        assert(!exhausted());
289
2.26k
        return *m_position;
290
2.26k
    }
291
292
    /// Moves the position to the next non-whitespace character.
293
804
    void skip_whitespace() {
294
804
        m_position = std::find_if_not(m_position, m_end, [](char c) { return std::isspace(c); });
295
804
    }
296
297
    /// Moves the position n bytes forward.
298
372
    void skip(size_t n) {
299
372
        assert(remaining() >= n);
300
372
        m_position += n;
301
372
        skip_whitespace();
302
372
    }
303
304
352
    void advance() { m_position++; }
305
306
454
    void clear_leg_ptr() { leg_ptr = nullptr; }
307
308
238
    void set_leg_ptr(char* ptr) {
309
238
        clear_leg_ptr();
310
238
        leg_ptr = ptr;
311
238
    }
312
313
312
    char* get_leg_ptr() { return leg_ptr; }
314
315
232
    void clear_leg_len() { leg_len = 0; }
316
317
522
    void add_leg_len() { leg_len++; }
318
319
448
    unsigned int get_leg_len() const { return leg_len; }
320
321
22
    void remove_escapes() {
322
22
        unsigned int new_len = 0;
323
164
        for (unsigned int i = 0; i < leg_len; ++i) {
324
144
            if (leg_ptr[i] != ESCAPE) {
325
86
                leg_ptr[new_len++] = leg_ptr[i];
326
86
                continue;
327
86
            }
328
329
58
            ++i;
330
58
            if (i >= leg_len) {
331
2
                break;
332
2
            }
333
334
56
            switch (leg_ptr[i]) {
335
4
            case 'b':
336
4
                leg_ptr[new_len++] = '\b';
337
4
                break;
338
4
            case 'f':
339
4
                leg_ptr[new_len++] = '\f';
340
4
                break;
341
6
            case 'n':
342
6
                leg_ptr[new_len++] = '\n';
343
6
                break;
344
4
            case 'r':
345
4
                leg_ptr[new_len++] = '\r';
346
4
                break;
347
4
            case 't':
348
4
                leg_ptr[new_len++] = '\t';
349
4
                break;
350
22
            case 'u': {
351
22
                if (i + 4 >= leg_len || leg_ptr[i + 1] != '0' || leg_ptr[i + 2] != '0') {
352
4
                    leg_ptr[new_len++] = leg_ptr[i];
353
4
                    break;
354
4
                }
355
356
36
                auto hex_to_int = [](char c) -> int {
357
36
                    if (c >= '0' && c <= '9') {
358
24
                        return c - '0';
359
24
                    }
360
12
                    if (c >= 'a' && c <= 'f') {
361
4
                        return c - 'a' + 10;
362
4
                    }
363
8
                    if (c >= 'A' && c <= 'F') {
364
2
                        return c - 'A' + 10;
365
2
                    }
366
6
                    return -1;
367
8
                };
368
18
                int high = hex_to_int(leg_ptr[i + 3]);
369
18
                int low = hex_to_int(leg_ptr[i + 4]);
370
18
                if (high < 0 || low < 0) {
371
6
                    leg_ptr[new_len++] = leg_ptr[i];
372
6
                    break;
373
6
                }
374
12
                leg_ptr[new_len++] = static_cast<char>((high << 4) | low);
375
12
                i += 4;
376
12
                break;
377
18
            }
378
12
            default:
379
12
                leg_ptr[new_len++] = leg_ptr[i];
380
12
                break;
381
56
            }
382
56
        }
383
22
        leg_ptr[new_len] = '\0';
384
22
        leg_len = new_len;
385
22
    }
386
387
238
    void set_has_escapes(bool has) { has_escapes = has; }
388
389
96
    bool get_has_escapes() const { return has_escapes; }
390
391
private:
392
    /// The current position in the stream.
393
    const char* m_position = nullptr;
394
395
    /// The end of the stream.
396
    const char* const m_end;
397
398
    ///path leg ptr
399
    char* leg_ptr = nullptr;
400
401
    ///path leg len
402
    unsigned int leg_len;
403
404
    ///Whether to contain escape characters
405
    bool has_escapes = false;
406
};
407
408
struct leg_info {
409
    ///path leg ptr
410
    char* leg_ptr = nullptr;
411
412
    ///path leg len
413
    unsigned int leg_len;
414
415
    ///array_index
416
    int array_index;
417
418
    ///type: 0 is member 1 is array
419
    unsigned int type;
420
421
    // NOLINTNEXTLINE(readability-non-const-parameter): str is an output parameter.
422
14
    bool to_string(std::string* str) const {
423
14
        if (type == MEMBER_CODE) {
424
14
            str->push_back(BEGIN_MEMBER);
425
14
            bool contains_space = false;
426
14
            std::string tmp;
427
108
            for (auto* it = leg_ptr; it != (leg_ptr + leg_len); ++it) {
428
94
                auto c = static_cast<unsigned char>(*it);
429
94
                if (std::isspace(c)) {
430
18
                    contains_space = true;
431
18
                }
432
433
94
                switch (*it) {
434
4
                case '"':
435
4
                    tmp.append("\\\"");
436
4
                    break;
437
4
                case ESCAPE:
438
4
                    tmp.append("\\\\");
439
4
                    break;
440
4
                case '\b':
441
4
                    tmp.append("\\b");
442
4
                    break;
443
4
                case '\f':
444
4
                    tmp.append("\\f");
445
4
                    break;
446
6
                case '\n':
447
6
                    tmp.append("\\n");
448
6
                    break;
449
4
                case '\r':
450
4
                    tmp.append("\\r");
451
4
                    break;
452
4
                case '\t':
453
4
                    tmp.append("\\t");
454
4
                    break;
455
64
                default:
456
64
                    if (c < 0x20) {
457
12
                        constexpr char hex[] = "0123456789abcdef";
458
12
                        tmp.append("\\u00");
459
12
                        tmp.push_back(hex[c >> 4]);
460
12
                        tmp.push_back(hex[c & 0x0F]);
461
52
                    } else {
462
52
                        tmp.push_back(*it);
463
52
                    }
464
64
                    break;
465
94
                }
466
94
            }
467
14
            if (contains_space) {
468
10
                str->push_back(DOUBLE_QUOTE);
469
10
            }
470
14
            str->append(tmp);
471
14
            if (contains_space) {
472
10
                str->push_back(DOUBLE_QUOTE);
473
10
            }
474
14
            return true;
475
14
        } else if (type == ARRAY_CODE) {
476
0
            str->push_back(BEGIN_ARRAY);
477
0
            std::string int_str = std::to_string(array_index);
478
0
            str->append(int_str);
479
0
            str->push_back(END_ARRAY);
480
0
            return true;
481
0
        } else {
482
0
            return false;
483
0
        }
484
14
    }
485
};
486
487
class JsonbPath {
488
public:
489
    // parse json path
490
    static bool parsePath(Stream* stream, JsonbPath* path);
491
492
    static bool parse_array(Stream* stream, JsonbPath* path);
493
    static bool parse_member(Stream* stream, JsonbPath* path);
494
495
    //return true if json path valid else return false
496
    bool seek(const char* string, size_t length);
497
498
232
    void add_leg_to_leg_vector(std::unique_ptr<leg_info> leg) {
499
232
        leg_vector.emplace_back(leg.release());
500
232
    }
501
502
0
    void pop_leg_from_leg_vector() { leg_vector.pop_back(); }
503
504
    // NOLINTNEXTLINE(readability-non-const-parameter): res is an output parameter.
505
6
    bool to_string(std::string* res) const {
506
6
        res->push_back(SCOPE);
507
10
        for (const auto& leg : leg_vector) {
508
10
            auto valid = leg->to_string(res);
509
10
            if (!valid) {
510
0
                return false;
511
0
            }
512
10
        }
513
6
        return true;
514
6
    }
515
516
456
    size_t get_leg_vector_size() const { return leg_vector.size(); }
517
518
772
    leg_info* get_leg_from_leg_vector(size_t i) const { return leg_vector[i].get(); }
519
520
18
    bool is_wildcard() const { return _is_wildcard; }
521
218
    bool is_supper_wildcard() const { return _is_supper_wildcard; }
522
523
12
    void clean() { leg_vector.clear(); }
524
525
private:
526
    std::vector<std::unique_ptr<leg_info>> leg_vector;
527
    bool _is_wildcard = false;        // whether the path is a wildcard path
528
    bool _is_supper_wildcard = false; // supper wildcard likes '$**.a' or '$**[1]'
529
};
530
531
/*
532
 * JsonbFwdIteratorT implements JSONB's iterator template.
533
 *
534
 * Note: it is an FORWARD iterator only due to the design of JSONB format.
535
 */
536
template <class Iter_Type, class Cont_Type>
537
class JsonbFwdIteratorT {
538
public:
539
    using iterator = Iter_Type;
540
    using pointer = typename std::iterator_traits<Iter_Type>::pointer;
541
    using reference = typename std::iterator_traits<Iter_Type>::reference;
542
543
    explicit JsonbFwdIteratorT() : current_(nullptr) {}
544
41.5k
    explicit JsonbFwdIteratorT(const iterator& i) : current_(i) {}
_ZN5doris17JsonbFwdIteratorTIPKNS_13JsonbKeyValueENS_9ObjectValEEC2ERKS3_
Line
Count
Source
544
41.2k
    explicit JsonbFwdIteratorT(const iterator& i) : current_(i) {}
_ZN5doris17JsonbFwdIteratorTIPKNS_10JsonbValueENS_8ArrayValEEC2ERKS3_
Line
Count
Source
544
302
    explicit JsonbFwdIteratorT(const iterator& i) : current_(i) {}
545
546
    // allow non-const to const iterator conversion (same container type)
547
    template <class Iter_Ty>
548
    JsonbFwdIteratorT(const JsonbFwdIteratorT<Iter_Ty, Cont_Type>& rhs) : current_(rhs.base()) {}
549
550
41.7k
    bool operator==(const JsonbFwdIteratorT& rhs) const { return (current_ == rhs.current_); }
_ZNK5doris17JsonbFwdIteratorTIPKNS_13JsonbKeyValueENS_9ObjectValEEeqERKS5_
Line
Count
Source
550
39.0k
    bool operator==(const JsonbFwdIteratorT& rhs) const { return (current_ == rhs.current_); }
_ZNK5doris17JsonbFwdIteratorTIPKNS_10JsonbValueENS_8ArrayValEEeqERKS5_
Line
Count
Source
550
2.77k
    bool operator==(const JsonbFwdIteratorT& rhs) const { return (current_ == rhs.current_); }
551
552
40.5k
    bool operator!=(const JsonbFwdIteratorT& rhs) const { return !operator==(rhs); }
_ZNK5doris17JsonbFwdIteratorTIPKNS_13JsonbKeyValueENS_9ObjectValEEneERKS5_
Line
Count
Source
552
37.9k
    bool operator!=(const JsonbFwdIteratorT& rhs) const { return !operator==(rhs); }
_ZNK5doris17JsonbFwdIteratorTIPKNS_10JsonbValueENS_8ArrayValEEneERKS5_
Line
Count
Source
552
2.62k
    bool operator!=(const JsonbFwdIteratorT& rhs) const { return !operator==(rhs); }
553
554
2.33k
    bool operator<(const JsonbFwdIteratorT& rhs) const { return (current_ < rhs.current_); }
555
556
    bool operator>(const JsonbFwdIteratorT& rhs) const { return !operator<(rhs); }
557
558
36.6k
    JsonbFwdIteratorT& operator++() {
559
36.6k
        current_ = (iterator)(((char*)current_) + current_->numPackedBytes());
560
36.6k
        return *this;
561
36.6k
    }
_ZN5doris17JsonbFwdIteratorTIPKNS_13JsonbKeyValueENS_9ObjectValEEppEv
Line
Count
Source
558
35.4k
    JsonbFwdIteratorT& operator++() {
559
35.4k
        current_ = (iterator)(((char*)current_) + current_->numPackedBytes());
560
35.4k
        return *this;
561
35.4k
    }
_ZN5doris17JsonbFwdIteratorTIPKNS_10JsonbValueENS_8ArrayValEEppEv
Line
Count
Source
558
1.23k
    JsonbFwdIteratorT& operator++() {
559
1.23k
        current_ = (iterator)(((char*)current_) + current_->numPackedBytes());
560
1.23k
        return *this;
561
1.23k
    }
562
563
    JsonbFwdIteratorT operator++(int) {
564
        auto tmp = *this;
565
        current_ = (iterator)(((char*)current_) + current_->numPackedBytes());
566
        return tmp;
567
    }
568
569
1.23k
    explicit operator pointer() { return current_; }
570
571
86
    reference operator*() const { return *current_; }
Unexecuted instantiation: _ZNK5doris17JsonbFwdIteratorTIPKNS_10JsonbValueENS_8ArrayValEEdeEv
_ZNK5doris17JsonbFwdIteratorTIPKNS_13JsonbKeyValueENS_9ObjectValEEdeEv
Line
Count
Source
571
86
    reference operator*() const { return *current_; }
572
573
58.5k
    pointer operator->() const { return current_; }
_ZNK5doris17JsonbFwdIteratorTIPKNS_13JsonbKeyValueENS_9ObjectValEEptEv
Line
Count
Source
573
58.5k
    pointer operator->() const { return current_; }
_ZNK5doris17JsonbFwdIteratorTIPKNS_10JsonbValueENS_8ArrayValEEptEv
Line
Count
Source
573
6
    pointer operator->() const { return current_; }
574
575
    iterator base() const { return current_; }
576
577
private:
578
    iterator current_;
579
};
580
using JsonbTypeUnder = std::underlying_type_t<JsonbType>;
581
582
#if defined(__clang__)
583
#pragma clang diagnostic push
584
#pragma clang diagnostic ignored "-Wzero-length-array"
585
#endif
586
#pragma pack(push, 1)
587
588
/*
589
 * JsonbDocument is the main object that accesses and queries JSONB packed
590
 * bytes. NOTE: JsonbDocument only allows object container as the top level
591
 * JSONB value. However, you can use the static method "createValue" to get any
592
 * JsonbValue object from the packed bytes.
593
 *
594
 * JsonbDocument object also dereferences to an object container value
595
 * (ObjectVal) once JSONB is loaded.
596
 *
597
 * ** Load **
598
 * JsonbDocument is usable after loading packed bytes (memory location) into
599
 * the object. We only need the header and first few bytes of the payload after
600
 * header to verify the JSONB.
601
 *
602
 * Note: creating an JsonbDocument (through createDocument) does not allocate
603
 * any memory. The document object is an efficient wrapper on the packed bytes
604
 * which is accessed directly.
605
 *
606
 * ** Query **
607
 * Query is through dereferencing into ObjectVal.
608
 */
609
class JsonbDocument {
610
public:
611
    // create an JsonbDocument object from JSONB packed bytes
612
    [[nodiscard]] static Status checkAndCreateDocument(const char* pb, size_t size,
613
                                                       const JsonbDocument** doc);
614
615
    // create an JsonbValue from JSONB packed bytes
616
    static const JsonbValue* createValue(const char* pb, size_t size);
617
618
0
    uint8_t version() const { return header_.ver_; }
619
620
53.2k
    const JsonbValue* getValue() const { return ((const JsonbValue*)payload_); }
621
622
    unsigned int numPackedBytes() const;
623
624
    const ObjectVal* operator->() const;
625
626
private:
627
    /*
628
   * JsonbHeader class defines JSONB header (internal to JsonbDocument).
629
   *
630
   * Currently it only contains version information (1-byte). We may expand the
631
   * header to include checksum of the JSONB binary for more security.
632
   */
633
    struct JsonbHeader {
634
        uint8_t ver_;
635
    } header_;
636
637
    char payload_[0];
638
};
639
640
/*
641
 * JsonbKeyValue class defines JSONB key type, as described below.
642
 *
643
 * key ::=
644
 *   0x00 int8    //1-byte dictionary id
645
 * | int8 (byte*) //int8 (>0) is the size of the key string
646
 *
647
 * value ::= primitive_value | container
648
 *
649
 * JsonbKeyValue can be either an id mapping to the key string in an external
650
 * dictionary, or it is the original key string. Whether to read an id or a
651
 * string is decided by the first byte (size).
652
 *
653
 * Note: a key object must be followed by a value object. Therefore, a key
654
 * object implicitly refers to a key-value pair, and you can get the value
655
 * object right after the key object. The function numPackedBytes hence
656
 * indicates the total size of the key-value pair, so that we will be able go
657
 * to next pair from the key.
658
 *
659
 * ** Dictionary size **
660
 * By default, the dictionary size is 255 (1-byte). Users can define
661
 * "USE_LARGE_DICT" to increase the dictionary size to 655535 (2-byte).
662
 */
663
class JsonbKeyValue {
664
public:
665
    // now we use sMaxKeyId to represent an empty key
666
    static const int sMaxKeyId = 65535;
667
    using keyid_type = uint16_t;
668
669
    static const uint8_t sMaxKeyLen = 64;
670
671
    // size of the key. 0 indicates it is stored as id
672
2.91k
    uint8_t klen() const { return size; }
673
674
    // get the key string. Note the string may not be null terminated.
675
1.58k
    const char* getKeyStr() const { return key.str_; }
676
677
18.6k
    keyid_type getKeyId() const { return key.id_; }
678
679
71.3k
    unsigned int keyPackedBytes() const {
680
71.3k
        return size ? (sizeof(size) + size) : (sizeof(size) + sizeof(keyid_type));
681
71.3k
    }
682
683
35.7k
    const JsonbValue* value() const {
684
35.7k
        return (const JsonbValue*)(((char*)this) + keyPackedBytes());
685
35.7k
    }
686
687
    // size of the total packed bytes (key+value)
688
    unsigned int numPackedBytes() const;
689
690
    uint8_t size;
691
692
    union key_ {
693
        keyid_type id_;
694
        char str_[1];
695
    } key;
696
};
697
698
struct JsonbFindResult {
699
    const JsonbValue* value = nullptr;   // found value
700
    std::unique_ptr<JsonbWriter> writer; // writer to write the value
701
    bool is_wildcard = false;            // whether the path is a wildcard path
702
};
703
704
/*
705
 * JsonbValue is the base class of all JSONB types. It contains only one member
706
 * variable - type info, which can be retrieved by member functions is[Type]()
707
 * or type().
708
 */
709
struct JsonbValue {
710
    static const uint32_t sMaxValueLen = 1 << 24; // 16M
711
712
8.57k
    bool isNull() const { return (type == JsonbType::T_Null); }
713
42
    bool isTrue() const { return (type == JsonbType::T_True); }
714
2
    bool isFalse() const { return (type == JsonbType::T_False); }
715
19.5k
    bool isInt() const { return isInt8() || isInt16() || isInt32() || isInt64() || isInt128(); }
716
19.5k
    bool isInt8() const { return (type == JsonbType::T_Int8); }
717
19.3k
    bool isInt16() const { return (type == JsonbType::T_Int16); }
718
19.1k
    bool isInt32() const { return (type == JsonbType::T_Int32); }
719
12.2k
    bool isInt64() const { return (type == JsonbType::T_Int64); }
720
380
    bool isDouble() const { return (type == JsonbType::T_Double); }
721
306
    bool isFloat() const { return (type == JsonbType::T_Float); }
722
136
    bool isString() const { return (type == JsonbType::T_String); }
723
2.19k
    bool isBinary() const { return (type == JsonbType::T_Binary); }
724
210
    bool isObject() const { return (type == JsonbType::T_Object); }
725
58
    bool isArray() const { return (type == JsonbType::T_Array); }
726
8.41k
    bool isInt128() const { return (type == JsonbType::T_Int128); }
727
148
    bool isDecimal() const {
728
148
        return (type == JsonbType::T_Decimal32 || type == JsonbType::T_Decimal64 ||
729
148
                type == JsonbType::T_Decimal128 || type == JsonbType::T_Decimal256);
730
148
    }
731
2
    bool isDecimal32() const { return (type == JsonbType::T_Decimal32); }
732
2
    bool isDecimal64() const { return (type == JsonbType::T_Decimal64); }
733
2
    bool isDecimal128() const { return (type == JsonbType::T_Decimal128); }
734
2
    bool isDecimal256() const { return (type == JsonbType::T_Decimal256); }
735
736
22
    PrimitiveType get_primitive_type() const { return get_primitive_type_from_json_type(type); }
737
738
0
    const char* typeName() const {
739
0
        switch (type) {
740
0
        case JsonbType::T_Null:
741
0
            return "null";
742
0
        case JsonbType::T_True:
743
0
        case JsonbType::T_False:
744
0
            return "bool";
745
0
        case JsonbType::T_Int8:
746
0
        case JsonbType::T_Int16:
747
0
        case JsonbType::T_Int32:
748
0
            return "int";
749
0
        case JsonbType::T_Int64:
750
0
            return "bigint";
751
0
        case JsonbType::T_Int128:
752
0
            return "largeint";
753
0
        case JsonbType::T_Double:
754
0
            return "double";
755
0
        case JsonbType::T_Float:
756
0
            return "float";
757
0
        case JsonbType::T_String:
758
0
            return "string";
759
0
        case JsonbType::T_Binary:
760
0
            return "binary";
761
0
        case JsonbType::T_Object:
762
0
            return "object";
763
0
        case JsonbType::T_Array:
764
0
            return "array";
765
0
        case JsonbType::T_Decimal32:
766
0
            return "Decimal32";
767
0
        case JsonbType::T_Decimal64:
768
0
            return "Decimal64";
769
0
        case JsonbType::T_Decimal128:
770
0
            return "Decimal128";
771
0
        case JsonbType::T_Decimal256:
772
0
            return "Decimal256";
773
0
        default:
774
0
            return "unknown";
775
0
        }
776
0
    }
777
778
    // size of the total packed bytes
779
    unsigned int numPackedBytes() const;
780
781
    // size of the value in bytes
782
    unsigned int size() const;
783
784
    //Get the number of jsonbvalue elements
785
    int numElements() const;
786
787
    //Whether to include the jsonbvalue rhs
788
    bool contains(const JsonbValue* rhs) const;
789
790
    // find the JSONB value by JsonbPath
791
    JsonbFindResult findValue(JsonbPath& path) const;
792
    friend class JsonbDocument;
793
794
    JsonbType type; // type info
795
796
    char payload[0]; // payload, which is the packed bytes of the value
797
798
    /**
799
    * @brief Unpacks the underlying Jsonb binary content as a pointer to type `T`.
800
    *
801
    * @tparam T A POD (Plain Old Data) type that must satisfy the `JsonbPodType` concept.
802
    *           This ensures that `T` is trivially copyable, standard-layout, and safe to
803
    *           reinterpret from raw bytes without invoking undefined behavior.
804
    *
805
    * @return A pointer to a `const T` object, interpreted from the internal buffer.
806
    *
807
    * @note The caller must ensure that the current JsonbValue actually contains data
808
    *       compatible with type `T`, otherwise the result is undefined.
809
    */
810
    template <JsonbPodType T>
811
106k
    const T* unpack() const {
812
106k
        static_assert(is_pod_v<T>, "T must be a POD type");
813
106k
        return reinterpret_cast<const T*>(payload);
814
106k
    }
_ZNK5doris10JsonbValue6unpackITkNS_12JsonbPodTypeENS_9ObjectValEEEPKT_v
Line
Count
Source
811
39.9k
    const T* unpack() const {
812
39.9k
        static_assert(is_pod_v<T>, "T must be a POD type");
813
39.9k
        return reinterpret_cast<const T*>(payload);
814
39.9k
    }
_ZNK5doris10JsonbValue6unpackITkNS_12JsonbPodTypeENS_10NumberValTIaEEEEPKT_v
Line
Count
Source
811
1.53k
    const T* unpack() const {
812
1.53k
        static_assert(is_pod_v<T>, "T must be a POD type");
813
1.53k
        return reinterpret_cast<const T*>(payload);
814
1.53k
    }
_ZNK5doris10JsonbValue6unpackITkNS_12JsonbPodTypeENS_10NumberValTIsEEEEPKT_v
Line
Count
Source
811
228
    const T* unpack() const {
812
228
        static_assert(is_pod_v<T>, "T must be a POD type");
813
228
        return reinterpret_cast<const T*>(payload);
814
228
    }
_ZNK5doris10JsonbValue6unpackITkNS_12JsonbPodTypeENS_10NumberValTIiEEEEPKT_v
Line
Count
Source
811
6.99k
    const T* unpack() const {
812
6.99k
        static_assert(is_pod_v<T>, "T must be a POD type");
813
6.99k
        return reinterpret_cast<const T*>(payload);
814
6.99k
    }
_ZNK5doris10JsonbValue6unpackITkNS_12JsonbPodTypeENS_10NumberValTIlEEEEPKT_v
Line
Count
Source
811
3.91k
    const T* unpack() const {
812
3.91k
        static_assert(is_pod_v<T>, "T must be a POD type");
813
3.91k
        return reinterpret_cast<const T*>(payload);
814
3.91k
    }
_ZNK5doris10JsonbValue6unpackITkNS_12JsonbPodTypeENS_10NumberValTInEEEEPKT_v
Line
Count
Source
811
8.37k
    const T* unpack() const {
812
8.37k
        static_assert(is_pod_v<T>, "T must be a POD type");
813
8.37k
        return reinterpret_cast<const T*>(payload);
814
8.37k
    }
_ZNK5doris10JsonbValue6unpackITkNS_12JsonbPodTypeENS_10NumberValTIdEEEEPKT_v
Line
Count
Source
811
372
    const T* unpack() const {
812
372
        static_assert(is_pod_v<T>, "T must be a POD type");
813
372
        return reinterpret_cast<const T*>(payload);
814
372
    }
_ZNK5doris10JsonbValue6unpackITkNS_12JsonbPodTypeENS_10NumberValTIfEEEEPKT_v
Line
Count
Source
811
56
    const T* unpack() const {
812
56
        static_assert(is_pod_v<T>, "T must be a POD type");
813
56
        return reinterpret_cast<const T*>(payload);
814
56
    }
_ZNK5doris10JsonbValue6unpackITkNS_12JsonbPodTypeENS_15JsonbDecimalValINS_7DecimalIiEEEEEEPKT_v
Line
Count
Source
811
50
    const T* unpack() const {
812
50
        static_assert(is_pod_v<T>, "T must be a POD type");
813
50
        return reinterpret_cast<const T*>(payload);
814
50
    }
_ZNK5doris10JsonbValue6unpackITkNS_12JsonbPodTypeENS_15JsonbDecimalValINS_7DecimalIlEEEEEEPKT_v
Line
Count
Source
811
30
    const T* unpack() const {
812
30
        static_assert(is_pod_v<T>, "T must be a POD type");
813
30
        return reinterpret_cast<const T*>(payload);
814
30
    }
_ZNK5doris10JsonbValue6unpackITkNS_12JsonbPodTypeENS_15JsonbDecimalValINS_12Decimal128V3EEEEEPKT_v
Line
Count
Source
811
46
    const T* unpack() const {
812
46
        static_assert(is_pod_v<T>, "T must be a POD type");
813
46
        return reinterpret_cast<const T*>(payload);
814
46
    }
_ZNK5doris10JsonbValue6unpackITkNS_12JsonbPodTypeENS_15JsonbDecimalValINS_7DecimalIN4wide7integerILm256EiEEEEEEEEPKT_v
Line
Count
Source
811
26
    const T* unpack() const {
812
26
        static_assert(is_pod_v<T>, "T must be a POD type");
813
26
        return reinterpret_cast<const T*>(payload);
814
26
    }
_ZNK5doris10JsonbValue6unpackITkNS_12JsonbPodTypeENS_14JsonbBinaryValEEEPKT_v
Line
Count
Source
811
39.5k
    const T* unpack() const {
812
39.5k
        static_assert(is_pod_v<T>, "T must be a POD type");
813
39.5k
        return reinterpret_cast<const T*>(payload);
814
39.5k
    }
_ZNK5doris10JsonbValue6unpackITkNS_12JsonbPodTypeENS_12ContainerValEEEPKT_v
Line
Count
Source
811
4.14k
    const T* unpack() const {
812
4.14k
        static_assert(is_pod_v<T>, "T must be a POD type");
813
4.14k
        return reinterpret_cast<const T*>(payload);
814
4.14k
    }
_ZNK5doris10JsonbValue6unpackITkNS_12JsonbPodTypeENS_8ArrayValEEEPKT_v
Line
Count
Source
811
262
    const T* unpack() const {
812
262
        static_assert(is_pod_v<T>, "T must be a POD type");
813
262
        return reinterpret_cast<const T*>(payload);
814
262
    }
_ZNK5doris10JsonbValue6unpackITkNS_12JsonbPodTypeENS_14JsonbStringValEEEPKT_v
Line
Count
Source
811
698
    const T* unpack() const {
812
698
        static_assert(is_pod_v<T>, "T must be a POD type");
813
698
        return reinterpret_cast<const T*>(payload);
814
698
    }
815
816
    // /**
817
    // * @brief Unpacks the underlying Jsonb binary content as a pointer to type `T`.
818
    // *
819
    // * @tparam T A POD (Plain Old Data) type that must satisfy the `JsonbPodType` concept.
820
    // *           This ensures that `T` is trivially copyable, standard-layout, and safe to
821
    // *           reinterpret from raw bytes without invoking undefined behavior.
822
    // *
823
    // * @return A pointer to a `T` object, interpreted from the internal buffer.
824
    // *
825
    // * @note The caller must ensure that the current JsonbValue actually contains data
826
    // *       compatible with type `T`, otherwise the result is undefined.
827
    // */
828
    // template <JsonbPodType T>
829
    // T* unpack() {
830
    //     static_assert(is_pod_v<T>, "T must be a POD type");
831
    //     return reinterpret_cast<T*>(payload);
832
    // }
833
834
    int128_t int_val() const;
835
};
836
837
// inline ObjectVal* JsonbDocument::operator->() {
838
//     return (((JsonbValue*)payload_)->unpack<ObjectVal>());
839
// }
840
841
38.7k
inline const ObjectVal* JsonbDocument::operator->() const {
842
38.7k
    return (((const JsonbValue*)payload_)->unpack<ObjectVal>());
843
38.7k
}
844
845
/*
846
 * NumerValT is the template class (derived from JsonbValue) of all number
847
 * types (integers and double).
848
 */
849
template <typename T>
850
    requires std::is_integral_v<T> || std::is_floating_point_v<T>
851
struct NumberValT {
852
public:
853
21.4k
    T val() const { return num; }
_ZNK5doris10NumberValTIaE3valEv
Line
Count
Source
853
1.53k
    T val() const { return num; }
_ZNK5doris10NumberValTIsE3valEv
Line
Count
Source
853
228
    T val() const { return num; }
_ZNK5doris10NumberValTIiE3valEv
Line
Count
Source
853
6.99k
    T val() const { return num; }
_ZNK5doris10NumberValTIlE3valEv
Line
Count
Source
853
3.91k
    T val() const { return num; }
_ZNK5doris10NumberValTInE3valEv
Line
Count
Source
853
8.37k
    T val() const { return num; }
_ZNK5doris10NumberValTIdE3valEv
Line
Count
Source
853
372
    T val() const { return num; }
_ZNK5doris10NumberValTIfE3valEv
Line
Count
Source
853
56
    T val() const { return num; }
854
855
    static unsigned int numPackedBytes() { return sizeof(JsonbValue) + sizeof(T); }
856
857
    T num;
858
};
859
860
19.5k
inline int128_t JsonbValue::int_val() const {
861
19.5k
    switch (type) {
862
306
    case JsonbType::T_Int8:
863
306
        return unpack<JsonbInt8Val>()->val();
864
188
    case JsonbType::T_Int16:
865
188
        return unpack<JsonbInt16Val>()->val();
866
6.86k
    case JsonbType::T_Int32:
867
6.86k
        return unpack<JsonbInt32Val>()->val();
868
3.86k
    case JsonbType::T_Int64:
869
3.86k
        return unpack<JsonbInt64Val>()->val();
870
8.36k
    case JsonbType::T_Int128:
871
8.36k
        return unpack<JsonbInt128Val>()->val();
872
0
    default:
873
0
        throw Exception(ErrorCode::INTERNAL_ERROR, "Invalid JSONB value type: {}",
874
0
                        static_cast<int32_t>(type));
875
19.5k
    }
876
19.5k
}
877
878
template <JsonbDecimalType T>
879
struct JsonbDecimalVal {
880
public:
881
    using NativeType = typename T::NativeType;
882
883
    // get the decimal value
884
88
    NativeType val() const {
885
        // to avoid memory alignment issues, we use memcpy to copy the value
886
88
        NativeType tmp;
887
88
        memcpy(&tmp, &value, sizeof(NativeType));
888
88
        return tmp;
889
88
    }
_ZNK5doris15JsonbDecimalValINS_7DecimalIiEEE3valEv
Line
Count
Source
884
32
    NativeType val() const {
885
        // to avoid memory alignment issues, we use memcpy to copy the value
886
32
        NativeType tmp;
887
32
        memcpy(&tmp, &value, sizeof(NativeType));
888
32
        return tmp;
889
32
    }
_ZNK5doris15JsonbDecimalValINS_7DecimalIlEEE3valEv
Line
Count
Source
884
16
    NativeType val() const {
885
        // to avoid memory alignment issues, we use memcpy to copy the value
886
16
        NativeType tmp;
887
16
        memcpy(&tmp, &value, sizeof(NativeType));
888
16
        return tmp;
889
16
    }
_ZNK5doris15JsonbDecimalValINS_12Decimal128V3EE3valEv
Line
Count
Source
884
28
    NativeType val() const {
885
        // to avoid memory alignment issues, we use memcpy to copy the value
886
28
        NativeType tmp;
887
28
        memcpy(&tmp, &value, sizeof(NativeType));
888
28
        return tmp;
889
28
    }
_ZNK5doris15JsonbDecimalValINS_7DecimalIN4wide7integerILm256EiEEEEE3valEv
Line
Count
Source
884
12
    NativeType val() const {
885
        // to avoid memory alignment issues, we use memcpy to copy the value
886
12
        NativeType tmp;
887
12
        memcpy(&tmp, &value, sizeof(NativeType));
888
12
        return tmp;
889
12
    }
890
891
98
    static constexpr int numPackedBytes() {
892
98
        return sizeof(JsonbValue) + sizeof(precision) + sizeof(scale) + sizeof(value);
893
98
    }
_ZN5doris15JsonbDecimalValINS_7DecimalIiEEE14numPackedBytesEv
Line
Count
Source
891
38
    static constexpr int numPackedBytes() {
892
38
        return sizeof(JsonbValue) + sizeof(precision) + sizeof(scale) + sizeof(value);
893
38
    }
_ZN5doris15JsonbDecimalValINS_7DecimalIlEEE14numPackedBytesEv
Line
Count
Source
891
18
    static constexpr int numPackedBytes() {
892
18
        return sizeof(JsonbValue) + sizeof(precision) + sizeof(scale) + sizeof(value);
893
18
    }
_ZN5doris15JsonbDecimalValINS_12Decimal128V3EE14numPackedBytesEv
Line
Count
Source
891
30
    static constexpr int numPackedBytes() {
892
30
        return sizeof(JsonbValue) + sizeof(precision) + sizeof(scale) + sizeof(value);
893
30
    }
_ZN5doris15JsonbDecimalValINS_7DecimalIN4wide7integerILm256EiEEEEE14numPackedBytesEv
Line
Count
Source
891
12
    static constexpr int numPackedBytes() {
892
12
        return sizeof(JsonbValue) + sizeof(precision) + sizeof(scale) + sizeof(value);
893
12
    }
894
895
    uint32_t precision;
896
    uint32_t scale;
897
    NativeType value;
898
};
899
900
/*
901
 * BlobVal is the base class (derived from JsonbValue) for string and binary
902
 * types. The size indicates the total bytes of the payload.
903
 */
904
struct JsonbBinaryVal {
905
public:
906
    // size of the blob payload only
907
4.30k
    unsigned int getBlobLen() const { return size; }
908
909
    // return the blob as byte array
910
9.22k
    const char* getBlob() const { return payload; }
911
912
    // size of the total packed bytes
913
30.7k
    unsigned int numPackedBytes() const { return sizeof(JsonbValue) + sizeof(size) + size; }
914
    friend class JsonbDocument;
915
916
    uint32_t size;
917
    char payload[0];
918
};
919
920
/*
921
 * String type
922
 * Note: JSONB string may not be a c-string (NULL-terminated)
923
 */
924
struct JsonbStringVal : public JsonbBinaryVal {
925
public:
926
    /*
927
    This function return the actual size of a string. Since for
928
    a string, it can be null-terminated with null paddings or it
929
    can take all the space in the payload without null in the end.
930
    So we need to check it to get the true actual length of a string.
931
  */
932
350
    size_t length() const {
933
        // It's an empty string
934
350
        if (0 == size) {
935
0
            return size;
936
0
        }
937
        // The string stored takes all the spaces in payload
938
350
        if (payload[size - 1] != 0) {
939
350
            return size;
940
350
        }
941
        // It's shorter than the size of payload
942
0
        return strnlen(payload, size);
943
350
    }
944
};
945
946
/*
947
 * ContainerVal is the base class (derived from JsonbValue) for object and
948
 * array types. The size indicates the total bytes of the payload.
949
 */
950
struct ContainerVal {
951
    // size of the container payload only
952
0
    unsigned int getContainerSize() const { return size; }
953
954
    // return the container payload as byte array
955
0
    const char* getPayload() const { return payload; }
956
957
    // size of the total packed bytes
958
4.14k
    unsigned int numPackedBytes() const { return sizeof(JsonbValue) + sizeof(size) + size; }
959
    friend class JsonbDocument;
960
961
    uint32_t size;
962
    char payload[0];
963
};
964
965
/*
966
 * Object type
967
 */
968
struct ObjectVal : public ContainerVal {
969
    using value_type = JsonbKeyValue;
970
    using pointer = value_type*;
971
    using const_pointer = const value_type*;
972
    using const_iterator = JsonbFwdIteratorT<const_pointer, ObjectVal>;
973
974
2
    const_iterator search(const char* key) const {
975
2
        if (!key) {
976
0
            return end();
977
0
        }
978
2
        return search(key, (unsigned int)strlen(key));
979
2
    }
980
981
88
    const_iterator search(const char* key, unsigned int klen) const {
982
88
        if (!key || !klen) {
983
0
            return end();
984
0
        }
985
88
        return internalSearch(key, klen);
986
88
    }
987
988
    // Get number of elements in object
989
114
    int numElem() const {
990
114
        const char* pch = payload;
991
114
        const char* fence = payload + size;
992
993
114
        unsigned int num = 0;
994
298
        while (pch < fence) {
995
184
            auto* pkey = (JsonbKeyValue*)(pch);
996
184
            ++num;
997
184
            pch += pkey->numPackedBytes();
998
184
        }
999
1000
114
        assert(pch == fence);
1001
1002
114
        return num;
1003
114
    }
1004
1005
    // find the JSONB value by a key string (null terminated)
1006
2
    const JsonbValue* find(const char* key) const {
1007
2
        if (!key) {
1008
0
            return nullptr;
1009
0
        }
1010
2
        return find(key, (unsigned int)strlen(key));
1011
2
    }
1012
1013
    // find the JSONB value by a key string (with length)
1014
84
    const JsonbValue* find(const char* key, unsigned int klen) const {
1015
84
        const_iterator kv = search(key, klen);
1016
84
        if (end() == kv) {
1017
12
            return nullptr;
1018
12
        }
1019
72
        return kv->value();
1020
84
    }
1021
1022
3.55k
    const_iterator begin() const { return const_iterator((pointer)payload); }
1023
1024
37.6k
    const_iterator end() const { return const_iterator((pointer)(payload + size)); }
1025
1026
    std::vector<std::pair<StringRef, const JsonbValue*>> get_ordered_key_value_pairs() const;
1027
1028
private:
1029
88
    const_iterator internalSearch(const char* key, unsigned int klen) const {
1030
88
        const char* pch = payload;
1031
88
        const char* fence = payload + size;
1032
1033
108
        while (pch < fence) {
1034
96
            const auto* pkey = (const JsonbKeyValue*)(pch);
1035
96
            if (klen == pkey->klen() && strncmp(key, pkey->getKeyStr(), klen) == 0) {
1036
76
                return const_iterator(pkey);
1037
76
            }
1038
20
            pch += pkey->numPackedBytes();
1039
20
        }
1040
1041
88
        assert(pch == fence);
1042
1043
12
        return end();
1044
12
    }
1045
};
1046
1047
/*
1048
 * Array type
1049
 */
1050
struct ArrayVal : public ContainerVal {
1051
    using value_type = JsonbValue;
1052
    using pointer = value_type*;
1053
    using const_pointer = const value_type*;
1054
    using const_iterator = JsonbFwdIteratorT<const_pointer, ArrayVal>;
1055
1056
    // get the JSONB value at index
1057
152
    const JsonbValue* get(int idx) const {
1058
152
        if (idx < 0) {
1059
0
            return nullptr;
1060
0
        }
1061
1062
152
        const char* pch = payload;
1063
152
        const char* fence = payload + size;
1064
1065
276
        while (pch < fence && idx-- > 0) {
1066
124
            pch += ((const JsonbValue*)pch)->numPackedBytes();
1067
124
        }
1068
152
        if (idx > 0 || pch == fence) {
1069
18
            return nullptr;
1070
18
        }
1071
1072
134
        return (const JsonbValue*)pch;
1073
152
    }
1074
1075
    // Get number of elements in array
1076
72
    int numElem() const {
1077
72
        const char* pch = payload;
1078
72
        const char* fence = payload + size;
1079
1080
72
        unsigned int num = 0;
1081
222
        while (pch < fence) {
1082
150
            ++num;
1083
150
            pch += ((const JsonbValue*)pch)->numPackedBytes();
1084
150
        }
1085
1086
72
        assert(pch == fence);
1087
1088
72
        return num;
1089
72
    }
1090
1091
152
    const_iterator begin() const { return const_iterator((pointer)payload); }
1092
1093
150
    const_iterator end() const { return const_iterator((pointer)(payload + size)); }
1094
};
1095
1096
namespace jsonb_detail {
1097
1098
struct JsonbScaledDecimal {
1099
    wide::Int256 value;
1100
    uint32_t scale;
1101
};
1102
1103
52
inline void validate_decimal_scale(uint32_t scale) {
1104
52
    if (scale > static_cast<uint32_t>(BeConsts::MAX_DECIMALV3_SCALE)) {
1105
4
        throw Exception(ErrorCode::INTERNAL_ERROR,
1106
4
                        "Invalid JSONB decimal scale: {}, max allowed scale: {}", scale,
1107
4
                        BeConsts::MAX_DECIMALV3_SCALE);
1108
4
    }
1109
52
}
1110
1111
78
inline bool is_numeric(const JsonbValue* value) {
1112
78
    return value->isInt() || value->isDouble() || value->isFloat() || value->isDecimal();
1113
78
}
1114
1115
20
inline double floating_value(const JsonbValue* value) {
1116
20
    if (value->isDouble()) {
1117
20
        return value->unpack<JsonbDoubleVal>()->val();
1118
20
    }
1119
0
    return value->unpack<JsonbFloatVal>()->val();
1120
20
}
1121
1122
40
inline JsonbScaledDecimal get_scaled_decimal(const JsonbValue* value) {
1123
40
    switch (value->type) {
1124
24
    case JsonbType::T_Decimal32: {
1125
24
        const auto* decimal = value->unpack<JsonbDecimal32>();
1126
24
        validate_decimal_scale(decimal->scale);
1127
24
        return {wide::Int256(decimal->val()), decimal->scale};
1128
0
    }
1129
4
    case JsonbType::T_Decimal64: {
1130
4
        const auto* decimal = value->unpack<JsonbDecimal64>();
1131
4
        validate_decimal_scale(decimal->scale);
1132
4
        return {wide::Int256(decimal->val()), decimal->scale};
1133
0
    }
1134
12
    case JsonbType::T_Decimal128: {
1135
12
        const auto* decimal = value->unpack<JsonbDecimal128>();
1136
12
        validate_decimal_scale(decimal->scale);
1137
12
        return {wide::Int256(decimal->val()), decimal->scale};
1138
0
    }
1139
0
    case JsonbType::T_Decimal256: {
1140
0
        const auto* decimal = value->unpack<JsonbDecimal256>();
1141
0
        validate_decimal_scale(decimal->scale);
1142
0
        return {decimal->val(), decimal->scale};
1143
0
    }
1144
0
    default:
1145
0
        throw Exception(ErrorCode::INTERNAL_ERROR, "Invalid JSONB decimal value type: {}",
1146
0
                        static_cast<int32_t>(value->type));
1147
40
    }
1148
40
}
1149
1150
inline bool scaled_decimal_equal_decimal(const JsonbScaledDecimal& lhs,
1151
8
                                         const JsonbScaledDecimal& rhs) {
1152
8
    if (lhs.scale == rhs.scale) {
1153
0
        return lhs.value == rhs.value;
1154
0
    }
1155
1156
8
    if (lhs.scale < rhs.scale) {
1157
4
        const auto scale_multiplier = decimal_scale_multiplier<wide::Int256>(rhs.scale - lhs.scale);
1158
4
        return rhs.value % scale_multiplier == 0 && lhs.value == rhs.value / scale_multiplier;
1159
4
    }
1160
1161
4
    const auto scale_multiplier = decimal_scale_multiplier<wide::Int256>(lhs.scale - rhs.scale);
1162
4
    return lhs.value % scale_multiplier == 0 && lhs.value / scale_multiplier == rhs.value;
1163
8
}
1164
1165
8
inline bool scaled_decimal_equal_integer(const JsonbScaledDecimal& decimal, int128_t integer) {
1166
8
    const auto integer_value = wide::Int256(integer);
1167
8
    if (decimal.scale == 0) {
1168
0
        return decimal.value == integer_value;
1169
0
    }
1170
1171
8
    const auto scale_multiplier = decimal_scale_multiplier<wide::Int256>(decimal.scale);
1172
8
    return decimal.value % scale_multiplier == 0 &&
1173
8
           decimal.value / scale_multiplier == integer_value;
1174
8
}
1175
1176
inline constexpr auto kPowersOfFive = [] {
1177
    std::array<wide::Int256, BeConsts::MAX_DECIMALV3_SCALE + 1> powers {};
1178
    powers[0] = 1;
1179
    for (size_t i = 1; i < powers.size(); ++i) {
1180
        powers[i] = powers[i - 1] * 5;
1181
    }
1182
    return powers;
1183
}();
1184
1185
12
inline wide::Int256 power_of_five(uint32_t exponent) {
1186
12
    validate_decimal_scale(exponent);
1187
12
    return kPowersOfFive[exponent];
1188
12
}
1189
1190
12
inline bool scaled_binary_equal(wide::Int256 value, int exponent, wide::Int256 significand) {
1191
12
    if (exponent < 0) {
1192
8
        const int divisor_exponent = -exponent;
1193
8
        if (divisor_exponent >= std::numeric_limits<int64_t>::digits) {
1194
0
            return false;
1195
0
        }
1196
8
        const auto divisor = wide::Int256(1) << divisor_exponent;
1197
8
        return significand % divisor == 0 && value == significand / divisor;
1198
8
    }
1199
4
    constexpr int max_positive_int256_shift = std::numeric_limits<wide::Int256>::digits;
1200
    // wide::Int256 is signed, so shifting 1 by 255 reaches the sign bit.
1201
4
    if (exponent >= max_positive_int256_shift) {
1202
0
        return false;
1203
0
    }
1204
4
    const auto multiplier = wide::Int256(1) << exponent;
1205
4
    return value % multiplier == 0 && value / multiplier == significand;
1206
4
}
1207
1208
8
inline bool floating_equal_integer(const JsonbValue* floating, int128_t integer) {
1209
8
    const double value = floating_value(floating);
1210
8
    int exponent = 0;
1211
8
    std::frexp(value, &exponent);
1212
8
    if (!std::isfinite(value) || std::trunc(value) != value) {
1213
2
        return false;
1214
2
    }
1215
6
    if (exponent >= 128) {
1216
0
        return value == -std::ldexp(1.0, 127) && integer == std::numeric_limits<int128_t>::min();
1217
0
    }
1218
6
    if (exponent <= -1) {
1219
0
        return false;
1220
0
    }
1221
6
    return static_cast<int128_t>(value) == integer;
1222
6
}
1223
1224
12
inline bool floating_equal_decimal(const JsonbValue* floating, const JsonbScaledDecimal& decimal) {
1225
12
    const double value = floating_value(floating);
1226
12
    if (!std::isfinite(value)) {
1227
0
        return false;
1228
0
    }
1229
12
    if (value == 0) {
1230
0
        return decimal.value == 0;
1231
0
    }
1232
1233
12
    int exponent = 0;
1234
12
    const double significand_fraction = std::frexp(value, &exponent);
1235
12
    const double significand_double =
1236
12
            std::ldexp(significand_fraction, std::numeric_limits<double>::digits);
1237
12
    auto significand = wide::Int256(static_cast<int64_t>(significand_double));
1238
12
    exponent -= std::numeric_limits<double>::digits;
1239
1240
12
    const auto five_multiplier = power_of_five(decimal.scale);
1241
12
    if (decimal.value % five_multiplier != 0) {
1242
0
        return false;
1243
0
    }
1244
12
    const auto binary_scaled_decimal = decimal.value / five_multiplier;
1245
12
    return scaled_binary_equal(binary_scaled_decimal, exponent + decimal.scale, significand);
1246
12
}
1247
1248
78
inline bool numeric_equal(const JsonbValue* lhs, const JsonbValue* rhs) {
1249
78
    if (!is_numeric(rhs)) {
1250
4
        return false;
1251
4
    }
1252
1253
74
    if ((lhs->isDouble() || lhs->isFloat()) && rhs->isInt()) {
1254
2
        return floating_equal_integer(lhs, rhs->int_val());
1255
2
    }
1256
1257
72
    if ((rhs->isDouble() || rhs->isFloat()) && lhs->isInt()) {
1258
6
        return floating_equal_integer(rhs, lhs->int_val());
1259
6
    }
1260
1261
66
    if ((lhs->isDouble() || lhs->isFloat()) && rhs->isDecimal()) {
1262
8
        return floating_equal_decimal(lhs, get_scaled_decimal(rhs));
1263
8
    }
1264
1265
58
    if ((rhs->isDouble() || rhs->isFloat()) && lhs->isDecimal()) {
1266
8
        return floating_equal_decimal(rhs, get_scaled_decimal(lhs));
1267
8
    }
1268
1269
50
    if (lhs->isDouble() || lhs->isFloat()) {
1270
0
        return (rhs->isDouble() || rhs->isFloat()) && floating_value(lhs) == floating_value(rhs);
1271
0
    }
1272
1273
50
    if (lhs->isDecimal()) {
1274
12
        const auto lhs_decimal = get_scaled_decimal(lhs);
1275
12
        if (rhs->isDecimal()) {
1276
8
            return scaled_decimal_equal_decimal(lhs_decimal, get_scaled_decimal(rhs));
1277
8
        }
1278
4
        return scaled_decimal_equal_integer(lhs_decimal, rhs->int_val());
1279
12
    }
1280
1281
38
    if (rhs->isDecimal()) {
1282
4
        return scaled_decimal_equal_integer(get_scaled_decimal(rhs), lhs->int_val());
1283
4
    }
1284
1285
34
    return lhs->int_val() == rhs->int_val();
1286
38
}
1287
1288
28
inline bool array_contains_value(const ArrayVal* target_array, const JsonbValue* candidate) {
1289
28
    const int target_num = target_array->numElem();
1290
50
    for (int i = 0; i < target_num; ++i) {
1291
44
        if (target_array->get(i)->contains(candidate)) {
1292
22
            return true;
1293
22
        }
1294
44
    }
1295
6
    return false;
1296
28
}
1297
1298
14
inline bool array_contains_array(const ArrayVal* target_array, const ArrayVal* candidate_array) {
1299
14
    const int candidate_num = candidate_array->numElem();
1300
34
    for (int i = 0; i < candidate_num; ++i) {
1301
24
        if (!array_contains_value(target_array, candidate_array->get(i))) {
1302
4
            return false;
1303
4
        }
1304
24
    }
1305
10
    return true;
1306
14
}
1307
1308
} // namespace jsonb_detail
1309
1310
24
inline const JsonbValue* JsonbDocument::createValue(const char* pb, size_t size) {
1311
24
    if (!pb || size < sizeof(JsonbHeader) + sizeof(JsonbValue)) {
1312
0
        return nullptr;
1313
0
    }
1314
1315
24
    auto* doc = (JsonbDocument*)pb;
1316
24
    if (doc->header_.ver_ != JSONB_VER) {
1317
0
        return nullptr;
1318
0
    }
1319
1320
24
    const auto* val = (const JsonbValue*)doc->payload_;
1321
    // Same as checkAndCreateDocument(), this is intentionally a lightweight structural check for
1322
    // hot paths. Do not recursively validate container bodies here unless the caller is a clearly
1323
    // untrusted raw binary boundary and accepts the O(document size) cost.
1324
24
    if (size != sizeof(JsonbHeader) + val->numPackedBytes()) {
1325
0
        return nullptr;
1326
0
    }
1327
1328
24
    return val;
1329
24
}
1330
1331
12
inline unsigned int JsonbDocument::numPackedBytes() const {
1332
12
    return ((const JsonbValue*)payload_)->numPackedBytes() + sizeof(header_);
1333
12
}
1334
1335
35.6k
inline unsigned int JsonbKeyValue::numPackedBytes() const {
1336
35.6k
    unsigned int ks = keyPackedBytes();
1337
35.6k
    const auto* val = (const JsonbValue*)(((char*)this) + ks);
1338
35.6k
    return ks + val->numPackedBytes();
1339
35.6k
}
1340
1341
// Poor man's "virtual" function JsonbValue::numPackedBytes
1342
143k
inline unsigned int JsonbValue::numPackedBytes() const {
1343
143k
    switch (type) {
1344
5.54k
    case JsonbType::T_Null:
1345
24.6k
    case JsonbType::T_True:
1346
25.8k
    case JsonbType::T_False: {
1347
25.8k
        return sizeof(type);
1348
24.6k
    }
1349
1350
1.76k
    case JsonbType::T_Int8: {
1351
1.76k
        return sizeof(type) + sizeof(int8_t);
1352
24.6k
    }
1353
250
    case JsonbType::T_Int16: {
1354
250
        return sizeof(type) + sizeof(int16_t);
1355
24.6k
    }
1356
7.02k
    case JsonbType::T_Int32: {
1357
7.02k
        return sizeof(type) + sizeof(int32_t);
1358
24.6k
    }
1359
24.3k
    case JsonbType::T_Int64: {
1360
24.3k
        return sizeof(type) + sizeof(int64_t);
1361
24.6k
    }
1362
21.4k
    case JsonbType::T_Double: {
1363
21.4k
        return sizeof(type) + sizeof(double);
1364
24.6k
    }
1365
58
    case JsonbType::T_Float: {
1366
58
        return sizeof(type) + sizeof(float);
1367
24.6k
    }
1368
28.0k
    case JsonbType::T_Int128: {
1369
28.0k
        return sizeof(type) + sizeof(int128_t);
1370
24.6k
    }
1371
21.8k
    case JsonbType::T_String:
1372
30.7k
    case JsonbType::T_Binary: {
1373
30.7k
        return unpack<JsonbBinaryVal>()->numPackedBytes();
1374
21.8k
    }
1375
1376
3.75k
    case JsonbType::T_Object:
1377
4.14k
    case JsonbType::T_Array: {
1378
4.14k
        return unpack<ContainerVal>()->numPackedBytes();
1379
3.75k
    }
1380
38
    case JsonbType::T_Decimal32: {
1381
38
        return JsonbDecimal32::numPackedBytes();
1382
3.75k
    }
1383
18
    case JsonbType::T_Decimal64: {
1384
18
        return JsonbDecimal64::numPackedBytes();
1385
3.75k
    }
1386
30
    case JsonbType::T_Decimal128: {
1387
30
        return JsonbDecimal128::numPackedBytes();
1388
3.75k
    }
1389
12
    case JsonbType::T_Decimal256: {
1390
12
        return JsonbDecimal256::numPackedBytes();
1391
3.75k
    }
1392
0
    case JsonbType::NUM_TYPES:
1393
0
        break;
1394
143k
    }
1395
1396
0
    throw Exception(ErrorCode::INTERNAL_ERROR, "Invalid JSONB value type: {}",
1397
0
                    static_cast<int32_t>(type));
1398
143k
}
1399
1400
12
inline int JsonbValue::numElements() const {
1401
12
    switch (type) {
1402
0
    case JsonbType::T_Int8:
1403
0
    case JsonbType::T_Int16:
1404
0
    case JsonbType::T_Int32:
1405
0
    case JsonbType::T_Int64:
1406
0
    case JsonbType::T_Double:
1407
0
    case JsonbType::T_Float:
1408
0
    case JsonbType::T_Int128:
1409
2
    case JsonbType::T_String:
1410
2
    case JsonbType::T_Binary:
1411
4
    case JsonbType::T_Null:
1412
4
    case JsonbType::T_True:
1413
4
    case JsonbType::T_False:
1414
4
    case JsonbType::T_Decimal32:
1415
4
    case JsonbType::T_Decimal64:
1416
4
    case JsonbType::T_Decimal128:
1417
4
    case JsonbType::T_Decimal256: {
1418
4
        return 1;
1419
4
    }
1420
0
    case JsonbType::T_Object: {
1421
0
        return unpack<ObjectVal>()->numElem();
1422
4
    }
1423
8
    case JsonbType::T_Array: {
1424
8
        return unpack<ArrayVal>()->numElem();
1425
4
    }
1426
0
    case JsonbType::NUM_TYPES:
1427
0
        break;
1428
12
    }
1429
0
    throw Exception(ErrorCode::INTERNAL_ERROR, "Invalid JSONB value type: {}",
1430
0
                    static_cast<int32_t>(type));
1431
12
}
1432
1433
104
inline bool JsonbValue::contains(const JsonbValue* rhs) const {
1434
104
    switch (type) {
1435
46
    case JsonbType::T_Int8:
1436
46
    case JsonbType::T_Int16:
1437
46
    case JsonbType::T_Int32:
1438
46
    case JsonbType::T_Int64:
1439
48
    case JsonbType::T_Int128:
1440
58
    case JsonbType::T_Double:
1441
58
    case JsonbType::T_Float:
1442
70
    case JsonbType::T_Decimal32:
1443
72
    case JsonbType::T_Decimal64:
1444
78
    case JsonbType::T_Decimal128:
1445
78
    case JsonbType::T_Decimal256: {
1446
78
        return jsonb_detail::numeric_equal(this, rhs);
1447
78
    }
1448
2
    case JsonbType::T_String:
1449
2
    case JsonbType::T_Binary: {
1450
2
        if (rhs->isString() || rhs->isBinary()) {
1451
2
            const auto* str_value1 = unpack<JsonbStringVal>();
1452
2
            const auto* str_value2 = rhs->unpack<JsonbStringVal>();
1453
2
            return str_value1->length() == str_value2->length() &&
1454
2
                   std::memcmp(str_value1->getBlob(), str_value2->getBlob(),
1455
2
                               str_value1->length()) == 0;
1456
2
        }
1457
0
        return false;
1458
2
    }
1459
18
    case JsonbType::T_Array: {
1460
18
        const auto* lhs_array = unpack<ArrayVal>();
1461
18
        if (rhs->isArray()) {
1462
14
            return jsonb_detail::array_contains_array(lhs_array, rhs->unpack<ArrayVal>());
1463
14
        }
1464
4
        return jsonb_detail::array_contains_value(lhs_array, rhs);
1465
18
    }
1466
6
    case JsonbType::T_Object: {
1467
6
        if (rhs->isObject()) {
1468
4
            const auto* obj_value1 = unpack<ObjectVal>();
1469
4
            const auto* obj_value2 = rhs->unpack<ObjectVal>();
1470
6
            for (auto it = obj_value2->begin(); it != obj_value2->end(); ++it) {
1471
4
                const JsonbValue* value = obj_value1->find(it->getKeyStr(), it->klen());
1472
4
                if (value == nullptr || !value->contains(it->value())) {
1473
2
                    return false;
1474
2
                }
1475
4
            }
1476
2
            return true;
1477
4
        }
1478
2
        return false;
1479
6
    }
1480
0
    case JsonbType::T_Null: {
1481
0
        return rhs->isNull();
1482
6
    }
1483
0
    case JsonbType::T_True: {
1484
0
        return rhs->isTrue();
1485
6
    }
1486
0
    case JsonbType::T_False: {
1487
0
        return rhs->isFalse();
1488
6
    }
1489
0
    case JsonbType::NUM_TYPES:
1490
0
        break;
1491
104
    }
1492
1493
0
    throw Exception(ErrorCode::INTERNAL_ERROR, "Invalid JSONB value type: {}",
1494
0
                    static_cast<int32_t>(type));
1495
104
}
1496
1497
216
inline bool JsonbPath::seek(const char* key_path, size_t kp_len) {
1498
216
    while (kp_len > 0 && std::isspace(key_path[kp_len - 1])) {
1499
0
        --kp_len;
1500
0
    }
1501
1502
    //path invalid
1503
216
    if (!key_path || kp_len == 0) {
1504
0
        return false;
1505
0
    }
1506
216
    Stream stream(key_path, kp_len);
1507
216
    stream.skip_whitespace();
1508
216
    if (stream.exhausted() || stream.read() != SCOPE) {
1509
        //path invalid
1510
0
        return false;
1511
0
    }
1512
1513
432
    while (!stream.exhausted()) {
1514
216
        stream.skip_whitespace();
1515
216
        stream.clear_leg_ptr();
1516
216
        stream.clear_leg_len();
1517
216
        stream.set_has_escapes(false);
1518
1519
216
        if (!JsonbPath::parsePath(&stream, this)) {
1520
            //path invalid
1521
0
            return false;
1522
0
        }
1523
216
    }
1524
216
    return true;
1525
216
}
1526
1527
216
inline bool JsonbPath::parsePath(Stream* stream, JsonbPath* path) {
1528
    // $[0]
1529
216
    if (stream->peek() == BEGIN_ARRAY) {
1530
120
        return parse_array(stream, path);
1531
120
    }
1532
    // $.a or $.[0]
1533
    // Keep $.[0] for backward compatibility: although the dot before an array
1534
    // leg is non-standard, existing JSONB users may rely on it.
1535
96
    else if (stream->peek() == BEGIN_MEMBER) {
1536
        // advance past the .
1537
92
        stream->skip(1);
1538
1539
92
        if (stream->exhausted()) {
1540
0
            return false;
1541
0
        }
1542
1543
        // $.[0]
1544
92
        if (stream->peek() == BEGIN_ARRAY) {
1545
0
            return parse_array(stream, path);
1546
0
        }
1547
        // $.a
1548
92
        else {
1549
92
            return parse_member(stream, path);
1550
92
        }
1551
92
    } else if (stream->peek() == WILDCARD) {
1552
4
        stream->skip(1);
1553
4
        if (stream->exhausted()) {
1554
0
            return false;
1555
0
        }
1556
1557
        // $**
1558
4
        if (stream->peek() == WILDCARD) {
1559
4
            path->_is_supper_wildcard = true;
1560
4
        }
1561
1562
4
        stream->skip(1);
1563
4
        if (stream->exhausted()) {
1564
0
            return false;
1565
0
        }
1566
1567
4
        if (stream->peek() == BEGIN_ARRAY) {
1568
0
            return parse_array(stream, path);
1569
4
        } else if (stream->peek() == BEGIN_MEMBER) {
1570
            // advance past the .
1571
4
            stream->skip(1);
1572
1573
4
            if (stream->exhausted()) {
1574
0
                return false;
1575
0
            }
1576
1577
            // $**.[0]
1578
            // Keep the dot-array form compatible with the root path behavior.
1579
4
            if (stream->peek() == BEGIN_ARRAY) {
1580
0
                return parse_array(stream, path);
1581
0
            }
1582
            // $.a
1583
4
            else {
1584
4
                return parse_member(stream, path);
1585
4
            }
1586
4
        }
1587
0
        return false;
1588
4
    } else {
1589
0
        return false; //invalid json path
1590
0
    }
1591
216
}
1592
1593
120
inline bool JsonbPath::parse_array(Stream* stream, JsonbPath* path) {
1594
120
    assert(stream->peek() == BEGIN_ARRAY);
1595
120
    stream->skip(1);
1596
120
    if (stream->exhausted()) {
1597
0
        return false;
1598
0
    }
1599
1600
120
    if (stream->peek() == WILDCARD) {
1601
        // Called by function_jsonb.cpp, the variables passed in originate from a mutable block;
1602
        // using const_cast is acceptable.
1603
0
        stream->set_leg_ptr(const_cast<char*>(stream->position()));
1604
0
        stream->add_leg_len();
1605
0
        stream->skip(1);
1606
0
        if (stream->exhausted()) {
1607
0
            return false;
1608
0
        }
1609
1610
0
        if (stream->peek() == END_ARRAY) {
1611
0
            std::unique_ptr<leg_info> leg(
1612
0
                    new leg_info(stream->get_leg_ptr(), stream->get_leg_len(), 0, ARRAY_CODE));
1613
0
            path->add_leg_to_leg_vector(std::move(leg));
1614
0
            stream->skip(1);
1615
0
            path->_is_wildcard = true;
1616
0
            return true;
1617
0
        } else {
1618
0
            return false;
1619
0
        }
1620
0
    }
1621
1622
    // Called by function_jsonb.cpp, the variables passed in originate from a mutable block;
1623
    // using const_cast is acceptable.
1624
120
    stream->set_leg_ptr(const_cast<char*>(stream->position()));
1625
1626
240
    for (; !stream->exhausted() && stream->peek() != END_ARRAY; stream->advance()) {
1627
120
        stream->add_leg_len();
1628
120
    }
1629
1630
120
    if (stream->exhausted() || stream->peek() != END_ARRAY) {
1631
0
        return false;
1632
120
    } else {
1633
120
        stream->skip(1);
1634
120
    }
1635
1636
    //parse array index to int
1637
1638
120
    std::string_view idx_string(stream->get_leg_ptr(), stream->get_leg_len());
1639
120
    int index = 0;
1640
1641
    // Match "last" case-insensitively for compatibility with existing JSONB
1642
    // paths such as [Last] and [LAST].
1643
120
    if (stream->get_leg_len() >= 4 &&
1644
120
        std::equal(LAST, LAST + 4, stream->get_leg_ptr(),
1645
0
                   [](char c1, char c2) { return std::tolower(c1) == std::tolower(c2); })) {
1646
0
        auto pos = idx_string.find(MINUS);
1647
1648
0
        if (pos != std::string::npos) {
1649
0
            for (size_t i = 4; i < pos; ++i) {
1650
0
                if (std::isspace(idx_string[i])) {
1651
0
                    continue;
1652
0
                } else {
1653
                    // leading zeroes are not allowed
1654
0
                    LOG(WARNING) << "Non-space char in idx_string: '" << idx_string << "'";
1655
0
                    return false;
1656
0
                }
1657
0
            }
1658
0
            idx_string = idx_string.substr(pos + 1);
1659
0
            idx_string = trim(idx_string);
1660
1661
            // Keep numeric-prefix parsing for last-N offsets as existing JSONB
1662
            // path behavior.
1663
0
            auto result = std::from_chars(idx_string.data(), idx_string.data() + idx_string.size(),
1664
0
                                          index);
1665
0
            if (result.ec != std::errc()) {
1666
0
                LOG(WARNING) << "Invalid index in JSON path: '" << idx_string << "'";
1667
0
                return false;
1668
0
            }
1669
1670
0
        } else if (stream->get_leg_len() > 4) {
1671
0
            return false;
1672
0
        }
1673
1674
0
        std::unique_ptr<leg_info> leg(new leg_info(nullptr, 0, -index - 1, ARRAY_CODE));
1675
0
        path->add_leg_to_leg_vector(std::move(leg));
1676
1677
0
        return true;
1678
0
    }
1679
1680
    // Preserve legacy numeric-prefix parsing for array indexes. std::from_chars
1681
    // may stop before the end (for example [1.5] is parsed as index 1), and
1682
    // current JSONB path semantics treat that as supported behavior.
1683
120
    auto result = std::from_chars(idx_string.data(), idx_string.data() + idx_string.size(), index);
1684
1685
120
    if (result.ec != std::errc()) {
1686
0
        return false;
1687
0
    }
1688
1689
120
    std::unique_ptr<leg_info> leg(new leg_info(nullptr, 0, index, ARRAY_CODE));
1690
120
    path->add_leg_to_leg_vector(std::move(leg));
1691
1692
120
    return true;
1693
120
}
1694
1695
96
inline bool JsonbPath::parse_member(Stream* stream, JsonbPath* path) {
1696
96
    if (stream->exhausted()) {
1697
0
        return false;
1698
0
    }
1699
1700
96
    if (stream->peek() == WILDCARD) {
1701
        // Called by function_jsonb.cpp, the variables passed in originate from a mutable block;
1702
        // using const_cast is acceptable.
1703
0
        stream->set_leg_ptr(const_cast<char*>(stream->position()));
1704
0
        stream->add_leg_len();
1705
0
        stream->skip(1);
1706
0
        std::unique_ptr<leg_info> leg(
1707
0
                new leg_info(stream->get_leg_ptr(), stream->get_leg_len(), 0, MEMBER_CODE));
1708
0
        path->add_leg_to_leg_vector(std::move(leg));
1709
0
        path->_is_wildcard = true;
1710
0
        return true;
1711
0
    }
1712
1713
    // Called by function_jsonb.cpp, the variables passed in originate from a mutable block;
1714
    // using const_cast is acceptable.
1715
96
    stream->set_leg_ptr(const_cast<char*>(stream->position()));
1716
1717
96
    const char* left_quotation_marks = nullptr;
1718
96
    const char* right_quotation_marks = nullptr;
1719
1720
328
    for (; !stream->exhausted(); stream->advance()) {
1721
        // Only accept space characters quoted by double quotes.
1722
248
        if (std::isspace(stream->peek()) && left_quotation_marks == nullptr) {
1723
0
            return false;
1724
248
        } else if (stream->peek() == ESCAPE) {
1725
22
            stream->add_leg_len();
1726
22
            stream->skip(1);
1727
22
            stream->add_leg_len();
1728
22
            stream->set_has_escapes(true);
1729
22
            if (stream->exhausted()) {
1730
0
                return false;
1731
0
            }
1732
22
            continue;
1733
226
        } else if (stream->peek() == DOUBLE_QUOTE) {
1734
12
            if (left_quotation_marks == nullptr) {
1735
6
                left_quotation_marks = stream->position();
1736
                // Called by function_jsonb.cpp, the variables passed in originate from a mutable block;
1737
                // using const_cast is acceptable.
1738
6
                stream->set_leg_ptr(const_cast<char*>(++left_quotation_marks));
1739
6
                continue;
1740
6
            } else {
1741
6
                right_quotation_marks = stream->position();
1742
6
                stream->skip(1);
1743
6
                break;
1744
6
            }
1745
214
        } else if (stream->peek() == BEGIN_MEMBER || stream->peek() == BEGIN_ARRAY) {
1746
10
            if (left_quotation_marks == nullptr) {
1747
10
                break;
1748
10
            }
1749
10
        }
1750
1751
204
        stream->add_leg_len();
1752
204
    }
1753
1754
96
    if ((left_quotation_marks != nullptr && right_quotation_marks == nullptr) ||
1755
96
        stream->get_leg_ptr() == nullptr || stream->get_leg_len() == 0) {
1756
0
        return false; //invalid json path
1757
0
    }
1758
1759
96
    if (stream->get_has_escapes()) {
1760
6
        stream->remove_escapes();
1761
6
    }
1762
1763
96
    std::unique_ptr<leg_info> leg(
1764
96
            new leg_info(stream->get_leg_ptr(), stream->get_leg_len(), 0, MEMBER_CODE));
1765
96
    path->add_leg_to_leg_vector(std::move(leg));
1766
1767
96
    return true;
1768
96
}
1769
1770
static_assert(is_pod_v<JsonbDocument>, "JsonbDocument must be standard layout and trivial");
1771
static_assert(is_pod_v<JsonbValue>, "JsonbValue must be standard layout and trivial");
1772
static_assert(is_pod_v<JsonbDecimal32>, "JsonbDecimal32 must be standard layout and trivial");
1773
static_assert(is_pod_v<JsonbDecimal64>, "JsonbDecimal64 must be standard layout and trivial");
1774
static_assert(is_pod_v<JsonbDecimal128>, "JsonbDecimal128 must be standard layout and trivial");
1775
static_assert(is_pod_v<JsonbDecimal256>, "JsonbDecimal256 must be standard layout and trivial");
1776
static_assert(is_pod_v<JsonbInt8Val>, "JsonbInt8Val must be standard layout and trivial");
1777
static_assert(is_pod_v<JsonbInt32Val>, "JsonbInt32Val must be standard layout and trivial");
1778
static_assert(is_pod_v<JsonbInt64Val>, "JsonbInt64Val must be standard layout and trivial");
1779
static_assert(is_pod_v<JsonbInt128Val>, "JsonbInt128Val must be standard layout and trivial");
1780
static_assert(is_pod_v<JsonbDoubleVal>, "JsonbDoubleVal must be standard layout and trivial");
1781
static_assert(is_pod_v<JsonbFloatVal>, "JsonbFloatVal must be standard layout and trivial");
1782
static_assert(is_pod_v<JsonbBinaryVal>, "JsonbBinaryVal must be standard layout and trivial");
1783
static_assert(is_pod_v<ContainerVal>, "ContainerVal must be standard layout and trivial");
1784
1785
#define ASSERT_DECIMAL_LAYOUT(type)                \
1786
    static_assert(offsetof(type, precision) == 0); \
1787
    static_assert(offsetof(type, scale) == 4);     \
1788
    static_assert(offsetof(type, value) == 8);
1789
1790
ASSERT_DECIMAL_LAYOUT(JsonbDecimal32)
1791
ASSERT_DECIMAL_LAYOUT(JsonbDecimal64)
1792
ASSERT_DECIMAL_LAYOUT(JsonbDecimal128)
1793
ASSERT_DECIMAL_LAYOUT(JsonbDecimal256)
1794
1795
#define ASSERT_NUMERIC_LAYOUT(type) static_assert(offsetof(type, num) == 0);
1796
1797
ASSERT_NUMERIC_LAYOUT(JsonbInt8Val)
1798
ASSERT_NUMERIC_LAYOUT(JsonbInt32Val)
1799
ASSERT_NUMERIC_LAYOUT(JsonbInt64Val)
1800
ASSERT_NUMERIC_LAYOUT(JsonbInt128Val)
1801
ASSERT_NUMERIC_LAYOUT(JsonbDoubleVal)
1802
1803
static_assert(offsetof(JsonbBinaryVal, size) == 0);
1804
static_assert(offsetof(JsonbBinaryVal, payload) == 4);
1805
1806
static_assert(offsetof(ContainerVal, size) == 0);
1807
static_assert(offsetof(ContainerVal, payload) == 4);
1808
1809
#pragma pack(pop)
1810
#if defined(__clang__)
1811
#pragma clang diagnostic pop
1812
#endif
1813
} // namespace doris
1814
1815
#endif // JSONB_JSONBDOCUMENT_H