Coverage Report

Created: 2026-05-29 13:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/json/path_in_data.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
// This file is copied from
18
// https://github.com/ClickHouse/ClickHouse/blob/master/src/DataTypes/Serializations/PathInData.cpp
19
// and modified by Doris
20
21
#include "util/json/path_in_data.h"
22
23
#include <assert.h>
24
25
#include <string_view>
26
27
#include "common/cast_set.h"
28
#include "exec/common/sip_hash.h"
29
#include "exec/common/variant_util.h"
30
31
namespace doris {
32
33
1.37M
PathInData::PathInData(std::string_view path_, bool is_typed_) : path(path_), is_typed(is_typed_) {
34
1.37M
    const char* begin = path.data();
35
1.37M
    const char* end = path.data() + path.size();
36
33.7M
    for (const char* it = path.data(); it != end; ++it) {
37
32.3M
        if (*it == '.') {
38
2.77M
            size_t size = static_cast<size_t>(it - begin);
39
2.77M
            parts.emplace_back(std::string_view {begin, size}, false, 0);
40
2.77M
            begin = it + 1;
41
2.77M
        }
42
32.3M
    }
43
1.37M
    size_t size = static_cast<size_t>(end - begin);
44
1.37M
    parts.emplace_back(std::string_view {begin, size}, false, 0.);
45
1.37M
}
46
47
3
PathInData::PathInData(std::string_view path_, const Parts& parts_, bool is_typed_) {
48
3
    path = path_;
49
3
    is_typed = is_typed_;
50
9
    for (const auto& part : parts_) {
51
9
        has_nested |= part.is_nested;
52
9
        parts.emplace_back(part);
53
9
    }
54
3
}
55
56
14.7M
PathInData::PathInData(const Parts& parts_) {
57
14.7M
    build_path(parts_);
58
14.7M
    build_parts(parts_);
59
14.7M
}
60
89.4M
PathInData::PathInData(const PathInData& other) : path(other.path), is_typed(other.is_typed) {
61
89.4M
    build_parts(other.get_parts());
62
89.4M
}
63
64
8.49k
PathInData::PathInData(const std::string& root, const std::vector<std::string>& paths) {
65
8.49k
    PathInDataBuilder path_builder;
66
8.49k
    path_builder.append(root, false);
67
8.49k
    for (const std::string& p : paths) {
68
7.91k
        path_builder.append(p, false);
69
7.91k
    }
70
8.49k
    build_path(path_builder.get_parts());
71
8.49k
    build_parts(path_builder.get_parts());
72
8.49k
}
73
74
1
PathInData::PathInData(const std::vector<std::string>& paths) {
75
1
    PathInDataBuilder path_builder;
76
4
    for (size_t i = 0; i < paths.size(); ++i) {
77
3
        path_builder.append(paths[i], false);
78
3
    }
79
1
    build_path(path_builder.get_parts());
80
1
    build_parts(path_builder.get_parts());
81
1
}
82
83
536k
PathInData& PathInData::operator=(const PathInData& other) {
84
536k
    if (this != &other) {
85
536k
        path = other.path;
86
536k
        is_typed = other.is_typed;
87
536k
        build_parts(other.parts);
88
536k
    }
89
536k
    return *this;
90
536k
}
91
92
36.6M
UInt128 PathInData::get_parts_hash(const Parts& parts_, bool is_typed_) {
93
36.6M
    SipHash hash;
94
36.6M
    hash.update(parts_.size());
95
36.6M
    for (const auto& part : parts_) {
96
127k
        hash.update(part.key.data(), part.key.length());
97
127k
        hash.update(part.is_nested);
98
127k
        hash.update(part.anonymous_array_level);
99
127k
    }
100
36.6M
    hash.update(is_typed_);
101
36.6M
    UInt128 res;
102
36.6M
    hash.get128(res);
103
36.6M
    return res;
104
36.6M
}
105
106
15.0M
void PathInData::build_path(const Parts& other_parts) {
107
15.0M
    if (other_parts.empty()) {
108
1.71k
        return;
109
1.71k
    }
110
15.0M
    path.clear();
111
15.0M
    auto it = other_parts.begin();
112
15.0M
    path += it->key;
113
15.0M
    ++it;
114
18.1M
    for (; it != other_parts.end(); ++it) {
115
3.08M
        path += ".";
116
3.08M
        path += it->key;
117
3.08M
    }
118
15.0M
}
119
104M
void PathInData::build_parts(const Parts& other_parts) {
120
104M
    if (other_parts.empty()) {
121
71.2M
        return;
122
71.2M
    }
123
33.3M
    parts.clear();
124
33.3M
    parts.reserve(other_parts.size());
125
33.3M
    const char* begin = path.data();
126
50.2M
    for (const auto& part : other_parts) {
127
50.2M
        has_nested |= part.is_nested;
128
50.2M
        parts.emplace_back(std::string_view {begin, part.key.length()}, part.is_nested,
129
50.2M
                           part.anonymous_array_level);
130
50.2M
        begin += part.key.length() + 1;
131
50.2M
    }
132
33.3M
}
133
134
277k
void PathInData::from_protobuf(const segment_v2::ColumnPathInfo& pb) {
135
277k
    parts.clear();
136
277k
    path = pb.path();
137
277k
    has_nested = false;
138
277k
    is_typed = pb.is_typed();
139
277k
    parts.reserve(pb.path_part_infos().size());
140
277k
    const char* begin = path.data();
141
582k
    for (const segment_v2::ColumnPathPartInfo& part_info : pb.path_part_infos()) {
142
582k
        Part part;
143
582k
        part.is_nested = part_info.is_nested();
144
582k
        has_nested |= part.is_nested;
145
582k
        part.anonymous_array_level =
146
582k
                cast_set<uint8_t, uint32_t, false>(part_info.anonymous_array_level());
147
        // use string_view to ref data in path
148
582k
        part.key = std::string_view {begin, part_info.key().length()};
149
582k
        parts.push_back(part);
150
582k
        begin += part.key.length() + 1;
151
582k
    }
152
277k
}
153
154
2
std::string PathInData::to_jsonpath() const {
155
2
    std::string jsonpath = "$.";
156
2
    if (parts.empty()) {
157
0
        return jsonpath;
158
0
    }
159
2
    auto it = parts.begin();
160
2
    jsonpath += it->key;
161
2
    ++it;
162
3
    for (; it != parts.end(); ++it) {
163
1
        jsonpath += ".";
164
1
        jsonpath += it->key;
165
1
    }
166
2
    return jsonpath;
167
2
}
168
169
283k
void PathInData::to_protobuf(segment_v2::ColumnPathInfo* pb, int32_t parent_col_unique_id) const {
170
283k
    pb->set_path(path);
171
283k
    pb->set_has_nested(has_nested);
172
283k
    pb->set_parrent_column_unique_id(parent_col_unique_id);
173
283k
    pb->set_is_typed(is_typed);
174
175
    // set parts info
176
478k
    for (const Part& part : parts) {
177
478k
        segment_v2::ColumnPathPartInfo& part_info = *pb->add_path_part_infos();
178
478k
        part_info.set_key(std::string(part.key.data(), part.key.size()));
179
478k
        part_info.set_is_nested(part.is_nested);
180
478k
        part_info.set_anonymous_array_level(part.anonymous_array_level);
181
478k
    }
182
283k
}
183
184
35.7M
size_t PathInData::Hash::operator()(const PathInData& value) const {
185
35.7M
    auto hash = get_parts_hash(value.parts, value.is_typed);
186
35.7M
    return hash.low() ^ hash.high();
187
35.7M
}
188
189
1.11k
bool PathInData::need_record_stats() const {
190
1.11k
    return !empty() && !is_typed && !has_nested &&
191
1.11k
           path.find(DOC_VALUE_COLUMN_PATH) == std::string::npos;
192
1.11k
}
193
194
275k
PathInData PathInData::copy_pop_front() const {
195
275k
    return copy_pop_nfront(1);
196
275k
}
197
198
9
PathInData PathInData::get_nested_prefix_path() const {
199
9
    CHECK(has_nested_part());
200
9
    PathInData new_path;
201
9
    Parts new_parts;
202
17
    for (const Part& part : parts) {
203
17
        new_parts.push_back(part);
204
17
        if (part.is_nested) {
205
9
            break;
206
9
        }
207
17
    }
208
9
    new_path.build_path(new_parts);
209
9
    new_path.build_parts(new_parts);
210
9
    new_path.is_typed = is_typed;
211
9
    return new_path;
212
9
}
213
214
2
PathInData PathInData::copy_pop_back() const {
215
2
    if (parts.size() <= 1) {
216
1
        return {};
217
1
    }
218
1
    PathInData new_path;
219
1
    Parts new_parts = parts;
220
1
    new_parts.pop_back();
221
1
    new_path.build_path(new_parts);
222
1
    new_path.build_parts(new_parts);
223
1
    new_path.is_typed = is_typed;
224
1
    return new_path;
225
2
}
226
227
296k
PathInData PathInData::copy_pop_nfront(size_t n) const {
228
296k
    if (n >= parts.size()) {
229
14.8k
        return {};
230
14.8k
    }
231
281k
    PathInData new_path;
232
281k
    Parts new_parts;
233
284k
    if (!parts.empty()) {
234
284k
        std::copy(parts.begin() + n, parts.end(), std::back_inserter(new_parts));
235
284k
    }
236
281k
    new_path.build_path(new_parts);
237
281k
    new_path.build_parts(new_parts);
238
281k
    new_path.is_typed = is_typed;
239
281k
    return new_path;
240
296k
}
241
242
bool PathInData::try_strip_prefix(const std::string& name, const std::string& prefix_dot,
243
0
                                  std::string* out) {
244
0
    if (!name.starts_with(prefix_dot)) {
245
0
        return false;
246
0
    }
247
0
    *out = name.substr(prefix_dot.size());
248
0
    return !out->empty();
249
0
}
250
251
0
PathInData PathInData::append(const PathInData& base, std::string_view suffix) {
252
0
    if (suffix.empty()) {
253
0
        return base;
254
0
    }
255
0
    if (base.empty()) {
256
0
        return PathInData(suffix);
257
0
    }
258
0
    std::string new_path;
259
0
    new_path.reserve(base.get_path().size() + 1 + suffix.size());
260
0
    new_path.append(base.get_path());
261
0
    new_path.push_back('.');
262
0
    new_path.append(suffix.data(), suffix.size());
263
0
    return PathInData(new_path);
264
0
}
265
266
14.7M
PathInDataBuilder& PathInDataBuilder::append(std::string_view key, bool is_array) {
267
14.7M
    if (parts.empty()) {
268
13.5M
        current_anonymous_array_level += is_array;
269
13.5M
    }
270
14.7M
    if (!parts.empty()) {
271
1.26M
        parts.back().is_nested = is_array;
272
1.26M
    }
273
14.7M
    parts.emplace_back(key, false, current_anonymous_array_level);
274
14.7M
    current_anonymous_array_level = 0;
275
14.7M
    return *this;
276
14.7M
}
277
376k
PathInDataBuilder& PathInDataBuilder::append(const PathInData::Parts& path, bool is_array) {
278
376k
    if (parts.empty()) {
279
151k
        current_anonymous_array_level += is_array;
280
151k
    }
281
376k
    if (!path.empty()) {
282
6.02k
        if (!parts.empty()) {
283
6.01k
            parts.back().is_nested = is_array;
284
6.01k
        }
285
6.02k
        auto it = parts.insert(parts.end(), path.begin(), path.end());
286
18.0k
        for (; it != parts.end(); ++it) {
287
12.0k
            it->anonymous_array_level += current_anonymous_array_level;
288
12.0k
        }
289
6.02k
        current_anonymous_array_level = 0;
290
6.02k
    }
291
376k
    return *this;
292
376k
}
293
294
14.8M
void PathInDataBuilder::pop_back() {
295
14.8M
    if (!parts.empty()) {
296
14.8M
        parts.pop_back();
297
14.8M
    }
298
14.8M
}
299
300
375k
void PathInDataBuilder::pop_back(size_t n) {
301
375k
    assert(n <= parts.size());
302
375k
    parts.resize(parts.size() - n);
303
375k
}
304
305
} // namespace doris