#include #include #include #include #include #include #include #include #include #include "registerTableFunctions.h" namespace DB { namespace ErrorCodes { extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; } namespace { /* null(structure) - creates a temporary null storage * * Used for testing purposes, for convenience writing tests and demos. */ class TableFunctionNull : public ITableFunction { public: static constexpr auto name = "null"; std::string getName() const override { return name; } bool needStructureHint() const override { return structure == "auto"; } void setStructureHint(const ColumnsDescription & structure_hint_) override { structure_hint = structure_hint_; } private: StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const String & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; const char * getStorageTypeName() const override { return "Null"; } void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; String structure = "auto"; ColumnsDescription structure_hint; const ColumnsDescription default_structure{NamesAndTypesList{{"dummy", std::make_shared()}}}; }; void TableFunctionNull::parseArguments(const ASTPtr & ast_function, ContextPtr context) { const auto * function = ast_function->as(); if (!function || !function->arguments) throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Table function '{}' requires 'structure'", getName()); const auto & arguments = function->arguments->children; if (!arguments.empty() && arguments.size() != 1) throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Table function '{}' requires 'structure' argument or empty argument", getName()); if (!arguments.empty()) structure = checkAndGetLiteralArgument(evaluateConstantExpressionOrIdentifierAsLiteral(arguments[0], context), "structure"); } ColumnsDescription TableFunctionNull::getActualTableStructure(ContextPtr context, bool /*is_insert_query*/) const { if (structure != "auto") return parseColumnsListFromString(structure, context); return default_structure; } StoragePtr TableFunctionNull::executeImpl(const ASTPtr & /*ast_function*/, ContextPtr context, const std::string & table_name, ColumnsDescription /*cached_columns*/, bool /*is_insert_query*/) const { ColumnsDescription columns; if (structure != "auto") columns = parseColumnsListFromString(structure, context); else if (!structure_hint.empty()) columns = structure_hint; else columns = default_structure; auto res = std::make_shared(StorageID(getDatabaseName(), table_name), columns, ConstraintsDescription(), String{}); res->startup(); return res; } } void registerTableFunctionNull(TableFunctionFactory & factory) { factory.registerFunction({.documentation = {}, .allow_readonly = true}); } }