2020-06-18 12:52:05 +00:00
|
|
|
#include <Parsers/ParserDataType.h>
|
2020-11-04 00:08:55 +00:00
|
|
|
|
2020-06-18 12:52:05 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
2021-11-26 15:49:40 +00:00
|
|
|
#include <Parsers/ASTIdentifier_fwd.h>
|
2020-11-04 00:08:55 +00:00
|
|
|
#include <Parsers/CommonParsers.h>
|
|
|
|
#include <Parsers/ExpressionElementParsers.h>
|
2020-06-18 12:52:05 +00:00
|
|
|
#include <Parsers/ParserCreateQuery.h>
|
|
|
|
|
2020-11-04 00:08:55 +00:00
|
|
|
|
2020-06-18 12:52:05 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-11-04 00:08:55 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Wrapper to allow mixed lists of nested and normal types.
|
2021-01-28 03:34:44 +00:00
|
|
|
/// Parameters are either:
|
|
|
|
/// - Nested table elements;
|
|
|
|
/// - Enum element in form of 'a' = 1;
|
|
|
|
/// - literal;
|
|
|
|
/// - another data type (or identifier)
|
|
|
|
class ParserDataTypeArgument : public IParserBase
|
2020-11-04 00:08:55 +00:00
|
|
|
{
|
2021-01-28 03:34:44 +00:00
|
|
|
private:
|
|
|
|
const char * getName() const override { return "data type argument"; }
|
|
|
|
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override
|
|
|
|
{
|
|
|
|
ParserNestedTable nested_parser;
|
|
|
|
ParserDataType data_type_parser;
|
|
|
|
ParserLiteral literal_parser;
|
2020-11-04 00:08:55 +00:00
|
|
|
|
2021-01-28 03:34:44 +00:00
|
|
|
const char * operators[] = {"=", "equals", nullptr};
|
|
|
|
ParserLeftAssociativeBinaryOperatorList enum_parser(operators, std::make_unique<ParserLiteral>());
|
2020-11-04 00:08:55 +00:00
|
|
|
|
2021-02-05 19:21:30 +00:00
|
|
|
if (pos->type == TokenType::BareWord && std::string_view(pos->begin, pos->size()) == "Nested")
|
|
|
|
return nested_parser.parse(pos, node, expected);
|
|
|
|
|
|
|
|
return enum_parser.parse(pos, node, expected)
|
2021-01-28 03:34:44 +00:00
|
|
|
|| literal_parser.parse(pos, node, expected)
|
|
|
|
|| data_type_parser.parse(pos, node, expected);
|
|
|
|
}
|
2020-11-04 00:08:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-18 12:52:05 +00:00
|
|
|
bool ParserDataType::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
|
|
|
ParserNestedTable nested;
|
|
|
|
if (nested.parse(pos, node, expected))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
String type_name;
|
|
|
|
|
|
|
|
ParserIdentifier name_parser;
|
|
|
|
ASTPtr identifier;
|
|
|
|
if (!name_parser.parse(pos, identifier, expected))
|
|
|
|
return false;
|
|
|
|
tryGetIdentifierNameInto(identifier, type_name);
|
|
|
|
|
|
|
|
String type_name_upper = Poco::toUpper(type_name);
|
|
|
|
String type_name_suffix;
|
|
|
|
|
|
|
|
/// Special cases for compatibility with SQL standard. We can parse several words as type name
|
|
|
|
/// only for certain first words, otherwise we don't know how many words to parse
|
|
|
|
if (type_name_upper == "NATIONAL")
|
|
|
|
{
|
|
|
|
if (ParserKeyword("CHARACTER LARGE OBJECT").ignore(pos))
|
|
|
|
type_name_suffix = "CHARACTER LARGE OBJECT";
|
|
|
|
else if (ParserKeyword("CHARACTER VARYING").ignore(pos))
|
|
|
|
type_name_suffix = "CHARACTER VARYING";
|
|
|
|
else if (ParserKeyword("CHAR VARYING").ignore(pos))
|
|
|
|
type_name_suffix = "CHAR VARYING";
|
|
|
|
else if (ParserKeyword("CHARACTER").ignore(pos))
|
|
|
|
type_name_suffix = "CHARACTER";
|
|
|
|
else if (ParserKeyword("CHAR").ignore(pos))
|
|
|
|
type_name_suffix = "CHAR";
|
|
|
|
}
|
|
|
|
else if (type_name_upper == "BINARY" ||
|
|
|
|
type_name_upper == "CHARACTER" ||
|
|
|
|
type_name_upper == "CHAR" ||
|
|
|
|
type_name_upper == "NCHAR")
|
|
|
|
{
|
|
|
|
if (ParserKeyword("LARGE OBJECT").ignore(pos))
|
|
|
|
type_name_suffix = "LARGE OBJECT";
|
|
|
|
else if (ParserKeyword("VARYING").ignore(pos))
|
|
|
|
type_name_suffix = "VARYING";
|
|
|
|
}
|
|
|
|
else if (type_name_upper == "DOUBLE")
|
|
|
|
{
|
|
|
|
if (ParserKeyword("PRECISION").ignore(pos))
|
|
|
|
type_name_suffix = "PRECISION";
|
|
|
|
}
|
2020-06-22 11:45:45 +00:00
|
|
|
else if (type_name_upper.find("INT") != std::string::npos)
|
|
|
|
{
|
2022-04-16 22:53:11 +00:00
|
|
|
/// Support SIGNED and UNSIGNED integer type modifiers for compatibility with MySQL.
|
2020-06-22 11:45:45 +00:00
|
|
|
if (ParserKeyword("SIGNED").ignore(pos))
|
|
|
|
type_name_suffix = "SIGNED";
|
|
|
|
else if (ParserKeyword("UNSIGNED").ignore(pos))
|
|
|
|
type_name_suffix = "UNSIGNED";
|
|
|
|
}
|
2020-06-18 12:52:05 +00:00
|
|
|
|
|
|
|
if (!type_name_suffix.empty())
|
|
|
|
type_name = type_name_upper + " " + type_name_suffix;
|
|
|
|
|
|
|
|
auto function_node = std::make_shared<ASTFunction>();
|
|
|
|
function_node->name = type_name;
|
2020-12-04 02:15:44 +00:00
|
|
|
function_node->no_empty_args = true;
|
2020-06-18 12:52:05 +00:00
|
|
|
|
|
|
|
if (pos->type != TokenType::OpeningRoundBracket)
|
|
|
|
{
|
|
|
|
node = function_node;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
/// Parse optional parameters
|
2021-01-28 03:34:44 +00:00
|
|
|
ParserList args_parser(std::make_unique<ParserDataTypeArgument>(), std::make_unique<ParserToken>(TokenType::Comma));
|
2020-06-18 12:52:05 +00:00
|
|
|
ASTPtr expr_list_args;
|
|
|
|
|
|
|
|
if (!args_parser.parse(pos, expr_list_args, expected))
|
|
|
|
return false;
|
|
|
|
if (pos->type != TokenType::ClosingRoundBracket)
|
|
|
|
return false;
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
function_node->arguments = expr_list_args;
|
|
|
|
function_node->children.push_back(function_node->arguments);
|
|
|
|
|
|
|
|
node = function_node;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|