#include #include #include #include #include #include #include #include "registerTableFunctions.h" namespace DB { namespace ErrorCodes { extern const int BAD_ARGUMENTS; } const ASTSelectWithUnionQuery & TableFunctionView::getSelectQuery() const { return *create.select; } void TableFunctionView::parseArguments(const ASTPtr & ast_function, ContextPtr /*context*/) { const auto * function = ast_function->as(); if (function) { if (auto * select = function->tryGetQueryArgument()) { create.set(create.select, select->clone()); return; } } throw Exception("Table function '" + getName() + "' requires a query argument.", ErrorCodes::BAD_ARGUMENTS); } ColumnsDescription TableFunctionView::getActualTableStructure(ContextPtr context) const { assert(create.select); assert(create.children.size() == 1); assert(create.children[0]->as()); auto sample = InterpreterSelectWithUnionQuery::getSampleBlock(create.children[0], context); return ColumnsDescription(sample.getNamesAndTypesList()); } StoragePtr TableFunctionView::executeImpl( const ASTPtr & /*ast_function*/, ContextPtr context, const std::string & table_name, ColumnsDescription /*cached_columns*/) const { auto columns = getActualTableStructure(context); auto res = StorageView::create(StorageID(getDatabaseName(), table_name), create, columns, ""); res->startup(); return res; } void registerTableFunctionView(TableFunctionFactory & factory) { factory.registerFunction(); } }