aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Parsers/ParserProjectionSelectQuery.cpp
blob: b2adb5cf1546e9ee1db249ee647106b62cc8609c (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
#include <memory>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTProjectionSelectQuery.h>
#include <Parsers/CommonParsers.h>
#include <Parsers/ExpressionElementParsers.h>
#include <Parsers/ExpressionListParsers.h>
#include <Parsers/IParserBase.h>
#include <Parsers/ParserProjectionSelectQuery.h>


namespace DB
{
bool ParserProjectionSelectQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
    auto select_query = std::make_shared<ASTProjectionSelectQuery>();
    node = select_query;

    ParserKeyword s_with("WITH");
    ParserKeyword s_select("SELECT");
    ParserKeyword s_group_by("GROUP BY");
    ParserKeyword s_order_by("ORDER BY");

    ParserNotEmptyExpressionList exp_list_for_with_clause(false);
    ParserNotEmptyExpressionList exp_list_for_select_clause(true); /// Allows aliases without AS keyword.
    ParserExpression order_expression_p;

    ASTPtr with_expression_list;
    ASTPtr select_expression_list;
    ASTPtr group_expression_list;
    ASTPtr order_expression;

    /// WITH expr list
    {
        if (s_with.ignore(pos, expected))
        {
            if (!exp_list_for_with_clause.parse(pos, with_expression_list, expected))
                return false;
        }
    }

    /// SELECT [DISTINCT] [TOP N [WITH TIES]] expr list
    {
        if (!s_select.ignore(pos, expected))
            return false;

        if (!exp_list_for_select_clause.parse(pos, select_expression_list, expected))
            return false;
    }

    // If group by is specified, AggregatingMergeTree engine is used, and the group by keys are implied to be order by keys
    if (s_group_by.ignore(pos, expected))
    {
        if (!ParserList(std::make_unique<ParserExpression>(), std::make_unique<ParserToken>(TokenType::Comma))
                 .parse(pos, group_expression_list, expected))
            return false;
    }

    // ORDER BY needs to be an ASTFunction so that we can use it as a sorting key
    if (s_order_by.ignore(pos, expected))
    {
        ASTPtr expr_list;
        if (!ParserList(std::make_unique<ParserExpression>(), std::make_unique<ParserToken>(TokenType::Comma)).parse(pos, expr_list, expected))
            return false;

        if (expr_list->children.size() == 1)
        {
            order_expression = expr_list->children.front();
        }
        else
        {
            auto function_node = std::make_shared<ASTFunction>();
            function_node->name = "tuple";
            function_node->arguments = expr_list;
            function_node->children.push_back(expr_list);
            order_expression = function_node;
        }
    }

    select_query->setExpression(ASTProjectionSelectQuery::Expression::WITH, std::move(with_expression_list));
    select_query->setExpression(ASTProjectionSelectQuery::Expression::SELECT, std::move(select_expression_list));
    select_query->setExpression(ASTProjectionSelectQuery::Expression::GROUP_BY, std::move(group_expression_list));
    select_query->setExpression(ASTProjectionSelectQuery::Expression::ORDER_BY, std::move(order_expression));
    return true;
}

}