Coverage Report

Created: 2026-03-13 19:41

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
#include "common/compile_check_begin.h"
34
35
173k
PathInData::PathInData(std::string_view path_, bool is_typed_) : path(path_), is_typed(is_typed_) {
36
173k
    const char* begin = path.data();
37
173k
    const char* end = path.data() + path.size();
38
1.20M
    for (const char* it = path.data(); it != end; ++it) {
39
1.02M
        if (*it == '.') {
40
57.4k
            size_t size = static_cast<size_t>(it - begin);
41
57.4k
            parts.emplace_back(std::string_view {begin, size}, false, 0);
42
57.4k
            begin = it + 1;
43
57.4k
        }
44
1.02M
    }
45
173k
    size_t size = static_cast<size_t>(end - begin);
46
173k
    parts.emplace_back(std::string_view {begin, size}, false, 0.);
47
173k
}
48
49
3
PathInData::PathInData(std::string_view path_, const Parts& parts_, bool is_typed_) {
50
3
    path = path_;
51
3
    is_typed = is_typed_;
52
9
    for (const auto& part : parts_) {
53
9
        has_nested |= part.is_nested;
54
9
        parts.emplace_back(part);
55
9
    }
56
3
}
57
58
1.35M
PathInData::PathInData(const Parts& parts_) {
59
1.35M
    build_path(parts_);
60
1.35M
    build_parts(parts_);
61
1.35M
}
62
805k
PathInData::PathInData(const PathInData& other) : path(other.path), is_typed(other.is_typed) {
63
805k
    build_parts(other.get_parts());
64
805k
}
65
66
2
PathInData::PathInData(const std::string& root, const std::vector<std::string>& paths) {
67
2
    PathInDataBuilder path_builder;
68
2
    path_builder.append(root, false);
69
5
    for (const std::string& p : paths) {
70
5
        path_builder.append(p, false);
71
5
    }
72
2
    build_path(path_builder.get_parts());
73
2
    build_parts(path_builder.get_parts());
74
2
}
75
76
1
PathInData::PathInData(const std::vector<std::string>& paths) {
77
1
    PathInDataBuilder path_builder;
78
4
    for (size_t i = 0; i < paths.size(); ++i) {
79
3
        path_builder.append(paths[i], false);
80
3
    }
81
1
    build_path(path_builder.get_parts());
82
1
    build_parts(path_builder.get_parts());
83
1
}
84
85
7.71k
PathInData& PathInData::operator=(const PathInData& other) {
86
7.71k
    if (this != &other) {
87
7.71k
        path = other.path;
88
7.71k
        is_typed = other.is_typed;
89
7.71k
        build_parts(other.parts);
90
7.71k
    }
91
7.71k
    return *this;
92
7.71k
}
93
94
600k
UInt128 PathInData::get_parts_hash(const Parts& parts_, bool is_typed_) {
95
600k
    SipHash hash;
96
600k
    hash.update(parts_.size());
97
600k
    for (const auto& part : parts_) {
98
13.0k
        hash.update(part.key.data(), part.key.length());
99
13.0k
        hash.update(part.is_nested);
100
13.0k
        hash.update(part.anonymous_array_level);
101
13.0k
    }
102
600k
    hash.update(is_typed_);
103
600k
    UInt128 res;
104
600k
    hash.get128(res);
105
600k
    return res;
106
600k
}
107
108
1.36M
void PathInData::build_path(const Parts& other_parts) {
109
1.36M
    if (other_parts.empty()) {
110
1.61k
        return;
111
1.61k
    }
112
1.36M
    path.clear();
113
1.36M
    auto it = other_parts.begin();
114
1.36M
    path += it->key;
115
1.36M
    ++it;
116
1.44M
    for (; it != other_parts.end(); ++it) {
117
83.4k
        path += ".";
118
83.4k
        path += it->key;
119
83.4k
    }
120
1.36M
}
121
2.17M
void PathInData::build_parts(const Parts& other_parts) {
122
2.17M
    if (other_parts.empty()) {
123
236k
        return;
124
236k
    }
125
1.93M
    parts.clear();
126
1.93M
    parts.reserve(other_parts.size());
127
1.93M
    const char* begin = path.data();
128
8.34M
    for (const auto& part : other_parts) {
129
8.34M
        has_nested |= part.is_nested;
130
8.34M
        parts.emplace_back(std::string_view {begin, part.key.length()}, part.is_nested,
131
8.34M
                           part.anonymous_array_level);
132
8.34M
        begin += part.key.length() + 1;
133
8.34M
    }
134
1.93M
}
135
136
12.3k
void PathInData::from_protobuf(const segment_v2::ColumnPathInfo& pb) {
137
12.3k
    parts.clear();
138
12.3k
    path = pb.path();
139
12.3k
    has_nested = false;
140
12.3k
    is_typed = pb.is_typed();
141
12.3k
    parts.reserve(pb.path_part_infos().size());
142
12.3k
    const char* begin = path.data();
143
15.8k
    for (const segment_v2::ColumnPathPartInfo& part_info : pb.path_part_infos()) {
144
15.8k
        Part part;
145
15.8k
        part.is_nested = part_info.is_nested();
146
15.8k
        has_nested |= part.is_nested;
147
15.8k
        part.anonymous_array_level =
148
15.8k
                cast_set<uint8_t, uint32_t, false>(part_info.anonymous_array_level());
149
        // use string_view to ref data in path
150
15.8k
        part.key = std::string_view {begin, part_info.key().length()};
151
15.8k
        parts.push_back(part);
152
15.8k
        begin += part.key.length() + 1;
153
15.8k
    }
154
12.3k
}
155
156
2
std::string PathInData::to_jsonpath() const {
157
2
    std::string jsonpath = "$.";
158
2
    if (parts.empty()) {
159
0
        return jsonpath;
160
0
    }
161
2
    auto it = parts.begin();
162
2
    jsonpath += it->key;
163
2
    ++it;
164
3
    for (; it != parts.end(); ++it) {
165
1
        jsonpath += ".";
166
1
        jsonpath += it->key;
167
1
    }
168
2
    return jsonpath;
169
2
}
170
171
18.3k
void PathInData::to_protobuf(segment_v2::ColumnPathInfo* pb, int32_t parent_col_unique_id) const {
172
18.3k
    pb->set_path(path);
173
18.3k
    pb->set_has_nested(has_nested);
174
18.3k
    pb->set_parrent_column_unique_id(parent_col_unique_id);
175
18.3k
    pb->set_is_typed(is_typed);
176
177
    // set parts info
178
19.7k
    for (const Part& part : parts) {
179
19.7k
        segment_v2::ColumnPathPartInfo& part_info = *pb->add_path_part_infos();
180
19.7k
        part_info.set_key(std::string(part.key.data(), part.key.size()));
181
19.7k
        part_info.set_is_nested(part.is_nested);
182
19.7k
        part_info.set_anonymous_array_level(part.anonymous_array_level);
183
19.7k
    }
184
18.3k
}
185
186
31.5k
size_t PathInData::Hash::operator()(const PathInData& value) const {
187
31.5k
    auto hash = get_parts_hash(value.parts, value.is_typed);
188
31.5k
    return hash.low() ^ hash.high();
189
31.5k
}
190
191
37
bool PathInData::need_record_stats() const {
192
37
    return !empty() && !is_typed && !has_nested &&
193
37
           path.find(DOC_VALUE_COLUMN_PATH) == std::string::npos;
194
37
}
195
196
5.77k
PathInData PathInData::copy_pop_front() const {
197
5.77k
    return copy_pop_nfront(1);
198
5.77k
}
199
200
9
PathInData PathInData::get_nested_prefix_path() const {
201
9
    CHECK(has_nested_part());
202
9
    PathInData new_path;
203
9
    Parts new_parts;
204
17
    for (const Part& part : parts) {
205
17
        new_parts.push_back(part);
206
17
        if (part.is_nested) {
207
9
            break;
208
9
        }
209
17
    }
210
9
    new_path.build_path(new_parts);
211
9
    new_path.build_parts(new_parts);
212
9
    new_path.is_typed = is_typed;
213
9
    return new_path;
214
9
}
215
216
2
PathInData PathInData::copy_pop_back() const {
217
2
    if (parts.size() <= 1) {
218
1
        return {};
219
1
    }
220
1
    PathInData new_path;
221
1
    Parts new_parts = parts;
222
1
    new_parts.pop_back();
223
1
    new_path.build_path(new_parts);
224
1
    new_path.build_parts(new_parts);
225
1
    new_path.is_typed = is_typed;
226
1
    return new_path;
227
2
}
228
229
6.13k
PathInData PathInData::copy_pop_nfront(size_t n) const {
230
6.13k
    if (n >= parts.size()) {
231
1.39k
        return {};
232
1.39k
    }
233
4.73k
    PathInData new_path;
234
4.73k
    Parts new_parts;
235
4.73k
    if (!parts.empty()) {
236
4.73k
        std::copy(parts.begin() + n, parts.end(), std::back_inserter(new_parts));
237
4.73k
    }
238
4.73k
    new_path.build_path(new_parts);
239
4.73k
    new_path.build_parts(new_parts);
240
4.73k
    new_path.is_typed = is_typed;
241
4.73k
    return new_path;
242
6.13k
}
243
244
bool PathInData::try_strip_prefix(const std::string& name, const std::string& prefix_dot,
245
0
                                  std::string* out) {
246
0
    if (!name.starts_with(prefix_dot)) {
247
0
        return false;
248
0
    }
249
0
    *out = name.substr(prefix_dot.size());
250
0
    return !out->empty();
251
0
}
252
253
0
PathInData PathInData::append(const PathInData& base, std::string_view suffix) {
254
0
    if (suffix.empty()) {
255
0
        return base;
256
0
    }
257
0
    if (base.empty()) {
258
0
        return PathInData(suffix);
259
0
    }
260
0
    std::string new_path;
261
0
    new_path.reserve(base.get_path().size() + 1 + suffix.size());
262
0
    new_path.append(base.get_path());
263
0
    new_path.push_back('.');
264
0
    new_path.append(suffix.data(), suffix.size());
265
0
    return PathInData(new_path);
266
0
}
267
268
1.42M
PathInDataBuilder& PathInDataBuilder::append(std::string_view key, bool is_array) {
269
1.42M
    if (parts.empty()) {
270
1.35M
        current_anonymous_array_level += is_array;
271
1.35M
    }
272
1.42M
    if (!parts.empty()) {
273
73.1k
        parts.back().is_nested = is_array;
274
73.1k
    }
275
1.42M
    parts.emplace_back(key, false, current_anonymous_array_level);
276
1.42M
    current_anonymous_array_level = 0;
277
1.42M
    return *this;
278
1.42M
}
279
58.1k
PathInDataBuilder& PathInDataBuilder::append(const PathInData::Parts& path, bool is_array) {
280
58.1k
    if (parts.empty()) {
281
51.2k
        current_anonymous_array_level += is_array;
282
51.2k
    }
283
58.1k
    if (!path.empty()) {
284
6.02k
        if (!parts.empty()) {
285
6.01k
            parts.back().is_nested = is_array;
286
6.01k
        }
287
6.02k
        auto it = parts.insert(parts.end(), path.begin(), path.end());
288
18.0k
        for (; it != parts.end(); ++it) {
289
12.0k
            it->anonymous_array_level += current_anonymous_array_level;
290
12.0k
        }
291
6.02k
        current_anonymous_array_level = 0;
292
6.02k
    }
293
58.1k
    return *this;
294
58.1k
}
295
296
1.42M
void PathInDataBuilder::pop_back() {
297
1.42M
    if (!parts.empty()) {
298
1.42M
        parts.pop_back();
299
1.42M
    }
300
1.42M
}
301
302
58.1k
void PathInDataBuilder::pop_back(size_t n) {
303
58.1k
    assert(n <= parts.size());
304
58.1k
    parts.resize(parts.size() - n);
305
58.1k
}
306
307
#include "common/compile_check_end.h"
308
309
} // namespace doris