From ffde70b5c94daafe89e2c685e78e75560563b994 Mon Sep 17 00:00:00 2001 From: Pavel Kartavyy Date: Wed, 21 May 2014 17:25:22 +0400 Subject: [PATCH] dbms: changed type when add column to nested from Array(NN) -> NN [#METR-11063] --- .../Interpreters/InterpreterAlterQuery.cpp | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/dbms/src/Interpreters/InterpreterAlterQuery.cpp b/dbms/src/Interpreters/InterpreterAlterQuery.cpp index a832664f2a4..ab6a0d06f2c 100644 --- a/dbms/src/Interpreters/InterpreterAlterQuery.cpp +++ b/dbms/src/Interpreters/InterpreterAlterQuery.cpp @@ -131,12 +131,13 @@ void InterpreterAlterQuery::addColumnToAST(StoragePtr table, ASTs & columns, con size_t dot_pos = add_column.name.find('.'); bool insert_nested_column = dot_pos != std::string::npos; + const DataTypeFactory & data_type_factory = context.getDataTypeFactory(); + StringRange type_range = add_column.type->range; + String type(type_range.first, type_range.second - type_range.first); + DataTypePtr datatype = data_type_factory.get(type); if (insert_nested_column) { - const DataTypeFactory & data_type_factory = context.getDataTypeFactory(); - StringRange type_range = add_column.type->range; - String type(type_range.first, type_range.second - type_range.first); - if (!dynamic_cast(data_type_factory.get(type).get())) + if (!dynamic_cast(datatype.get())) { throw Exception("Cannot add column " + add_column.name + ". Because it is not an array. Only arrays could be nested and consist '.' in their names"); } @@ -158,8 +159,9 @@ void InterpreterAlterQuery::addColumnToAST(StoragePtr table, ASTs & columns, con ASTs & nested_columns = dynamic_cast(*nested_func->arguments).children; - ASTPtr new_nested_column = add_column_ptr->clone(); - dynamic_cast(*new_nested_column).name = add_column.name.substr(dot_pos + 1); + ASTPtr new_nested_column_ptr = add_column_ptr->clone(); + ASTNameTypePair& new_nested_column = dynamic_cast(*new_nested_column_ptr); + new_nested_column.name = add_column.name.substr(dot_pos + 1); ASTPtr new_after_column = after_column_ptr ? after_column_ptr->clone() : nullptr; if (new_after_column) @@ -172,7 +174,18 @@ void InterpreterAlterQuery::addColumnToAST(StoragePtr table, ASTs & columns, con dynamic_cast(*new_after_column).name = after_col->name.substr(after_dot_pos + 1); } - addColumnToAST1(nested_columns, new_nested_column, new_after_column); + + { + /// удаляем массив из типа, т.е. Array(String) -> String + ParserIdentifierWithOptionalParameters type_parser; + const char * expected; + const char * begin = new_nested_column.type->range.first + strlen("Array("); + const char * end = new_nested_column.type->range.second - static_cast(strlen(")")); + if (!type_parser.parse(begin, end, new_nested_column.type, expected)) + throw Exception("Fail to convert type like Array(SomeType) -> SomeType for type " + type); + } + + addColumnToAST1(nested_columns, new_nested_column_ptr, new_after_column); } else {