2011-09-04 05:14:52 +00:00
|
|
|
#pragma once
|
2010-06-24 19:12:10 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/IParserBase.h>
|
2010-06-24 19:12:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
class ParserArray : public IParserBase
|
|
|
|
{
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * getName() const { return "array"; }
|
2017-07-10 03:28:12 +00:00
|
|
|
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
|
2010-06-24 19:12:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-05-27 17:29:55 +00:00
|
|
|
/** If in parenthesis an expression from one element - returns this element in `node`;
|
|
|
|
* or if there is a SELECT subquery in parenthesis, then this subquery returned in `node`;
|
|
|
|
* otherwise returns `tuple` function from the contents of brackets.
|
2010-06-24 19:12:10 +00:00
|
|
|
*/
|
|
|
|
class ParserParenthesisExpression : public IParserBase
|
|
|
|
{
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * getName() const { return "parenthesized expression"; }
|
2017-07-10 03:28:12 +00:00
|
|
|
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
|
2010-06-24 19:12:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-05-27 17:29:55 +00:00
|
|
|
/** The SELECT subquery is in parenthesis.
|
2012-08-22 18:46:09 +00:00
|
|
|
*/
|
|
|
|
class ParserSubquery : public IParserBase
|
|
|
|
{
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * getName() const { return "SELECT subquery"; }
|
2017-07-10 03:28:12 +00:00
|
|
|
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
|
2012-08-22 18:46:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-05-27 17:29:55 +00:00
|
|
|
/** An identifier, for example, x_yz123 or `something special`
|
2010-06-24 19:12:10 +00:00
|
|
|
*/
|
|
|
|
class ParserIdentifier : public IParserBase
|
|
|
|
{
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * getName() const { return "identifier"; }
|
2017-07-10 03:28:12 +00:00
|
|
|
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
|
2010-06-24 19:12:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-05-27 17:29:55 +00:00
|
|
|
/** An identifier, possibly containing a dot, for example, x_yz123 or `something special` or Hits.EventTime
|
2013-07-16 15:26:35 +00:00
|
|
|
*/
|
|
|
|
class ParserCompoundIdentifier : public IParserBase
|
|
|
|
{
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * getName() const { return "compound identifier"; }
|
2017-07-10 03:28:12 +00:00
|
|
|
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
|
2013-07-16 15:26:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-01-04 11:23:27 +00:00
|
|
|
/// Just *
|
|
|
|
class ParserAsterisk : public IParserBase
|
|
|
|
{
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * getName() const { return "asterisk"; }
|
2017-07-10 03:28:12 +00:00
|
|
|
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
|
2017-01-04 11:23:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** Something like t.* or db.table.*
|
|
|
|
*/
|
|
|
|
class ParserQualifiedAsterisk : public IParserBase
|
|
|
|
{
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * getName() const { return "qualified asterisk"; }
|
2017-07-10 03:28:12 +00:00
|
|
|
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
|
2017-01-04 11:23:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-05-27 17:29:55 +00:00
|
|
|
/** A function, for example, f(x, y + 1, g(z)).
|
|
|
|
* Or an aggregate function: sum(x + f(y)), corr(x, y). The syntax is the same as the usual function.
|
|
|
|
* Or a parametric aggregate function: quantile(0.9)(x + y).
|
|
|
|
* Syntax - two pairs of parentheses instead of one. The first is for parameters, the second for arguments.
|
|
|
|
* For functions, the DISTINCT modifier can be specified, for example, count(DISTINCT x, y).
|
2010-06-24 19:12:10 +00:00
|
|
|
*/
|
|
|
|
class ParserFunction : public IParserBase
|
|
|
|
{
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * getName() const { return "function"; }
|
2017-07-10 03:28:12 +00:00
|
|
|
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
|
2010-06-24 19:12:10 +00:00
|
|
|
};
|
|
|
|
|
2015-12-24 17:14:10 +00:00
|
|
|
class ParserCastExpression : public IParserBase
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// this name is used for identifying CAST expression among other function calls
|
|
|
|
static constexpr auto name = "CAST";
|
2015-12-24 17:14:10 +00:00
|
|
|
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * getName() const override { return name; }
|
2017-07-10 03:28:12 +00:00
|
|
|
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
|
2015-12-24 17:14:10 +00:00
|
|
|
};
|
|
|
|
|
2018-04-13 19:35:08 +00:00
|
|
|
class ParserExtractExpression : public IParserBase
|
|
|
|
{
|
|
|
|
static constexpr auto name = "EXTRACT";
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const char * getName() const override { return name; }
|
|
|
|
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
|
|
|
|
};
|
|
|
|
|
2010-06-24 19:12:10 +00:00
|
|
|
|
2017-01-23 15:55:56 +00:00
|
|
|
/** NULL literal.
|
2010-06-24 19:12:10 +00:00
|
|
|
*/
|
|
|
|
class ParserNull : public IParserBase
|
|
|
|
{
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * getName() const { return "NULL"; }
|
2017-07-10 03:28:12 +00:00
|
|
|
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
|
2010-06-24 19:12:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-01-23 15:55:56 +00:00
|
|
|
/** Numeric literal.
|
2010-06-24 19:12:10 +00:00
|
|
|
*/
|
|
|
|
class ParserNumber : public IParserBase
|
|
|
|
{
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * getName() const { return "number"; }
|
2017-07-10 03:28:12 +00:00
|
|
|
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
|
2010-06-24 19:12:10 +00:00
|
|
|
};
|
|
|
|
|
2017-01-23 15:55:56 +00:00
|
|
|
/** Unsigned integer, used in right hand side of tuple access operator (x.1).
|
2015-11-07 23:18:39 +00:00
|
|
|
*/
|
|
|
|
class ParserUnsignedInteger : public IParserBase
|
|
|
|
{
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * getName() const { return "unsigned integer"; }
|
2017-07-10 03:28:12 +00:00
|
|
|
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
|
2015-11-07 23:18:39 +00:00
|
|
|
};
|
|
|
|
|
2010-06-24 19:12:10 +00:00
|
|
|
|
2017-01-23 15:55:56 +00:00
|
|
|
/** String in single quotes.
|
2010-06-24 19:12:10 +00:00
|
|
|
*/
|
|
|
|
class ParserStringLiteral : public IParserBase
|
|
|
|
{
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * getName() const { return "string literal"; }
|
2017-07-10 03:28:12 +00:00
|
|
|
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
|
2015-06-29 04:54:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-05-27 17:29:55 +00:00
|
|
|
/** An array of literals.
|
|
|
|
* Arrays can also be parsed as an application of [] operator.
|
|
|
|
* But parsing the whole array as a whole constant seriously speeds up the analysis of expressions in the case of very large arrays.
|
|
|
|
* We try to parse the array as an array of literals first (fast path),
|
|
|
|
* and if it did not work out (when the array consists of complex expressions) - parse as an application of [] operator (slow path).
|
2015-06-29 04:54:52 +00:00
|
|
|
*/
|
|
|
|
class ParserArrayOfLiterals : public IParserBase
|
|
|
|
{
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * getName() const { return "array"; }
|
2017-07-10 03:28:12 +00:00
|
|
|
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
|
2010-06-24 19:12:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-05-27 17:29:55 +00:00
|
|
|
/** The literal is one of: NULL, UInt64, Int64, Float64, String.
|
2010-06-24 19:12:10 +00:00
|
|
|
*/
|
|
|
|
class ParserLiteral : public IParserBase
|
|
|
|
{
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * getName() const { return "literal"; }
|
2017-07-10 03:28:12 +00:00
|
|
|
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
|
2010-06-24 19:12:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-05-27 17:29:55 +00:00
|
|
|
/** The alias is the identifier before which `AS` comes. For example: AS x_yz123.
|
2011-11-06 04:21:09 +00:00
|
|
|
*/
|
2015-12-24 17:14:10 +00:00
|
|
|
struct ParserAliasBase
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static const char * restricted_keywords[];
|
2015-12-24 17:14:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename ParserIdentifier>
|
|
|
|
class ParserAliasImpl : public IParserBase, ParserAliasBase
|
2011-11-06 04:21:09 +00:00
|
|
|
{
|
2015-11-08 00:28:12 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
ParserAliasImpl(bool allow_alias_without_as_keyword_)
|
|
|
|
: allow_alias_without_as_keyword(allow_alias_without_as_keyword_) {}
|
2011-11-06 04:21:09 +00:00
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
bool allow_alias_without_as_keyword;
|
2015-11-08 00:28:12 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * getName() const { return "alias"; }
|
2017-07-10 03:28:12 +00:00
|
|
|
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
|
2011-11-06 04:21:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-12-24 17:14:10 +00:00
|
|
|
class ParserTypeInCastExpression;
|
|
|
|
|
|
|
|
extern template class ParserAliasImpl<ParserIdentifier>;
|
|
|
|
extern template class ParserAliasImpl<ParserTypeInCastExpression>;
|
|
|
|
|
|
|
|
using ParserAlias = ParserAliasImpl<ParserIdentifier>;
|
|
|
|
using ParserCastExpressionAlias = ParserAliasImpl<ParserTypeInCastExpression>;
|
|
|
|
|
|
|
|
|
2017-05-27 17:29:55 +00:00
|
|
|
/** The expression element is one of: an expression in parentheses, an array, a literal, a function, an identifier, an asterisk.
|
2010-06-24 19:12:10 +00:00
|
|
|
*/
|
|
|
|
class ParserExpressionElement : public IParserBase
|
|
|
|
{
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * getName() const { return "element of expression"; }
|
2017-07-10 03:28:12 +00:00
|
|
|
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
|
2010-06-24 19:12:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-05-27 17:29:55 +00:00
|
|
|
/** An expression element, possibly with an alias, if appropriate.
|
2011-11-06 04:21:09 +00:00
|
|
|
*/
|
2015-12-24 17:14:10 +00:00
|
|
|
template <typename ParserAlias>
|
|
|
|
class ParserWithOptionalAliasImpl : public IParserBase
|
2011-11-06 04:21:09 +00:00
|
|
|
{
|
2011-11-06 20:47:07 +00:00
|
|
|
public:
|
2017-08-10 14:46:46 +00:00
|
|
|
ParserWithOptionalAliasImpl(ParserPtr && elem_parser_, bool allow_alias_without_as_keyword_, bool prefer_alias_to_column_name_ = false)
|
|
|
|
: elem_parser(std::move(elem_parser_)), allow_alias_without_as_keyword(allow_alias_without_as_keyword_),
|
|
|
|
prefer_alias_to_column_name(prefer_alias_to_column_name_) {}
|
2011-11-06 04:21:09 +00:00
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
ParserPtr elem_parser;
|
|
|
|
bool allow_alias_without_as_keyword;
|
2017-08-10 14:46:46 +00:00
|
|
|
bool prefer_alias_to_column_name;
|
2015-04-11 03:10:23 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * getName() const { return "element of expression with optional alias"; }
|
2017-07-10 03:28:12 +00:00
|
|
|
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
|
2011-11-06 04:21:09 +00:00
|
|
|
};
|
|
|
|
|
2015-12-24 17:14:10 +00:00
|
|
|
extern template class ParserWithOptionalAliasImpl<ParserAlias>;
|
|
|
|
extern template class ParserWithOptionalAliasImpl<ParserCastExpressionAlias>;
|
|
|
|
|
|
|
|
using ParserWithOptionalAlias = ParserWithOptionalAliasImpl<ParserAlias>;
|
|
|
|
using ParserCastExpressionWithOptionalAlias = ParserWithOptionalAliasImpl<ParserCastExpressionAlias>;
|
|
|
|
|
2011-11-06 04:21:09 +00:00
|
|
|
|
2017-03-12 12:56:59 +00:00
|
|
|
/** Element of ORDER BY expression - same as expression element, but in addition, ASC[ENDING] | DESC[ENDING] could be specified
|
|
|
|
* and optionally, NULLS LAST|FIRST
|
|
|
|
* and optionally, COLLATE 'locale'.
|
2011-09-04 05:14:52 +00:00
|
|
|
*/
|
|
|
|
class ParserOrderByElement : public IParserBase
|
|
|
|
{
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * getName() const { return "element of ORDER BY expression"; }
|
2017-07-10 03:28:12 +00:00
|
|
|
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
|
2011-09-04 05:14:52 +00:00
|
|
|
};
|
2010-06-24 19:12:10 +00:00
|
|
|
|
2011-09-04 05:14:52 +00:00
|
|
|
}
|