ClickHouse/dbms/src/Parsers/ParserCase.cpp

133 lines
3.5 KiB
C++
Raw Normal View History

#include <Parsers/ParserCase.h>
#include <Parsers/ExpressionElementParsers.h>
#include <Parsers/ExpressionListParsers.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTLiteral.h>
#include <Core/Field.h>
2016-05-03 23:19:14 +00:00
namespace DB
{
bool ParserCase::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
{
Pos begin = pos;
2016-05-03 23:19:14 +00:00
2017-06-15 17:55:57 +00:00
ParserWhitespaceOrComments ws;
ParserString s_case{"CASE", true, true};
ParserString s_when{"WHEN", true, true};
ParserString s_then{"THEN", true, true};
ParserString s_else{"ELSE", true, true};
ParserString s_end{ "END", true, true};
ParserExpressionWithOptionalAlias p_expr{false};
2016-05-03 23:19:14 +00:00
if (!s_case.parse(pos, end, node, max_parsed_pos, expected))
{
/// Parse as a simple ASTFunction.
return ParserFunction{}.parse(pos = begin, end, node, max_parsed_pos, expected);
}
2016-05-03 23:19:14 +00:00
ws.ignore(pos, end);
2016-05-03 23:19:14 +00:00
bool has_case_expr = false;
2016-05-03 23:19:14 +00:00
auto old_pos = pos;
has_case_expr = !s_when.parse(pos, end, node, max_parsed_pos, expected);
pos = old_pos;
2016-05-03 23:19:14 +00:00
ASTs args;
2016-05-03 23:19:14 +00:00
auto parse_branches = [&]()
{
bool has_branch = false;
while (s_when.parse(pos, end, node, max_parsed_pos, expected))
{
has_branch = true;
2016-05-03 23:19:14 +00:00
ws.ignore(pos, end);
2016-05-03 23:19:14 +00:00
ASTPtr expr_when;
if (!p_expr.parse(pos, end, expr_when, max_parsed_pos, expected))
return false;
args.push_back(expr_when);
2016-05-03 23:19:14 +00:00
ws.ignore(pos, end);
2016-05-03 23:19:14 +00:00
if (!s_then.parse(pos, end, node, max_parsed_pos, expected))
return false;
2016-05-03 23:19:14 +00:00
ws.ignore(pos, end);
2016-05-03 23:19:14 +00:00
ASTPtr expr_then;
if (!p_expr.parse(pos, end, expr_then, max_parsed_pos, expected))
return false;
args.push_back(expr_then);
2016-05-03 23:19:14 +00:00
ws.ignore(pos, end);
}
2016-05-03 23:19:14 +00:00
if (!has_branch)
return false;
2016-05-03 23:19:14 +00:00
ws.ignore(pos, end);
2016-05-03 23:19:14 +00:00
if (!s_else.parse(pos, end, node, max_parsed_pos, expected))
return false;
2016-05-03 23:19:14 +00:00
ws.ignore(pos, end);
2016-05-03 23:19:14 +00:00
ASTPtr expr_else;
if (!p_expr.parse(pos, end, expr_else, max_parsed_pos, expected))
return false;
args.push_back(expr_else);
2016-05-03 23:19:14 +00:00
ws.ignore(pos, end);
2016-05-03 23:19:14 +00:00
if (!s_end.parse(pos, end, node, max_parsed_pos, expected))
return false;
2016-05-03 23:19:14 +00:00
return true;
};
2016-05-03 23:19:14 +00:00
if (has_case_expr)
{
ASTPtr case_expr;
if (!p_expr.parse(pos, end, case_expr, max_parsed_pos, expected))
return false;
args.push_back(case_expr);
2016-05-03 23:19:14 +00:00
ws.ignore(pos, end);
2016-05-03 23:19:14 +00:00
if (!parse_branches())
return false;
2016-05-03 23:19:14 +00:00
auto function_args = std::make_shared<ASTExpressionList>(StringRange{begin, pos});
function_args->children = std::move(args);
2016-05-03 23:19:14 +00:00
auto function = std::make_shared<ASTFunction>(StringRange{begin, pos});
function->name = "caseWithExpr";
function->arguments = function_args;
function->children.push_back(function->arguments);
2016-05-03 23:19:14 +00:00
node = function;
}
else
{
if (!parse_branches())
return false;
2016-05-03 23:19:14 +00:00
auto function_args = std::make_shared<ASTExpressionList>(StringRange{begin, pos});
function_args->children = std::move(args);
2016-05-03 23:19:14 +00:00
auto function = std::make_shared<ASTFunction>(StringRange{begin, pos});
function->name = "caseWithoutExpr";
function->arguments = function_args;
function->children.push_back(function->arguments);
2016-05-03 23:19:14 +00:00
node = function;
}
2016-05-03 23:19:14 +00:00
return true;
2016-05-03 23:19:14 +00:00
}
}