ASTCreateQuery.cpp 12.3 KB
Newer Older
A
alesapin 已提交
1
#include <Parsers/ASTCreateQuery.h>
2 3 4
#include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTSelectWithUnionQuery.h>
A
alesapin 已提交
5
#include <Parsers/ASTSetQuery.h>
6
#include <Common/quoteString.h>
7
#include <Interpreters/StorageID.h>
8
#include <IO/Operators.h>
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28


namespace DB
{

ASTPtr ASTStorage::clone() const
{
    auto res = std::make_shared<ASTStorage>(*this);
    res->children.clear();

    if (engine)
        res->set(res->engine, engine->clone());
    if (partition_by)
        res->set(res->partition_by, partition_by->clone());
    if (primary_key)
        res->set(res->primary_key, primary_key->clone());
    if (order_by)
        res->set(res->order_by, order_by->clone());
    if (sample_by)
        res->set(res->sample_by, sample_by->clone());
A
Anton Popov 已提交
29 30
    if (ttl_table)
        res->set(res->ttl_table, ttl_table->clone());
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

    if (settings)
        res->set(res->settings, settings->clone());

    return res;
}

void ASTStorage::formatImpl(const FormatSettings & s, FormatState & state, FormatStateStacked frame) const
{
    if (engine)
    {
        s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << "ENGINE" << (s.hilite ? hilite_none : "") << " = ";
        engine->formatImpl(s, state, frame);
    }
    if (partition_by)
    {
        s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << "PARTITION BY " << (s.hilite ? hilite_none : "");
        partition_by->formatImpl(s, state, frame);
    }
    if (primary_key)
    {
        s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << "PRIMARY KEY " << (s.hilite ? hilite_none : "");
        primary_key->formatImpl(s, state, frame);
    }
    if (order_by)
    {
        s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << "ORDER BY " << (s.hilite ? hilite_none : "");
        order_by->formatImpl(s, state, frame);
    }
    if (sample_by)
    {
        s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << "SAMPLE BY " << (s.hilite ? hilite_none : "");
        sample_by->formatImpl(s, state, frame);
    }
A
Anton Popov 已提交
65 66 67 68 69
    if (ttl_table)
    {
        s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << "TTL " << (s.hilite ? hilite_none : "");
        ttl_table->formatImpl(s, state, frame);
    }
70 71 72 73 74 75 76 77 78
    if (settings)
    {
        s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << "SETTINGS " << (s.hilite ? hilite_none : "");
        settings->formatImpl(s, state, frame);
    }

}


A
Alexey Milovidov 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91
class ASTColumnsElement : public IAST
{
public:
    String prefix;
    IAST * elem;

    String getID(char c) const override { return "ASTColumnsElement for " + elem->getID(c); }

    ASTPtr clone() const override;

