Coverage Report

Created: 2026-04-15 18:59

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
346k
PathInData::PathInData(std::string_view path_, bool is_typed_) : path(path_), is_typed(is_typed_) {
34
346k
    const char* begin = path.data();
35
346k
    const char* end = path.data() + path.size();
36
1.79M
    for (const char* it = path.data(); it != end; ++it) {
37
1.44M
        if (*it == '.') {
38
12.9k
            size_t size = static_cast<size_t>(it - begin);
39
12.9k
            parts.emplace_back(std::string_view {begin, size}, false, 0);
40
12.9k
            begin = it + 1;
41
12.9k
        }
42
1.44M
    }
43
346k
    size_t size = static_cast<size_t>(end - begin);
44
346k
    parts.emplace_back(std::string_view {begin, size}, false, 0.);
45
346k
}
46
47
6
PathInData::PathInData(std::string_view path_, const Parts& parts_, bool is_typed_) {
48
6
    path = path_;
49
6
    is_typed = is_typed_;
50
18
    for (const auto& part : parts_) {
51
18
        has_nested |= part.is_nested;
52
18
        parts.emplace_back(part);
53
18
    }
54
6
}
55
56
2.71M
PathInData::PathInData(const Parts& parts_) {
57
2.71M
    build_path(parts_);
58
2.71M
    build_parts(parts_);
59
2.71M
}
60
1.61M
PathInData::PathInData(const PathInData& other) : path(other.path), is_typed(other.is_typed) {
61
1.61M
    build_parts(other.get_parts());
62
1.61M
}
63
64
4
PathInData::PathInData(const std::string& root, const std::vector<std::string>& paths) {
65
4
    PathInDataBuilder path_builder;
66
4
    path_builder.append(root, false);
67
10
    for (const std::string& p : paths) {
68
10
        path_builder.append(p, false);
69
10
    }
70
4
    build_path(path_builder.get_parts());
71
4
    build_parts(path_builder.get_parts());
72
4
}
73
74
2
PathInData::PathInData(const std::vector<std::string>& paths) {
75
2
    PathInDataBuilder path_builder;
76
8
    for (size_t i = 0; i < paths.size(); ++i) {
77
6
        path_builder.append(paths[i], false);
78
6
    }
79
2
    build_path(path_builder.get_parts());
80
2
    build_parts(path_builder.get_parts());
81
2
}
82
83
15.8k
PathInData& PathInData::operator=(const PathInData& other) {
84
15.8k
    if (this != &other) {
85
15.8k
        path = other.path;
86
15.8k
        is_typed = other.is_typed;
87
15.8k
        build_parts(other.parts);
88
15.8k
    }
89
15.8k
    return *this;
90
15.8k
}
91
92
1.20M
UInt128 PathInData::get_parts_hash(const Parts& parts_, bool is_typed_) {
93
1.20M
    SipHash hash;
94
1.20M
    hash.update(parts_.size());
95
1.20M
    for (const auto& part : parts_) {
96
26.1k
        hash.update(part.key.data(), part.key.length());
97
26.1k
        hash.update(part.is_nested);
98
26.1k
        hash.update(part.anonymous_array_level);
99
26.1k
    }
100
1.20M
    hash.update(is_typed_);
101
1.20M
    UInt128 res;
102
1.20M
    hash.get128(res);
103
1.20M
    return res;
104
1.20M
}
105
106
2.72M
void PathInData::build_path(const Parts& other_parts) {
107
2.72M
    if (other_parts.empty()) {
108
3.22k
        return;
109
3.22k
    }
110
2.72M
    path.clear();
111
2.72M
    auto it = other_parts.begin();
112
2.72M
    path += it->key;
113
2.72M
    ++it;
114
2.89M
    for (; it != other_parts.end(); ++it) {
115
166k
        path += ".";
116
166k
        path += it->key;
117
166k
    }
118
2.72M
}
119
4.35M
void PathInData::build_parts(const Parts& other_parts) {
120
4.35M
    if (other_parts.empty()) {
121
473k
        return;
122
473k
    }
123
3.88M
    parts.clear();
124
3.88M
    parts.reserve(other_parts.size());
125
3.88M
    const char* begin = path.data();
126
17.0M
    for (const auto& part : other_parts) {
127
17.0M
        has_nested |= part.is_nested;
128
17.0M
        parts.emplace_back(std::string_view {begin, part.key.length()}, part.is_nested,
129
17.0M
                           part.anonymous_array_level);
130
17.0M
        begin += part.key.length() + 1;
131
17.0M
    }
132
3.88M
}
133
134
24.7k
void PathInData::from_protobuf(const segment_v2::ColumnPathInfo& pb) {
135
24.7k
    parts.clear();
136
24.7k
    path = pb.path();
137
24.7k
    has_nested = false;
138
24.7k
    is_typed = pb.is_typed();
139
24.7k
    parts.reserve(pb.path_part_infos().size());
140
24.7k
    const char* begin = path.data();
141
31.7k
    for (const segment_v2::ColumnPathPartInfo& part_info : pb.path_part_infos()) {
142
31.7k
        Part part;
143
31.7k
        part.is_nested = part_info.is_nested();
144
31.7k
        has_nested |= part.is_nested;
145
31.7k
        part.anonymous_array_level =
146
31.7k
                cast_set<uint8_t, uint32_t, false>(part_info.anonymous_array_level());
147
        // use string_view to ref data in path
148
31.7k
        part.key = std::string_view {begin, part_info.key().length()};
149
31.7k
        parts.push_back(part);
150
31.7k
        begin += part.key.length() + 1;
151
31.7k
    }
152
24.7k
}
153
154
4
std::string PathInData::to_jsonpath() const {
155
4
    std::string jsonpath = "$.";
156
4
    if (parts.empty()) {
157
0
        return jsonpath;
158
0
    }
159
4
    auto it = parts.begin();
160
4
    jsonpath += it->key;
161
4
    ++it;
162
6
    for (; it != parts.end(); ++it) {
163
2
        jsonpath += ".";
164
2
        jsonpath += it->key;
165
2
    }
166
4
    return jsonpath;
167
4
}
168
169
36.8k
void PathInData::to_protobuf(segment_v2::ColumnPathInfo* pb, int32_t parent_col_unique_id) const {
170
36.8k
    pb->set_path(path);
171
36.8k
    pb->set_has_nested(has_nested);
172
36.8k
    pb->set_parrent_column_unique_id(parent_col_unique_id);
173
36.8k
    pb->set_is_typed(is_typed);
174
175
    // set parts info
176
39.6k
    for (const Part& part : parts) {
177
39.6k
        segment_v2::ColumnPathPartInfo& part_info = *pb->add_path_part_infos();
178
39.6k
        part_info.set_key(std::string(part.key.data(), part.key.size()));
179
39.6k
        part_info.set_is_nested(part.is_nested);
180
39.6k
        part_info.set_anonymous_array_level(part.anonymous_array_level);
181
39.6k
    }
182
36.8k
}
183
184
63.0k
size_t PathInData::Hash::operator()(const PathInData& value) const {
185
63.0k
    auto hash = get_parts_hash(value.parts, value.is_typed);
186
63.0k
    return hash.low() ^ hash.high();
187
63.0k
}
188
189
74
bool PathInData::need_record_stats() const {
190
74
    return !empty() && !is_typed && !has_nested &&
191
74
           path.find(DOC_VALUE_COLUMN_PATH) == std::string::npos;
192
74
}
193
194
11.6k
PathInData PathInData::copy_pop_front() const {
195
11.6k
    return copy_pop_nfront(1);
196
11.6k
}
197
198
18
PathInData PathInData::get_nested_prefix_path() const {
199
18
    CHECK(has_nested_part());
200
18
    PathInData new_path;
201
18
    Parts new_parts;
202
34
    for (const Part& part : parts) {
203
34
        new_parts.push_back(part);
204
34
        if (part.is_nested) {
205
18
            break;
206
18
        }
207
34
    }
208
18
    new_path.build_path(new_parts);
209
18
    new_path.build_parts(new_parts);
210
18
    new_path.is_typed = is_typed;
211
18
    return new_path;
212
18
}
213
214
4
PathInData PathInData::copy_pop_back() const {
215
4
    if (parts.size() <= 1) {
216
2
        return {};
217
2
    }
218
2
    PathInData new_path;
219
2
    Parts new_parts = parts;
220
2
    new_parts.pop_back();
221
2
    new_path.build_path(new_parts);
222
2
    new_path.build_parts(new_parts);
223
2
    new_path.is_typed = is_typed;
224
2
    return new_path;
225
4
}
226
227
12.3k
PathInData PathInData::copy_pop_nfront(size_t n) const {
228
12.3k
    if (n >= parts.size()) {
229
2.81k
        return {};
230
2.81k
    }
231
9.51k
    PathInData new_path;
232
9.51k
    Parts new_parts;
233
9.51k
    if (!parts.empty()) {
234
9.51k
        std::copy(parts.begin() + n, parts.end(), std::back_inserter(new_parts));
235
9.51k
    }
236
9.51k
    new_path.build_path(new_parts);
237
9.51k
    new_path.build_parts(new_parts);
238
9.51k
    new_path.is_typed = is_typed;
239
9.51k
    return new_path;
240
12.3k
}
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
2.85M
PathInDataBuilder& PathInDataBuilder::append(std::string_view key, bool is_array) {
267
2.85M
    if (parts.empty()) {
268
2.70M
        current_anonymous_array_level += is_array;
269
2.70M
    }
270
2.85M
    if (!parts.empty()) {
271
146k
        parts.back().is_nested = is_array;
272
146k
    }
273
2.85M
    parts.emplace_back(key, false, current_anonymous_array_level);
274
2.85M
    current_anonymous_array_level = 0;
275
2.85M
    return *this;
276
2.85M
}
277
116k
PathInDataBuilder& PathInDataBuilder::append(const PathInData::Parts& path, bool is_array) {
278
116k
    if (parts.empty()) {
279
102k
        current_anonymous_array_level += is_array;
280
102k
    }
281
116k
    if (!path.empty()) {
282
12.0k
        if (!parts.empty()) {
283
12.0k
            parts.back().is_nested = is_array;
284
12.0k
        }
285
12.0k
        auto it = parts.insert(parts.end(), path.begin(), path.end());
286
36.1k
        for (; it != parts.end(); ++it) {
287
24.0k
            it->anonymous_array_level += current_anonymous_array_level;
288
24.0k
        }
289
12.0k
        current_anonymous_array_level = 0;
290
12.0k
    }
291
116k
    return *this;
292
116k
}
293
294
2.85M
void PathInDataBuilder::pop_back() {
295
2.85M
    if (!parts.empty()) {
296
2.85M
        parts.pop_back();
297
2.85M
    }
298
2.85M
}
299
300
116k
void PathInDataBuilder::pop_back(size_t n) {
301
116k
    assert(n <= parts.size());
302
116k
    parts.resize(parts.size() - n);
303
116k
}
304
305
} // namespace doris