| 240 | |
| 241 | |
| 242 | ASTPtr getCreateQueryFromStorage(const StoragePtr & storage, const ASTPtr & ast_storage, bool only_ordinary, |
| 243 | uint32_t max_parser_depth, uint32_t max_parser_backtracks, bool throw_on_error, ContextPtr context) |
| 244 | { |
| 245 | auto table_id = storage->getStorageID(); |
| 246 | auto metadata_ptr = storage->getInMemoryMetadataPtr(context, false); |
| 247 | if (metadata_ptr == nullptr) |
| 248 | { |
| 249 | if (throw_on_error) |
| 250 | throw Exception(ErrorCodes::CANNOT_GET_CREATE_TABLE_QUERY, "Cannot get metadata of {}.{}", |
| 251 | backQuote(table_id.database_name), backQuote(table_id.table_name)); |
| 252 | return nullptr; |
| 253 | } |
| 254 | |
| 255 | auto create_table_query = make_intrusive<ASTCreateQuery>(); |
| 256 | create_table_query->attach = false; |
| 257 | create_table_query->setTable(table_id.table_name); |
| 258 | create_table_query->setDatabase(table_id.database_name); |
| 259 | create_table_query->set(create_table_query->storage, ast_storage); |
| 260 | |
| 261 | /// setup create table query columns info. |
| 262 | { |
| 263 | auto ast_columns_list = make_intrusive<ASTColumns>(); |
| 264 | auto ast_expression_list = make_intrusive<ASTExpressionList>(); |
| 265 | NamesAndTypesList columns; |
| 266 | if (only_ordinary) |
| 267 | columns = metadata_ptr->columns.getOrdinary(); |
| 268 | else |
| 269 | columns = metadata_ptr->columns.getAll(); |
| 270 | for (const auto & column_name_and_type: columns) |
| 271 | { |
| 272 | const auto ast_column_declaration = make_intrusive<ASTColumnDeclaration>(); |
| 273 | ast_column_declaration->name = column_name_and_type.name; |
| 274 | /// parser typename |
| 275 | { |
| 276 | ASTPtr ast_type; |
| 277 | auto type_name = column_name_and_type.type->getName(); |
| 278 | const auto * string_end = type_name.c_str() + type_name.length(); |
| 279 | Expected expected; |
| 280 | expected.max_parsed_pos = string_end; |
| 281 | Tokens tokens(type_name.c_str(), string_end); |
| 282 | IParser::Pos pos(tokens, max_parser_depth, max_parser_backtracks); |
| 283 | ParserDataType parser; |
| 284 | if (!parser.parse(pos, ast_type, expected)) |
| 285 | { |
| 286 | if (throw_on_error) |
| 287 | throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot parse metadata of {}.{}", |
| 288 | backQuote(table_id.database_name), backQuote(table_id.table_name)); |
| 289 | return nullptr; |
| 290 | } |
| 291 | ast_column_declaration->setType(std::move(ast_type)); |
| 292 | |
| 293 | if (auto column_default = metadata_ptr->columns.getDefault(column_name_and_type.name)) |
| 294 | { |
| 295 | ast_column_declaration->default_specifier = toColumnDefaultSpecifier(column_default->kind); |
| 296 | ast_column_declaration->setDefaultExpression(column_default->expression->clone()); |
| 297 | } |
| 298 | } |
| 299 | ast_expression_list->children.emplace_back(ast_column_declaration); |
no test coverage detected