ClickHouse/src/TableFunctions/TableFunctionMerge.cpp

169 lines
5.9 KiB
C++
Raw Normal View History

#include <Common/OptimizedRegularExpression.h>
2017-07-13 20:58:19 +00:00
#include <Common/typeid_cast.h>
#include <Storages/StorageMerge.h>
#include <Storages/checkAndGetLiteralArgument.h>
#include <Parsers/ASTLiteral.h>
#include <Parsers/ASTFunction.h>
#include <TableFunctions/ITableFunction.h>
#include <Interpreters/evaluateConstantExpression.h>
#include <Interpreters/Context.h>
#include <Access/ContextAccess.h>
#include <TableFunctions/TableFunctionMerge.h>
#include <TableFunctions/TableFunctionFactory.h>
#include <TableFunctions/registerTableFunctions.h>
namespace DB
{
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
2021-07-04 15:41:33 +00:00
extern const int BAD_ARGUMENTS;
}
namespace
{
2021-06-07 09:14:29 +00:00
[[noreturn]] void throwNoTablesMatchRegexp(const String & source_database_regexp, const String & source_table_regexp)
{
throw Exception(
2021-07-04 15:41:33 +00:00
ErrorCodes::BAD_ARGUMENTS,
"Error while executing table function merge. Either there is no database, which matches regular expression `{}`, or there are "
"no tables in database matches `{}`, which fit tables expression: {}",
source_database_regexp,
source_database_regexp,
source_table_regexp);
}
}
void TableFunctionMerge::parseArguments(const ASTPtr & ast_function, ContextPtr context)
{
ASTs & args_func = ast_function->children;
if (args_func.size() != 1)
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
"Table function 'merge' requires exactly 2 arguments - name "
"of source database and regexp for table names.");
ASTs & args = args_func.at(0)->children;
if (args.size() != 2)
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
"Table function 'merge' requires exactly 2 arguments - name "
"of source database and regexp for table names.");
auto [is_regexp, database_ast] = StorageMerge::evaluateDatabaseName(args[0], context);
2021-06-25 13:51:17 +00:00
database_is_regexp = is_regexp;
2021-06-27 06:09:23 +00:00
if (!is_regexp)
args[0] = database_ast;
source_database_name_or_regexp = checkAndGetLiteralArgument<String>(database_ast, "database_name");
2021-06-25 13:51:17 +00:00
args[1] = evaluateConstantExpressionAsLiteral(args[1], context);
source_table_regexp = checkAndGetLiteralArgument<String>(args[1], "table_name_regexp");
}
2020-08-26 20:56:30 +00:00
const TableFunctionMerge::DBToTableSetMap & TableFunctionMerge::getSourceDatabasesAndTables(ContextPtr context) const
{
2021-06-04 14:48:48 +00:00
if (source_databases_and_tables)
return *source_databases_and_tables;
2021-06-25 13:51:17 +00:00
source_databases_and_tables.emplace();
2021-06-25 13:51:17 +00:00
/// database_name is not a regexp
if (!database_is_regexp)
{
auto source_tables = getMatchedTablesWithAccess(source_database_name_or_regexp, source_table_regexp, context);
if (source_tables.empty())
throwNoTablesMatchRegexp(source_database_name_or_regexp, source_table_regexp);
(*source_databases_and_tables)[source_database_name_or_regexp] = source_tables;
}
2021-06-07 09:14:29 +00:00
2021-06-25 13:51:17 +00:00
/// database_name is a regexp
else
{
2021-06-25 13:51:17 +00:00
OptimizedRegularExpression database_re(source_database_name_or_regexp);
auto databases = DatabaseCatalog::instance().getDatabases();
for (const auto & db : databases)
if (database_re.match(db.first))
(*source_databases_and_tables)[db.first] = getMatchedTablesWithAccess(db.first, source_table_regexp, context);
if (source_databases_and_tables->empty())
2021-06-25 13:51:17 +00:00
throwNoTablesMatchRegexp(source_database_name_or_regexp, source_table_regexp);
}
2021-06-07 09:14:29 +00:00
return *source_databases_and_tables;
}
ColumnsDescription TableFunctionMerge::getActualTableStructure(ContextPtr context) const
{
2021-06-07 09:14:29 +00:00
for (const auto & db_with_tables : getSourceDatabasesAndTables(context))
{
2021-07-02 02:11:28 +00:00
for (const auto & table : db_with_tables.second)
{
auto storage = DatabaseCatalog::instance().tryGetTable(StorageID{db_with_tables.first, table}, context);
if (storage)
return ColumnsDescription{storage->getInMemoryMetadataPtr()->getColumns().getAllPhysical()};
}
}
2021-06-25 13:51:17 +00:00
throwNoTablesMatchRegexp(source_database_name_or_regexp, source_table_regexp);
}
StoragePtr TableFunctionMerge::executeImpl(const ASTPtr & /*ast_function*/, ContextPtr context, const std::string & table_name, ColumnsDescription /*cached_columns*/) const
{
auto res = std::make_shared<StorageMerge>(
2019-12-04 16:06:55 +00:00
StorageID(getDatabaseName(), table_name),
getActualTableStructure(context),
2021-04-23 12:18:23 +00:00
String{},
2021-06-25 13:51:17 +00:00
source_database_name_or_regexp,
database_is_regexp,
2021-06-04 14:48:48 +00:00
getSourceDatabasesAndTables(context),
context);
res->startup();
return res;
}
2021-07-02 02:11:28 +00:00
TableFunctionMerge::TableSet
2021-06-25 13:51:17 +00:00
TableFunctionMerge::getMatchedTablesWithAccess(const String & database_name, const String & table_regexp, const ContextPtr & context)
{
OptimizedRegularExpression table_re(table_regexp);
auto table_name_match = [&](const String & table_name) { return table_re.match(table_name); };
auto access = context->getAccess();
auto database = DatabaseCatalog::instance().getDatabase(database_name);
bool granted_show_on_all_tables = access->isGranted(AccessType::SHOW_TABLES, database_name);
bool granted_select_on_all_tables = access->isGranted(AccessType::SELECT, database_name);
2021-07-02 02:11:28 +00:00
TableSet tables;
2021-06-25 13:51:17 +00:00
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, database_name, it->name());
if (!granted_show)
continue;
if (!granted_select_on_all_tables)
access->checkAccess(AccessType::SELECT, database_name, it->name());
2021-07-01 14:07:55 +00:00
tables.emplace(it->name());
2021-06-25 13:51:17 +00:00
}
return tables;
}
void registerTableFunctionMerge(TableFunctionFactory & factory)
{
2017-12-02 02:47:12 +00:00
factory.registerFunction<TableFunctionMerge>();
}
}