ClickHouse/dbms/include/DB/Parsers/ExpressionElementParsers.h

221 lines
7.9 KiB
C
Raw Normal View History

2011-09-04 05:14:52 +00:00
#pragma once
2010-06-24 19:12:10 +00:00
#include <DB/Parsers/IParserBase.h>
namespace DB
{
class ParserArray : public IParserBase
{
protected:
const char * getName() const { return "array"; }
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected);
2010-06-24 19:12:10 +00:00
};
/** Если в скобках выражение из одного элемента - возвращает в node этот элемент;
2012-08-22 18:46:09 +00:00
* или если в скобках - подзапрос SELECT - то возвращает в node этот подзапрос;
2010-06-24 19:12:10 +00:00
* иначе возвращает функцию tuple от содержимого скобок.
*/
class ParserParenthesisExpression : public IParserBase
{
protected:
const char * getName() const { return "parenthesized expression"; }
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected);
2010-06-24 19:12:10 +00:00
};
2012-08-22 18:46:09 +00:00
/** Подзапрос SELECT в скобках.
*/
class ParserSubquery : public IParserBase
{
protected:
const char * getName() const { return "SELECT subquery"; }
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected);
2012-08-22 18:46:09 +00:00
};
2011-11-01 17:57:37 +00:00
/** Идентификатор, например, x_yz123 или `something special`
2010-06-24 19:12:10 +00:00
*/
class ParserIdentifier : public IParserBase
{
protected:
const char * getName() const { return "identifier"; }
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected);
2010-06-24 19:12:10 +00:00
};
/** Идентификатор, возможно, содержащий точку, например, x_yz123 или `something special` или Hits.EventTime
*/
class ParserCompoundIdentifier : public IParserBase
{
protected:
const char * getName() const { return "compound identifier"; }
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected);
};
/** Функция, например, f(x, y + 1, g(z)).
* Или агрегатная функция: sum(x + f(y)), corr(x, y). По синтаксису - такая же, как обычная функция.
* Или параметрическая агрегатная функция: quantile(0.9)(x + y).
* Синтаксис - две пары круглых скобок вместо одной. Первая - для параметров, вторая - для аргументов.
2010-06-24 19:12:10 +00:00
*/
class ParserFunction : public IParserBase
{
protected:
const char * getName() const { return "function"; }
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected);
2010-06-24 19:12:10 +00:00
};
class ParserCastExpression : public IParserBase
{
/// this name is used for identifying CAST expression among other function calls
static constexpr auto name = "CAST";
protected:
const char * getName() const { return name; }
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected) override;
};
2010-06-24 19:12:10 +00:00
/** NULL.
*/
class ParserNull : public IParserBase
{
protected:
const char * getName() const { return "NULL"; }
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected);
2010-06-24 19:12:10 +00:00
};
/** Число.
*/
class ParserNumber : public IParserBase
{
protected:
const char * getName() const { return "number"; }
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected);
2010-06-24 19:12:10 +00:00
};
/** Беззнаковое целое число, используется в качестве правой части оператора взятия элемента кортежа (x.1).
*/
class ParserUnsignedInteger : public IParserBase
{
protected:
const char * getName() const { return "unsigned integer"; }
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected);
};
2010-06-24 19:12:10 +00:00
/** Строка в одинарных кавычках.
*/
class ParserStringLiteral : public IParserBase
{
protected:
const char * getName() const { return "string literal"; }
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected);
};
/** Массив литералов.
* Массивы могут распарситься и как применение оператора [].
* Но парсинг всего массива как целой константы серьёзно ускоряет анализ выражений в случае очень больших массивов.
* Мы пробуем распарсить массив как массив литералов сначала (fast path),
* а если не получилось (когда массив состоит из сложных выражений) - парсим как применение оператора [] (slow path).
*/
class ParserArrayOfLiterals : public IParserBase
{
protected:
const char * getName() const { return "array"; }
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected);
2010-06-24 19:12:10 +00:00
};
/** Литерал - одно из: NULL, UInt64, Int64, Float64, String.
*/
class ParserLiteral : public IParserBase
{
protected:
const char * getName() const { return "literal"; }
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected);
2010-06-24 19:12:10 +00:00
};
2011-11-06 04:21:09 +00:00
/** Алиас - идентификатор, перед которым идёт AS. Например: AS x_yz123.
*/
struct ParserAliasBase
{
static const char * restricted_keywords[];
};
template <typename ParserIdentifier>
class ParserAliasImpl : public IParserBase, ParserAliasBase
2011-11-06 04:21:09 +00:00
{
public:
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:
bool allow_alias_without_as_keyword;
const char * getName() const { return "alias"; }
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected);
2011-11-06 04:21:09 +00:00
};
class ParserTypeInCastExpression;
extern template class ParserAliasImpl<ParserIdentifier>;
extern template class ParserAliasImpl<ParserTypeInCastExpression>;
using ParserAlias = ParserAliasImpl<ParserIdentifier>;
using ParserCastExpressionAlias = ParserAliasImpl<ParserTypeInCastExpression>;
2011-08-28 08:50:27 +00:00
/** Элемент выражения - одно из: выражение в круглых скобках, массив, литерал, функция, идентификатор, звёздочка.
2010-06-24 19:12:10 +00:00
*/
class ParserExpressionElement : public IParserBase
{
protected:
const char * getName() const { return "element of expression"; }
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected);
2010-06-24 19:12:10 +00:00
};
2011-11-06 04:21:09 +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:
ParserWithOptionalAliasImpl(ParserPtr && elem_parser_, bool allow_alias_without_as_keyword_)
: elem_parser(std::move(elem_parser_)), allow_alias_without_as_keyword(allow_alias_without_as_keyword_) {}
2011-11-06 04:21:09 +00:00
protected:
2011-11-06 20:47:07 +00:00
ParserPtr elem_parser;
bool allow_alias_without_as_keyword;
const char * getName() const { return "element of expression with optional alias"; }
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected);
2011-11-06 04:21:09 +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
2013-05-29 11:33:55 +00:00
/** Элемент выражения ORDER BY - то же самое, что и элемент выражения, но после него ещё может быть указано ASC[ENDING] | DESC[ENDING]
* и, возможно, COLLATE 'locale'.
2011-09-04 05:14:52 +00:00
*/
class ParserOrderByElement : public IParserBase
{
protected:
const char * getName() const { return "element of ORDER BY expression"; }
bool parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, 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
}