ClickHouse/dbms/Parsers/ExpressionListParsers.h

425 lines
12 KiB
C++
Raw Normal View History

2011-09-04 05:14:52 +00:00
#pragma once
2010-06-24 19:12:10 +00:00
2010-06-25 16:36:13 +00:00
#include <list>
2010-06-24 19:12:10 +00:00
#include <Parsers/IParserBase.h>
#include <Parsers/CommonParsers.h>
2010-06-24 19:12:10 +00:00
namespace DB
{
2017-05-27 17:29:55 +00:00
/** Consequent pairs of rows: the operator and the corresponding function. For example, "+" -> "plus".
* The parsing order of the operators is significant.
2010-06-25 16:36:13 +00:00
*/
using Operators_t = const char **;
2010-06-24 19:12:10 +00:00
2017-05-27 17:29:55 +00:00
/** List of elements separated by something. */
2011-08-18 18:48:00 +00:00
class ParserList : public IParserBase
{
public:
2019-10-07 16:23:16 +00:00
ParserList(ParserPtr && elem_parser_, ParserPtr && separator_parser_, bool allow_empty_ = true, char result_separator_ = ',')
: elem_parser(std::move(elem_parser_))
, separator_parser(std::move(separator_parser_))
, allow_empty(allow_empty_)
, result_separator(result_separator_)
{
}
2011-08-18 18:48:00 +00:00
protected:
const char * getName() const override { return "list of elements"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
2011-08-18 18:48:00 +00:00
private:
ParserPtr elem_parser;
ParserPtr separator_parser;
bool allow_empty;
2019-10-07 16:23:16 +00:00
char result_separator;
2011-08-18 18:48:00 +00:00
};
2017-05-27 17:29:55 +00:00
/** An expression with an infix binary left-associative operator.
* For example, a + b - c + d.
2010-06-24 19:12:10 +00:00
*/
class ParserLeftAssociativeBinaryOperatorList : public IParserBase
{
private:
Operators_t operators;
ParserPtr first_elem_parser;
ParserPtr remaining_elem_parser;
2010-06-24 19:12:10 +00:00
public:
2017-05-27 17:29:55 +00:00
/** `operators_` - allowed operators and their corresponding functions
*/
ParserLeftAssociativeBinaryOperatorList(Operators_t operators_, ParserPtr && first_elem_parser_)
: operators(operators_), first_elem_parser(std::move(first_elem_parser_))
{
}
ParserLeftAssociativeBinaryOperatorList(Operators_t operators_, ParserPtr && first_elem_parser_,
ParserPtr && remaining_elem_parser_)
: operators(operators_), first_elem_parser(std::move(first_elem_parser_)),
remaining_elem_parser(std::move(remaining_elem_parser_))
{
}
2010-06-24 19:12:10 +00:00
protected:
const char * getName() const override { return "list, delimited by binary operators"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
2010-06-24 19:12:10 +00:00
};
2017-05-27 17:29:55 +00:00
/** Expression with an infix operator of arbitrary arity.
* For example, a AND b AND c AND d.
*/
class ParserVariableArityOperatorList : public IParserBase
{
private:
const char * infix;
const char * function_name;
ParserPtr elem_parser;
public:
ParserVariableArityOperatorList(const char * infix_, const char * function_, ParserPtr && elem_parser_)
: infix(infix_), function_name(function_), elem_parser(std::move(elem_parser_))
{
}
protected:
const char * getName() const override { return "list, delimited by operator of variable arity"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
};
2017-05-27 17:29:55 +00:00
/** An expression with a prefix unary operator.
* Example, NOT x.
2010-06-24 19:12:10 +00:00
*/
class ParserPrefixUnaryOperatorExpression : public IParserBase
{
private:
Operators_t operators;
ParserPtr elem_parser;
2010-06-24 19:12:10 +00:00
public:
2017-05-27 17:29:55 +00:00
/** `operators_` - allowed operators and their corresponding functions
*/
ParserPrefixUnaryOperatorExpression(Operators_t operators_, ParserPtr && elem_parser_)
: operators(operators_), elem_parser(std::move(elem_parser_))
{
}
2010-06-24 19:12:10 +00:00
protected:
const char * getName() const override { return "expression with prefix unary operator"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
2010-06-24 19:12:10 +00:00
};
2019-10-31 15:22:48 +00:00
class ParserArrayElementExpression : public IParserBase
2010-06-24 19:12:10 +00:00
{
private:
static const char * operators[];
2010-06-24 19:12:10 +00:00
protected:
const char * getName() const override{ return "array element expression"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
};
2019-10-31 15:22:48 +00:00
class ParserTupleElementExpression : public IParserBase
{
private:
static const char * operators[];
protected:
const char * getName() const override { return "tuple element expression"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
2010-06-24 19:12:10 +00:00
};
class ParserUnaryMinusExpression : public IParserBase
{
private:
static const char * operators[];
ParserPrefixUnaryOperatorExpression operator_parser {operators, std::make_unique<ParserTupleElementExpression>()};
2010-06-24 19:12:10 +00:00
protected:
const char * getName() const override { return "unary minus expression"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
2010-06-24 19:12:10 +00:00
};
class ParserMultiplicativeExpression : public IParserBase
{
private:
static const char * operators[];
ParserLeftAssociativeBinaryOperatorList operator_parser {operators, std::make_unique<ParserUnaryMinusExpression>()};
2010-06-24 19:12:10 +00:00
protected:
const char * getName() const override { return "multiplicative expression"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override
{
return operator_parser.parse(pos, node, expected);
}
2010-06-24 19:12:10 +00:00
};
/// DATE operator. "DATE '2001-01-01'" would be parsed as "toDate('2001-01-01')".
class ParserDateOperatorExpression : public IParserBase
{
protected:
ParserMultiplicativeExpression next_parser;
const char * getName() const override { return "DATE operator expression"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
};
/// TIMESTAMP operator. "TIMESTAMP '2001-01-01 12:34:56'" would be parsed as "toDateTime('2001-01-01 12:34:56')".
class ParserTimestampOperatorExpression : public IParserBase
{
protected:
ParserDateOperatorExpression next_parser;
const char * getName() const override { return "TIMESTAMP operator expression"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
};
2010-06-24 19:12:10 +00:00
/// Optional conversion to INTERVAL data type. Example: "INTERVAL x SECOND" parsed as "toIntervalSecond(x)".
class ParserIntervalOperatorExpression : public IParserBase
{
protected:
ParserTimestampOperatorExpression next_parser;
const char * getName() const override { return "INTERVAL operator expression"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
};
2010-06-24 19:12:10 +00:00
class ParserAdditiveExpression : public IParserBase
{
private:
static const char * operators[];
ParserLeftAssociativeBinaryOperatorList operator_parser {operators, std::make_unique<ParserIntervalOperatorExpression>()};
2010-06-24 19:12:10 +00:00
protected:
const char * getName() const override { return "additive expression"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override
{
return operator_parser.parse(pos, node, expected);
}
2010-06-24 19:12:10 +00:00
};
class ParserConcatExpression : public IParserBase
{
ParserVariableArityOperatorList operator_parser {"||", "concat", std::make_unique<ParserAdditiveExpression>()};
protected:
const char * getName() const override { return "string concatenation expression"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override
{
return operator_parser.parse(pos, node, expected);
}
};
class ParserBetweenExpression : public IParserBase
{
private:
ParserConcatExpression elem_parser;
protected:
const char * getName() const override { return "BETWEEN expression"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
};
2010-06-24 19:12:10 +00:00
class ParserComparisonExpression : public IParserBase
{
private:
static const char * operators[];
ParserLeftAssociativeBinaryOperatorList operator_parser {operators, std::make_unique<ParserBetweenExpression>()};
2010-06-24 19:12:10 +00:00
protected:
const char * getName() const override{ return "comparison expression"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override
{
return operator_parser.parse(pos, node, expected);
}
2010-06-24 19:12:10 +00:00
};
/** Parser for nullity checking with IS (NOT) NULL.
*/
class ParserNullityChecking : public IParserBase
{
2019-10-31 15:22:48 +00:00
private:
ParserComparisonExpression elem_parser;
protected:
const char * getName() const override { return "nullity checking"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
};
2010-06-24 19:12:10 +00:00
class ParserLogicalNotExpression : public IParserBase
{
private:
static const char * operators[];
ParserPrefixUnaryOperatorExpression operator_parser {operators, std::make_unique<ParserNullityChecking>()};
2010-06-24 19:12:10 +00:00
protected:
const char * getName() const override{ return "logical-NOT expression"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override
{
return operator_parser.parse(pos, node, expected);
}
2010-06-24 19:12:10 +00:00
};
class ParserLogicalAndExpression : public IParserBase
{
private:
ParserVariableArityOperatorList operator_parser {"AND", "and", std::make_unique<ParserLogicalNotExpression>()};
2010-06-24 19:12:10 +00:00
protected:
const char * getName() const override { return "logical-AND expression"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override
{
return operator_parser.parse(pos, node, expected);
}
2010-06-24 19:12:10 +00:00
};
class ParserLogicalOrExpression : public IParserBase
{
private:
ParserVariableArityOperatorList operator_parser {"OR", "or", std::make_unique<ParserLogicalAndExpression>()};
2010-06-24 19:12:10 +00:00
protected:
const char * getName() const override { return "logical-OR expression"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override
{
return operator_parser.parse(pos, node, expected);
}
2010-06-24 19:12:10 +00:00
};
2016-11-30 17:31:05 +00:00
/** An expression with ternary operator.
* For example, a = 1 ? b + 1 : c * 2.
*/
class ParserTernaryOperatorExpression : public IParserBase
{
private:
ParserLogicalOrExpression elem_parser;
protected:
const char * getName() const override { return "expression with ternary operator"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
};
class ParserLambdaExpression : public IParserBase
{
private:
ParserTernaryOperatorExpression elem_parser;
protected:
const char * getName() const override { return "lambda expression"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
};
using ParserExpression = ParserLambdaExpression;
2011-11-06 20:47:07 +00:00
class ParserExpressionWithOptionalAlias : public IParserBase
{
public:
ParserExpressionWithOptionalAlias(bool allow_alias_without_as_keyword);
2011-11-06 20:47:07 +00:00
protected:
ParserPtr impl;
2011-11-06 20:47:07 +00:00
const char * getName() const override { return "expression with optional alias"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override
{
return impl->parse(pos, node, expected);
}
2011-11-06 20:47:07 +00:00
};
2017-05-27 17:29:55 +00:00
/** A comma-separated list of expressions, probably empty. */
2010-06-24 19:12:10 +00:00
class ParserExpressionList : public IParserBase
{
public:
ParserExpressionList(bool allow_alias_without_as_keyword_)
: allow_alias_without_as_keyword(allow_alias_without_as_keyword_) {}
2010-06-24 19:12:10 +00:00
protected:
bool allow_alias_without_as_keyword;
const char * getName() const override { return "list of expressions"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
2010-06-24 19:12:10 +00:00
};
class ParserNotEmptyExpressionList : public IParserBase
{
public:
ParserNotEmptyExpressionList(bool allow_alias_without_as_keyword)
: nested_parser(allow_alias_without_as_keyword) {}
2010-06-24 19:12:10 +00:00
private:
ParserExpressionList nested_parser;
2010-06-24 19:12:10 +00:00
protected:
const char * getName() const override { return "not empty list of expressions"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
2010-06-24 19:12:10 +00:00
};
2011-09-04 05:14:52 +00:00
class ParserOrderByExpressionList : public IParserBase
{
protected:
const char * getName() const override { return "order by expression"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
2011-09-04 05:14:52 +00:00
};
2010-06-24 19:12:10 +00:00
2019-10-08 13:26:15 +00:00
/// Parser for key-value pair, where value can be list of pairs.
2019-10-07 16:23:16 +00:00
class ParserKeyValuePair : public IParserBase
{
protected:
const char * getName() const override { return "key-value pair"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
};
2019-10-09 13:02:05 +00:00
2019-10-08 13:26:15 +00:00
/// Parser for list of key-value pairs.
2019-10-07 16:23:16 +00:00
class ParserKeyValuePairsList : public IParserBase
{
protected:
const char * getName() const override { return "list of pairs"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
};
2019-10-09 13:02:05 +00:00
class ParserTTLExpressionList : public IParserBase
{
protected:
const char * getName() const override { return "ttl expression"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
2019-10-09 13:02:05 +00:00
};
2011-09-04 05:14:52 +00:00
}