#include #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; } std::vector TableFunctionView::skipAnalysisForArguments(const QueryTreeNodePtr &, ContextPtr) const { return {0}; } 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(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()); 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(StorageID(getDatabaseName(), table_name), create, columns, ""); res->startup(); return res; } void registerTableFunctionView(TableFunctionFactory & factory) { factory.registerFunction({.documentation = {}, .allow_readonly = true}); } }