2020-08-28 14:07:14 +00:00
|
|
|
#include <Interpreters/InterpreterSelectWithUnionQuery.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;
|
|
|
|
}
|
|
|
|
|
2022-02-18 14:42:32 +00:00
|
|
|
|
|
|
|
const ASTSelectWithUnionQuery & TableFunctionView::getSelectQuery() const
|
|
|
|
{
|
|
|
|
return *create.select;
|
|
|
|
}
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
void TableFunctionView::parseArguments(const ASTPtr & ast_function, ContextPtr /*context*/)
|
2020-08-28 14:07:14 +00:00
|
|
|
{
|
2020-10-14 12:19:29 +00:00
|
|
|
const auto * function = ast_function->as<ASTFunction>();
|
|
|
|
if (function)
|
2020-08-28 14:07:14 +00:00
|
|
|
{
|
2020-09-09 07:41:38 +00:00
|
|
|
if (auto * select = function->tryGetQueryArgument())
|
2020-08-28 14:07:14 +00:00
|
|
|
{
|
2020-10-14 12:19:29 +00:00
|
|
|
create.set(create.select, select->clone());
|
|
|
|
return;
|
2020-08-28 14:07:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
throw Exception("Table function '" + getName() + "' requires a query argument.", ErrorCodes::BAD_ARGUMENTS);
|
2020-09-02 17:39:37 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
ColumnsDescription TableFunctionView::getActualTableStructure(ContextPtr context) const
|
2020-10-14 12:19:29 +00:00
|
|
|
{
|
|
|
|
assert(create.select);
|
|
|
|
assert(create.children.size() == 1);
|
|
|
|
assert(create.children[0]->as<ASTSelectWithUnionQuery>());
|
|
|
|
auto sample = InterpreterSelectWithUnionQuery::getSampleBlock(create.children[0], context);
|
|
|
|
return ColumnsDescription(sample.getNamesAndTypesList());
|
|
|
|
}
|
|
|
|
|
2021-01-02 04:51:13 +00:00
|
|
|
StoragePtr TableFunctionView::executeImpl(
|
2021-04-10 23:33:54 +00:00
|
|
|
const ASTPtr & /*ast_function*/, ContextPtr context, const std::string & table_name, ColumnsDescription /*cached_columns*/) const
|
2020-10-14 12:19:29 +00:00
|
|
|
{
|
|
|
|
auto columns = getActualTableStructure(context);
|
2021-06-22 14:32:02 +00:00
|
|
|
auto res = StorageView::create(StorageID(getDatabaseName(), table_name), create, columns, "");
|
2020-10-14 12:19:29 +00:00
|
|
|
res->startup();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-08-28 14:07:14 +00:00
|
|
|
void registerTableFunctionView(TableFunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<TableFunctionView>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|