2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/OptimizedRegularExpression.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/StorageMerge.h>
|
|
|
|
#include <Parsers/ASTExpressionList.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <TableFunctions/ITableFunction.h>
|
|
|
|
#include <Interpreters/evaluateConstantExpression.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Databases/IDatabase.h>
|
|
|
|
#include <TableFunctions/TableFunctionMerge.h>
|
2017-06-10 09:04:31 +00:00
|
|
|
#include <TableFunctions/TableFunctionFactory.h>
|
2016-05-13 03:22:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
extern const int UNKNOWN_TABLE;
|
2016-05-13 03:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-25 21:57:29 +00:00
|
|
|
static NamesAndTypesList chooseColumns(const String & source_database, const String & table_name_regexp_, const Context & context)
|
2016-05-13 03:22:16 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
OptimizedRegularExpression table_name_regexp(table_name_regexp_);
|
2019-06-02 12:11:01 +00:00
|
|
|
auto table_name_match = [&](const String & table_name) { return table_name_regexp.match(table_name); };
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
StoragePtr any_table;
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
auto database = context.getDatabase(source_database);
|
2019-06-02 12:11:01 +00:00
|
|
|
auto iterator = database->getIterator(context, table_name_match);
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2019-06-02 12:11:01 +00:00
|
|
|
if (iterator->isValid())
|
|
|
|
any_table = iterator->table();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!any_table)
|
|
|
|
throw Exception("Error while executing table function merge. In database " + source_database + " no one matches regular expression: "
|
|
|
|
+ table_name_regexp_, ErrorCodes::UNKNOWN_TABLE);
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2018-03-13 15:00:28 +00:00
|
|
|
return any_table->getColumns().getAllPhysical();
|
2016-05-13 03:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-18 18:29:49 +00:00
|
|
|
StoragePtr TableFunctionMerge::executeImpl(const ASTPtr & ast_function, const Context & context, const std::string & table_name) const
|
2016-05-13 03:22:16 +00:00
|
|
|
{
|
2019-03-11 12:49:39 +00:00
|
|
|
ASTs & args_func = ast_function->children;
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (args_func.size() != 1)
|
2017-06-10 09:04:31 +00:00
|
|
|
throw Exception("Table function 'merge' requires exactly 2 arguments"
|
2017-04-01 07:20:54 +00:00
|
|
|
" - name of source database and regexp for table names.",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2019-03-11 12:49:39 +00:00
|
|
|
ASTs & args = args_func.at(0)->children;
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (args.size() != 2)
|
2017-06-10 09:04:31 +00:00
|
|
|
throw Exception("Table function 'merge' requires exactly 2 arguments"
|
2017-04-01 07:20:54 +00:00
|
|
|
" - name of source database and regexp for table names.",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2017-08-18 19:30:55 +00:00
|
|
|
args[0] = evaluateConstantExpressionOrIdentifierAsLiteral(args[0], context);
|
2017-04-01 07:20:54 +00:00
|
|
|
args[1] = evaluateConstantExpressionAsLiteral(args[1], context);
|
2016-08-25 17:23:29 +00:00
|
|
|
|
2019-03-15 16:14:13 +00:00
|
|
|
String source_database = args[0]->as<ASTLiteral &>().value.safeGet<String>();
|
|
|
|
String table_name_regexp = args[1]->as<ASTLiteral &>().value.safeGet<String>();
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2017-06-06 17:06:14 +00:00
|
|
|
auto res = StorageMerge::create(
|
2019-07-09 15:40:21 +00:00
|
|
|
getDatabaseName(),
|
2019-07-18 18:29:49 +00:00
|
|
|
table_name,
|
2018-03-10 17:03:57 +00:00
|
|
|
ColumnsDescription{chooseColumns(source_database, table_name_regexp, context)},
|
2017-04-01 07:20:54 +00:00
|
|
|
source_database,
|
|
|
|
table_name_regexp,
|
|
|
|
context);
|
2017-06-06 17:06:14 +00:00
|
|
|
res->startup();
|
|
|
|
return res;
|
2016-05-13 03:22:16 +00:00
|
|
|
}
|
|
|
|
|
2017-06-10 09:04:31 +00:00
|
|
|
|
|
|
|
void registerTableFunctionMerge(TableFunctionFactory & factory)
|
|
|
|
{
|
2017-12-02 02:47:12 +00:00
|
|
|
factory.registerFunction<TableFunctionMerge>();
|
2017-06-10 09:04:31 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 03:22:16 +00:00
|
|
|
}
|