2018-03-02 05:03:28 +00:00
|
|
|
#include <TableFunctions/ITableFunction.h>
|
2020-04-06 05:19:40 +00:00
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Storages/StorageFactory.h>
|
2020-10-14 12:19:29 +00:00
|
|
|
#include <Storages/StorageTableFunction.h>
|
2020-04-06 05:19:40 +00:00
|
|
|
#include <Access/AccessFlags.h>
|
2018-03-02 05:03:28 +00:00
|
|
|
#include <Common/ProfileEvents.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
|
|
|
extern const Event TableFunctionExecute;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-10-14 12:19:29 +00:00
|
|
|
StoragePtr ITableFunction::execute(const ASTPtr & ast_function, const Context & context, const std::string & table_name,
|
|
|
|
ColumnsDescription cached_columns) const
|
2018-03-02 05:03:28 +00:00
|
|
|
{
|
|
|
|
ProfileEvents::increment(ProfileEvents::TableFunctionExecute);
|
2020-04-06 05:19:40 +00:00
|
|
|
context.checkAccess(AccessType::CREATE_TEMPORARY_TABLE | StorageFactory::instance().getSourceAccessType(getStorageTypeName()));
|
2020-10-14 12:19:29 +00:00
|
|
|
|
2021-03-23 11:29:29 +00:00
|
|
|
if (cached_columns.empty())
|
2020-10-14 12:19:29 +00:00
|
|
|
return executeImpl(ast_function, context, table_name, std::move(cached_columns));
|
|
|
|
|
2021-03-23 11:29:29 +00:00
|
|
|
/// We have table structure, so it's CREATE AS table_function().
|
|
|
|
/// We should use global context here because there will be no query context on server startup
|
|
|
|
/// and because storage lifetime is bigger than query context lifetime.
|
|
|
|
const Context & global_context = context.getGlobalContext();
|
|
|
|
if (hasStaticStructure() && cached_columns == getActualTableStructure(context))
|
|
|
|
return executeImpl(ast_function, global_context, table_name, std::move(cached_columns));
|
|
|
|
|
|
|
|
auto this_table_function = shared_from_this();
|
|
|
|
auto get_storage = [=, &global_context]() -> StoragePtr
|
2020-10-14 12:19:29 +00:00
|
|
|
{
|
2021-03-23 11:29:29 +00:00
|
|
|
return this_table_function->executeImpl(ast_function, global_context, table_name, cached_columns);
|
2020-10-14 12:19:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// It will request actual table structure and create underlying storage lazily
|
|
|
|
return std::make_shared<StorageTableFunctionProxy>(StorageID(getDatabaseName(), table_name), std::move(get_storage),
|
|
|
|
std::move(cached_columns), needStructureConversion());
|
2018-03-02 05:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|