aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Analyzer/Identifier.h
blob: cf64bcf8bfbf57a08106098149a8dd8313f2b990 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#pragma once

#include <vector>
#include <string>

#include <fmt/core.h>
#include <fmt/format.h>

#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/join.hpp>


namespace DB
{

/** Identifier consists from identifier parts.
  * Each identifier part is arbitrary long sequence of digits, underscores, lowercase and uppercase letters.
  * Example: a, a.b, a.b.c.
  */
class Identifier
{
public:
    Identifier() = default;

    /// Create Identifier from parts
    explicit Identifier(const std::vector<std::string> & parts_)
        : parts(parts_)
        , full_name(boost::algorithm::join(parts, "."))
    {
    }

    /// Create Identifier from parts
    explicit Identifier(std::vector<std::string> && parts_)
        : parts(std::move(parts_))
        , full_name(boost::algorithm::join(parts, "."))
    {
    }

    /// Create Identifier from full name, full name is split with '.' as separator.
    explicit Identifier(const std::string & full_name_)
        : full_name(full_name_)
    {
        boost::split(parts, full_name, [](char c) { return c == '.'; });
    }

    /// Create Identifier from full name, full name is split with '.' as separator.
    explicit Identifier(std::string && full_name_)
        : full_name(std::move(full_name_))
    {
        boost::split(parts, full_name, [](char c) { return c == '.'; });
    }

    const std::string & getFullName() const
    {
        return full_name;
    }

    const std::vector<std::string> & getParts() const
    {
        return parts;
    }

    size_t getPartsSize() const
    {
        return parts.size();
    }

    bool empty() const
    {
        return parts.empty();
    }

    bool isEmpty() const
    {
        return parts.empty();
    }

    bool isShort() const
    {
        return parts.size() == 1;
    }

    bool isCompound() const
    {
        return parts.size() > 1;
    }

    const std::string & at(size_t index) const
    {
        if (index >= parts.size())
            throw std::out_of_range("identifier access part is out of range");

        return parts[index];
    }

    const std::string & operator[](size_t index) const
    {
        return parts[index];
    }

    const std::string & front() const
    {
        return parts.front();
    }

    const std::string & back() const
    {
        return parts.back();
    }

    /// Returns true, if identifier starts with part, false otherwise
    bool startsWith(const std::string_view & part)
    {
        return !parts.empty() && parts[0] == part;
    }

    /// Returns true, if identifier ends with part, false otherwise
    bool endsWith(const std::string_view & part)
    {
        return !parts.empty() && parts.back() == part;
    }

    using const_iterator = std::vector<std::string>::const_iterator;

    const_iterator begin() const
    {
        return parts.begin();
    }

    const_iterator end() const
    {
        return parts.end();
    }

    void popFirst(size_t parts_to_remove_size)
    {
        assert(parts_to_remove_size <= parts.size());

        size_t parts_size = parts.size();
        std::vector<std::string> result_parts;
        result_parts.reserve(parts_size - parts_to_remove_size);

        for (size_t i = parts_to_remove_size; i < parts_size; ++i)
            result_parts.push_back(std::move(parts[i]));

        parts = std::move(result_parts);
        full_name = boost::algorithm::join(parts, ".");
    }

    void popFirst()
    {
        return popFirst(1);
    }

    void pop_front() /// NOLINT
    {
        return popFirst();
    }

    void popLast(size_t parts_to_remove_size)
    {
        assert(parts_to_remove_size <= parts.size());

        for (size_t i = 0; i < parts_to_remove_size; ++i)
        {
            size_t last_part_size = parts.back().size();
            parts.pop_back();
            bool is_not_last = !parts.empty();
            full_name.resize(full_name.size() - (last_part_size + static_cast<size_t>(is_not_last)));
        }
    }

    void popLast()
    {
        return popLast(1);
    }

    void pop_back() /// NOLINT
    {
        popLast();
    }

    void push_back(std::string && part) /// NOLINT
    {
        emplace_back(std::move(part));
    }

    void push_back(const std::string & part) /// NOLINT
    {
        emplace_back(part);
    }

    template <typename ...Args>
    void emplace_back(Args&&... args) /// NOLINT
    {
        parts.emplace_back(std::forward<Args>(args)...);
        bool was_not_empty = parts.size() != 1;
        if (was_not_empty)
            full_name += '.';
        full_name += parts.back();
    }
private:
    std::vector<std::string> parts;
    std::string full_name;
};

inline bool operator==(const Identifier & lhs, const Identifier & rhs)
{
    return lhs.getFullName() == rhs.getFullName();
}

inline bool operator!=(const Identifier & lhs, const Identifier & rhs)
{
    return !(lhs == rhs);
}

inline std::ostream & operator<<(std::ostream & stream, const Identifier & identifier)
{
    stream << identifier.getFullName();
    return stream;
}

using Identifiers = std::vector<Identifier>;

/// View for Identifier
class IdentifierView
{
public:
    IdentifierView() = default;

