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>
|
2020-12-10 20:16:53 +00:00
|
|
|
#include <Access/ContextAccess.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#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-12-10 20:16:53 +00:00
|
|
|
namespace
|
2020-12-15 15:07:20 +00:00
|
|
|
{
|
2020-12-10 20:16:53 +00:00
|
|
|
[[noreturn]] void throwNoTablesMatchRegexp(const String & source_database, const String & source_table_regexp)
|
2020-12-15 15:07:20 +00:00
|
|
|
{
|
2020-12-10 20:16:53 +00:00
|
|
|
throw Exception(
|
|
|
|
"Error while executing table function merge. In database " + source_database
|
|
|
|
+ " no one matches regular expression: " + source_table_regexp,
|
|
|
|
ErrorCodes::UNKNOWN_TABLE);
|
2020-12-15 15:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-10 20:16:53 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
void TableFunctionMerge::parseArguments(const ASTPtr & ast_function, ContextPtr 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>();
|
2020-12-10 20:16:53 +00:00
|
|
|
source_table_regexp = args[1]->as<ASTLiteral &>().value.safeGet<String>();
|
2020-10-14 12:19:29 +00:00
|
|
|
}
|
2020-08-26 20:56:30 +00:00
|
|
|
|
2020-12-10 20:16:53 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
const Strings & TableFunctionMerge::getSourceTables(ContextPtr context) const
|
2020-12-10 20:16:53 +00:00
|
|
|
{
|
|
|
|
if (source_tables)
|
|
|
|
return *source_tables;
|
|
|
|
|
|
|
|
auto database = DatabaseCatalog::instance().getDatabase(source_database);
|
|
|
|
|
|
|
|
OptimizedRegularExpression re(source_table_regexp);
|
|
|
|
auto table_name_match = [&](const String & table_name_) { return re.match(table_name_); };
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
auto access = context->getAccess();
|
2020-12-10 20:16:53 +00:00
|
|
|
bool granted_show_on_all_tables = access->isGranted(AccessType::SHOW_TABLES, source_database);
|
|
|
|
bool granted_select_on_all_tables = access->isGranted(AccessType::SELECT, source_database);
|
|
|
|
|
|
|
|
source_tables.emplace();
|
|
|
|
for (auto it = database->getTablesIterator(context, table_name_match); it->isValid(); it->next())
|
|
|
|
{
|
|
|
|
if (!it->table())
|
|
|
|
continue;
|
|
|
|
bool granted_show = granted_show_on_all_tables || access->isGranted(AccessType::SHOW_TABLES, source_database, it->name());
|
|
|
|
if (!granted_show)
|
|
|
|
continue;
|
|
|
|
if (!granted_select_on_all_tables)
|
|
|
|
access->checkAccess(AccessType::SELECT, source_database, it->name());
|
|
|
|
source_tables->emplace_back(it->name());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (source_tables->empty())
|
|
|
|
throwNoTablesMatchRegexp(source_database, source_table_regexp);
|
|
|
|
|
|
|
|
return *source_tables;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
ColumnsDescription TableFunctionMerge::getActualTableStructure(ContextPtr context) const
|
2020-10-14 12:19:29 +00:00
|
|
|
{
|
2020-12-10 20:16:53 +00:00
|
|
|
for (const auto & table_name : getSourceTables(context))
|
|
|
|
{
|
|
|
|
auto storage = DatabaseCatalog::instance().tryGetTable(StorageID{source_database, table_name}, context);
|
|
|
|
if (storage)
|
|
|
|
return ColumnsDescription{storage->getInMemoryMetadataPtr()->getColumns().getAllPhysical()};
|
|
|
|
}
|
|
|
|
|
|
|
|
throwNoTablesMatchRegexp(source_database, source_table_regexp);
|
2020-10-14 12:19:29 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 20:16:53 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
StoragePtr TableFunctionMerge::executeImpl(const ASTPtr & /*ast_function*/, ContextPtr context, const std::string & table_name, ColumnsDescription /*cached_columns*/) const
|
2020-10-14 12:19:29 +00:00
|
|
|
{
|
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,
|
2020-12-10 20:16:53 +00:00
|
|
|
getSourceTables(context),
|
2017-04-01 07:20:54 +00:00
|
|
|
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
|
|
|
}
|