ClickHouse/src/TableFunctions/TableFunctionView.cpp

75 lines
2.3 KiB
C++

#include <Interpreters/InterpreterSelectWithUnionQuery.h>
#include <Interpreters/InterpreterSelectQueryAnalyzer.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTSelectWithUnionQuery.h>
#include <Storages/StorageView.h>
#include <TableFunctions/ITableFunction.h>
#include <TableFunctions/TableFunctionFactory.h>
#include <TableFunctions/TableFunctionView.h>
#include "registerTableFunctions.h"
namespace DB
{
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
}
const ASTSelectWithUnionQuery & TableFunctionView::getSelectQuery() const
{
return *create.select;
}
std::vector<size_t> TableFunctionView::skipAnalysisForArguments(const QueryTreeNodePtr &, ContextPtr) const
{
return {0};
}
void TableFunctionView::parseArguments(const ASTPtr & ast_function, ContextPtr /*context*/)
{
const auto * function = ast_function->as<ASTFunction>();
if (function)
{
if (auto * select = function->tryGetQueryArgument())
{
create.set(create.select, select->clone());
return;
}
}
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Table function '{}' requires a query argument.", getName());
}
ColumnsDescription TableFunctionView::getActualTableStructure(ContextPtr context, bool /*is_insert_query*/) const
{
assert(create.select);
assert(create.children.size() == 1);
assert(create.children[0]->as<ASTSelectWithUnionQuery>());
Block sample_block;
if (context->getSettingsRef().allow_experimental_analyzer)
sample_block = InterpreterSelectQueryAnalyzer::getSampleBlock(create.children[0], context);
else
sample_block = InterpreterSelectWithUnionQuery::getSampleBlock(create.children[0], context);
return ColumnsDescription(sample_block.getNamesAndTypesList());
}
StoragePtr TableFunctionView::executeImpl(
const ASTPtr & /*ast_function*/, ContextPtr context, const std::string & table_name, ColumnsDescription /*cached_columns*/, bool is_insert_query) const
{
auto columns = getActualTableStructure(context, is_insert_query);
auto res = std::make_shared<StorageView>(StorageID(getDatabaseName(), table_name), create, columns, "");
res->startup();
return res;
}
void registerTableFunctionView(TableFunctionFactory & factory)
{
factory.registerFunction<TableFunctionView>({.documentation = {}, .allow_readonly = true});
}
}