    IdentifierView(const Identifier & identifier) /// NOLINT
        : full_name_view(identifier.getFullName())
        , parts_start_it(identifier.begin())
        , parts_end_it(identifier.end())
    {}

    std::string_view getFullName() const
    {
        return full_name_view;
    }

    size_t getPartsSize() const
    {
        return parts_end_it - parts_start_it;
    }

    bool empty() const
    {
        return parts_start_it == parts_end_it;
    }

    bool isEmpty() const
    {
        return parts_start_it == parts_end_it;
    }

    bool isShort() const
    {
        return getPartsSize() == 1;
    }

    bool isCompound() const
    {
        return getPartsSize() > 1;
    }

    std::string_view at(size_t index) const
    {
        if (index >= getPartsSize())
            throw std::out_of_range("identifier access part is out of range");

        return *(parts_start_it + index);
    }

    std::string_view operator[](size_t index) const
    {
        return *(parts_start_it + index);
    }

    std::string_view front() const
    {
        return *parts_start_it;
    }

    std::string_view back() const
    {
        return *(parts_end_it - 1);
    }

    bool startsWith(std::string_view part) const
    {
        return !isEmpty() && *parts_start_it == part;
    }

    bool endsWith(std::string_view part) const
    {
        return !isEmpty() && *(parts_end_it - 1) == part;
    }

    void popFirst(size_t parts_to_remove_size)
    {
        assert(parts_to_remove_size <= getPartsSize());

        for (size_t i = 0; i < parts_to_remove_size; ++i)
        {
            size_t part_size = parts_start_it->size();
            ++parts_start_it;
            bool is_not_last = parts_start_it != parts_end_it;
            full_name_view.remove_prefix(part_size + is_not_last);
        }
    }

    void popFirst()
    {
        popFirst(1);
    }

    void popLast(size_t parts_to_remove_size)
    {
        assert(parts_to_remove_size <= getPartsSize());

        for (size_t i = 0; i < parts_to_remove_size; ++i)
        {
            size_t last_part_size = (parts_end_it - 1)->size();
            --parts_end_it;
            bool is_not_last = parts_start_it != parts_end_it;
            full_name_view.remove_suffix(last_part_size + is_not_last);
        }
    }

    void popLast()
    {
        popLast(1);
    }

    using const_iterator = Identifier::const_iterator;

    const_iterator begin() const
    {
        return parts_start_it;
    }

    const_iterator end() const
    {
        return parts_end_it;
    }
private:
    std::string_view full_name_view;
    const_iterator parts_start_it;
    const_iterator parts_end_it;
};

inline bool operator==(const IdentifierView & lhs, const IdentifierView & rhs)
{
    return lhs.getFullName() == rhs.getFullName();
}

inline bool operator!=(const IdentifierView & lhs, const IdentifierView & rhs)
{
    return !(lhs == rhs);
}

inline std::ostream & operator<<(std::ostream & stream, const IdentifierView & identifier_view)
{
    stream << identifier_view.getFullName();
    return stream;
}

}

template <>
struct std::hash<DB::Identifier>
{
    size_t operator()(const DB::Identifier & identifier) const
    {
        std::hash<std::string> hash;
        return hash(identifier.getFullName());
    }
};

template <>
struct std::hash<DB::IdentifierView>
{
    size_t operator()(const DB::IdentifierView & identifier) const
    {
        std::hash<std::string_view> hash;
        return hash(identifier.getFullName());
    }
};

/// See https://fmt.dev/latest/api.html#formatting-user-defined-types

template <>
struct fmt::formatter<DB::Identifier>
{
    constexpr static auto parse(format_parse_context & ctx)
    {
        const auto * it = ctx.begin();
        const auto * end = ctx.end();

        /// Only support {}.
        if (it != end && *it != '}')
            throw fmt::format_error("invalid format");

        return it;
    }

    template <typename FormatContext>
    auto format(const DB::Identifier & identifier, FormatContext & ctx)
    {
        return fmt::format_to(ctx.out(), "{}", identifier.getFullName());
    }
};

template <>
struct fmt::formatter<DB::IdentifierView>
{
    constexpr static auto parse(format_parse_context & ctx)
    {
        const auto * it = ctx.begin();
        const auto * end = ctx.end();

        /// Only support {}.
        if (it != end && *it != '}')
            throw fmt::format_error("invalid format");

        return it;
    }

    template <typename FormatContext>
    auto format(const DB::IdentifierView & identifier_view, FormatContext & ctx)
    {
        return fmt::format_to(ctx.out(), "{}", identifier_view.getFullName());
    }
};