    void formatImpl(const FormatSettings & s, FormatState & state, FormatStateStacked frame) const override;
};

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
ASTPtr ASTColumnsElement::clone() const
{
    auto res = std::make_shared<ASTColumnsElement>();
    res->prefix = prefix;
    if (elem)
        res->set(res->elem, elem->clone());
    return res;
}

void ASTColumnsElement::formatImpl(const FormatSettings & s, FormatState & state, FormatStateStacked frame) const
{
    if (!elem)
        return;

    if (prefix.empty())
    {
        elem->formatImpl(s, state, frame);
        return;
    }

    s.ostr << (s.hilite ? hilite_keyword : "") << prefix << (s.hilite ? hilite_none : "");
113 114
    s.ostr << ' ';
    elem->formatImpl(s, state, frame);
115 116 117 118 119 120 121 122 123 124 125
}


ASTPtr ASTColumns::clone() const
{
    auto res = std::make_shared<ASTColumns>();

    if (columns)
        res->set(res->columns, columns->clone());
    if (indices)
        res->set(res->indices, indices->clone());
G
Gleb Novikov 已提交
126 127
    if (constraints)
        res->set(res->constraints, constraints->clone());
128 129
    if (primary_key)
        res->set(res->primary_key, primary_key->clone());
130 131 132 133 134 135 136 137 138

    return res;
}

void ASTColumns::formatImpl(const FormatSettings & s, FormatState & state, FormatStateStacked frame) const
{
    ASTExpressionList list;

    if (columns)
A
Alexey Milovidov 已提交
139
    {
140 141 142 143 144 145 146
        for (const auto & column : columns->children)
        {
            auto elem = std::make_shared<ASTColumnsElement>();
            elem->prefix = "";
            elem->set(elem->elem, column->clone());
            list.children.push_back(elem);
        }
A
Alexey Milovidov 已提交
147
    }
148
    if (indices)
A
Alexey Milovidov 已提交
149
    {
150 151 152 153 154 155 156
        for (const auto & index : indices->children)
        {
            auto elem = std::make_shared<ASTColumnsElement>();
            elem->prefix = "INDEX";
            elem->set(elem->elem, index->clone());
            list.children.push_back(elem);
        }
A
Alexey Milovidov 已提交
157
    }
G
Gleb Novikov 已提交
158 159 160 161 162 163 164 165 166 167
    if (constraints)
    {
        for (const auto & constraint : constraints->children)
        {
            auto elem = std::make_shared<ASTColumnsElement>();
            elem->prefix = "CONSTRAINT";
            elem->set(elem->elem, constraint->clone());
            list.children.push_back(elem);
        }
    }
168 169

    if (!list.children.empty())
170 171 172 173 174 175
    {
        if (s.one_line)
            list.formatImpl(s, state, frame);
        else
            list.formatImplMultiline(s, state, frame);
    }
176 177 178 179 180 181 182 183 184 185 186 187 188 189
}


ASTPtr ASTCreateQuery::clone() const
{
    auto res = std::make_shared<ASTCreateQuery>(*this);
    res->children.clear();

    if (columns_list)
        res->set(res->columns_list, columns_list->clone());
    if (storage)
        res->set(res->storage, storage->clone());
    if (select)
        res->set(res->select, select->clone());
190 191
    if (tables)
        res->set(res->tables, tables->clone());
A
wip  
Alexander Kuzmenkov 已提交
192

A
alesapin 已提交
193
    if (dictionary)
A
wip  
Alexander Kuzmenkov 已提交
194 195 196
    {
        assert(is_dictionary);
        res->set(res->dictionary_attributes_list, dictionary_attributes_list->clone());
A
alesapin 已提交
197
        res->set(res->dictionary, dictionary->clone());
A
wip  
Alexander Kuzmenkov 已提交
198
    }
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

    cloneOutputOptions(*res);

    return res;
}

void ASTCreateQuery::formatQueryImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
{
    frame.need_parens = false;

    if (!database.empty() && table.empty())
    {
        settings.ostr << (settings.hilite ? hilite_keyword : "")
            << (attach ? "ATTACH DATABASE " : "CREATE DATABASE ")
            << (if_not_exists ? "IF NOT EXISTS " : "")
            << (settings.hilite ? hilite_none : "")
            << backQuoteIfNeed(database);
216 217 218 219 220 221 222

        if (uuid != UUIDHelpers::Nil)
        {
            settings.ostr << (settings.hilite ? hilite_keyword : "") << " UUID " << (settings.hilite ? hilite_none : "")
                          << quoteString(toString(uuid));
        }

223 224 225 226 227 228 229 230
        formatOnCluster(settings);

        if (storage)
            storage->formatImpl(settings, state, frame);

        return;
    }

A
alesapin 已提交
231
    if (!is_dictionary)
232
    {
233 234 235 236 237 238 239 240 241 242 243
        String action = "CREATE";
        if (attach)
            action = "ATTACH";
        else if (replace_view)
            action = "CREATE OR REPLACE";
        else if (replace_table && create_or_replace)
            action = "CREATE OR REPLACE";
        else if (replace_table)
            action = "REPLACE";

        String what = "TABLE";
S
spongedc 已提交
244
        if (is_ordinary_view)
245
            what = "VIEW";
246
        else if (is_materialized_view)
247
            what = "MATERIALIZED VIEW";
248
        else if (is_live_view)
249
            what = "LIVE VIEW";
250 251 252

        settings.ostr
            << (settings.hilite ? hilite_keyword : "")
253
                << action << " "
254 255 256 257
                << (temporary ? "TEMPORARY " : "")
                << what << " "
                << (if_not_exists ? "IF NOT EXISTS " : "")
            << (settings.hilite ? hilite_none : "")
A
Alexander Tokmakov 已提交
258
            << (!database.empty() ? backQuoteIfNeed(database) + "." : "") << backQuoteIfNeed(table);
259

A
Alexander Tokmakov 已提交
260
        if (uuid != UUIDHelpers::Nil)
A
Alexander Tokmakov 已提交
261
            settings.ostr << (settings.hilite ? hilite_keyword : "") << " UUID " << (settings.hilite ? hilite_none : "")
262
                          << quoteString(toString(uuid));
263

264 265 266 267
        assert(attach || !attach_from_path);
        if (attach_from_path)
            settings.ostr << (settings.hilite ? hilite_keyword : "") << " FROM " << (settings.hilite ? hilite_none : "")
                          << quoteString(*attach_from_path);
268

269 270 271
        if (live_view_timeout)
            settings.ostr << (settings.hilite ? hilite_keyword : "") << " WITH TIMEOUT " << (settings.hilite ? hilite_none : "")
                          << *live_view_timeout;
272

273 274 275 276
        if (live_view_periodic_refresh)
        {
            if (live_view_timeout)
                settings.ostr << (settings.hilite ? hilite_keyword : "") << " AND" << (settings.hilite ? hilite_none : "");
V
Vitaliy Zakaznikov 已提交
277 278
            else
                settings.ostr << (settings.hilite ? hilite_keyword : "") << " WITH" << (settings.hilite ? hilite_none : "");
279

280 281
            settings.ostr << (settings.hilite ? hilite_keyword : "") << " PERIODIC REFRESH " << (settings.hilite ? hilite_none : "")
                << *live_view_periodic_refresh;
282 283
        }

A
Alexander Tokmakov 已提交
284
        formatOnCluster(settings);
285
    }
A
alesapin 已提交
286 287
    else
    {
288 289 290 291
        /// Always DICTIONARY
        settings.ostr << (settings.hilite ? hilite_keyword : "") << (attach ? "ATTACH " : "CREATE ") << "DICTIONARY "
                      << (if_not_exists ? "IF NOT EXISTS " : "") << (settings.hilite ? hilite_none : "")
                      << (!database.empty() ? backQuoteIfNeed(database) + "." : "") << backQuoteIfNeed(table);
292 293 294
        if (uuid != UUIDHelpers::Nil)
            settings.ostr << (settings.hilite ? hilite_keyword : "") << " UUID " << (settings.hilite ? hilite_none : "")
                          << quoteString(toString(uuid));
295
        formatOnCluster(settings);
A
alesapin 已提交
296 297
    }

A
fixes  
Alexander Tokmakov 已提交
298
    if (to_table_id)
299
    {
300
        assert(is_materialized_view && to_inner_uuid == UUIDHelpers::Nil);
301 302
        settings.ostr
            << (settings.hilite ? hilite_keyword : "") << " TO " << (settings.hilite ? hilite_none : "")
A
Alexander Tokmakov 已提交
303 304
            << (!to_table_id.database_name.empty() ? backQuoteIfNeed(to_table_id.database_name) + "." : "")
            << backQuoteIfNeed(to_table_id.table_name);
305 306
    }

307 308 309 310 311 312 313
    if (to_inner_uuid != UUIDHelpers::Nil)
    {
        assert(is_materialized_view && !to_table_id);
        settings.ostr << (settings.hilite ? hilite_keyword : "") << " TO INNER UUID " << (settings.hilite ? hilite_none : "")
                      << quoteString(toString(to_inner_uuid));
    }

314 315 316 317 318 319 320
    if (!as_table.empty())
    {
        settings.ostr
            << (settings.hilite ? hilite_keyword : "") << " AS " << (settings.hilite ? hilite_none : "")
            << (!as_database.empty() ? backQuoteIfNeed(as_database) + "." : "") << backQuoteIfNeed(as_table);
    }

321 322
    if (as_table_function)
    {
323 324 325 326 327 328 329 330 331 332
        if (columns_list)
        {
            frame.expression_list_always_start_on_new_line = true;
            settings.ostr << (settings.one_line ? " (" : "\n(");
            FormatStateStacked frame_nested = frame;
            columns_list->formatImpl(settings, state, frame_nested);
            settings.ostr << (settings.one_line ? ")" : "\n)");
            frame.expression_list_always_start_on_new_line = false;
        }

333 334 335 336
        settings.ostr << (settings.hilite ? hilite_keyword : "") << " AS " << (settings.hilite ? hilite_none : "");
        as_table_function->formatImpl(settings, state, frame);
    }

A
Alexey Milovidov 已提交
337 338
    frame.expression_list_always_start_on_new_line = true;

339
    if (columns_list && !as_table_function)
340 341 342 343 344 345 346
    {
        settings.ostr << (settings.one_line ? " (" : "\n(");
        FormatStateStacked frame_nested = frame;
        columns_list->formatImpl(settings, state, frame_nested);
        settings.ostr << (settings.one_line ? ")" : "\n)");
    }

A
alesapin 已提交
347 348 349 350
    if (dictionary_attributes_list)
    {
        settings.ostr << (settings.one_line ? " (" : "\n(");
        FormatStateStacked frame_nested = frame;
351 352 353 354
        if (settings.one_line)
            dictionary_attributes_list->formatImpl(settings, state, frame_nested);
        else
            dictionary_attributes_list->formatImplMultiline(settings, state, frame_nested);
A
alesapin 已提交
355 356 357
        settings.ostr << (settings.one_line ? ")" : "\n)");
    }

A
Alexey Milovidov 已提交
358 359
    frame.expression_list_always_start_on_new_line = false;

360 361 362
    if (storage)
        storage->formatImpl(settings, state, frame);

A
alesapin 已提交
363 364 365
    if (dictionary)
        dictionary->formatImpl(settings, state, frame);

366 367 368 369 370 371 372 373
    if (is_populate)
        settings.ostr << (settings.hilite ? hilite_keyword : "") << " POPULATE" << (settings.hilite ? hilite_none : "");

    if (select)
    {
        settings.ostr << (settings.hilite ? hilite_keyword : "") << " AS" << settings.nl_or_ws << (settings.hilite ? hilite_none : "");
        select->formatImpl(settings, state, frame);
    }
374 375 376 377 378 379

    if (tables)
    {
        settings.ostr << (settings.hilite ? hilite_keyword : "") << " WITH " << (settings.hilite ? hilite_none : "");
        tables->formatImpl(settings, state, frame);
    }
380 381
}

A
alesapin 已提交
382
}