StoragePtr TableFunctionMerge::execute(const ASTPtr & ast_function, const Context & context) const { ASTs & args_func = typeid_cast<ASTFunction &>(*ast_function).children; if (args_func.size() != 1) throw Exception("Table function 'merge' requires exactly 2 arguments" " - name of source database and regexp for table names.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH); ASTs & args = typeid_cast<ASTExpressionList &>(*args_func.at(0)).children; if (args.size() != 2) throw Exception("Table function 'merge' requires exactly 2 arguments" " - name of source database and regexp for table names.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH); args[0] = evaluateConstantExpressionOrIdentidierAsLiteral(args[0], context); args[1] = evaluateConstantExpressionAsLiteral(args[1], context); String source_database = static_cast<const ASTLiteral &>(*args[0]).value.safeGet<String>(); String table_name_regexp = static_cast<const ASTLiteral &>(*args[1]).value.safeGet<String>(); auto res = StorageMerge::create( getName(), std::make_shared<NamesAndTypesList>(chooseColumns(source_database, table_name_regexp, context)), source_database, table_name_regexp, context); res->startup(); return res; }
void registerStorageMerge(StorageFactory & factory) { factory.registerStorage("Merge", [](const StorageFactory::Arguments & args) { /** In query, the name of database is specified as table engine argument which contains source tables, * as well as regex for source-table names. */ ASTs & engine_args = args.engine_args; if (engine_args.size() != 2) throw Exception("Storage Merge requires exactly 2 parameters" " - name of source database and regexp for table names.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH); engine_args[0] = evaluateConstantExpressionOrIdentifierAsLiteral(engine_args[0], args.local_context); engine_args[1] = evaluateConstantExpressionAsLiteral(engine_args[1], args.local_context); String source_database = static_cast<const ASTLiteral &>(*engine_args[0]).value.safeGet<String>(); String table_name_regexp = static_cast<const ASTLiteral &>(*engine_args[1]).value.safeGet<String>(); return StorageMerge::create( args.table_name, args.columns, source_database, table_name_regexp, args.context); }); }