2010-06-24 19:12:10 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2018-07-19 20:51:03 +00:00
|
|
|
#include <Poco/String.h>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <IO/ReadBufferFromMemory.h>
|
2019-01-14 18:15:04 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2018-12-13 15:26:28 +00:00
|
|
|
#include <Parsers/DumpASTNode.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
|
|
|
|
#include <Parsers/IAST.h>
|
|
|
|
#include <Parsers/ASTExpressionList.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/ASTAsterisk.h>
|
|
|
|
#include <Parsers/ASTQualifiedAsterisk.h>
|
2019-05-18 21:07:23 +00:00
|
|
|
#include <Parsers/ASTQueryParameter.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTOrderByElement.h>
|
|
|
|
#include <Parsers/ASTSubquery.h>
|
2019-10-07 16:23:16 +00:00
|
|
|
#include <Parsers/ASTFunctionWithKeyValueArguments.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
|
2019-11-16 13:54:52 +00:00
|
|
|
#include <Parsers/parseIntervalKind.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ExpressionListParsers.h>
|
2018-02-25 00:50:53 +00:00
|
|
|
#include <Parsers/ParserSelectWithUnionQuery.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ParserCase.h>
|
|
|
|
|
|
|
|
#include <Parsers/ExpressionElementParsers.h>
|
|
|
|
#include <Parsers/ParserCreateQuery.h>
|
2010-06-24 19:12:10 +00:00
|
|
|
|
2018-06-07 20:25:38 +00:00
|
|
|
#include <Parsers/queryToString.h>
|
2018-12-13 15:26:28 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2019-07-21 17:03:58 +00:00
|
|
|
#include "ASTColumnsMatcher.h"
|
2018-06-07 20:25:38 +00:00
|
|
|
|
2010-06-24 19:12:10 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int SYNTAX_ERROR;
|
2017-07-27 21:27:10 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2010-06-24 19:12:10 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserArray::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2010-06-24 19:12:10 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTPtr contents_node;
|
|
|
|
ParserExpressionList contents(false);
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (pos->type != TokenType::OpeningSquareBracket)
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2017-07-10 03:28:12 +00:00
|
|
|
++pos;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!contents.parse(pos, contents_node, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (pos->type != TokenType::ClosingSquareBracket)
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2017-07-10 03:28:12 +00:00
|
|
|
++pos;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-02-26 03:37:08 +00:00
|
|
|
auto function_node = std::make_shared<ASTFunction>();
|
2017-04-01 07:20:54 +00:00
|
|
|
function_node->name = "array";
|
|
|
|
function_node->arguments = contents_node;
|
|
|
|
function_node->children.push_back(contents_node);
|
|
|
|
node = function_node;
|
|
|
|
|
|
|
|
return true;
|
2010-06-24 19:12:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserParenthesisExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2010-06-24 19:12:10 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTPtr contents_node;
|
|
|
|
ParserExpressionList contents(false);
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (pos->type != TokenType::OpeningRoundBracket)
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2017-07-10 03:28:12 +00:00
|
|
|
++pos;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!contents.parse(pos, contents_node, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
2019-10-31 15:22:48 +00:00
|
|
|
bool is_elem = true;
|
|
|
|
if (pos->type == TokenType::Comma)
|
|
|
|
{
|
|
|
|
is_elem = false;
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (pos->type != TokenType::ClosingRoundBracket)
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2017-07-10 03:28:12 +00:00
|
|
|
++pos;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-03-15 16:14:13 +00:00
|
|
|
const auto & expr_list = contents_node->as<ASTExpressionList &>();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// empty expression in parentheses is not allowed
|
2019-03-15 16:14:13 +00:00
|
|
|
if (expr_list.children.empty())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-13 04:20:56 +00:00
|
|
|
expected.add(pos, "non-empty parenthesized list of expressions");
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-31 15:22:48 +00:00
|
|
|
if (expr_list.children.size() == 1 && is_elem)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-03-15 16:14:13 +00:00
|
|
|
node = expr_list.children.front();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-02-26 03:37:08 +00:00
|
|
|
auto function_node = std::make_shared<ASTFunction>();
|
2017-04-01 07:20:54 +00:00
|
|
|
function_node->name = "tuple";
|
|
|
|
function_node->arguments = contents_node;
|
|
|
|
function_node->children.push_back(contents_node);
|
|
|
|
node = function_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2010-06-24 19:12:10 +00:00
|
|
|
}
|
2012-08-22 18:46:09 +00:00
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserSubquery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2012-08-22 18:46:09 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTPtr select_node;
|
2018-02-25 00:50:53 +00:00
|
|
|
ParserSelectWithUnionQuery select;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (pos->type != TokenType::OpeningRoundBracket)
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2017-07-10 03:28:12 +00:00
|
|
|
++pos;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!select.parse(pos, select_node, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (pos->type != TokenType::ClosingRoundBracket)
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2017-07-10 03:28:12 +00:00
|
|
|
++pos;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-02-26 03:37:08 +00:00
|
|
|
node = std::make_shared<ASTSubquery>();
|
2019-03-11 12:49:39 +00:00
|
|
|
node->children.push_back(select_node);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2012-08-22 18:46:09 +00:00
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2010-06-24 19:12:10 +00:00
|
|
|
|
2017-12-01 18:36:55 +00:00
|
|
|
bool ParserIdentifier::parseImpl(Pos & pos, ASTPtr & node, Expected &)
|
2010-06-24 19:12:10 +00:00
|
|
|
{
|
2017-06-24 01:49:15 +00:00
|
|
|
/// Identifier in backquotes or in double quotes
|
2017-07-10 03:28:12 +00:00
|
|
|
if (pos->type == TokenType::QuotedIdentifier)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
ReadBufferFromMemory buf(pos->begin, pos->size());
|
2017-04-01 07:20:54 +00:00
|
|
|
String s;
|
2017-06-24 01:49:15 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (*pos->begin == '`')
|
2017-06-25 03:43:37 +00:00
|
|
|
readBackQuotedStringWithSQLStyle(s, buf);
|
2017-06-24 01:49:15 +00:00
|
|
|
else
|
2017-06-25 03:43:37 +00:00
|
|
|
readDoubleQuotedStringWithSQLStyle(s, buf);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
if (s.empty()) /// Identifiers "empty string" are not allowed.
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
2018-02-26 03:37:08 +00:00
|
|
|
node = std::make_shared<ASTIdentifier>(s);
|
2017-07-10 03:28:12 +00:00
|
|
|
++pos;
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-07-10 03:28:12 +00:00
|
|
|
else if (pos->type == TokenType::BareWord)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-02-26 03:37:08 +00:00
|
|
|
node = std::make_shared<ASTIdentifier>(String(pos->begin, pos->end));
|
2017-07-12 03:23:46 +00:00
|
|
|
++pos;
|
2017-07-10 03:28:12 +00:00
|
|
|
return true;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-07-10 03:28:12 +00:00
|
|
|
|
|
|
|
return false;
|
2013-07-16 15:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserCompoundIdentifier::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2013-07-16 15:26:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTPtr id_list;
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!ParserList(std::make_unique<ParserIdentifier>(), std::make_unique<ParserToken>(TokenType::Dot), false)
|
|
|
|
.parse(pos, id_list, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2015-11-08 01:29:37 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String name;
|
2019-01-17 17:01:48 +00:00
|
|
|
std::vector<String> parts;
|
2019-03-15 16:14:13 +00:00
|
|
|
const auto & list = id_list->as<ASTExpressionList &>();
|
|
|
|
for (const auto & child : list.children)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (!name.empty())
|
|
|
|
name += '.';
|
2019-08-08 20:02:30 +00:00
|
|
|
parts.emplace_back(getIdentifierName(child));
|
2019-01-17 17:01:48 +00:00
|
|
|
name += parts.back();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2011-11-01 17:57:37 +00:00
|
|
|
|
2019-01-17 17:01:48 +00:00
|
|
|
if (parts.size() == 1)
|
|
|
|
parts.clear();
|
|
|
|
node = std::make_shared<ASTIdentifier>(name, std::move(parts));
|
2015-11-08 01:29:37 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2010-06-24 19:12:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserFunction::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2010-06-24 19:12:10 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ParserIdentifier id_parser;
|
2017-06-18 03:07:03 +00:00
|
|
|
ParserKeyword distinct("DISTINCT");
|
2017-04-01 07:20:54 +00:00
|
|
|
ParserExpressionList contents(false);
|
|
|
|
|
|
|
|
bool has_distinct_modifier = false;
|
|
|
|
|
|
|
|
ASTPtr identifier;
|
|
|
|
ASTPtr expr_list_args;
|
|
|
|
ASTPtr expr_list_params;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!id_parser.parse(pos, identifier, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (pos->type != TokenType::OpeningRoundBracket)
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2017-07-10 03:28:12 +00:00
|
|
|
++pos;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (distinct.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
has_distinct_modifier = true;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
const char * contents_begin = pos->begin;
|
|
|
|
if (!contents.parse(pos, expr_list_args, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2017-07-10 03:28:12 +00:00
|
|
|
const char * contents_end = pos->begin;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (pos->type != TokenType::ClosingRoundBracket)
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2017-07-10 03:28:12 +00:00
|
|
|
++pos;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/** Check for a common error case - often due to the complexity of quoting command-line arguments,
|
|
|
|
* an expression of the form toDate(2014-01-01) appears in the query instead of toDate('2014-01-01').
|
|
|
|
* If you do not report that the first option is an error, then the argument will be interpreted as 2014 - 01 - 01 - some number,
|
|
|
|
* and the query silently returns an unexpected result.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
2019-08-08 20:02:30 +00:00
|
|
|
if (getIdentifierName(identifier) == "toDate"
|
2017-04-01 07:20:54 +00:00
|
|
|
&& contents_end - contents_begin == strlen("2014-01-01")
|
|
|
|
&& contents_begin[0] >= '2' && contents_begin[0] <= '3'
|
|
|
|
&& contents_begin[1] >= '0' && contents_begin[1] <= '9'
|
|
|
|
&& contents_begin[2] >= '0' && contents_begin[2] <= '9'
|
|
|
|
&& contents_begin[3] >= '0' && contents_begin[3] <= '9'
|
|
|
|
&& contents_begin[4] == '-'
|
|
|
|
&& contents_begin[5] >= '0' && contents_begin[5] <= '9'
|
|
|
|
&& contents_begin[6] >= '0' && contents_begin[6] <= '9'
|
|
|
|
&& contents_begin[7] == '-'
|
|
|
|
&& contents_begin[8] >= '0' && contents_begin[8] <= '9'
|
|
|
|
&& contents_begin[9] >= '0' && contents_begin[9] <= '9')
|
|
|
|
{
|
2018-08-26 02:19:18 +00:00
|
|
|
std::string contents_str(contents_begin, contents_end - contents_begin);
|
|
|
|
throw Exception("Argument of function toDate is unquoted: toDate(" + contents_str + "), must be: toDate('" + contents_str + "')"
|
2017-04-01 07:20:54 +00:00
|
|
|
, ErrorCodes::SYNTAX_ERROR);
|
|
|
|
}
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// The parametric aggregate function has two lists (parameters and arguments) in parentheses. Example: quantile(0.9)(x).
|
2019-11-18 16:49:23 +00:00
|
|
|
if (allow_function_parameters && pos->type == TokenType::OpeningRoundBracket)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
++pos;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Parametric aggregate functions cannot have DISTINCT in parameters list.
|
|
|
|
if (has_distinct_modifier)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
expr_list_params = expr_list_args;
|
|
|
|
expr_list_args = nullptr;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (distinct.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
has_distinct_modifier = true;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!contents.parse(pos, expr_list_args, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (pos->type != TokenType::ClosingRoundBracket)
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2017-07-10 03:28:12 +00:00
|
|
|
++pos;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2018-02-26 03:37:08 +00:00
|
|
|
auto function_node = std::make_shared<ASTFunction>();
|
2019-08-08 20:02:30 +00:00
|
|
|
tryGetIdentifierNameInto(identifier, function_node->name);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// func(DISTINCT ...) is equivalent to funcDistinct(...)
|
|
|
|
if (has_distinct_modifier)
|
|
|
|
function_node->name += "Distinct";
|
|
|
|
|
|
|
|
function_node->arguments = expr_list_args;
|
|
|
|
function_node->children.push_back(function_node->arguments);
|
|
|
|
|
|
|
|
if (expr_list_params)
|
|
|
|
{
|
|
|
|
function_node->parameters = expr_list_params;
|
|
|
|
function_node->children.push_back(function_node->parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
node = function_node;
|
|
|
|
return true;
|
2015-12-24 17:14:10 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 15:26:28 +00:00
|
|
|
bool ParserCodecDeclarationList::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
|
|
|
return ParserList(std::make_unique<ParserIdentifierWithOptionalParameters>(),
|
|
|
|
std::make_unique<ParserToken>(TokenType::Comma), false).parse(pos, node, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ParserCodec::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
|
|
|
ParserCodecDeclarationList codecs;
|
|
|
|
ASTPtr expr_list_args;
|
|
|
|
|
|
|
|
if (pos->type != TokenType::OpeningRoundBracket)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
++pos;
|
|
|
|
if (!codecs.parse(pos, expr_list_args, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (pos->type != TokenType::ClosingRoundBracket)
|
|
|
|
return false;
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
auto function_node = std::make_shared<ASTFunction>();
|
2018-12-24 17:12:14 +00:00
|
|
|
function_node->name = "CODEC";
|
2018-12-13 15:26:28 +00:00
|
|
|
function_node->arguments = expr_list_args;
|
|
|
|
function_node->children.push_back(function_node->arguments);
|
|
|
|
|
|
|
|
node = function_node;
|
|
|
|
return true;
|
|
|
|
}
|
2015-12-24 17:14:10 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserCastExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2015-12-24 17:14:10 +00:00
|
|
|
{
|
2018-06-07 20:25:38 +00:00
|
|
|
/// Either CAST(expr AS type) or CAST(expr, 'type')
|
|
|
|
/// The latter will be parsed normally as a function later.
|
|
|
|
|
|
|
|
ASTPtr expr_node;
|
|
|
|
ASTPtr type_node;
|
|
|
|
|
|
|
|
if (ParserKeyword("CAST").ignore(pos, expected)
|
|
|
|
&& ParserToken(TokenType::OpeningRoundBracket).ignore(pos, expected)
|
|
|
|
&& ParserExpression().parse(pos, expr_node, expected)
|
|
|
|
&& ParserKeyword("AS").ignore(pos, expected)
|
|
|
|
&& ParserIdentifierWithOptionalParameters().parse(pos, type_node, expected)
|
|
|
|
&& ParserToken(TokenType::ClosingRoundBracket).ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-06-07 20:25:38 +00:00
|
|
|
/// Convert to canonical representation in functional form: CAST(expr, 'type')
|
2018-02-26 04:47:27 +00:00
|
|
|
|
2018-06-07 20:25:38 +00:00
|
|
|
auto type_literal = std::make_shared<ASTLiteral>(queryToString(type_node));
|
2015-12-24 17:14:10 +00:00
|
|
|
|
2018-06-07 20:25:38 +00:00
|
|
|
auto expr_list_args = std::make_shared<ASTExpressionList>();
|
|
|
|
expr_list_args->children.push_back(expr_node);
|
|
|
|
expr_list_args->children.push_back(std::move(type_literal));
|
2017-07-27 19:25:52 +00:00
|
|
|
|
2018-06-07 20:25:38 +00:00
|
|
|
auto func_node = std::make_shared<ASTFunction>();
|
|
|
|
func_node->name = "CAST";
|
|
|
|
func_node->arguments = std::move(expr_list_args);
|
|
|
|
func_node->children.push_back(func_node->arguments);
|
2015-12-24 17:14:10 +00:00
|
|
|
|
2018-06-07 20:25:38 +00:00
|
|
|
node = std::move(func_node);
|
|
|
|
return true;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2015-12-24 17:14:10 +00:00
|
|
|
|
2018-06-07 20:25:38 +00:00
|
|
|
return false;
|
2010-06-24 19:12:10 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 12:54:02 +00:00
|
|
|
bool ParserSubstringExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
|
|
|
/// Either SUBSTRING(expr FROM start) or SUBSTRING(expr FROM start FOR length) or SUBSTRING(expr, start, length)
|
|
|
|
/// The latter will be parsed normally as a function later.
|
|
|
|
|
|
|
|
ASTPtr expr_node;
|
|
|
|
ASTPtr start_node;
|
|
|
|
ASTPtr length_node;
|
|
|
|
|
|
|
|
if (!ParserKeyword("SUBSTRING").ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (pos->type != TokenType::OpeningRoundBracket)
|
|
|
|
return false;
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
if (!ParserExpression().parse(pos, expr_node, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (pos->type != TokenType::Comma)
|
|
|
|
{
|
|
|
|
if (!ParserKeyword("FROM").ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ParserExpression().parse(pos, start_node, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (pos->type == TokenType::ClosingRoundBracket)
|
|
|
|
{
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (pos->type != TokenType::Comma)
|
|
|
|
{
|
|
|
|
if (!ParserKeyword("FOR").ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ParserExpression().parse(pos, length_node, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ParserToken(TokenType::ClosingRoundBracket).ignore(pos, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert to canonical representation in functional form: SUBSTRING(expr, start, length)
|
|
|
|
|
|
|
|
auto expr_list_args = std::make_shared<ASTExpressionList>();
|
|
|
|
expr_list_args->children = {expr_node, start_node};
|
|
|
|
|
|
|
|
if (length_node)
|
|
|
|
expr_list_args->children.push_back(length_node);
|
|
|
|
|
|
|
|
auto func_node = std::make_shared<ASTFunction>();
|
|
|
|
func_node->name = "substring";
|
|
|
|
func_node->arguments = std::move(expr_list_args);
|
|
|
|
func_node->children.push_back(func_node->arguments);
|
|
|
|
|
|
|
|
node = std::move(func_node);
|
|
|
|
return true;
|
|
|
|
}
|
2018-06-07 20:25:38 +00:00
|
|
|
|
2018-12-13 11:50:37 +00:00
|
|
|
bool ParserTrimExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
|
|
|
/// Handles all possible TRIM/LTRIM/RTRIM call variants
|
|
|
|
|
2018-12-17 20:07:31 +00:00
|
|
|
std::string func_name;
|
2018-12-13 11:50:37 +00:00
|
|
|
bool trim_left = false;
|
|
|
|
bool trim_right = false;
|
|
|
|
bool char_override = false;
|
|
|
|
ASTPtr expr_node;
|
|
|
|
ASTPtr pattern_node;
|
|
|
|
ASTPtr to_remove;
|
|
|
|
|
|
|
|
if (ParserKeyword("LTRIM").ignore(pos, expected))
|
|
|
|
{
|
|
|
|
if (pos->type != TokenType::OpeningRoundBracket)
|
|
|
|
return false;
|
|
|
|
++pos;
|
|
|
|
trim_left = true;
|
|
|
|
}
|
|
|
|
else if (ParserKeyword("RTRIM").ignore(pos, expected))
|
|
|
|
{
|
|
|
|
if (pos->type != TokenType::OpeningRoundBracket)
|
|
|
|
return false;
|
|
|
|
++pos;
|
|
|
|
trim_right = true;
|
|
|
|
}
|
|
|
|
else if (ParserKeyword("TRIM").ignore(pos, expected))
|
|
|
|
{
|
|
|
|
if (pos->type != TokenType::OpeningRoundBracket)
|
|
|
|
return false;
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
if (ParserKeyword("BOTH").ignore(pos, expected))
|
|
|
|
{
|
|
|
|
trim_left = true;
|
|
|
|
trim_right = true;
|
|
|
|
char_override = true;
|
|
|
|
}
|
|
|
|
else if (ParserKeyword("LEADING").ignore(pos, expected))
|
|
|
|
{
|
|
|
|
trim_left = true;
|
|
|
|
char_override = true;
|
|
|
|
}
|
|
|
|
else if (ParserKeyword("TRAILING").ignore(pos, expected))
|
|
|
|
{
|
|
|
|
trim_right = true;
|
|
|
|
char_override = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
trim_left = true;
|
|
|
|
trim_right = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (char_override)
|
|
|
|
{
|
2018-12-13 12:58:00 +00:00
|
|
|
if (!ParserExpression().parse(pos, to_remove, expected))
|
2018-12-13 11:50:37 +00:00
|
|
|
return false;
|
|
|
|
if (!ParserKeyword("FROM").ignore(pos, expected))
|
|
|
|
return false;
|
2018-12-13 12:58:00 +00:00
|
|
|
|
|
|
|
auto quote_meta_func_node = std::make_shared<ASTFunction>();
|
|
|
|
auto quote_meta_list_args = std::make_shared<ASTExpressionList>();
|
|
|
|
quote_meta_list_args->children = {to_remove};
|
|
|
|
|
|
|
|
quote_meta_func_node->name = "regexpQuoteMeta";
|
|
|
|
quote_meta_func_node->arguments = std::move(quote_meta_list_args);
|
|
|
|
quote_meta_func_node->children.push_back(quote_meta_func_node->arguments);
|
|
|
|
|
|
|
|
to_remove = std::move(quote_meta_func_node);
|
2018-12-13 11:50:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(trim_left || trim_right))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!ParserExpression().parse(pos, expr_node, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (pos->type != TokenType::ClosingRoundBracket)
|
|
|
|
return false;
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
/// Convert to regexp replace function call
|
|
|
|
|
|
|
|
if (char_override)
|
|
|
|
{
|
2018-12-13 12:58:00 +00:00
|
|
|
auto pattern_func_node = std::make_shared<ASTFunction>();
|
2018-12-13 11:50:37 +00:00
|
|
|
auto pattern_list_args = std::make_shared<ASTExpressionList>();
|
|
|
|
if (trim_left && trim_right)
|
|
|
|
{
|
|
|
|
pattern_list_args->children = {
|
|
|
|
std::make_shared<ASTLiteral>("^["),
|
|
|
|
to_remove,
|
|
|
|
std::make_shared<ASTLiteral>("]*|["),
|
|
|
|
to_remove,
|
|
|
|
std::make_shared<ASTLiteral>("]*$")
|
|
|
|
};
|
|
|
|
func_name = "replaceRegexpAll";
|
|
|
|
}
|
2018-12-13 16:41:56 +00:00
|
|
|
else
|
|
|
|
{
|
2018-12-13 11:50:37 +00:00
|
|
|
if (trim_left)
|
|
|
|
{
|
|
|
|
pattern_list_args->children = {
|
|
|
|
std::make_shared<ASTLiteral>("^["),
|
|
|
|
to_remove,
|
|
|
|
std::make_shared<ASTLiteral>("]*")
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// trim_right == false not possible
|
|
|
|
pattern_list_args->children = {
|
|
|
|
std::make_shared<ASTLiteral>("["),
|
|
|
|
to_remove,
|
|
|
|
std::make_shared<ASTLiteral>("]*$")
|
|
|
|
};
|
|
|
|
}
|
2018-12-17 20:07:31 +00:00
|
|
|
func_name = "replaceRegexpOne";
|
2018-12-13 11:50:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pattern_func_node->name = "concat";
|
|
|
|
pattern_func_node->arguments = std::move(pattern_list_args);
|
|
|
|
pattern_func_node->children.push_back(pattern_func_node->arguments);
|
|
|
|
|
|
|
|
pattern_node = std::move(pattern_func_node);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (trim_left && trim_right)
|
|
|
|
{
|
2018-12-17 20:07:31 +00:00
|
|
|
func_name = "trimBoth";
|
2018-12-13 11:50:37 +00:00
|
|
|
}
|
2018-12-13 16:41:56 +00:00
|
|
|
else
|
|
|
|
{
|
2018-12-13 11:50:37 +00:00
|
|
|
if (trim_left)
|
|
|
|
{
|
2018-12-17 20:07:31 +00:00
|
|
|
func_name = "trimLeft";
|
2018-12-13 11:50:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// trim_right == false not possible
|
2018-12-17 20:07:31 +00:00
|
|
|
func_name = "trimRight";
|
2018-12-13 11:50:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto expr_list_args = std::make_shared<ASTExpressionList>();
|
2018-12-17 20:07:31 +00:00
|
|
|
if (char_override)
|
|
|
|
expr_list_args->children = {expr_node, pattern_node, std::make_shared<ASTLiteral>("")};
|
|
|
|
else
|
|
|
|
expr_list_args->children = {expr_node};
|
2018-12-13 11:50:37 +00:00
|
|
|
|
|
|
|
auto func_node = std::make_shared<ASTFunction>();
|
|
|
|
func_node->name = func_name;
|
|
|
|
func_node->arguments = std::move(expr_list_args);
|
|
|
|
func_node->children.push_back(func_node->arguments);
|
|
|
|
|
|
|
|
node = std::move(func_node);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-13 09:34:12 +00:00
|
|
|
bool ParserLeftExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
|
|
|
/// Rewrites left(expr, length) to SUBSTRING(expr, 1, length)
|
|
|
|
|
|
|
|
ASTPtr expr_node;
|
|
|
|
ASTPtr start_node;
|
|
|
|
ASTPtr length_node;
|
|
|
|
|
|
|
|
if (!ParserKeyword("LEFT").ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (pos->type != TokenType::OpeningRoundBracket)
|
|
|
|
return false;
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
if (!ParserExpression().parse(pos, expr_node, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ParserToken(TokenType::Comma).ignore(pos, expected);
|
|
|
|
|
|
|
|
if (!ParserExpression().parse(pos, length_node, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (pos->type != TokenType::ClosingRoundBracket)
|
|
|
|
return false;
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
auto expr_list_args = std::make_shared<ASTExpressionList>();
|
|
|
|
start_node = std::make_shared<ASTLiteral>(1);
|
|
|
|
expr_list_args->children = {expr_node, start_node, length_node};
|
|
|
|
|
|
|
|
auto func_node = std::make_shared<ASTFunction>();
|
|
|
|
func_node->name = "substring";
|
|
|
|
func_node->arguments = std::move(expr_list_args);
|
|
|
|
func_node->children.push_back(func_node->arguments);
|
|
|
|
|
|
|
|
node = std::move(func_node);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ParserRightExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
2018-12-14 11:42:49 +00:00
|
|
|
/// Rewrites RIGHT(expr, length) to substring(expr, -length)
|
2018-12-13 09:34:12 +00:00
|
|
|
|
|
|
|
ASTPtr expr_node;
|
|
|
|
ASTPtr length_node;
|
|
|
|
|
|
|
|
if (!ParserKeyword("RIGHT").ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (pos->type != TokenType::OpeningRoundBracket)
|
|
|
|
return false;
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
if (!ParserExpression().parse(pos, expr_node, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ParserToken(TokenType::Comma).ignore(pos, expected);
|
|
|
|
|
|
|
|
if (!ParserExpression().parse(pos, length_node, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (pos->type != TokenType::ClosingRoundBracket)
|
|
|
|
return false;
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
auto start_expr_list_args = std::make_shared<ASTExpressionList>();
|
2018-12-14 11:42:49 +00:00
|
|
|
start_expr_list_args->children = {length_node};
|
2018-12-13 09:34:12 +00:00
|
|
|
|
|
|
|
auto start_node = std::make_shared<ASTFunction>();
|
2018-12-14 11:42:49 +00:00
|
|
|
start_node->name = "negate";
|
2018-12-13 09:34:12 +00:00
|
|
|
start_node->arguments = std::move(start_expr_list_args);
|
|
|
|
start_node->children.push_back(start_node->arguments);
|
|
|
|
|
|
|
|
auto expr_list_args = std::make_shared<ASTExpressionList>();
|
|
|
|
expr_list_args->children = {expr_node, start_node};
|
|
|
|
|
|
|
|
auto func_node = std::make_shared<ASTFunction>();
|
|
|
|
func_node->name = "substring";
|
|
|
|
func_node->arguments = std::move(expr_list_args);
|
|
|
|
func_node->children.push_back(func_node->arguments);
|
|
|
|
|
|
|
|
node = std::move(func_node);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-13 19:35:08 +00:00
|
|
|
bool ParserExtractExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
2018-06-07 20:25:38 +00:00
|
|
|
if (!ParserKeyword("EXTRACT").ignore(pos, expected))
|
2018-04-13 19:35:08 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (pos->type != TokenType::OpeningRoundBracket)
|
|
|
|
return false;
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
ASTPtr expr;
|
|
|
|
|
2019-11-16 13:54:52 +00:00
|
|
|
IntervalKind interval_kind;
|
|
|
|
if (!parseIntervalKind(pos, expected, interval_kind))
|
2018-04-13 19:35:08 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
ParserKeyword s_from("FROM");
|
|
|
|
if (!s_from.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ParserExpression elem_parser;
|
|
|
|
if (!elem_parser.parse(pos, expr, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (pos->type != TokenType::ClosingRoundBracket)
|
|
|
|
return false;
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
auto function = std::make_shared<ASTFunction>();
|
|
|
|
auto exp_list = std::make_shared<ASTExpressionList>();
|
2019-11-16 13:54:52 +00:00
|
|
|
function->name = interval_kind.toNameOfFunctionExtractTimePart();
|
2018-04-13 19:35:08 +00:00
|
|
|
function->arguments = exp_list;
|
|
|
|
function->children.push_back(exp_list);
|
|
|
|
exp_list->children.push_back(expr);
|
|
|
|
node = function;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-19 11:37:14 +00:00
|
|
|
bool ParserDateAddExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
|
|
|
const char * function_name = nullptr;
|
2018-12-20 08:22:24 +00:00
|
|
|
ASTPtr timestamp_node;
|
|
|
|
ASTPtr offset_node;
|
2018-12-19 11:37:14 +00:00
|
|
|
|
|
|
|
if (ParserKeyword("DATEADD").ignore(pos, expected) || ParserKeyword("DATE_ADD").ignore(pos, expected)
|
|
|
|
|| ParserKeyword("TIMESTAMPADD").ignore(pos, expected) || ParserKeyword("TIMESTAMP_ADD").ignore(pos, expected))
|
|
|
|
function_name = "plus";
|
|
|
|
else if (ParserKeyword("DATESUB").ignore(pos, expected) || ParserKeyword("DATE_SUB").ignore(pos, expected)
|
|
|
|
|| ParserKeyword("TIMESTAMPSUB").ignore(pos, expected) || ParserKeyword("TIMESTAMP_SUB").ignore(pos, expected))
|
|
|
|
function_name = "minus";
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (pos->type != TokenType::OpeningRoundBracket)
|
|
|
|
return false;
|
|
|
|
++pos;
|
|
|
|
|
2019-11-16 13:54:52 +00:00
|
|
|
IntervalKind interval_kind;
|
|
|
|
if (parseIntervalKind(pos, expected, interval_kind))
|
2018-12-20 08:22:24 +00:00
|
|
|
{
|
|
|
|
/// function(unit, offset, timestamp)
|
|
|
|
if (pos->type != TokenType::Comma)
|
|
|
|
return false;
|
|
|
|
++pos;
|
2018-12-19 11:37:14 +00:00
|
|
|
|
2018-12-20 08:22:24 +00:00
|
|
|
if (!ParserExpression().parse(pos, offset_node, expected))
|
|
|
|
return false;
|
2018-12-19 11:37:14 +00:00
|
|
|
|
2018-12-20 08:22:24 +00:00
|
|
|
if (pos->type != TokenType::Comma)
|
|
|
|
return false;
|
|
|
|
++pos;
|
2018-12-19 11:37:14 +00:00
|
|
|
|
2018-12-20 08:22:24 +00:00
|
|
|
if (!ParserExpression().parse(pos, timestamp_node, expected))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// function(timestamp, INTERVAL offset unit)
|
|
|
|
if (!ParserExpression().parse(pos, timestamp_node, expected))
|
|
|
|
return false;
|
2018-12-19 11:37:14 +00:00
|
|
|
|
2018-12-20 08:22:24 +00:00
|
|
|
if (pos->type != TokenType::Comma)
|
|
|
|
return false;
|
|
|
|
++pos;
|
2018-12-19 11:37:14 +00:00
|
|
|
|
2018-12-20 08:22:24 +00:00
|
|
|
if (!ParserKeyword("INTERVAL").ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!ParserExpression().parse(pos, offset_node, expected))
|
|
|
|
return false;
|
2018-12-19 11:37:14 +00:00
|
|
|
|
2019-11-16 13:54:52 +00:00
|
|
|
if (!parseIntervalKind(pos, expected, interval_kind))
|
|
|
|
return false;
|
2018-12-20 08:22:24 +00:00
|
|
|
}
|
2018-12-19 11:37:14 +00:00
|
|
|
if (pos->type != TokenType::ClosingRoundBracket)
|
|
|
|
return false;
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
auto interval_expr_list_args = std::make_shared<ASTExpressionList>();
|
2018-12-20 08:22:24 +00:00
|
|
|
interval_expr_list_args->children = {offset_node};
|
2018-12-19 11:37:14 +00:00
|
|
|
|
|
|
|
auto interval_func_node = std::make_shared<ASTFunction>();
|
2019-11-16 13:54:52 +00:00
|
|
|
interval_func_node->name = interval_kind.toNameOfFunctionToIntervalDataType();
|
2018-12-19 11:37:14 +00:00
|
|
|
interval_func_node->arguments = std::move(interval_expr_list_args);
|
|
|
|
interval_func_node->children.push_back(interval_func_node->arguments);
|
|
|
|
|
|
|
|
auto expr_list_args = std::make_shared<ASTExpressionList>();
|
2018-12-20 08:22:24 +00:00
|
|
|
expr_list_args->children = {timestamp_node, interval_func_node};
|
2018-12-19 11:37:14 +00:00
|
|
|
|
|
|
|
auto func_node = std::make_shared<ASTFunction>();
|
|
|
|
func_node->name = function_name;
|
|
|
|
func_node->arguments = std::move(expr_list_args);
|
|
|
|
func_node->children.push_back(func_node->arguments);
|
|
|
|
|
|
|
|
node = std::move(func_node);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-18 14:40:30 +00:00
|
|
|
bool ParserDateDiffExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
|
|
|
ASTPtr left_node;
|
|
|
|
ASTPtr right_node;
|
|
|
|
|
2018-12-19 11:37:14 +00:00
|
|
|
if (!(ParserKeyword("DATEDIFF").ignore(pos, expected) || ParserKeyword("DATE_DIFF").ignore(pos, expected)
|
|
|
|
|| ParserKeyword("TIMESTAMPDIFF").ignore(pos, expected) || ParserKeyword("TIMESTAMP_DIFF").ignore(pos, expected)))
|
2018-12-18 14:40:30 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (pos->type != TokenType::OpeningRoundBracket)
|
|
|
|
return false;
|
|
|
|
++pos;
|
|
|
|
|
2019-11-16 13:54:52 +00:00
|
|
|
IntervalKind interval_kind;
|
|
|
|
if (!parseIntervalKind(pos, expected, interval_kind))
|
2018-12-18 14:40:30 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (pos->type != TokenType::Comma)
|
|
|
|
return false;
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
if (!ParserExpression().parse(pos, left_node, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (pos->type != TokenType::Comma)
|
|
|
|
return false;
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
if (!ParserExpression().parse(pos, right_node, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (pos->type != TokenType::ClosingRoundBracket)
|
|
|
|
return false;
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
auto expr_list_args = std::make_shared<ASTExpressionList>();
|
2019-11-16 13:54:52 +00:00
|
|
|
expr_list_args->children = {std::make_shared<ASTLiteral>(interval_kind.toDateDiffUnit()), left_node, right_node};
|
2018-12-18 14:40:30 +00:00
|
|
|
|
|
|
|
auto func_node = std::make_shared<ASTFunction>();
|
|
|
|
func_node->name = "dateDiff";
|
|
|
|
func_node->arguments = std::move(expr_list_args);
|
|
|
|
func_node->children.push_back(func_node->arguments);
|
|
|
|
|
|
|
|
node = std::move(func_node);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-24 19:12:10 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserNull::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2010-06-24 19:12:10 +00:00
|
|
|
{
|
2017-06-18 03:07:03 +00:00
|
|
|
ParserKeyword nested_parser("NULL");
|
2017-07-10 03:28:12 +00:00
|
|
|
if (nested_parser.parse(pos, node, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-02-26 03:37:08 +00:00
|
|
|
node = std::make_shared<ASTLiteral>(Null());
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
2010-06-24 19:12:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserNumber::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2010-06-24 19:12:10 +00:00
|
|
|
{
|
2019-05-22 00:57:34 +00:00
|
|
|
Pos literal_begin = pos;
|
2017-07-12 19:20:57 +00:00
|
|
|
bool negative = false;
|
|
|
|
|
|
|
|
if (pos->type == TokenType::Minus)
|
|
|
|
{
|
|
|
|
++pos;
|
|
|
|
negative = true;
|
|
|
|
}
|
2017-07-12 19:23:10 +00:00
|
|
|
else if (pos->type == TokenType::Plus) /// Leading plus is simply ignored.
|
|
|
|
++pos;
|
2017-07-12 19:20:57 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Field res;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!pos.isValid())
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/** Maximum length of number. 319 symbols is enough to write maximum double in decimal form.
|
|
|
|
* Copy is needed to use strto* functions, which require 0-terminated string.
|
|
|
|
*/
|
2017-07-10 03:28:12 +00:00
|
|
|
static constexpr size_t MAX_LENGTH_OF_NUMBER = 319;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (pos->size() > MAX_LENGTH_OF_NUMBER)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-13 04:20:56 +00:00
|
|
|
expected.add(pos, "number");
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
char buf[MAX_LENGTH_OF_NUMBER + 1];
|
|
|
|
|
|
|
|
memcpy(buf, pos->begin, pos->size());
|
|
|
|
buf[pos->size()] = 0;
|
|
|
|
|
|
|
|
char * pos_double = buf;
|
|
|
|
errno = 0; /// Functions strto* don't clear errno.
|
|
|
|
Float64 float_value = std::strtod(buf, &pos_double);
|
2017-07-13 01:54:51 +00:00
|
|
|
if (pos_double != buf + pos->size() || errno == ERANGE)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-13 04:20:56 +00:00
|
|
|
expected.add(pos, "number");
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-12 19:20:57 +00:00
|
|
|
if (float_value < 0)
|
|
|
|
throw Exception("Logical error: token number cannot begin with minus, but parsed float number is less than zero.", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
if (negative)
|
|
|
|
float_value = -float_value;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
res = float_value;
|
|
|
|
|
2017-07-12 19:20:57 +00:00
|
|
|
/// try to use more exact type: UInt64
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
char * pos_integer = buf;
|
2017-07-12 19:20:57 +00:00
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
UInt64 uint_value = std::strtoull(buf, &pos_integer, 0);
|
2017-07-13 00:01:17 +00:00
|
|
|
if (pos_integer == pos_double && errno != ERANGE && (!negative || uint_value <= (1ULL << 63)))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-12 19:20:57 +00:00
|
|
|
if (negative)
|
2018-12-26 03:21:39 +00:00
|
|
|
res = static_cast<Int64>(-uint_value);
|
2017-07-12 19:20:57 +00:00
|
|
|
else
|
2017-04-01 07:20:54 +00:00
|
|
|
res = uint_value;
|
|
|
|
}
|
|
|
|
|
2019-05-22 00:57:34 +00:00
|
|
|
auto literal = std::make_shared<ASTLiteral>(res);
|
|
|
|
literal->begin = literal_begin;
|
|
|
|
literal->end = ++pos;
|
|
|
|
node = literal;
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2010-06-24 19:12:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserUnsignedInteger::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2015-11-07 23:18:39 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Field res;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!pos.isValid())
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
UInt64 x = 0;
|
2017-07-10 03:28:12 +00:00
|
|
|
ReadBufferFromMemory in(pos->begin, pos->size());
|
2017-07-13 01:54:51 +00:00
|
|
|
if (!tryReadIntText(x, in) || in.count() != pos->size())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-13 04:20:56 +00:00
|
|
|
expected.add(pos, "unsigned integer");
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = x;
|
2019-05-22 00:57:34 +00:00
|
|
|
auto literal = std::make_shared<ASTLiteral>(res);
|
|
|
|
literal->begin = pos;
|
|
|
|
literal->end = ++pos;
|
|
|
|
node = literal;
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2015-11-07 23:18:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserStringLiteral::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2010-06-24 19:12:10 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (pos->type != TokenType::StringLiteral)
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
String s;
|
|
|
|
ReadBufferFromMemory in(pos->begin, pos->size());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2017-06-25 03:43:37 +00:00
|
|
|
readQuotedStringWithSQLStyle(s, in);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2018-08-26 01:33:20 +00:00
|
|
|
catch (const Exception &)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-13 04:20:56 +00:00
|
|
|
expected.add(pos, "string literal");
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-13 01:54:51 +00:00
|
|
|
if (in.count() != pos->size())
|
|
|
|
{
|
2017-07-13 04:20:56 +00:00
|
|
|
expected.add(pos, "string literal");
|
2017-07-13 01:54:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-22 00:57:34 +00:00
|
|
|
auto literal = std::make_shared<ASTLiteral>(s);
|
|
|
|
literal->begin = pos;
|
|
|
|
literal->end = ++pos;
|
|
|
|
node = literal;
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2010-06-24 19:12:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserArrayOfLiterals::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2015-06-29 04:54:52 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (pos->type != TokenType::OpeningSquareBracket)
|
|
|
|
return false;
|
|
|
|
|
2019-05-22 00:57:34 +00:00
|
|
|
Pos literal_begin = pos;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Array arr;
|
|
|
|
|
|
|
|
ParserLiteral literal_p;
|
|
|
|
|
|
|
|
++pos;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
while (pos.isValid())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (!arr.empty())
|
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (pos->type == TokenType::ClosingSquareBracket)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-05-22 00:57:34 +00:00
|
|
|
auto literal = std::make_shared<ASTLiteral>(arr);
|
|
|
|
literal->begin = literal_begin;
|
|
|
|
literal->end = ++pos;
|
|
|
|
node = literal;
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-07-10 03:28:12 +00:00
|
|
|
else if (pos->type == TokenType::Comma)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-13 04:20:56 +00:00
|
|
|
expected.add(pos, "comma or closing square bracket");
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTPtr literal_node;
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!literal_p.parse(pos, literal_node, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
2019-03-15 16:14:13 +00:00
|
|
|
arr.push_back(literal_node->as<ASTLiteral &>().value);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 04:20:56 +00:00
|
|
|
expected.add(pos, "closing square bracket");
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2015-06-29 04:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserLiteral::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2010-06-24 19:12:10 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ParserNull null_p;
|
|
|
|
ParserNumber num_p;
|
|
|
|
ParserStringLiteral str_p;
|
2010-06-24 19:12:10 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (null_p.parse(pos, node, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2010-06-24 19:12:10 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (num_p.parse(pos, node, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2010-06-24 19:12:10 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (str_p.parse(pos, node, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2010-06-24 19:12:10 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2010-06-24 19:12:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-07 20:25:38 +00:00
|
|
|
const char * ParserAlias::restricted_keywords[] =
|
2015-11-08 00:28:12 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
"FROM",
|
|
|
|
"FINAL",
|
|
|
|
"SAMPLE",
|
|
|
|
"ARRAY",
|
|
|
|
"LEFT",
|
|
|
|
"RIGHT",
|
|
|
|
"INNER",
|
|
|
|
"FULL",
|
|
|
|
"CROSS",
|
|
|
|
"JOIN",
|
|
|
|
"GLOBAL",
|
|
|
|
"ANY",
|
|
|
|
"ALL",
|
2019-12-02 18:07:27 +00:00
|
|
|
"ASOF",
|
|
|
|
"SEMI",
|
|
|
|
"ANTI",
|
|
|
|
"ONLY", /// YQL synonym for ANTI
|
2017-04-01 07:20:54 +00:00
|
|
|
"ON",
|
|
|
|
"USING",
|
|
|
|
"PREWHERE",
|
|
|
|
"WHERE",
|
|
|
|
"GROUP",
|
|
|
|
"WITH",
|
|
|
|
"HAVING",
|
|
|
|
"ORDER",
|
|
|
|
"LIMIT",
|
|
|
|
"SETTINGS",
|
|
|
|
"FORMAT",
|
|
|
|
"UNION",
|
|
|
|
"INTO",
|
2019-02-10 20:18:47 +00:00
|
|
|
"NOT",
|
|
|
|
"BETWEEN",
|
|
|
|
"LIKE",
|
2017-04-01 07:20:54 +00:00
|
|
|
nullptr
|
2015-11-08 00:28:12 +00:00
|
|
|
};
|
|
|
|
|
2018-06-07 20:25:38 +00:00
|
|
|
bool ParserAlias::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2011-11-06 04:21:09 +00:00
|
|
|
{
|
2017-06-18 03:07:03 +00:00
|
|
|
ParserKeyword s_as("AS");
|
2017-04-01 07:20:54 +00:00
|
|
|
ParserIdentifier id_p;
|
2011-11-06 04:21:09 +00:00
|
|
|
|
2018-02-26 04:47:27 +00:00
|
|
|
bool has_as_word = s_as.ignore(pos, expected);
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!allow_alias_without_as_keyword && !has_as_word)
|
|
|
|
return false;
|
2011-11-06 04:21:09 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!id_p.parse(pos, node, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2011-11-06 04:21:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!has_as_word)
|
|
|
|
{
|
2017-04-02 17:37:49 +00:00
|
|
|
/** In this case, the alias can not match the keyword -
|
|
|
|
* so that in the query "SELECT x FROM t", the word FROM was not considered an alias,
|
|
|
|
* and in the query "SELECT x FRO FROM t", the word FRO was considered an alias.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
2015-11-08 00:28:12 +00:00
|
|
|
|
2019-08-08 20:02:30 +00:00
|
|
|
const String name = getIdentifierName(node);
|
2015-11-08 00:28:12 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (const char ** keyword = restricted_keywords; *keyword != nullptr; ++keyword)
|
|
|
|
if (0 == strcasecmp(name.data(), *keyword))
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-08 00:28:12 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2011-11-06 04:21:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-21 17:03:58 +00:00
|
|
|
bool ParserColumnsMatcher::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2019-07-12 11:17:38 +00:00
|
|
|
{
|
|
|
|
ParserKeyword columns("COLUMNS");
|
|
|
|
ParserStringLiteral regex;
|
|
|
|
|
|
|
|
if (!columns.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
2019-07-16 20:05:00 +00:00
|
|
|
if (pos->type != TokenType::OpeningRoundBracket)
|
|
|
|
return false;
|
|
|
|
++pos;
|
|
|
|
|
2019-07-12 11:17:38 +00:00
|
|
|
ASTPtr regex_node;
|
|
|
|
if (!regex.parse(pos, regex_node, expected))
|
|
|
|
return false;
|
|
|
|
|
2019-07-16 20:05:00 +00:00
|
|
|
if (pos->type != TokenType::ClosingRoundBracket)
|
|
|
|
return false;
|
|
|
|
++pos;
|
|
|
|
|
2019-07-21 17:03:58 +00:00
|
|
|
auto res = std::make_shared<ASTColumnsMatcher>();
|
2019-07-12 11:17:38 +00:00
|
|
|
res->setPattern(regex_node->as<ASTLiteral &>().value.get<String>());
|
|
|
|
res->children.push_back(regex_node);
|
|
|
|
node = std::move(res);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-16 20:05:00 +00:00
|
|
|
|
2017-12-01 18:36:55 +00:00
|
|
|
bool ParserAsterisk::parseImpl(Pos & pos, ASTPtr & node, Expected &)
|
2017-01-04 11:23:27 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (pos->type == TokenType::Asterisk)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
++pos;
|
2018-02-26 03:37:08 +00:00
|
|
|
node = std::make_shared<ASTAsterisk>();
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2017-01-04 11:23:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserQualifiedAsterisk::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2010-06-24 19:12:10 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!ParserCompoundIdentifier().parse(pos, node, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2017-01-04 11:23:27 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (pos->type != TokenType::Dot)
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2017-07-10 03:28:12 +00:00
|
|
|
++pos;
|
2017-01-04 11:23:27 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (pos->type != TokenType::Asterisk)
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2017-07-10 03:28:12 +00:00
|
|
|
++pos;
|
2017-01-04 11:23:27 +00:00
|
|
|
|
2018-02-26 03:37:08 +00:00
|
|
|
auto res = std::make_shared<ASTQualifiedAsterisk>();
|
2017-04-01 07:20:54 +00:00
|
|
|
res->children.push_back(node);
|
|
|
|
node = std::move(res);
|
|
|
|
return true;
|
2017-01-04 11:23:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-25 13:43:52 +00:00
|
|
|
bool ParserSubstitution::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2019-05-18 21:07:23 +00:00
|
|
|
{
|
2019-05-25 13:43:52 +00:00
|
|
|
if (pos->type != TokenType::OpeningCurlyBrace)
|
2019-05-18 21:07:23 +00:00
|
|
|
return false;
|
|
|
|
|
2019-05-25 13:43:52 +00:00
|
|
|
++pos;
|
2019-05-18 21:07:23 +00:00
|
|
|
|
2019-05-25 13:43:52 +00:00
|
|
|
if (pos->type != TokenType::BareWord)
|
|
|
|
{
|
2019-06-04 18:15:32 +00:00
|
|
|
expected.add(pos, "substitution name (identifier)");
|
2019-05-25 13:43:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-14 16:24:56 +00:00
|
|
|
String name(pos->begin, pos->end);
|
2019-05-25 13:43:52 +00:00
|
|
|
++pos;
|
2019-05-18 21:07:23 +00:00
|
|
|
|
|
|
|
if (pos->type != TokenType::Colon)
|
|
|
|
{
|
|
|
|
expected.add(pos, "colon between name and type");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-25 13:43:52 +00:00
|
|
|
++pos;
|
2019-05-18 21:07:23 +00:00
|
|
|
|
2019-06-14 18:38:58 +00:00
|
|
|
auto old_pos = pos;
|
|
|
|
ParserIdentifierWithOptionalParameters type_parser;
|
|
|
|
if (!type_parser.ignore(pos, expected))
|
2019-05-25 13:43:52 +00:00
|
|
|
{
|
2019-06-04 18:15:32 +00:00
|
|
|
expected.add(pos, "substitution type");
|
2019-05-25 13:43:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-14 16:24:56 +00:00
|
|
|
String type(old_pos->begin, pos->begin);
|
2019-05-18 21:07:23 +00:00
|
|
|
|
2019-05-25 13:43:52 +00:00
|
|
|
if (pos->type != TokenType::ClosingCurlyBrace)
|
2019-05-18 21:07:23 +00:00
|
|
|
{
|
2019-05-25 13:43:52 +00:00
|
|
|
expected.add(pos, "closing curly brace");
|
2019-05-18 21:07:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
++pos;
|
2019-05-25 13:43:52 +00:00
|
|
|
node = std::make_shared<ASTQueryParameter>(name, type);
|
2019-05-18 21:07:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserExpressionElement::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2017-01-04 11:23:27 +00:00
|
|
|
{
|
2018-06-07 20:25:38 +00:00
|
|
|
return ParserSubquery().parse(pos, node, expected)
|
|
|
|
|| ParserParenthesisExpression().parse(pos, node, expected)
|
|
|
|
|| ParserArrayOfLiterals().parse(pos, node, expected)
|
|
|
|
|| ParserArray().parse(pos, node, expected)
|
|
|
|
|| ParserLiteral().parse(pos, node, expected)
|
|
|
|
|| ParserCastExpression().parse(pos, node, expected)
|
2018-12-10 12:54:02 +00:00
|
|
|
|| ParserExtractExpression().parse(pos, node, expected)
|
2018-12-19 11:37:14 +00:00
|
|
|
|| ParserDateAddExpression().parse(pos, node, expected)
|
2018-12-18 14:40:30 +00:00
|
|
|
|| ParserDateDiffExpression().parse(pos, node, expected)
|
2018-12-10 12:54:02 +00:00
|
|
|
|| ParserSubstringExpression().parse(pos, node, expected)
|
2018-12-13 11:50:37 +00:00
|
|
|
|| ParserTrimExpression().parse(pos, node, expected)
|
2018-12-13 09:34:12 +00:00
|
|
|
|| ParserLeftExpression().parse(pos, node, expected)
|
|
|
|
|| ParserRightExpression().parse(pos, node, expected)
|
2018-06-07 20:25:38 +00:00
|
|
|
|| ParserCase().parse(pos, node, expected)
|
2019-07-21 17:03:58 +00:00
|
|
|
|| ParserColumnsMatcher().parse(pos, node, expected) /// before ParserFunction because it can be also parsed as a function.
|
2018-06-07 20:25:38 +00:00
|
|
|
|| ParserFunction().parse(pos, node, expected)
|
|
|
|
|| ParserQualifiedAsterisk().parse(pos, node, expected)
|
|
|
|
|| ParserAsterisk().parse(pos, node, expected)
|
2019-05-18 21:07:23 +00:00
|
|
|
|| ParserCompoundIdentifier().parse(pos, node, expected)
|
2019-05-25 13:43:52 +00:00
|
|
|
|| ParserSubstitution().parse(pos, node, expected);
|
2010-06-24 19:12:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-07 20:25:38 +00:00
|
|
|
bool ParserWithOptionalAlias::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2011-11-06 04:21:09 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!elem_parser->parse(pos, node, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/** Little hack.
|
2017-04-01 07:20:54 +00:00
|
|
|
*
|
2017-04-02 17:37:49 +00:00
|
|
|
* In the SELECT section, we allow parsing aliases without specifying the AS keyword.
|
|
|
|
* These aliases can not be the same as the query keywords.
|
|
|
|
* And the expression itself can be an identifier that matches the keyword.
|
|
|
|
* For example, a column may be called where. And in the query it can be written `SELECT where AS x FROM table` or even `SELECT where x FROM table`.
|
|
|
|
* Even can be written `SELECT where AS from FROM table`, but it can not be written `SELECT where from FROM table`.
|
|
|
|
* See the ParserAlias implementation for details.
|
2017-04-01 07:20:54 +00:00
|
|
|
*
|
2017-04-02 17:37:49 +00:00
|
|
|
* But there is a small problem - an inconvenient error message if there is an extra comma in the SELECT section at the end.
|
|
|
|
* Although this error is very common. Example: `SELECT x, y, z, FROM tbl`
|
|
|
|
* If you do nothing, it's parsed as a column with the name FROM and alias tbl.
|
|
|
|
* To avoid this situation, we do not allow the parsing of the alias without the AS keyword for the identifier with the name FROM.
|
2017-04-01 07:20:54 +00:00
|
|
|
*
|
2017-04-02 17:37:49 +00:00
|
|
|
* Note: this also filters the case when the identifier is quoted.
|
|
|
|
* Example: SELECT x, y, z, `FROM` tbl. But such a case could be solved.
|
2017-04-01 07:20:54 +00:00
|
|
|
*
|
2017-04-02 17:37:49 +00:00
|
|
|
* In the future it would be easier to disallow unquoted identifiers that match the keywords.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
|
|
|
bool allow_alias_without_as_keyword_now = allow_alias_without_as_keyword;
|
|
|
|
if (allow_alias_without_as_keyword)
|
2019-08-08 20:02:30 +00:00
|
|
|
if (auto opt_id = tryGetIdentifierName(node))
|
2019-01-14 18:15:04 +00:00
|
|
|
if (0 == strcasecmp(opt_id->data(), "FROM"))
|
2017-04-01 07:20:54 +00:00
|
|
|
allow_alias_without_as_keyword_now = false;
|
|
|
|
|
|
|
|
ASTPtr alias_node;
|
2017-07-10 03:28:12 +00:00
|
|
|
if (ParserAlias(allow_alias_without_as_keyword_now).parse(pos, alias_node, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-03-12 12:41:57 +00:00
|
|
|
/// FIXME: try to prettify this cast using `as<>()`
|
|
|
|
if (auto * ast_with_alias = dynamic_cast<ASTWithAlias *>(node.get()))
|
2017-08-10 14:46:46 +00:00
|
|
|
{
|
2019-08-08 20:02:30 +00:00
|
|
|
tryGetIdentifierNameInto(alias_node, ast_with_alias->alias);
|
2017-08-10 14:46:46 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
|
|
|
{
|
2017-07-13 04:20:56 +00:00
|
|
|
expected.add(pos, "alias cannot be here");
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2011-11-06 04:21:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserOrderByElement::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2011-09-04 05:14:52 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ParserExpressionWithOptionalAlias elem_p(false);
|
2017-06-18 03:07:03 +00:00
|
|
|
ParserKeyword ascending("ASCENDING");
|
|
|
|
ParserKeyword descending("DESCENDING");
|
|
|
|
ParserKeyword asc("ASC");
|
|
|
|
ParserKeyword desc("DESC");
|
|
|
|
ParserKeyword nulls("NULLS");
|
|
|
|
ParserKeyword first("FIRST");
|
|
|
|
ParserKeyword last("LAST");
|
|
|
|
ParserKeyword collate("COLLATE");
|
2019-04-21 03:36:59 +00:00
|
|
|
ParserKeyword with_fill("WITH FILL");
|
|
|
|
ParserKeyword from("FROM");
|
|
|
|
ParserKeyword to("TO");
|
|
|
|
ParserKeyword step("STEP");
|
2017-04-01 07:20:54 +00:00
|
|
|
ParserStringLiteral collate_locale_parser;
|
2019-04-21 03:36:59 +00:00
|
|
|
ParserExpressionWithOptionalAlias exp_parser(false);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
ASTPtr expr_elem;
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!elem_p.parse(pos, expr_elem, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
int direction = 1;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (descending.ignore(pos) || desc.ignore(pos))
|
2017-04-01 07:20:54 +00:00
|
|
|
direction = -1;
|
|
|
|
else
|
2017-07-10 03:28:12 +00:00
|
|
|
ascending.ignore(pos) || asc.ignore(pos);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
int nulls_direction = direction;
|
|
|
|
bool nulls_direction_was_explicitly_specified = false;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (nulls.ignore(pos))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
nulls_direction_was_explicitly_specified = true;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (first.ignore(pos))
|
2017-04-01 07:20:54 +00:00
|
|
|
nulls_direction = -direction;
|
2017-07-10 03:28:12 +00:00
|
|
|
else if (last.ignore(pos))
|
2017-04-01 07:20:54 +00:00
|
|
|
;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTPtr locale_node;
|
2017-07-10 03:28:12 +00:00
|
|
|
if (collate.ignore(pos))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!collate_locale_parser.parse(pos, locale_node, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-21 03:36:59 +00:00
|
|
|
/// WITH FILL [FROM x] [TO y] [STEP z]
|
2019-08-19 20:22:45 +00:00
|
|
|
bool has_with_fill = false;
|
2019-04-21 03:36:59 +00:00
|
|
|
ASTPtr fill_from;
|
|
|
|
ASTPtr fill_to;
|
|
|
|
ASTPtr fill_step;
|
|
|
|
if (with_fill.ignore(pos))
|
|
|
|
{
|
2019-08-19 20:22:45 +00:00
|
|
|
has_with_fill = true;
|
2019-04-21 03:36:59 +00:00
|
|
|
if (from.ignore(pos) && !exp_parser.parse(pos, fill_from, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (to.ignore(pos) && !exp_parser.parse(pos, fill_to, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (step.ignore(pos) && !exp_parser.parse(pos, fill_step, expected))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
node = std::make_shared<ASTOrderByElement>(
|
|
|
|
direction, nulls_direction, nulls_direction_was_explicitly_specified, locale_node,
|
2019-08-19 20:22:45 +00:00
|
|
|
has_with_fill, fill_from, fill_to, fill_step);
|
2017-04-01 07:20:54 +00:00
|
|
|
node->children.push_back(expr_elem);
|
|
|
|
if (locale_node)
|
|
|
|
node->children.push_back(locale_node);
|
|
|
|
|
|
|
|
return true;
|
2011-09-04 05:14:52 +00:00
|
|
|
}
|
|
|
|
|
2019-10-07 16:23:16 +00:00
|
|
|
bool ParserFunctionWithKeyValueArguments::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
|
|
|
ParserIdentifier id_parser;
|
|
|
|
ParserKeyValuePairsList pairs_list_parser;
|
|
|
|
|
|
|
|
ASTPtr identifier;
|
|
|
|
ASTPtr expr_list_args;
|
|
|
|
if (!id_parser.parse(pos, identifier, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (pos.get().type != TokenType::OpeningRoundBracket)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
++pos;
|
|
|
|
if (!pairs_list_parser.parse(pos, expr_list_args, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (pos.get().type != TokenType::ClosingRoundBracket)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
++pos;
|
|
|
|
auto function = std::make_shared<ASTFunctionWithKeyValueArguments>();
|
|
|
|
function->name = Poco::toLower(typeid_cast<ASTIdentifier &>(*identifier.get()).name);
|
|
|
|
function->elements = expr_list_args;
|
|
|
|
function->children.push_back(function->elements);
|
|
|
|
node = function;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ParserIdentifierWithOptionalParameters::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
|
|
|
ParserIdentifier non_parametric;
|
|
|
|
ParserIdentifierWithParameters parametric;
|
|
|
|
|
|
|
|
if (parametric.parse(pos, node, expected))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
ASTPtr ident;
|
|
|
|
if (non_parametric.parse(pos, ident, expected))
|
|
|
|
{
|
|
|
|
auto func = std::make_shared<ASTFunction>();
|
|
|
|
tryGetIdentifierNameInto(ident, func->name);
|
|
|
|
node = func;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-06-24 19:12:10 +00:00
|
|
|
}
|