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>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/StorageMerge.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <TableFunctions/ITableFunction.h>
|
|
|
|
#include <Interpreters/evaluateConstantExpression.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <TableFunctions/TableFunctionMerge.h>
|
2017-06-10 09:04:31 +00:00
|
|
|
#include <TableFunctions/TableFunctionFactory.h>
|
2020-02-19 18:58:29 +00:00
|
|
|
#include <TableFunctions/registerTableFunctions.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
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-28 20:10:45 +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
|
|
|
{
|
2020-02-10 13:10:17 +00:00
|
|
|
auto database = DatabaseCatalog::instance().getDatabase(source_database);
|
2020-05-28 20:10:45 +00:00
|
|
|
auto iterator = database->getTablesIterator(context, table_name_match);
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2019-06-02 12:11:01 +00:00
|
|
|
if (iterator->isValid())
|
2020-06-02 02:06:16 +00:00
|
|
|
if (const auto & table = iterator->table())
|
|
|
|
any_table = 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
|
|
|
|
2020-06-17 16:39:58 +00:00
|
|
|
return any_table->getInMemoryMetadataPtr()->getColumns().getAllPhysical();
|
2016-05-13 03:22:16 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 12:19:29 +00:00
|
|
|
void TableFunctionMerge::parseArguments(const ASTPtr & ast_function, const Context & context)
|
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
|
|
|
|
2020-02-19 18:58:29 +00:00
|
|
|
args[0] = evaluateConstantExpressionForDatabaseName(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
|
|
|
|
2020-10-14 12:19:29 +00:00
|
|
|
source_database = args[0]->as<ASTLiteral &>().value.safeGet<String>();
|
|
|
|
table_name_regexp = args[1]->as<ASTLiteral &>().value.safeGet<String>();
|
|
|
|
}
|
2020-08-26 20:56:30 +00:00
|
|
|
|
2020-10-14 12:19:29 +00:00
|
|
|
ColumnsDescription TableFunctionMerge::getActualTableStructure(const Context & context) const
|
|
|
|
{
|
|
|
|
return ColumnsDescription{chooseColumns(source_database, table_name_regexp, context)};
|
|
|
|
}
|
|
|
|
|
|
|
|
StoragePtr TableFunctionMerge::executeImpl(const ASTPtr & /*ast_function*/, const Context & context, const std::string & table_name, ColumnsDescription /*cached_columns*/) const
|
|
|
|
{
|
2020-08-31 17:45:21 +00:00
|
|
|
auto res = StorageMerge::create(
|
2019-12-04 16:06:55 +00:00
|
|
|
StorageID(getDatabaseName(), table_name),
|
2020-10-14 12:19:29 +00:00
|
|
|
getActualTableStructure(context),
|
2017-04-01 07:20:54 +00:00
|
|
|
source_database,
|
|
|
|
table_name_regexp,
|
|
|
|
context);
|
2020-10-14 12:19:29 +00:00
|
|
|
|
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
|
|
|
}
|