Coverage Report

Created: 2026-04-14 13:42

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
173k
PathInData::PathInData(std::string_view path_, bool is_typed_) : path(path_), is_typed(is_typed_) {
34
173k
    const char* begin = path.data();
35
173k
    const char* end = path.data() + path.size();
36
898k
    for (const char* it = path.data(); it != end; ++it) {
37
725k
        if (*it == '.') {
38
6.50k
            size_t size = static_cast<size_t>(it - begin);
39
6.50k
            parts.emplace_back(std::string_view {begin, size}, false, 0);
40
6.50k
            begin = it + 1;
41
6.50k
        }
42
725k
    }
43
173k
    size_t size = static_cast<size_t>(end - begin);
44
173k
    parts.emplace_back(std::string_view {begin, size}, false, 0.);
45
173k
}
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
1.35M
PathInData::PathInData(const Parts& parts_) {
57
1.35M
    build_path(parts_);
58
1.35M
    build_parts(parts_);
59
1.35M
}
60
806k
PathInData::PathInData(const PathInData& other) : path(other.path), is_typed(other.is_typed) {
61
806k
    build_parts(other.get_parts());
62
806k
}
63
64
2
PathInData::PathInData(const std::string& root, const std::vector<std::string>& paths) {
65
2
    PathInDataBuilder path_builder;
66
2
    path_builder.append(root, false);
67
5
    for (const std::string& p : paths) {
68
5
        path_builder.append(p, false);
69
5
    }
70
2
    build_path(path_builder.get_parts());
71
2
    build_parts(path_builder.get_parts());
72
2
}
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
7.91k
PathInData& PathInData::operator=(const PathInData& other) {
84
7.91k
    if (this != &other) {
85
7.91k
        path = other.path;
86
7.91k
        is_typed = other.is_typed;
87
7.91k
        build_parts(other.parts);
88
7.91k
    }
89
7.91k
    return *this;
90
7.91k
}
91
92
600k
UInt128 PathInData::get_parts_hash(const Parts& parts_, bool is_typed_) {
93
600k
    SipHash hash;
94
600k
    hash.update(parts_.size());
95
600k
    for (const auto& part : parts_) {
96
13.0k
        hash.update(part.key.data(), part.key.length());
97
13.0k
        hash.update(part.is_nested);
98
13.0k
        hash.update(part.anonymous_array_level);
99
13.0k
    }
100
600k
    hash.update(is_typed_);
101
600k
    UInt128 res;
102
600k
    hash.get128(res);
103
600k
    return res;
104
600k
}
105
106
1.36M
void PathInData::build_path(const Parts& other_parts) {
107
1.36M
    if (other_parts.empty()) {
108
1.61k
        return;
109
1.61k
    }
110
1.36M
    path.clear();
111
1.36M
    auto it = other_parts.begin();
112
1.36M
    path += it->key;
113
1.36M
    ++it;
114
1.44M
    for (; it != other_parts.end(); ++it) {
115
83.4k
        path += ".";
116
83.4k
        path += it->key;
117
83.4k
    }
118
1.36M
}
119
2.17M
void PathInData::build_parts(const Parts& other_parts) {
120
2.17M
    if (other_parts.empty()) {
121
236k
        return;
122
236k
    }
123
1.94M
    parts.clear();
124
1.94M
    parts.reserve(other_parts.size());
125
1.94M
    const char* begin = path.data();
126
8.53M
    for (const auto& part : other_parts) {
127
8.53M
        has_nested |= part.is_nested;
128
8.53M
        parts.emplace_back(std::string_view {begin, part.key.length()}, part.is_nested,
129
8.53M
                           part.anonymous_array_level);
130
8.53M
        begin += part.key.length() + 1;
131
8.53M
    }
132
1.94M
}
133
134
12.3k
void PathInData::from_protobuf(const segment_v2::ColumnPathInfo& pb) {
135
12.3k
    parts.clear();
136
12.3k
    path = pb.path();
137
12.3k
    has_nested = false;
138
12.3k
    is_typed = pb.is_typed();
139
12.3k
    parts.reserve(pb.path_part_infos().size());
140
12.3k
    const char* begin = path.data();
141
15.9k
    for (const segment_v2::ColumnPathPartInfo& part_info : pb.path_part_infos()) {
142
15.9k
        Part part;
143
15.9k
        part.is_nested = part_info.is_nested();
144
15.9k
        has_nested |= part.is_nested;
145
15.9k
        part.anonymous_array_level =
146
15.9k
                cast_set<uint8_t, uint32_t, false>(part_info.anonymous_array_level());
147
        // use string_view to ref data in path
148
15.9k
        part.key = std::string_view {begin, part_info.key().length()};
149
15.9k
        parts.push_back(part);
150
15.9k
        begin += part.key.length() + 1;
151
15.9k
    }
152
12.3k
}
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
18.4k
void PathInData::to_protobuf(segment_v2::ColumnPathInfo* pb, int32_t parent_col_unique_id) const {
170
18.4k
    pb->set_path(path);
171
18.4k
    pb->set_has_nested(has_nested);
172
18.4k
    pb->set_parrent_column_unique_id(parent_col_unique_id);
173
18.4k
    pb->set_is_typed(is_typed);
174
175
    // set parts info
176
19.8k
    for (const Part& part : parts) {
177
19.8k
        segment_v2::ColumnPathPartInfo& part_info = *pb->add_path_part_infos();
178
19.8k
        part_info.set_key(std::string(part.key.data(), part.key.size()));
179
19.8k
        part_info.set_is_nested(part.is_nested);
180
19.8k
        part_info.set_anonymous_array_level(part.anonymous_array_level);
181
19.8k
    }
182
18.4k
}
183
184
31.5k
size_t PathInData::Hash::operator()(const PathInData& value) const {
185
31.5k
    auto hash = get_parts_hash(value.parts, value.is_typed);
186
31.5k
    return hash.low() ^ hash.high();
187
31.5k
}
188
189
37
bool PathInData::need_record_stats() const {
190
37
    return !empty() && !is_typed && !has_nested &&
191
37
           path.find(DOC_VALUE_COLUMN_PATH) == std::string::npos;
192
37
}
193
194
5.83k
PathInData PathInData::copy_pop_front() const {
195
5.83k
    return copy_pop_nfront(1);
196
5.83k
}
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
6.18k
PathInData PathInData::copy_pop_nfront(size_t n) const {
228
6.18k
    if (n >= parts.size()) {
229
1.40k
        return {};
230
1.40k
    }
231
4.78k
    PathInData new_path;
232
4.78k
    Parts new_parts;
233
4.78k
    if (!parts.empty()) {
234
4.78k
        std::copy(parts.begin() + n, parts.end(), std::back_inserter(new_parts));
235
4.78k
    }
236
4.78k
    new_path.build_path(new_parts);
237
4.78k
    new_path.build_parts(new_parts);
238
4.78k
    new_path.is_typed = is_typed;
239
4.78k
    return new_path;
240
6.18k
}
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
1.42M
PathInDataBuilder& PathInDataBuilder::append(std::string_view key, bool is_array) {
267
1.42M
    if (parts.empty()) {
268
1.35M
        current_anonymous_array_level += is_array;
269
1.35M
    }
270
1.42M
    if (!parts.empty()) {
271
73.1k
        parts.back().is_nested = is_array;
272
73.1k
    }
273
1.42M
    parts.emplace_back(key, false, current_anonymous_array_level);
274
1.42M
    current_anonymous_array_level = 0;
275
1.42M
    return *this;
276
1.42M
}
277
58.1k
PathInDataBuilder& PathInDataBuilder::append(const PathInData::Parts& path, bool is_array) {
278
58.1k
    if (parts.empty()) {
279
51.2k
        current_anonymous_array_level += is_array;
280
51.2k
    }
281
58.1k
    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
58.1k
    return *this;
292
58.1k
}
293
294
1.42M
void PathInDataBuilder::pop_back() {
295
1.42M
    if (!parts.empty()) {
296
1.42M
        parts.pop_back();
297
1.42M
    }
298
1.42M
}
299
300
58.1k
void PathInDataBuilder::pop_back(size_t n) {
301
58.1k
    assert(n <= parts.size());
302
58.1k
    parts.resize(parts.size() - n);
303
58.1k
}
304
305
} // namespace doris