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-08-31 17:45:21 +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-09-02 15:07:53 +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-08-31 17:45:21 +00:00
|
|
|
|
2020-09-01 14:41:49 +00:00
|
|
|
bool no_conversion_required = hasStaticStructure() && cached_columns == getActualTableStructure(context);
|
2020-08-31 17:45:21 +00:00
|
|
|
if (cached_columns.empty() || no_conversion_required)
|
2020-09-02 15:07:53 +00:00
|
|
|
return executeImpl(ast_function, context, table_name, std::move(cached_columns));
|
2020-08-31 17:45:21 +00:00
|
|
|
|
|
|
|
auto get_storage = [=, tf = shared_from_this()]() -> StoragePtr
|
|
|
|
{
|
2020-09-02 15:07:53 +00:00
|
|
|
return tf->executeImpl(ast_function, context, table_name, cached_columns);
|
2020-08-31 17:45:21 +00:00
|
|
|
};
|
|
|
|
|
2020-09-02 15:07:53 +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
|
|
|
}
|
|
|
|
|
|
|
|
}
|