aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Parsers/Access/ASTRowPolicyName.cpp
blob: 81a90de9d539f88a2662d036b2706bf0f98009bd (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
#include <Parsers/Access/ASTRowPolicyName.h>
#include <Common/quoteString.h>
#include <IO/Operators.h>


namespace DB
{
namespace ErrorCodes
{
    extern const int LOGICAL_ERROR;
}


void ASTRowPolicyName::formatImpl(const FormatSettings & settings, FormatState &, FormatStateStacked) const
{
    const String & database = full_name.database;
    const String & table_name = full_name.table_name;
    const String & short_name = full_name.short_name;
    settings.ostr << backQuoteIfNeed(short_name) << (settings.hilite ? hilite_keyword : "") << " ON "
                  << (settings.hilite ? hilite_none : "") << (database.empty() ? String{} : backQuoteIfNeed(database) + ".")
                  << backQuoteIfNeed(table_name);

    formatOnCluster(settings);
}


void ASTRowPolicyName::replaceEmptyDatabase(const String & current_database)
{
    if (full_name.database.empty())
        full_name.database = current_database;
}

String ASTRowPolicyNames::tableOrAsterisk(const String & table_name) const
{
    return table_name == RowPolicyName::ANY_TABLE_MARK ? "*" : backQuoteIfNeed(table_name);
}


void ASTRowPolicyNames::formatImpl(const FormatSettings & settings, FormatState &, FormatStateStacked) const
{
    if (full_names.empty())
        throw Exception(ErrorCodes::LOGICAL_ERROR, "No names of row policies in AST");

    bool same_short_name = true;
    if (full_names.size() > 1)
    {
        for (size_t i = 1; i != full_names.size(); ++i)
            if (full_names[i].short_name != full_names[0].short_name)
            {
                same_short_name = false;
                break;
            }
    }

    bool same_db_and_table_name = true;
    if (full_names.size() > 1)
    {
        for (size_t i = 1; i != full_names.size(); ++i)
            if ((full_names[i].database != full_names[0].database) || (full_names[i].table_name != full_names[0].table_name))
            {
                same_db_and_table_name = false;
                break;
            }
    }

    if (same_short_name)
    {
        const String & short_name = full_names[0].short_name;
        settings.ostr << backQuoteIfNeed(short_name) << (settings.hilite ? hilite_keyword : "") << " ON "
                      << (settings.hilite ? hilite_none : "");

        bool need_comma = false;
        for (const auto & full_name : full_names)
        {
            if (std::exchange(need_comma, true))
                settings.ostr << ", ";
            const String & database = full_name.database;
            const String & table_name = full_name.table_name;
            if (!database.empty())
                settings.ostr << backQuoteIfNeed(database) + ".";
            settings.ostr << tableOrAsterisk(table_name);
        }
    }
    else if (same_db_and_table_name)
    {
        bool need_comma = false;
        for (const auto & full_name : full_names)
        {
            if (std::exchange(need_comma, true))
                settings.ostr << ", ";
            const String & short_name = full_name.short_name;
            settings.ostr << backQuoteIfNeed(short_name);
        }

        const String & database = full_names[0].database;
        const String & table_name = full_names[0].table_name;
        settings.ostr << (settings.hilite ? hilite_keyword : "") << " ON " << (settings.hilite ? hilite_none : "");
        if (!database.empty())
            settings.ostr << backQuoteIfNeed(database) + ".";
        settings.ostr << tableOrAsterisk(table_name);
    }
    else
    {
        bool need_comma = false;
        for (const auto & full_name : full_names)
        {
            if (std::exchange(need_comma, true))
                settings.ostr << ", ";
            const String & short_name = full_name.short_name;
            const String & database = full_name.database;
            const String & table_name = full_name.table_name;
            settings.ostr << backQuoteIfNeed(short_name) << (settings.hilite ? hilite_keyword : "") << " ON "
                          << (settings.hilite ? hilite_none : "");
            if (!database.empty())
                settings.ostr << backQuoteIfNeed(database) + ".";
            settings.ostr << tableOrAsterisk(table_name);
        }
    }

    formatOnCluster(settings);
}


Strings ASTRowPolicyNames::toStrings() const
{
    Strings res;
    res.reserve(full_names.size());
    for (const auto & full_name : full_names)
        res.emplace_back(full_name.toString());
    return res;
}


void ASTRowPolicyNames::replaceEmptyDatabase(const String & current_database)
{
    for (auto & full_name : full_names)
        if (full_name.database.empty())
            full_name.database = current_database;
}

}