From bee14177cd29dfda02e5c8d9543e85bae3b2b685 Mon Sep 17 00:00:00 2001 From: potya Date: Sat, 23 May 2020 17:32:47 +0300 Subject: [PATCH] Fix NOT nULL modifier --- src/Interpreters/InterpreterCreateQuery.cpp | 14 +++++----- src/Parsers/ASTColumnDeclaration.cpp | 18 +++++++------ src/Parsers/ASTColumnDeclaration.h | 2 +- src/Parsers/ParserCreateQuery.h | 29 ++++++++++++--------- 4 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/Interpreters/InterpreterCreateQuery.cpp b/src/Interpreters/InterpreterCreateQuery.cpp index ec83c3f9c7f..586f2d0f056 100644 --- a/src/Interpreters/InterpreterCreateQuery.cpp +++ b/src/Interpreters/InterpreterCreateQuery.cpp @@ -287,25 +287,25 @@ ColumnsDescription InterpreterCreateQuery::getColumnsDescription(const ASTExpres const auto & col_decl = ast->as(); DataTypePtr column_type = nullptr; - if (col_decl.isNULL && col_decl.isNotNULL) - throw Exception{"Cant use NOT NULL and NULL together", ErrorCodes::EMPTY_LIST_OF_COLUMNS_PASSED}; + if (!col_decl.isNULL && col_decl.isNot) + throw Exception{"Cant use NOT without NULL", ErrorCodes::EMPTY_LIST_OF_COLUMNS_PASSED}; if (col_decl.type) { column_type = DataTypeFactory::instance().get(col_decl.type); - if (col_decl.isNULL) { + if (col_decl.isNot && col_decl.isNULL) { + if (column_type->isNullable()) + throw Exception{"Cant use NOT NULL with Nullable", ErrorCodes::EMPTY_LIST_OF_COLUMNS_PASSED}; + } else if (col_decl.isNULL && !col_decl.isNot) { if (column_type->isNullable()) throw Exception{"Cant use NULL with Nullable", ErrorCodes::EMPTY_LIST_OF_COLUMNS_PASSED}; else { column_type = makeNullable(column_type); } - } else if (col_decl.isNotNULL) { - if (column_type->isNullable()) - throw Exception{"Cant use NOT NULL with Nullable", ErrorCodes::EMPTY_LIST_OF_COLUMNS_PASSED}; } - if (context.getSettingsRef().data_type_default_nullable && !column_type->isNullable() && !col_decl.isNotNULL) + if (context.getSettingsRef().data_type_default_nullable && !column_type->isNullable() && !col_decl.isNot && !col_decl.isNULL) column_type = makeNullable(column_type); column_names_and_types.emplace_back(col_decl.name, column_type); diff --git a/src/Parsers/ASTColumnDeclaration.cpp b/src/Parsers/ASTColumnDeclaration.cpp index a6c4b819fdf..40513b45586 100644 --- a/src/Parsers/ASTColumnDeclaration.cpp +++ b/src/Parsers/ASTColumnDeclaration.cpp @@ -22,10 +22,10 @@ ASTPtr ASTColumnDeclaration::clone() const res->children.push_back(res->isNULL); } - if (isNULL) + if (isNot) { - res->isNotNULL = isNotNULL; - res->children.push_back(res->isNotNULL); + res->isNot = isNot; + res->children.push_back(res->isNot); } if (default_expression) @@ -71,17 +71,19 @@ void ASTColumnDeclaration::formatImpl(const FormatSettings & settings, FormatSta type->formatImpl(settings, state, frame); } + if (isNot) + { + settings.ostr << ' '; + isNot->formatImpl(settings, state, frame); + } + if (isNULL) { settings.ostr << ' '; isNULL->formatImpl(settings, state, frame); } - if (isNotNULL) - { - settings.ostr << ' '; - isNotNULL->formatImpl(settings, state, frame); - } + if (default_expression) { diff --git a/src/Parsers/ASTColumnDeclaration.h b/src/Parsers/ASTColumnDeclaration.h index d3c50d453d5..406a8cebded 100644 --- a/src/Parsers/ASTColumnDeclaration.h +++ b/src/Parsers/ASTColumnDeclaration.h @@ -14,7 +14,7 @@ public: String name; ASTPtr type; ASTPtr isNULL; - ASTPtr isNotNULL; + ASTPtr isNot; String default_specifier; ASTPtr default_expression; ASTPtr comment; diff --git a/src/Parsers/ParserCreateQuery.h b/src/Parsers/ParserCreateQuery.h index 66a2005334d..8976de8f1bc 100644 --- a/src/Parsers/ParserCreateQuery.h +++ b/src/Parsers/ParserCreateQuery.h @@ -118,7 +118,7 @@ bool IParserColumnDeclaration::parseImpl(Pos & pos, ASTPtr & node, E ParserIdentifierWithOptionalParameters type_parser; ParserKeyword s_default{"DEFAULT"}; ParserKeyword s_null{"NULL"}; - ParserKeyword s_not_null{"NOT NULL"}; + ParserKeyword s_not{"NOT"}; ParserKeyword s_materialized{"MATERIALIZED"}; ParserKeyword s_alias{"ALIAS"}; ParserKeyword s_comment{"COMMENT"}; @@ -129,7 +129,7 @@ bool IParserColumnDeclaration::parseImpl(Pos & pos, ASTPtr & node, E ParserCodec codec_parser; ParserExpression expression_parser; ParserIdentifier null_parser; - ParserIdentifier not_null_parser; + ParserCompoundIdentifier not_null_parser; /// mandatory column name ASTPtr name; @@ -142,7 +142,7 @@ bool IParserColumnDeclaration::parseImpl(Pos & pos, ASTPtr & node, E ASTPtr type; String default_specifier; ASTPtr isNull; - ASTPtr isNotNull; + ASTPtr isNot; ASTPtr default_expression; ASTPtr comment_expression; ASTPtr codec_expression; @@ -171,14 +171,19 @@ bool IParserColumnDeclaration::parseImpl(Pos & pos, ASTPtr & node, E if (require_type && !type && !default_expression) return false; /// reject column name without type - Pos pos_before_null = pos; + // Pos pos_before_null = pos; - if (s_null.check(pos, expected)) { - if (!null_parser.parse(pos_before_null, isNull, expected)) - return false; - } else if (s_not_null.check(pos, expected)) { - if (!not_null_parser.parse(pos_before_null, isNotNull, expected)) + if (s_not.check(pos, expected)) { + if (s_null.check(pos, expected)) { + isNot = std::make_shared("NOT"); + isNull = std::make_shared("NULL"); + } else { return false; + } + } else { + if (s_null.check(pos, expected)) { + isNull = std::make_shared("NULL"); + } } if (s_comment.ignore(pos, expected)) @@ -215,9 +220,9 @@ bool IParserColumnDeclaration::parseImpl(Pos & pos, ASTPtr & node, E column_declaration->children.push_back(std::move(isNull)); } - if (isNotNull) { - column_declaration->isNotNULL = isNotNull; - column_declaration->children.push_back(std::move(isNotNull)); + if (isNot) { + column_declaration->isNot = isNot; + column_declaration->children.push_back(std::move(isNot)); } if (default_expression)