Coverage Report

Created: 2026-05-31 14:31

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.50M
PathInData::PathInData(std::string_view path_, bool is_typed_) : path(path_), is_typed(is_typed_) {
34
1.50M
    const char* begin = path.data();
35
1.50M
    const char* end = path.data() + path.size();
36
35.2M
    for (const char* it = path.data(); it != end; ++it) {
37
33.7M
        if (*it == '.') {
38
2.91M
            size_t size = static_cast<size_t>(it - begin);
39
2.91M
            parts.emplace_back(std::string_view {begin, size}, false, 0);
40
2.91M
            begin = it + 1;
41
2.91M
        }
42
33.7M
    }
43
1.50M
    size_t size = static_cast<size_t>(end - begin);
44
1.50M
    parts.emplace_back(std::string_view {begin, size}, false, 0.);
45
1.50M
}
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.6M
PathInData::PathInData(const Parts& parts_) {
57
14.6M
    build_path(parts_);
58
14.6M
    build_parts(parts_);
59
14.6M
}
60
37.5M
PathInData::PathInData(const PathInData& other) : path(other.path), is_typed(other.is_typed) {
61
37.5M
    build_parts(other.get_parts());
62
37.5M
}
63
64
13.4k
PathInData::PathInData(const std::string& root, const std::vector<std::string>& paths) {
65
13.4k
    PathInDataBuilder path_builder;
66
13.4k
    path_builder.append(root, false);
67
13.4k
    for (const std::string& p : paths) {
68
13.3k
        path_builder.append(p, false);
69
13.3k
    }
70
13.4k
    build_path(path_builder.get_parts());
71
13.4k
    build_parts(path_builder.get_parts());
72
13.4k
}
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
453k
PathInData& PathInData::operator=(const PathInData& other) {
84
453k
    if (this != &other) {
85
453k
        path = other.path;
86
453k
        is_typed = other.is_typed;
87
453k
        build_parts(other.parts);
88
453k
    }
89
453k
    return *this;
90
453k
}
91
92
18.9M
UInt128 PathInData::get_parts_hash(const Parts& parts_, bool is_typed_) {
93
18.9M
    SipHash hash;
94
18.9M
    hash.update(parts_.size());
95
18.9M
    for (const auto& part : parts_) {
96
82.2k
        hash.update(part.key.data(), part.key.length());
97
82.2k
        hash.update(part.is_nested);
98
82.2k
        hash.update(part.anonymous_array_level);
99
82.2k
    }
100
18.9M
    hash.update(is_typed_);
101
18.9M
    UInt128 res;
102
18.9M
    hash.get128(res);
103
18.9M
    return res;
104
18.9M
}
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.0M
    for (; it != other_parts.end(); ++it) {
115
3.03M
        path += ".";
116
3.03M
        path += it->key;
117
3.03M
    }
118
15.0M
}
119
52.6M
void PathInData::build_parts(const Parts& other_parts) {
120
52.6M
    if (other_parts.empty()) {
121
21.2M
        return;
122
21.2M
    }
123
31.3M
    parts.clear();
124
31.3M
    parts.reserve(other_parts.size());
125
31.3M
    const char* begin = path.data();
126
48.1M
    for (const auto& part : other_parts) {
127
48.1M
        has_nested |= part.is_nested;
128
48.1M
        parts.emplace_back(std::string_view {begin, part.key.length()}, part.is_nested,
129
48.1M
                           part.anonymous_array_level);
130
48.1M
        begin += part.key.length() + 1;
131
48.1M
    }
132
31.3M
}
133
134
180k
void PathInData::from_protobuf(const segment_v2::ColumnPathInfo& pb) {
135
180k
    parts.clear();
136
180k
    path = pb.path();
137
180k
    has_nested = false;
138
180k
    is_typed = pb.is_typed();
139
180k
    parts.reserve(pb.path_part_infos().size());
140
180k
    const char* begin = path.data();
141
392k
    for (const segment_v2::ColumnPathPartInfo& part_info : pb.path_part_infos()) {
142
392k
        Part part;
143
392k
        part.is_nested = part_info.is_nested();
144
392k
        has_nested |= part.is_nested;
145
392k
        part.anonymous_array_level =
146
392k
                cast_set<uint8_t, uint32_t, false>(part_info.anonymous_array_level());
147
        // use string_view to ref data in path
148
392k
        part.key = std::string_view {begin, part_info.key().length()};
149
392k
        parts.push_back(part);
150
392k
        begin += part.key.length() + 1;
151
392k
    }
152
180k
}
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
165k
void PathInData::to_protobuf(segment_v2::ColumnPathInfo* pb, int32_t parent_col_unique_id) const {
170
165k
    pb->set_path(path);
171
165k
    pb->set_has_nested(has_nested);
172
165k
    pb->set_parrent_column_unique_id(parent_col_unique_id);
173
165k
    pb->set_is_typed(is_typed);
174
175
    // set parts info
176
357k
    for (const Part& part : parts) {
177
357k
        segment_v2::ColumnPathPartInfo& part_info = *pb->add_path_part_infos();
178
357k
        part_info.set_key(std::string(part.key.data(), part.key.size()));
179
357k
        part_info.set_is_nested(part.is_nested);
180
357k
        part_info.set_anonymous_array_level(part.anonymous_array_level);
181
357k
    }
182
165k
}
183
184
17.9M
size_t PathInData::Hash::operator()(const PathInData& value) const {
185
17.9M
    auto hash = get_parts_hash(value.parts, value.is_typed);
186
17.9M
    return hash.low() ^ hash.high();
187
17.9M
}
188
189
900
bool PathInData::need_record_stats() const {
190
900
    return !empty() && !is_typed && !has_nested &&
191
900
           path.find(DOC_VALUE_COLUMN_PATH) == std::string::npos;
192
900
}
193
194
257k
PathInData PathInData::copy_pop_front() const {
195
257k
    return copy_pop_nfront(1);
196
257k
}
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
259k
PathInData PathInData::copy_pop_nfront(size_t n) const {
228
259k
    if (n >= parts.size()) {
229
10.2k
        return {};
230
10.2k
    }
231
249k
    PathInData new_path;
232
249k
    Parts new_parts;
233
249k
    if (!parts.empty()) {
234
249k
        std::copy(parts.begin() + n, parts.end(), std::back_inserter(new_parts));
235
249k
    }
236
249k
    new_path.build_path(new_parts);
237
249k
    new_path.build_parts(new_parts);
238
249k
    new_path.is_typed = is_typed;
239
249k
    return new_path;
240
259k
}
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.4M
        current_anonymous_array_level += is_array;
269
13.4M
    }
270
14.7M
    if (!parts.empty()) {
271
1.27M
        parts.back().is_nested = is_array;
272
1.27M
    }
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
375k
PathInDataBuilder& PathInDataBuilder::append(const PathInData::Parts& path, bool is_array) {
278
375k
    if (parts.empty()) {
279
150k
        current_anonymous_array_level += is_array;
280
150k
    }
281
375k
    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
375k
    return *this;
292
375k
}
293
294
14.7M
void PathInDataBuilder::pop_back() {
295
14.7M
    if (!parts.empty()) {
296
14.7M
        parts.pop_back();
297
14.7M
    }
298
14.7M
}
299
300
374k
void PathInDataBuilder::pop_back(size_t n) {
301
374k
    assert(n <= parts.size());
302
375k
    parts.resize(parts.size() - n);
303
375k
}
304
305
} // namespace doris