2017-04-01 09:19:00 +00:00
|
|
|
#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
|
|
|
|
{
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserCase::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2016-05-03 23:19:14 +00:00
|
|
|
{
|
2017-06-18 03:07:03 +00:00
|
|
|
ParserKeyword s_case{"CASE"};
|
|
|
|
ParserKeyword s_when{"WHEN"};
|
|
|
|
ParserKeyword s_then{"THEN"};
|
|
|
|
ParserKeyword s_else{"ELSE"};
|
|
|
|
ParserKeyword s_end{ "END"};
|
2017-07-12 02:42:34 +00:00
|
|
|
ParserExpressionWithOptionalAlias p_expr{false};
|
2016-05-03 23:19:14 +00:00
|
|
|
|
2018-06-07 20:25:38 +00:00
|
|
|
if (!s_case.ignore(pos, expected))
|
|
|
|
return false;
|
2016-05-03 23:19:14 +00:00
|
|
|
|
|
|
|
auto old_pos = pos;
|
2018-06-07 20:25:38 +00:00
|
|
|
bool has_case_expr = !s_when.ignore(pos, expected);
|
2016-05-03 23:19:14 +00:00
|
|
|
pos = old_pos;
|
|
|
|
|
|
|
|
ASTs args;
|
|
|
|
|
|
|
|
auto parse_branches = [&]()
|
|
|
|
{
|
|
|
|
bool has_branch = false;
|
2018-06-07 20:25:38 +00:00
|
|
|
while (s_when.ignore(pos, expected))
|
2016-05-03 23:19:14 +00:00
|
|
|
{
|
|
|
|
has_branch = true;
|
|
|
|
|
|
|
|
ASTPtr expr_when;
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!p_expr.parse(pos, expr_when, expected))
|
2016-05-03 23:19:14 +00:00
|
|
|
return false;
|
|
|
|
args.push_back(expr_when);
|
|
|
|
|
2018-06-07 20:25:38 +00:00
|
|
|
if (!s_then.ignore(pos, expected))
|
2016-05-03 23:19:14 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
ASTPtr expr_then;
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!p_expr.parse(pos, expr_then, expected))
|
2016-05-03 23:19:14 +00:00
|
|
|
return false;
|
|
|
|
args.push_back(expr_then);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!has_branch)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ASTPtr expr_else;
|
2018-08-22 15:17:40 +00:00
|
|
|
if (s_else.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
if (!p_expr.parse(pos, expr_else, expected))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Field field_with_null;
|
|
|
|
ASTLiteral null_literal(field_with_null);
|
|
|
|
expr_else = std::make_shared<ASTLiteral>(null_literal);
|
|
|
|
}
|
2016-05-03 23:19:14 +00:00
|
|
|
args.push_back(expr_else);
|
|
|
|
|
2020-03-08 22:08:39 +00:00
|
|
|
return s_end.ignore(pos, expected);
|
2016-05-03 23:19:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (has_case_expr)
|
|
|
|
{
|
|
|
|
ASTPtr case_expr;
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!p_expr.parse(pos, case_expr, expected))
|
2016-05-03 23:19:14 +00:00
|
|
|
return false;
|
2016-08-12 14:06:58 +00:00
|
|
|
args.push_back(case_expr);
|
2016-05-03 23:19:14 +00:00
|
|
|
|
|
|
|
if (!parse_branches())
|
|
|
|
return false;
|
|
|
|
|
2018-02-26 03:37:08 +00:00
|
|
|
auto function_args = std::make_shared<ASTExpressionList>();
|
2016-08-12 14:06:58 +00:00
|
|
|
function_args->children = std::move(args);
|
2016-05-03 23:19:14 +00:00
|
|
|
|
2018-02-26 03:37:08 +00:00
|
|
|
auto function = std::make_shared<ASTFunction>();
|
2017-08-07 01:36:20 +00:00
|
|
|
function->name = "caseWithExpression";
|
2016-05-03 23:19:14 +00:00
|
|
|
function->arguments = function_args;
|
|
|
|
function->children.push_back(function->arguments);
|
|
|
|
|
|
|
|
node = function;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!parse_branches())
|
|
|
|
return false;
|
|
|
|
|
2018-02-26 03:37:08 +00:00
|
|
|
auto function_args = std::make_shared<ASTExpressionList>();
|
2016-05-03 23:19:14 +00:00
|
|
|
function_args->children = std::move(args);
|
|
|
|
|
2018-02-26 03:37:08 +00:00
|
|
|
auto function = std::make_shared<ASTFunction>();
|
2017-12-07 01:43:06 +00:00
|
|
|
function->name = "multiIf";
|
2016-05-03 23:19:14 +00:00
|
|
|
function->arguments = function_args;
|
|
|
|
function->children.push_back(function->arguments);
|
|
|
|
|
|
|
|
node = function;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|