aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Parsers/MySQL/ASTCreateQuery.cpp
blob: 227ac62f86d35170013f0eaf2f87ebb0dc99ad3c (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
#include <Parsers/MySQL/ASTCreateQuery.h>

#include <Interpreters/StorageID.h>
#include <Parsers/ASTIdentifier.h>
#include <Parsers/CommonParsers.h>
#include <Parsers/ExpressionElementParsers.h>
#include <Parsers/ExpressionListParsers.h>
#include <Parsers/MySQL/ASTCreateDefines.h>
#include <Parsers/MySQL/ASTDeclarePartitionOptions.h>
#include <Parsers/MySQL/ASTDeclareTableOptions.h>

namespace DB
{

namespace MySQLParser
{

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

    if (columns_list)
    {
        res->columns_list = columns_list->clone();
        res->children.emplace_back(res->columns_list);
    }

    if (table_options)
    {
        res->table_options = table_options->clone();
        res->children.emplace_back(res->table_options);
    }

    if (partition_options)
    {
        res->partition_options = partition_options->clone();
        res->children.emplace_back(res->partition_options);
    }

    return res;
}

bool ParserCreateQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
    ASTPtr table;
    ASTPtr like_table;
    ASTPtr columns_list;
    ASTPtr table_options;
    ASTPtr partition_options;
    bool is_temporary = false;
    bool if_not_exists = false;

    if (!ParserKeyword("CREATE").ignore(pos, expected))
        return false;

    if (ParserKeyword("TEMPORARY").ignore(pos, expected))
        is_temporary = true;

    if (!ParserKeyword("TABLE").ignore(pos, expected))
        return false;

    if (ParserKeyword("IF NOT EXISTS").ignore(pos, expected))
        if_not_exists = true;

    if (!ParserCompoundIdentifier(true).parse(pos, table, expected))
        return false;

    if (ParserKeyword("LIKE").ignore(pos, expected))
    {
        if (!ParserCompoundIdentifier(true).parse(pos, like_table, expected))
            return false;
    }
    else if (ParserToken(TokenType::OpeningRoundBracket).ignore(pos, expected))
    {
        if (ParserKeyword("LIKE").ignore(pos, expected))
        {
            if (!ParserCompoundIdentifier(true).parse(pos, like_table, expected))
                return false;

            if (!ParserToken(TokenType::ClosingRoundBracket).ignore(pos, expected))
                return false;
        }
        else
        {
            if (!ParserCreateDefines().parse(pos, columns_list, expected))
                return false;

            if (!ParserToken(TokenType::ClosingRoundBracket).ignore(pos, expected))
                return false;

            ParserDeclareTableOptions().parse(pos, table_options, expected);
            ParserDeclarePartitionOptions().parse(pos, partition_options, expected);
        }
    }
    else
        return false;

    auto create_query = std::make_shared<ASTCreateQuery>();

    create_query->temporary = is_temporary;
    create_query->if_not_exists = if_not_exists;

    auto table_id = table->as<ASTTableIdentifier>()->getTableId();
    create_query->table = table_id.table_name;
    create_query->database = table_id.database_name;
    create_query->like_table = like_table;
    create_query->columns_list = columns_list;
    create_query->table_options = table_options;
    create_query->partition_options = partition_options;

    if (create_query->like_table)
        create_query->children.emplace_back(create_query->like_table);

    if (create_query->columns_list)
        create_query->children.emplace_back(create_query->columns_list);

    if (create_query->table_options)
        create_query->children.emplace_back(create_query->table_options);

    if (create_query->partition_options)
        create_query->children.emplace_back(create_query->partition_options);

    node = create_query;
    return true;
}
}

}