Coverage Report

Created: 2026-05-14 04:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/json/json_parser.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
// This file is copied from
18
// https://github.com/ClickHouse/ClickHouse/blob/master/src/Common/JSONParsers/SimdJSONParser.h
19
// and modified by Doris
20
21
#pragma once
22
23
#include <parallel_hashmap/phmap.h>
24
#include <stddef.h>
25
26
#include <optional>
27
#include <string>
28
#include <utility>
29
#include <vector>
30
31
#include "core/column/column.h"
32
#include "core/data_type/primitive_type.h"
33
#include "core/field.h"
34
#include "core/string_ref.h"
35
#include "core/uint128.h"
36
#include "util/json/path_in_data.h"
37
#include "util/json/simd_json_parser.h"
38
#include "util/jsonb_writer.h"
39
40
namespace doris {
41
42
template <typename Element>
43
1.88M
Field getValueAsField(const Element& element) {
44
    // bool will convert to type FiledType::UInt64
45
1.88M
    if (element.isBool()) {
46
20.6k
        return Field::create_field<TYPE_BOOLEAN>(element.getBool());
47
20.6k
    }
48
1.86M
    if (element.isInt64()) {
49
804k
        return Field::create_field<TYPE_BIGINT>(element.getInt64());
50
804k
    }
51
    // doris only support signed integers at present
52
    // use largeint to store unsigned int64
53
1.05M
    if (element.isUInt64()) {
54
20
        return Field::create_field<TYPE_LARGEINT>(static_cast<int128_t>(element.getUInt64()));
55
20
    }
56
1.05M
    if (element.isDouble()) {
57
116k
        return Field::create_field<TYPE_DOUBLE>(element.getDouble());
58
116k
    }
59
942k
    if (element.isString()) {
60
930k
        return Field::create_field<TYPE_STRING>(String(element.getString()));
61
930k
    }
62
11.9k
    if (element.isNull()) {
63
11.9k
        return Field();
64
11.9k
    }
65
0
    return Field();
66
11.9k
}
67
68
template <typename Element>
69
2.81k
void writeValueAsJsonb(const Element& element, JsonbWriter& writer) {
70
    // bool will convert to type FiledType::UInt64
71
2.81k
    if (element.isBool()) {
72
77
        writer.writeBool(element.getBool());
73
77
        return;
74
77
    }
75
2.74k
    if (element.isInt64()) {
76
358
        writer.writeInt64(element.getInt64());
77
358
        return;
78
358
    }
79
    // doris only support signed integers at present
80
    // use largeint to store unsigned int64
81
2.38k
    if (element.isUInt64()) {
82
0
        writer.writeInt128(static_cast<int128_t>(element.getUInt64()));
83
0
        return;
84
0
    }
85
2.38k
    if (element.isDouble()) {
86
303
        writer.writeDouble(element.getDouble());
87
303
        return;
88
303
    }
89
2.07k
    if (element.isString()) {
90
2.00k
        writer.writeStartString();
91
2.00k
        std::string_view str = element.getString();
92
2.00k
        writer.writeString(str.data(), str.size());
93
2.00k
        writer.writeEndString();
94
2.00k
        return;
95
2.00k
    }
96
77
    if (element.isNull()) {
97
77
        writer.writeNull();
98
77
        return;
99
77
    }
100
77
}
101
102
struct ParseConfig {
103
    bool deprecated_enable_flatten_nested = false;
104
    bool check_duplicate_json_path = false;
105
    bool reject_json_null_value = false;
106
    bool record_empty_object_path = false;
107
    enum class ParseTo {
108
        OnlySubcolumns = 0,
109
        OnlyDocValueColumn = 1,
110
    };
111
    ParseTo parse_to = ParseTo::OnlySubcolumns;
112
};
113
/// Result of parsing of a document.
114
/// Contains all paths extracted from document
115
/// and values which are related to them.
116
struct ParseResult {
117
    std::vector<PathInData> paths;
118
    std::vector<Field> values;
119
};
120
template <typename ParserImpl>
121
class JSONDataParser {
122
public:
123
    using Element = typename ParserImpl::Element;
124
    using JSONObject = typename ParserImpl::Object;
125
    using JSONArray = typename ParserImpl::Array;
126
    std::optional<ParseResult> parse(const char* begin, size_t length, const ParseConfig& config);
127
128
private:
129
    struct ParseContext {
130
        PathInDataBuilder builder;
131
        std::vector<PathInData::Parts> paths;
132
        std::vector<Field> values;
133
        phmap::flat_hash_set<std::string> visited_path_names;
134
        bool deprecated_enable_flatten_nested = false;
135
        bool check_duplicate_json_path = false;
136
        bool reject_json_null_value = false;
137
        bool record_empty_object_path = false;
138
        bool has_nested_in_flatten = false;
139
        bool is_top_array = false;
140
    };
141
    using PathPartsWithArray = std::pair<PathInData::Parts, Array>;
142
    using PathToArray = phmap::flat_hash_map<UInt128, PathPartsWithArray, UInt128TrivialHash>;
143
    using KeyToSizes = phmap::flat_hash_map<StringRef, std::vector<size_t>, StringRefHash>;
144
    struct ParseArrayContext {
145
        size_t current_size = 0;
146
        size_t total_size = 0;
147
        PathToArray arrays_by_path;
148
        KeyToSizes nested_sizes_by_key;
149
        bool has_nested_in_flatten = false;
150
        bool is_top_array = false;
151
        bool check_duplicate_json_path = false;
152
        bool reject_json_null_value = false;
153
    };
154
    void traverse(const Element& element, ParseContext& ctx);
155
    void traverseObject(const JSONObject& object, ParseContext& ctx);
156
    void traverseArray(const JSONArray& array, ParseContext& ctx);
157
    void appendValueIfNotDuplicate(ParseContext& ctx, const PathInData::Parts& path, Field&& value);
158
    void traverseArrayElement(const Element& element, ParseArrayContext& ctx);
159
    void checkAmbiguousStructure(const ParseArrayContext& ctx,
160
                                 const std::vector<PathInData::Parts>& paths);
161
    void handleExistingPath(std::pair<PathInData::Parts, Array>& path_data,
162
                            const PathInData::Parts& path, Field& value, ParseArrayContext& ctx,
163
                            size_t& keys_to_update);
164
    void handleNewPath(UInt128 hash, const PathInData::Parts& path, Field& value,
165
                       ParseArrayContext& ctx);
166
    static void fillMissedValuesInArrays(ParseArrayContext& ctx);
167
    static bool tryInsertDefaultFromNested(ParseArrayContext& ctx, const PathInData::Parts& path,
168
                                           Array& array);
169
    static StringRef getNameOfNested(const PathInData::Parts& path, const Field& value);
170
171
    bool has_nested = false;
172
    void check_has_nested_object(const Element& element);
173
    void traverseAsJsonb(const Element& element, JsonbWriter& writer);
174
    void traverseObjectAsJsonb(const JSONObject& object, JsonbWriter& writer);
175
    void traverseArrayAsJsonb(const JSONArray& array, JsonbWriter& writer);
176
177
    ParserImpl parser;
178
};
179
180
} // namespace doris