2011-08-28 00:31:30 +00:00
|
|
|
#include <DB/Parsers/ASTSelectQuery.h>
|
2011-08-28 02:22:23 +00:00
|
|
|
#include <DB/Parsers/ASTIdentifier.h>
|
2011-08-28 00:31:30 +00:00
|
|
|
#include <DB/Parsers/IParserBase.h>
|
|
|
|
#include <DB/Parsers/CommonParsers.h>
|
|
|
|
#include <DB/Parsers/ExpressionElementParsers.h>
|
|
|
|
#include <DB/Parsers/ExpressionListParsers.h>
|
|
|
|
#include <DB/Parsers/ParserSelectQuery.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
bool ParserSelectQuery::parseImpl(Pos & pos, Pos end, ASTPtr & node, String & expected)
|
|
|
|
{
|
|
|
|
Pos begin = pos;
|
|
|
|
|
|
|
|
ASTSelectQuery * select_query = new ASTSelectQuery(StringRange(begin, pos));
|
|
|
|
node = select_query;
|
|
|
|
|
|
|
|
ParserWhiteSpaceOrComments ws;
|
|
|
|
ParserString s_select("SELECT", true, true);
|
|
|
|
ParserString s_from("FROM", true, true);
|
|
|
|
ParserString s_where("WHERE", true, true);
|
2013-04-23 11:08:41 +00:00
|
|
|
ParserString s_final("FINAL", true, true);
|
2012-12-12 14:25:55 +00:00
|
|
|
ParserString s_sample("SAMPLE", true, true);
|
2011-08-28 00:31:30 +00:00
|
|
|
ParserString s_group("GROUP", true, true);
|
|
|
|
ParserString s_by("BY", true, true);
|
2013-05-04 15:46:50 +00:00
|
|
|
ParserString s_with("WITH", true, true);
|
|
|
|
ParserString s_totals("TOTALS", true, true);
|
2011-08-28 00:31:30 +00:00
|
|
|
ParserString s_having("HAVING", true, true);
|
|
|
|
ParserString s_order("ORDER", true, true);
|
|
|
|
ParserString s_limit("LIMIT", true, true);
|
2011-10-30 05:19:41 +00:00
|
|
|
ParserString s_format("FORMAT", true, true);
|
2011-08-28 00:31:30 +00:00
|
|
|
ParserNotEmptyExpressionList exp_list;
|
2011-11-06 20:47:07 +00:00
|
|
|
ParserExpressionWithOptionalAlias exp_elem;
|
2011-09-04 05:14:52 +00:00
|
|
|
ParserOrderByExpressionList order_list;
|
2011-08-28 00:31:30 +00:00
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
|
|
/// SELECT expr list
|
|
|
|
{
|
|
|
|
if (!s_select.ignore(pos, end, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
|
|
if (!exp_list.parse(pos, end, select_query->select_expression_list, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
}
|
|
|
|
|
2011-11-06 02:29:13 +00:00
|
|
|
/// FROM database.table или FROM table или FROM (subquery)
|
2011-08-28 00:31:30 +00:00
|
|
|
if (s_from.ignore(pos, end, expected))
|
|
|
|
{
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
2011-11-06 02:29:13 +00:00
|
|
|
ParserString s_lparen("(");
|
|
|
|
ParserString s_rparen(")");
|
2011-08-28 00:31:30 +00:00
|
|
|
ParserString s_dot(".");
|
|
|
|
ParserIdentifier ident;
|
|
|
|
|
2011-11-06 02:29:13 +00:00
|
|
|
if (s_lparen.ignore(pos, end, expected))
|
2011-08-28 00:31:30 +00:00
|
|
|
{
|
2011-11-06 02:29:13 +00:00
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
|
|
ParserSelectQuery select_p;
|
|
|
|
if (!select_p.parse(pos, end, select_query->table, expected))
|
2011-08-28 00:31:30 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
2011-08-28 02:22:23 +00:00
|
|
|
|
2011-11-06 02:29:13 +00:00
|
|
|
if (!s_rparen.ignore(pos, end, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
}
|
|
|
|
else if (ident.parse(pos, end, select_query->table, expected))
|
|
|
|
{
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
|
|
if (s_dot.ignore(pos, end, expected))
|
|
|
|
{
|
|
|
|
select_query->database = select_query->table;
|
|
|
|
if (!ident.parse(pos, end, select_query->table, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (select_query->database)
|
|
|
|
dynamic_cast<ASTIdentifier &>(*select_query->database).kind = ASTIdentifier::Database;
|
|
|
|
dynamic_cast<ASTIdentifier &>(*select_query->table).kind = ASTIdentifier::Table;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
2011-08-28 00:31:30 +00:00
|
|
|
}
|
2013-04-23 11:08:41 +00:00
|
|
|
|
|
|
|
/// FINAL
|
|
|
|
if (s_final.ignore(pos, end, expected))
|
|
|
|
{
|
|
|
|
select_query->final = true;
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
}
|
2011-08-28 00:31:30 +00:00
|
|
|
|
2012-12-12 14:25:55 +00:00
|
|
|
/// SAMPLE number
|
|
|
|
if (s_sample.ignore(pos, end, expected))
|
|
|
|
{
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
|
|
ParserNumber num;
|
|
|
|
|
|
|
|
if (!num.parse(pos, end, select_query->sample_size, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
}
|
|
|
|
|
2011-08-28 00:31:30 +00:00
|
|
|
/// WHERE expr
|
|
|
|
if (s_where.ignore(pos, end, expected))
|
|
|
|
{
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
|
|
if (!exp_elem.parse(pos, end, select_query->where_expression, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// GROUP BY expr list
|
|
|
|
if (s_group.ignore(pos, end, expected))
|
|
|
|
{
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
if (!s_by.ignore(pos, end, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!exp_list.parse(pos, end, select_query->group_expression_list, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
2013-05-04 15:46:50 +00:00
|
|
|
|
|
|
|
/// WITH TOTALS
|
|
|
|
if (s_with.ignore(pos, end, expected))
|
|
|
|
{
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
if (!s_totals.ignore(pos, end, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
select_query->group_by_with_totals = true;
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
}
|
2011-08-28 00:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// HAVING expr
|
|
|
|
if (s_having.ignore(pos, end, expected))
|
|
|
|
{
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
|
|
if (!exp_elem.parse(pos, end, select_query->having_expression, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
}
|
|
|
|
|
2013-05-29 11:33:55 +00:00
|
|
|
/// ORDER BY expr ASC|DESC COLLATE 'locale' list
|
2011-08-28 00:31:30 +00:00
|
|
|
if (s_order.ignore(pos, end, expected))
|
|
|
|
{
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
if (!s_by.ignore(pos, end, expected))
|
|
|
|
return false;
|
|
|
|
|
2011-09-04 05:14:52 +00:00
|
|
|
if (!order_list.parse(pos, end, select_query->order_expression_list, expected))
|
2011-08-28 00:31:30 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// LIMIT length или LIMIT offset, length
|
|
|
|
if (s_limit.ignore(pos, end, expected))
|
|
|
|
{
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
|
|
ParserString s_comma(",");
|
|
|
|
ParserNumber num;
|
|
|
|
|
|
|
|
if (!num.parse(pos, end, select_query->limit_length, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
|
|
if (s_comma.ignore(pos, end, expected))
|
|
|
|
{
|
|
|
|
select_query->limit_offset = select_query->limit_length;
|
|
|
|
if (!num.parse(pos, end, select_query->limit_length, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-30 05:19:41 +00:00
|
|
|
/// FORMAT format_name
|
|
|
|
if (s_format.ignore(pos, end, expected))
|
|
|
|
{
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
|
|
ParserIdentifier format_p;
|
|
|
|
|
|
|
|
if (!format_p.parse(pos, end, select_query->format, expected))
|
|
|
|
return false;
|
|
|
|
dynamic_cast<ASTIdentifier &>(*select_query->format).kind = ASTIdentifier::Format;
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
}
|
|
|
|
|
2011-08-28 00:31:30 +00:00
|
|
|
select_query->children.push_back(select_query->select_expression_list);
|
|
|
|
if (select_query->database)
|
|
|
|
select_query->children.push_back(select_query->database);
|
|
|
|
if (select_query->table)
|
|
|
|
select_query->children.push_back(select_query->table);
|
2012-12-12 14:25:55 +00:00
|
|
|
if (select_query->sample_size)
|
|
|
|
select_query->children.push_back(select_query->sample_size);
|
2011-08-28 00:31:30 +00:00
|
|
|
if (select_query->where_expression)
|
|
|
|
select_query->children.push_back(select_query->where_expression);
|
|
|
|
if (select_query->group_expression_list)
|
|
|
|
select_query->children.push_back(select_query->group_expression_list);
|
|
|
|
if (select_query->having_expression)
|
|
|
|
select_query->children.push_back(select_query->having_expression);
|
|
|
|
if (select_query->order_expression_list)
|
|
|
|
select_query->children.push_back(select_query->order_expression_list);
|
|
|
|
if (select_query->limit_offset)
|
|
|
|
select_query->children.push_back(select_query->limit_offset);
|
|
|
|
if (select_query->limit_length)
|
|
|
|
select_query->children.push_back(select_query->limit_length);
|
2011-10-30 05:19:41 +00:00
|
|
|
if (select_query->format)
|
|
|
|
select_query->children.push_back(select_query->format);
|
2011-08-28 00:31:30 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|