diff --git a/src/TableFunctions/Hive/TableFunctionHive.cpp b/src/TableFunctions/Hive/TableFunctionHive.cpp index ebebee13092..d8885087532 100644 --- a/src/TableFunctions/Hive/TableFunctionHive.cpp +++ b/src/TableFunctions/Hive/TableFunctionHive.cpp @@ -1,12 +1,13 @@ -#include +#include "config.h" #if USE_HIVE + +#include +#include #include #include #include -#include #include -#include #include #include #include @@ -15,75 +16,111 @@ #include #include #include -#include namespace DB { - namespace ErrorCodes - { - extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; - } - void TableFunctionHive::parseArguments(const ASTPtr & ast_function_, ContextPtr context_) - { - ASTs & args_func = ast_function_->children; - if (args_func.size() != 1) - throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Table function '{}' must have arguments.", getName()); - - ASTs & args = args_func.at(0)->children; - - if (args.size() != 5) - throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, - "The signature of function {} is:\n - hive_url, hive_database, hive_table, structure, partition_by_keys", - getName()); - - for (auto & arg : args) - arg = evaluateConstantExpressionOrIdentifierAsLiteral(arg, context_); - - hive_metastore_url = checkAndGetLiteralArgument(args[0], "hive_url"); - hive_database = checkAndGetLiteralArgument(args[1], "hive_database"); - hive_table = checkAndGetLiteralArgument(args[2], "hive_table"); - table_structure = checkAndGetLiteralArgument(args[3], "structure"); - partition_by_def = checkAndGetLiteralArgument(args[4], "partition_by_keys"); - - actual_columns = parseColumnsListFromString(table_structure, context_); - } - - ColumnsDescription TableFunctionHive::getActualTableStructure(ContextPtr /*context_*/, bool /*is_insert_query*/) const { return actual_columns; } - - StoragePtr TableFunctionHive::executeImpl( - const ASTPtr & /*ast_function_*/, - ContextPtr context_, - const std::string & table_name_, - ColumnsDescription /*cached_columns_*/, - bool /*is_insert_query*/) const - { - const Settings & settings = context_->getSettings(); - ParserExpression partition_by_parser; - ASTPtr partition_by_ast = parseQuery( - partition_by_parser, - "(" + partition_by_def + ")", - "partition by declaration list", - settings.max_query_size, - settings.max_parser_depth); - StoragePtr storage; - storage = std::make_shared( - hive_metastore_url, - hive_database, - hive_table, - StorageID(getDatabaseName(), table_name_), - actual_columns, - ConstraintsDescription{}, - "", - partition_by_ast, - std::make_unique(), - context_); - - return storage; - } +namespace ErrorCodes +{ + extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; +} - void registerTableFunctionHive(TableFunctionFactory & factory_) { factory_.registerFunction(); } +namespace +{ + +class TableFunctionHive : public ITableFunction +{ +public: + static constexpr auto name = "hive"; + static constexpr auto storage_type_name = "Hive"; + std::string getName() const override { return name; } + + bool hasStaticStructure() const override { return true; } + + StoragePtr executeImpl( + const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; + + const char * getStorageTypeName() const override { return storage_type_name; } + ColumnsDescription getActualTableStructure(ContextPtr, bool is_insert_query) const override; + void parseArguments(const ASTPtr & ast_function_, ContextPtr context_) override; + +private: + Poco::Logger * logger = &Poco::Logger::get("TableFunctionHive"); + + String cluster_name; + String hive_metastore_url; + String hive_database; + String hive_table; + String table_structure; + String partition_by_def; + + ColumnsDescription actual_columns; +}; + +void TableFunctionHive::parseArguments(const ASTPtr & ast_function_, ContextPtr context_) +{ + ASTs & args_func = ast_function_->children; + if (args_func.size() != 1) + throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Table function '{}' must have arguments.", getName()); + + ASTs & args = args_func.at(0)->children; + + if (args.size() != 5) + throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, + "The signature of function {} is:\n - hive_url, hive_database, hive_table, structure, partition_by_keys", + getName()); + + for (auto & arg : args) + arg = evaluateConstantExpressionOrIdentifierAsLiteral(arg, context_); + + hive_metastore_url = checkAndGetLiteralArgument(args[0], "hive_url"); + hive_database = checkAndGetLiteralArgument(args[1], "hive_database"); + hive_table = checkAndGetLiteralArgument(args[2], "hive_table"); + table_structure = checkAndGetLiteralArgument(args[3], "structure"); + partition_by_def = checkAndGetLiteralArgument(args[4], "partition_by_keys"); + + actual_columns = parseColumnsListFromString(table_structure, context_); +} + +ColumnsDescription TableFunctionHive::getActualTableStructure(ContextPtr /*context_*/, bool /*is_insert_query*/) const { return actual_columns; } + +StoragePtr TableFunctionHive::executeImpl( + const ASTPtr & /*ast_function_*/, + ContextPtr context_, + const std::string & table_name_, + ColumnsDescription /*cached_columns_*/, + bool /*is_insert_query*/) const +{ + const Settings & settings = context_->getSettings(); + ParserExpression partition_by_parser; + ASTPtr partition_by_ast = parseQuery( + partition_by_parser, + "(" + partition_by_def + ")", + "partition by declaration list", + settings.max_query_size, + settings.max_parser_depth); + StoragePtr storage; + storage = std::make_shared( + hive_metastore_url, + hive_database, + hive_table, + StorageID(getDatabaseName(), table_name_), + actual_columns, + ConstraintsDescription{}, + "", + partition_by_ast, + std::make_unique(), + context_); + + return storage; +} } + + +void registerTableFunctionHive(TableFunctionFactory & factory_) { factory_.registerFunction(); } + +} + #endif diff --git a/src/TableFunctions/Hive/TableFunctionHive.h b/src/TableFunctions/Hive/TableFunctionHive.h deleted file mode 100644 index 5e48be46ce1..00000000000 --- a/src/TableFunctions/Hive/TableFunctionHive.h +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once -#include "config.h" - -#if USE_HIVE -#include -#include -namespace DB -{ -class Context; -class TableFunctionHive : public ITableFunction -{ -public: - static constexpr auto name = "hive"; - static constexpr auto storage_type_name = "Hive"; - std::string getName() const override { return name; } - - bool hasStaticStructure() const override { return true; } - - StoragePtr executeImpl( - const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; - - const char * getStorageTypeName() const override { return storage_type_name; } - ColumnsDescription getActualTableStructure(ContextPtr, bool is_insert_query) const override; - void parseArguments(const ASTPtr & ast_function_, ContextPtr context_) override; - -private: - Poco::Logger * logger = &Poco::Logger::get("TableFunctionHive"); - - String cluster_name; - String hive_metastore_url; - String hive_database; - String hive_table; - String table_structure; - String partition_by_def; - - ColumnsDescription actual_columns; -}; -} -#endif diff --git a/src/TableFunctions/ITableFunctionXDBC.cpp b/src/TableFunctions/ITableFunctionXDBC.cpp index 59702259b35..b1746ea769f 100644 --- a/src/TableFunctions/ITableFunctionXDBC.cpp +++ b/src/TableFunctions/ITableFunctionXDBC.cpp @@ -9,12 +9,16 @@ #include #include #include -#include #include #include #include #include "registerTableFunctions.h" +#include +#include + +#include "config.h" + namespace DB { @@ -24,6 +28,79 @@ namespace ErrorCodes extern const int LOGICAL_ERROR; } +namespace +{ + +/** + * Base class for table functions, that works over external bridge + * Xdbc (Xdbc connect string, table) - creates a temporary StorageXDBC. + */ +class ITableFunctionXDBC : public ITableFunction +{ +private: + StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; + + /* A factory method to create bridge helper, that will assist in remote interaction */ + virtual BridgeHelperPtr createBridgeHelper(ContextPtr context, + Poco::Timespan http_timeout_, + const std::string & connection_string_, + bool use_connection_pooling_) const = 0; + + ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; + + void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; + + void startBridgeIfNot(ContextPtr context) const; + + String connection_string; + String schema_name; + String remote_table_name; + mutable BridgeHelperPtr helper; +}; + +class TableFunctionJDBC : public ITableFunctionXDBC +{ +public: + static constexpr auto name = "jdbc"; + std::string getName() const override + { + return name; + } + +private: + BridgeHelperPtr createBridgeHelper(ContextPtr context, + Poco::Timespan http_timeout_, + const std::string & connection_string_, + bool use_connection_pooling_) const override + { + return std::make_shared>(context, http_timeout_, connection_string_, use_connection_pooling_); + } + + const char * getStorageTypeName() const override { return "JDBC"; } +}; + +class TableFunctionODBC : public ITableFunctionXDBC +{ +public: + static constexpr auto name = "odbc"; + std::string getName() const override + { + return name; + } + +private: + BridgeHelperPtr createBridgeHelper(ContextPtr context, + Poco::Timespan http_timeout_, + const std::string & connection_string_, + bool use_connection_pooling_) const override + { + return std::make_shared>(context, http_timeout_, connection_string_, use_connection_pooling_); + } + + const char * getStorageTypeName() const override { return "ODBC"; } +}; + + void ITableFunctionXDBC::parseArguments(const ASTPtr & ast_function, ContextPtr context) { const auto & args_func = ast_function->as(); @@ -102,6 +179,8 @@ StoragePtr ITableFunctionXDBC::executeImpl(const ASTPtr & /*ast_function*/, Cont return result; } +} + void registerTableFunctionJDBC(TableFunctionFactory & factory) { factory.registerFunction(); diff --git a/src/TableFunctions/ITableFunctionXDBC.h b/src/TableFunctions/ITableFunctionXDBC.h deleted file mode 100644 index da0fa83033b..00000000000 --- a/src/TableFunctions/ITableFunctionXDBC.h +++ /dev/null @@ -1,80 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include "config.h" - -namespace DB -{ -/** - * Base class for table functions, that works over external bridge - * Xdbc (Xdbc connect string, table) - creates a temporary StorageXDBC. - */ -class ITableFunctionXDBC : public ITableFunction -{ -private: - StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; - - /* A factory method to create bridge helper, that will assist in remote interaction */ - virtual BridgeHelperPtr createBridgeHelper(ContextPtr context, - Poco::Timespan http_timeout_, - const std::string & connection_string_, - bool use_connection_pooling_) const = 0; - - ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; - - void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; - - void startBridgeIfNot(ContextPtr context) const; - - String connection_string; - String schema_name; - String remote_table_name; - mutable BridgeHelperPtr helper; -}; - -class TableFunctionJDBC : public ITableFunctionXDBC -{ -public: - static constexpr auto name = "jdbc"; - std::string getName() const override - { - return name; - } - -private: - BridgeHelperPtr createBridgeHelper(ContextPtr context, - Poco::Timespan http_timeout_, - const std::string & connection_string_, - bool use_connection_pooling_) const override - { - return std::make_shared>(context, http_timeout_, connection_string_, use_connection_pooling_); - } - - const char * getStorageTypeName() const override { return "JDBC"; } -}; - -class TableFunctionODBC : public ITableFunctionXDBC -{ -public: - static constexpr auto name = "odbc"; - std::string getName() const override - { - return name; - } - -private: - BridgeHelperPtr createBridgeHelper(ContextPtr context, - Poco::Timespan http_timeout_, - const std::string & connection_string_, - bool use_connection_pooling_) const override - { - return std::make_shared>(context, http_timeout_, connection_string_, use_connection_pooling_); - } - - const char * getStorageTypeName() const override { return "ODBC"; } -}; -} diff --git a/src/TableFunctions/TableFunctionDictionary.cpp b/src/TableFunctions/TableFunctionDictionary.cpp index f0060acb411..7506036b1b2 100644 --- a/src/TableFunctions/TableFunctionDictionary.cpp +++ b/src/TableFunctions/TableFunctionDictionary.cpp @@ -6,7 +6,6 @@ #include #include -#include #include #include @@ -15,6 +14,7 @@ #include + namespace DB { @@ -87,6 +87,7 @@ StoragePtr TableFunctionDictionary::executeImpl( return result; } + void registerTableFunctionDictionary(TableFunctionFactory & factory) { factory.registerFunction(); diff --git a/src/TableFunctions/TableFunctionDictionary.h b/src/TableFunctions/TableFunctionDictionary.h index d0beb292fe1..2cf722c95f0 100644 --- a/src/TableFunctions/TableFunctionDictionary.h +++ b/src/TableFunctions/TableFunctionDictionary.h @@ -27,4 +27,6 @@ public: private: String dictionary_name; ColumnsDescription dictionary_columns; -};} +}; + +} diff --git a/src/TableFunctions/TableFunctionExecutable.cpp b/src/TableFunctions/TableFunctionExecutable.cpp index 5a64a988156..209446dc9dd 100644 --- a/src/TableFunctions/TableFunctionExecutable.cpp +++ b/src/TableFunctions/TableFunctionExecutable.cpp @@ -1,4 +1,3 @@ -#include #include #include @@ -12,9 +11,7 @@ #include #include #include -#include #include -#include #include #include "registerTableFunctions.h" @@ -30,6 +27,44 @@ namespace ErrorCodes extern const int ILLEGAL_TYPE_OF_ARGUMENT; } +namespace +{ + +/* executable(script_name_optional_arguments, format, structure, input_query) - creates a temporary storage from executable file + * + * + * The file must be in the clickhouse data directory. + * The relative path begins with the clickhouse data directory. + */ +class TableFunctionExecutable : public ITableFunction +{ +public: + static constexpr auto name = "executable"; + + std::string getName() const override { return name; } + + bool hasStaticStructure() const override { return true; } + +private: + StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; + + const char * getStorageTypeName() const override { return "Executable"; } + + ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; + + std::vector skipAnalysisForArguments(const QueryTreeNodePtr & query_node_table_function, ContextPtr context) const override; + + void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; + + String script_name; + std::vector arguments; + String format; + String structure; + std::vector input_queries; + ASTPtr settings_query = nullptr; +}; + + std::vector TableFunctionExecutable::skipAnalysisForArguments(const QueryTreeNodePtr & query_node_table_function, ContextPtr) const { const auto & table_function_node = query_node_table_function->as(); @@ -140,6 +175,8 @@ StoragePtr TableFunctionExecutable::executeImpl(const ASTPtr & /*ast_function*/, return storage; } +} + void registerTableFunctionExecutable(TableFunctionFactory & factory) { factory.registerFunction(); diff --git a/src/TableFunctions/TableFunctionExecutable.h b/src/TableFunctions/TableFunctionExecutable.h deleted file mode 100644 index aa595312fe4..00000000000 --- a/src/TableFunctions/TableFunctionExecutable.h +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once - -#include - -namespace DB -{ - -class Context; -class ASTSetQuery; - -/* executable(script_name_optional_arguments, format, structure, input_query) - creates a temporary storage from executable file - * - * - * The file must be in the clickhouse data directory. - * The relative path begins with the clickhouse data directory. - */ -class TableFunctionExecutable : public ITableFunction -{ -public: - static constexpr auto name = "executable"; - - std::string getName() const override { return name; } - - bool hasStaticStructure() const override { return true; } - -private: - StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; - - const char * getStorageTypeName() const override { return "Executable"; } - - ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; - - std::vector skipAnalysisForArguments(const QueryTreeNodePtr & query_node_table_function, ContextPtr context) const override; - - void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; - - String script_name; - std::vector arguments; - String format; - String structure; - std::vector input_queries; - ASTPtr settings_query = nullptr; -}; -} diff --git a/src/TableFunctions/TableFunctionExplain.cpp b/src/TableFunctions/TableFunctionExplain.cpp index f127979d92a..cf986e1817d 100644 --- a/src/TableFunctions/TableFunctionExplain.cpp +++ b/src/TableFunctions/TableFunctionExplain.cpp @@ -1,27 +1,54 @@ #include #include -#include #include #include #include #include #include #include -#include #include #include #include #include +#include #include + namespace DB { + namespace ErrorCodes { extern const int LOGICAL_ERROR; extern const int BAD_ARGUMENTS; } +namespace +{ + +class TableFunctionExplain : public ITableFunction +{ +public: + static constexpr auto name = "viewExplain"; + + std::string getName() const override { return name; } + +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 "Explain"; } + + std::vector skipAnalysisForArguments(const QueryTreeNodePtr & query_node_table_function, ContextPtr context) const override; + + void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; + + ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; + + InterpreterExplainQuery getInterpreter(ContextPtr context) const; + + ASTPtr query = nullptr; +}; + std::vector TableFunctionExplain::skipAnalysisForArguments(const QueryTreeNodePtr & query_node_table_function, ContextPtr /*context*/) const { const auto & table_function_node = query_node_table_function->as(); @@ -145,6 +172,8 @@ InterpreterExplainQuery TableFunctionExplain::getInterpreter(ContextPtr context) return InterpreterExplainQuery(query, context); } +} + void registerTableFunctionExplain(TableFunctionFactory & factory) { factory.registerFunction({.documentation = { diff --git a/src/TableFunctions/TableFunctionExplain.h b/src/TableFunctions/TableFunctionExplain.h deleted file mode 100644 index 2eb7e35d0b5..00000000000 --- a/src/TableFunctions/TableFunctionExplain.h +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once - -#include -#include -#include -#include - - -namespace DB -{ - -class TableFunctionExplain : public ITableFunction -{ -public: - static constexpr auto name = "viewExplain"; - - std::string getName() const override { return name; } - -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 "Explain"; } - - std::vector skipAnalysisForArguments(const QueryTreeNodePtr & query_node_table_function, ContextPtr context) const override; - - void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; - - ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; - - InterpreterExplainQuery getInterpreter(ContextPtr context) const; - - ASTPtr query = nullptr; -}; - -} diff --git a/src/TableFunctions/TableFunctionFile.cpp b/src/TableFunctions/TableFunctionFile.cpp index f75c56e6523..52d9ca26f37 100644 --- a/src/TableFunctions/TableFunctionFile.cpp +++ b/src/TableFunctions/TableFunctionFile.cpp @@ -1,5 +1,5 @@ -#include #include +#include #include "Parsers/IAST_fwd.h" #include "registerTableFunctions.h" @@ -12,6 +12,7 @@ #include #include + namespace DB { @@ -20,6 +21,42 @@ namespace ErrorCodes extern const int BAD_ARGUMENTS; } +namespace +{ + +/* file(path, format[, structure, compression]) - creates a temporary storage from file + * + * The file must be in the clickhouse data directory. + * The relative path begins with the clickhouse data directory. + */ +class TableFunctionFile : public ITableFunctionFileLike +{ +public: + static constexpr auto name = "file"; + std::string getName() const override + { + return name; + } + + ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; + + std::unordered_set getVirtualsToCheckBeforeUsingStructureHint() const override + { + return {"_path", "_file"}; + } + +protected: + int fd = -1; + void parseFirstArguments(const ASTPtr & arg, const ContextPtr & context) override; + String getFormatFromFirstArgument() override; + +private: + StoragePtr getStorage( + const String & source, const String & format_, const ColumnsDescription & columns, ContextPtr global_context, + const std::string & table_name, const std::string & compression_method_) const override; + const char * getStorageTypeName() const override { return "File"; } +}; + void TableFunctionFile::parseFirstArguments(const ASTPtr & arg, const ContextPtr & context) { if (context->getApplicationType() != Context::ApplicationType::LOCAL) @@ -111,6 +148,8 @@ ColumnsDescription TableFunctionFile::getActualTableStructure(ContextPtr context return parseColumnsListFromString(structure, context); } +} + void registerTableFunctionFile(TableFunctionFactory & factory) { factory.registerFunction(); diff --git a/src/TableFunctions/TableFunctionFile.h b/src/TableFunctions/TableFunctionFile.h deleted file mode 100644 index 439ae87b4ae..00000000000 --- a/src/TableFunctions/TableFunctionFile.h +++ /dev/null @@ -1,42 +0,0 @@ -#pragma once - -#include - - -namespace DB -{ - -/* file(path, format[, structure, compression]) - creates a temporary storage from file - * - * The file must be in the clickhouse data directory. - * The relative path begins with the clickhouse data directory. - */ -class TableFunctionFile : public ITableFunctionFileLike -{ -public: - static constexpr auto name = "file"; - std::string getName() const override - { - return name; - } - - ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; - - std::unordered_set getVirtualsToCheckBeforeUsingStructureHint() const override - { - return {"_path", "_file"}; - } - -protected: - int fd = -1; - void parseFirstArguments(const ASTPtr & arg, const ContextPtr & context) override; - String getFormatFromFirstArgument() override; - -private: - StoragePtr getStorage( - const String & source, const String & format_, const ColumnsDescription & columns, ContextPtr global_context, - const std::string & table_name, const std::string & compression_method_) const override; - const char * getStorageTypeName() const override { return "File"; } -}; - -} diff --git a/src/TableFunctions/TableFunctionFormat.cpp b/src/TableFunctions/TableFunctionFormat.cpp index 3afe7ffde58..4b6d0f70c0a 100644 --- a/src/TableFunctions/TableFunctionFormat.cpp +++ b/src/TableFunctions/TableFunctionFormat.cpp @@ -18,7 +18,6 @@ #include #include -#include #include @@ -31,6 +30,32 @@ namespace ErrorCodes extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; } +namespace +{ + +/* format(format_name, data) - ... + */ +class TableFunctionFormat : public ITableFunction +{ +public: + static constexpr auto name = "format"; + std::string getName() const override { return name; } + bool hasStaticStructure() const override { return false; } + +private: + StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; + const char * getStorageTypeName() const override { return "Values"; } + + ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; + void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; + + Block parseData(ColumnsDescription columns, ContextPtr context) const; + + String format; + String data; + String structure = "auto"; +}; + void TableFunctionFormat::parseArguments(const ASTPtr & ast_function, ContextPtr context) { ASTs & args_func = ast_function->children; @@ -104,7 +129,7 @@ StoragePtr TableFunctionFormat::executeImpl(const ASTPtr & /*ast_function*/, Con return res; } -static const FunctionDocumentation format_table_function_documentation = +const FunctionDocumentation format_table_function_documentation = { .description=R"( Extracts table structure from data and parses it according to specified input format. @@ -168,8 +193,12 @@ Result: .categories{"format", "table-functions"} }; +} + + void registerTableFunctionFormat(TableFunctionFactory & factory) { factory.registerFunction({format_table_function_documentation, false}, TableFunctionFactory::CaseInsensitive); } + } diff --git a/src/TableFunctions/TableFunctionFormat.h b/src/TableFunctions/TableFunctionFormat.h deleted file mode 100644 index e20e8b6ea4b..00000000000 --- a/src/TableFunctions/TableFunctionFormat.h +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include - - -namespace DB -{ - -class Context; - -/* format(format_name, data) - ... - */ -class TableFunctionFormat : public ITableFunction -{ -public: - static constexpr auto name = "format"; - std::string getName() const override { return name; } - bool hasStaticStructure() const override { return false; } - -private: - StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; - const char * getStorageTypeName() const override { return "Values"; } - - ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; - void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; - - Block parseData(ColumnsDescription columns, ContextPtr context) const; - - String format; - String data; - String structure = "auto"; -}; - -} diff --git a/src/TableFunctions/TableFunctionGenerateRandom.cpp b/src/TableFunctions/TableFunctionGenerateRandom.cpp index c6a9154cc66..af284594987 100644 --- a/src/TableFunctions/TableFunctionGenerateRandom.cpp +++ b/src/TableFunctions/TableFunctionGenerateRandom.cpp @@ -8,7 +8,6 @@ #include #include -#include #include #include #include @@ -28,6 +27,36 @@ namespace ErrorCodes extern const int LOGICAL_ERROR; } +namespace +{ + +/* generateRandom([structure, max_array_length, max_string_length, random_seed]) + * - creates a temporary storage that generates columns with random data + */ +class TableFunctionGenerateRandom : public ITableFunction +{ +public: + static constexpr auto name = "generateRandom"; + std::string getName() const override { return name; } + bool hasStaticStructure() const override { return structure != "auto"; } + + 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 std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; + const char * getStorageTypeName() const override { return "GenerateRandom"; } + + ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; + void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; + + String structure = "auto"; + UInt64 max_string_length = 10; + UInt64 max_array_length = 10; + std::optional random_seed; + ColumnsDescription structure_hint; +}; + void TableFunctionGenerateRandom::parseArguments(const ASTPtr & ast_function, ContextPtr context) { ASTs & args_func = ast_function->children; @@ -122,11 +151,11 @@ StoragePtr TableFunctionGenerateRandom::executeImpl(const ASTPtr & /*ast_functio return res; } +} + void registerTableFunctionGenerate(TableFunctionFactory & factory) { factory.registerFunction({.documentation = {}, .allow_readonly = true}); } } - - diff --git a/src/TableFunctions/TableFunctionGenerateRandom.h b/src/TableFunctions/TableFunctionGenerateRandom.h deleted file mode 100644 index a5d11ce0af6..00000000000 --- a/src/TableFunctions/TableFunctionGenerateRandom.h +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -#include - -namespace DB -{ - -/* generateRandom([structure, max_array_length, max_string_length, random_seed]) - * - creates a temporary storage that generates columns with random data - */ -class TableFunctionGenerateRandom : public ITableFunction -{ -public: - static constexpr auto name = "generateRandom"; - std::string getName() const override { return name; } - bool hasStaticStructure() const override { return structure != "auto"; } - - 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 std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; - const char * getStorageTypeName() const override { return "GenerateRandom"; } - - ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; - void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; - - String structure = "auto"; - UInt64 max_string_length = 10; - UInt64 max_array_length = 10; - std::optional random_seed; - ColumnsDescription structure_hint; -}; - - -} diff --git a/src/TableFunctions/TableFunctionInput.cpp b/src/TableFunctions/TableFunctionInput.cpp index 658a55c6fc4..29a377ceaab 100644 --- a/src/TableFunctions/TableFunctionInput.cpp +++ b/src/TableFunctions/TableFunctionInput.cpp @@ -1,4 +1,3 @@ -#include #include #include #include @@ -21,6 +20,31 @@ namespace ErrorCodes extern const int CANNOT_EXTRACT_TABLE_STRUCTURE; } +namespace +{ + +/* input(structure) - allows to make INSERT SELECT from incoming stream of data + */ +class TableFunctionInput : public ITableFunction +{ +public: + static constexpr auto name = "input"; + std::string getName() const override { return name; } + bool hasStaticStructure() const override { return true; } + bool needStructureHint() const override { return true; } + void setStructureHint(const ColumnsDescription & structure_hint_) override { structure_hint = structure_hint_; } + +private: + StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; + const char * getStorageTypeName() const override { return "Input"; } + + ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; + void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; + + String structure; + ColumnsDescription structure_hint; +}; + void TableFunctionInput::parseArguments(const ASTPtr & ast_function, ContextPtr context) { const auto * function = ast_function->as(); @@ -65,6 +89,8 @@ StoragePtr TableFunctionInput::executeImpl(const ASTPtr & /*ast_function*/, Cont return storage; } +} + void registerTableFunctionInput(TableFunctionFactory & factory) { factory.registerFunction(); diff --git a/src/TableFunctions/TableFunctionInput.h b/src/TableFunctions/TableFunctionInput.h deleted file mode 100644 index 3164ce43eef..00000000000 --- a/src/TableFunctions/TableFunctionInput.h +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -#include - - -namespace DB -{ - -class Context; - -/* input(structure) - allows to make INSERT SELECT from incoming stream of data - */ -class TableFunctionInput : public ITableFunction -{ -public: - static constexpr auto name = "input"; - std::string getName() const override { return name; } - bool hasStaticStructure() const override { return true; } - bool needStructureHint() const override { return true; } - void setStructureHint(const ColumnsDescription & structure_hint_) override { structure_hint = structure_hint_; } - -private: - StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; - const char * getStorageTypeName() const override { return "Input"; } - - ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; - void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; - - String structure; - ColumnsDescription structure_hint; -}; - -} diff --git a/src/TableFunctions/TableFunctionMerge.cpp b/src/TableFunctions/TableFunctionMerge.cpp index 599953a1add..e7b5a34958f 100644 --- a/src/TableFunctions/TableFunctionMerge.cpp +++ b/src/TableFunctions/TableFunctionMerge.cpp @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include @@ -10,7 +9,6 @@ #include #include #include -#include #include #include @@ -26,18 +24,46 @@ namespace ErrorCodes namespace { - [[noreturn]] void throwNoTablesMatchRegexp(const String & source_database_regexp, const String & source_table_regexp) - { - throw Exception( - ErrorCodes::BAD_ARGUMENTS, - "Error while executing table function merge. Either there is no database, which matches regular expression `{}`, or there are " - "no tables in database matches `{}`, which fit tables expression: {}", - source_database_regexp, - source_database_regexp, - source_table_regexp); - } + +[[noreturn]] void throwNoTablesMatchRegexp(const String & source_database_regexp, const String & source_table_regexp) +{ + throw Exception( + ErrorCodes::BAD_ARGUMENTS, + "Error while executing table function merge. Either there is no database, which matches regular expression `{}`, or there are " + "no tables in database matches `{}`, which fit tables expression: {}", + source_database_regexp, + source_database_regexp, + source_table_regexp); } +/* merge (db_name, tables_regexp) - creates a temporary StorageMerge. + * The structure of the table is taken from the first table that came up, suitable for regexp. + * If there is no such table, an exception is thrown. + */ +class TableFunctionMerge : public ITableFunction +{ +public: + static constexpr auto name = "merge"; + std::string getName() const override { return name; } + +private: + StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; + const char * getStorageTypeName() const override { return "Merge"; } + + using TableSet = std::set; + using DBToTableSetMap = std::map; + const DBToTableSetMap & getSourceDatabasesAndTables(ContextPtr context) const; + ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; + std::vector skipAnalysisForArguments(const QueryTreeNodePtr & query_node_table_function, ContextPtr context) const override; + void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; + static TableSet getMatchedTablesWithAccess(const String & database_name, const String & table_regexp, const ContextPtr & context); + + String source_database_name_or_regexp; + String source_table_regexp; + bool database_is_regexp = false; + mutable std::optional source_databases_and_tables; +}; + std::vector TableFunctionMerge::skipAnalysisForArguments(const QueryTreeNodePtr & query_node_table_function, ContextPtr) const { auto & table_function_node = query_node_table_function->as(); @@ -179,6 +205,8 @@ TableFunctionMerge::getMatchedTablesWithAccess(const String & database_name, con return tables; } +} + void registerTableFunctionMerge(TableFunctionFactory & factory) { factory.registerFunction(); diff --git a/src/TableFunctions/TableFunctionMerge.h b/src/TableFunctions/TableFunctionMerge.h deleted file mode 100644 index 8cc5119978a..00000000000 --- a/src/TableFunctions/TableFunctionMerge.h +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once - -#include - - -namespace DB -{ - -/* merge (db_name, tables_regexp) - creates a temporary StorageMerge. - * The structure of the table is taken from the first table that came up, suitable for regexp. - * If there is no such table, an exception is thrown. - */ -class TableFunctionMerge : public ITableFunction -{ -public: - static constexpr auto name = "merge"; - std::string getName() const override { return name; } - -private: - StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; - const char * getStorageTypeName() const override { return "Merge"; } - - using TableSet = std::set; - using DBToTableSetMap = std::map; - const DBToTableSetMap & getSourceDatabasesAndTables(ContextPtr context) const; - ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; - std::vector skipAnalysisForArguments(const QueryTreeNodePtr & query_node_table_function, ContextPtr context) const override; - void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; - static TableSet getMatchedTablesWithAccess(const String & database_name, const String & table_regexp, const ContextPtr & context); - - String source_database_name_or_regexp; - String source_table_regexp; - bool database_is_regexp = false; - mutable std::optional source_databases_and_tables; -}; - - -} diff --git a/src/TableFunctions/TableFunctionMongoDB.cpp b/src/TableFunctions/TableFunctionMongoDB.cpp index 5c7c1d98cdf..b2cf1b4675e 100644 --- a/src/TableFunctions/TableFunctionMongoDB.cpp +++ b/src/TableFunctions/TableFunctionMongoDB.cpp @@ -1,12 +1,11 @@ -#include +#include +#include #include -#include #include #include -#include #include #include @@ -25,6 +24,29 @@ namespace ErrorCodes extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; } +namespace +{ + +class TableFunctionMongoDB : public ITableFunction +{ +public: + static constexpr auto name = "mongodb"; + + std::string getName() const override { return name; } + +private: + StoragePtr executeImpl( + const ASTPtr & ast_function, ContextPtr context, + const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; + + const char * getStorageTypeName() const override { return "MongoDB"; } + + ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; + void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; + + std::optional configuration; + String structure; +}; StoragePtr TableFunctionMongoDB::executeImpl(const ASTPtr & /*ast_function*/, ContextPtr context, const String & table_name, ColumnsDescription /*cached_columns*/, bool is_insert_query) const @@ -97,6 +119,7 @@ void TableFunctionMongoDB::parseArguments(const ASTPtr & ast_function, ContextPt configuration = StorageMongoDB::getConfiguration(main_arguments, context); } +} void registerTableFunctionMongoDB(TableFunctionFactory & factory) { diff --git a/src/TableFunctions/TableFunctionMongoDB.h b/src/TableFunctions/TableFunctionMongoDB.h deleted file mode 100644 index c2c15cabe5a..00000000000 --- a/src/TableFunctions/TableFunctionMongoDB.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace DB -{ - -class TableFunctionMongoDB : public ITableFunction -{ -public: - static constexpr auto name = "mongodb"; - - std::string getName() const override { return name; } - -private: - StoragePtr executeImpl( - const ASTPtr & ast_function, ContextPtr context, - const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; - - const char * getStorageTypeName() const override { return "MongoDB"; } - - ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; - void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; - - std::optional configuration; - String structure; -}; - -} diff --git a/src/TableFunctions/TableFunctionMySQL.cpp b/src/TableFunctions/TableFunctionMySQL.cpp index 03bd2264551..b557a48f29e 100644 --- a/src/TableFunctions/TableFunctionMySQL.cpp +++ b/src/TableFunctions/TableFunctionMySQL.cpp @@ -1,15 +1,15 @@ #include "config.h" #if USE_MYSQL + +#include #include #include -#include #include #include #include #include #include -#include #include #include #include @@ -27,6 +27,32 @@ namespace ErrorCodes extern const int LOGICAL_ERROR; } +namespace +{ + +/* mysql ('host:port', database, table, user, password) - creates a temporary StorageMySQL. + * The structure of the table is taken from the mysql query DESCRIBE table. + * If there is no such table, an exception is thrown. + */ +class TableFunctionMySQL : public ITableFunction +{ +public: + static constexpr auto name = "mysql"; + std::string getName() const override + { + return name; + } +private: + StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; + const char * getStorageTypeName() const override { return "MySQL"; } + + ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; + void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; + + mutable std::optional pool; + std::optional configuration; +}; + void TableFunctionMySQL::parseArguments(const ASTPtr & ast_function, ContextPtr context) { const auto & args_func = ast_function->as(); @@ -88,11 +114,14 @@ StoragePtr TableFunctionMySQL::executeImpl( return res; } +} + void registerTableFunctionMySQL(TableFunctionFactory & factory) { factory.registerFunction(); } + } #endif diff --git a/src/TableFunctions/TableFunctionMySQL.h b/src/TableFunctions/TableFunctionMySQL.h deleted file mode 100644 index 04f619f5f4b..00000000000 --- a/src/TableFunctions/TableFunctionMySQL.h +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once -#include "config.h" - -#if USE_MYSQL -#include -#include -#include - - -namespace DB -{ - -/* mysql ('host:port', database, table, user, password) - creates a temporary StorageMySQL. - * The structure of the table is taken from the mysql query DESCRIBE table. - * If there is no such table, an exception is thrown. - */ -class TableFunctionMySQL : public ITableFunction -{ -public: - static constexpr auto name = "mysql"; - std::string getName() const override - { - return name; - } -private: - StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; - const char * getStorageTypeName() const override { return "MySQL"; } - - ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; - void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; - - mutable std::optional pool; - std::optional configuration; -}; - -} - -#endif diff --git a/src/TableFunctions/TableFunctionNull.cpp b/src/TableFunctions/TableFunctionNull.cpp index 57911e16d4b..75a97bccb97 100644 --- a/src/TableFunctions/TableFunctionNull.cpp +++ b/src/TableFunctions/TableFunctionNull.cpp @@ -1,12 +1,12 @@ #include -#include #include #include #include #include #include -#include #include +#include +#include #include "registerTableFunctions.h" @@ -17,6 +17,36 @@ 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(); @@ -54,8 +84,11 @@ StoragePtr TableFunctionNull::executeImpl(const ASTPtr & /*ast_function*/, Conte return res; } +} + void registerTableFunctionNull(TableFunctionFactory & factory) { factory.registerFunction({.documentation = {}, .allow_readonly = true}); } + } diff --git a/src/TableFunctions/TableFunctionNull.h b/src/TableFunctions/TableFunctionNull.h deleted file mode 100644 index e80552d4cff..00000000000 --- a/src/TableFunctions/TableFunctionNull.h +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once - -#include -#include -#include - - -namespace DB -{ - -/* 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()}}}; -}; -} diff --git a/src/TableFunctions/TableFunctionNumbers.cpp b/src/TableFunctions/TableFunctionNumbers.cpp index d6cf50bc7d6..9abd764f91d 100644 --- a/src/TableFunctions/TableFunctionNumbers.cpp +++ b/src/TableFunctions/TableFunctionNumbers.cpp @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -21,6 +20,28 @@ namespace ErrorCodes extern const int ILLEGAL_TYPE_OF_ARGUMENT; } +namespace +{ + +/* numbers(limit), numbers_mt(limit) + * - the same as SELECT number FROM system.numbers LIMIT limit. + * Used for testing purposes, as a simple example of table function. + */ +template +class TableFunctionNumbers : public ITableFunction +{ +public: + static constexpr auto name = multithreaded ? "numbers_mt" : "numbers"; + std::string getName() const override { return name; } + bool hasStaticStructure() const override { return true; } +private: + StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; + const char * getStorageTypeName() const override { return "SystemNumbers"; } + + UInt64 evaluateArgument(ContextPtr context, ASTPtr & argument) const; + + ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; +}; template ColumnsDescription TableFunctionNumbers::getActualTableStructure(ContextPtr /*context*/, bool /*is_insert_query*/) const @@ -49,12 +70,6 @@ StoragePtr TableFunctionNumbers::executeImpl(const ASTPtr & ast_f throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Table function '{}' requires 'limit' or 'offset, limit'.", getName()); } -void registerTableFunctionNumbers(TableFunctionFactory & factory) -{ - factory.registerFunction>({.documentation = {}, .allow_readonly = true}); - factory.registerFunction>({.documentation = {}, .allow_readonly = true}); -} - template UInt64 TableFunctionNumbers::evaluateArgument(ContextPtr context, ASTPtr & argument) const { @@ -72,3 +87,11 @@ UInt64 TableFunctionNumbers::evaluateArgument(ContextPtr context, } } + +void registerTableFunctionNumbers(TableFunctionFactory & factory) +{ + factory.registerFunction>({.documentation = {}, .allow_readonly = true}); + factory.registerFunction>({.documentation = {}, .allow_readonly = true}); +} + +} diff --git a/src/TableFunctions/TableFunctionNumbers.h b/src/TableFunctions/TableFunctionNumbers.h deleted file mode 100644 index e380f40f7b2..00000000000 --- a/src/TableFunctions/TableFunctionNumbers.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include -#include - - -namespace DB -{ - -/* numbers(limit), numbers_mt(limit) - * - the same as SELECT number FROM system.numbers LIMIT limit. - * Used for testing purposes, as a simple example of table function. - */ -template -class TableFunctionNumbers : public ITableFunction -{ -public: - static constexpr auto name = multithreaded ? "numbers_mt" : "numbers"; - std::string getName() const override { return name; } - bool hasStaticStructure() const override { return true; } -private: - StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; - const char * getStorageTypeName() const override { return "SystemNumbers"; } - - UInt64 evaluateArgument(ContextPtr context, ASTPtr & argument) const; - - ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; -}; - - -} diff --git a/src/TableFunctions/TableFunctionPostgreSQL.cpp b/src/TableFunctions/TableFunctionPostgreSQL.cpp index 322e0df7c15..baa81c4d96d 100644 --- a/src/TableFunctions/TableFunctionPostgreSQL.cpp +++ b/src/TableFunctions/TableFunctionPostgreSQL.cpp @@ -1,13 +1,15 @@ -#include +#include "config.h" #if USE_LIBPQXX -#include -#include + #include +#include +#include +#include #include #include -#include "registerTableFunctions.h" #include +#include "registerTableFunctions.h" namespace DB @@ -18,6 +20,28 @@ namespace ErrorCodes extern const int BAD_ARGUMENTS; } +namespace +{ + +class TableFunctionPostgreSQL : public ITableFunction +{ +public: + static constexpr auto name = "postgresql"; + std::string getName() const override { return name; } + +private: + StoragePtr executeImpl( + const ASTPtr & ast_function, ContextPtr context, + const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; + + const char * getStorageTypeName() const override { return "PostgreSQL"; } + + ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; + void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; + + postgres::PoolWithFailoverPtr connection_pool; + std::optional configuration; +}; StoragePtr TableFunctionPostgreSQL::executeImpl(const ASTPtr & /*ast_function*/, ContextPtr context, const std::string & table_name, ColumnsDescription /*cached_columns*/, bool /*is_insert_query*/) const @@ -60,6 +84,8 @@ void TableFunctionPostgreSQL::parseArguments(const ASTPtr & ast_function, Contex settings.postgresql_connection_pool_auto_close_connection); } +} + void registerTableFunctionPostgreSQL(TableFunctionFactory & factory) { diff --git a/src/TableFunctions/TableFunctionPostgreSQL.h b/src/TableFunctions/TableFunctionPostgreSQL.h deleted file mode 100644 index f7d77567dd4..00000000000 --- a/src/TableFunctions/TableFunctionPostgreSQL.h +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once -#include "config.h" - -#if USE_LIBPQXX -#include -#include -#include -#include - - -namespace DB -{ - -class TableFunctionPostgreSQL : public ITableFunction -{ -public: - static constexpr auto name = "postgresql"; - std::string getName() const override { return name; } - -private: - StoragePtr executeImpl( - const ASTPtr & ast_function, ContextPtr context, - const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; - - const char * getStorageTypeName() const override { return "PostgreSQL"; } - - ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; - void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; - - postgres::PoolWithFailoverPtr connection_pool; - std::optional configuration; -}; - -} - -#endif diff --git a/src/TableFunctions/TableFunctionRedis.cpp b/src/TableFunctions/TableFunctionRedis.cpp index 0b7433845b4..f87ba6d1c6d 100644 --- a/src/TableFunctions/TableFunctionRedis.cpp +++ b/src/TableFunctions/TableFunctionRedis.cpp @@ -1,5 +1,3 @@ -#include - #include #include @@ -15,6 +13,10 @@ #include #include +#include +#include +#include + namespace DB { @@ -24,6 +26,33 @@ namespace ErrorCodes extern const int BAD_ARGUMENTS; } +namespace +{ + +/* Implements Redis table function. + * Use redis(host:port, key, structure[, db_index[, password[, pool_size]]]); + */ +class TableFunctionRedis : public ITableFunction +{ +public: + static constexpr auto name = "redis"; + String getName() const override { return name; } + +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 "Redis"; } + + ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; + void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; + + RedisConfiguration configuration; + String structure; + String primary_key; +}; + StoragePtr TableFunctionRedis::executeImpl( const ASTPtr & /*ast_function*/, ContextPtr context, const String & table_name, ColumnsDescription /*cached_columns*/, bool is_insert_query) const { @@ -85,6 +114,7 @@ void TableFunctionRedis::parseArguments(const ASTPtr & ast_function, ContextPtr throw Exception(ErrorCodes::BAD_ARGUMENTS, "Bad arguments redis table function structure should contains key."); } +} void registerTableFunctionRedis(TableFunctionFactory & factory) { diff --git a/src/TableFunctions/TableFunctionRedis.h b/src/TableFunctions/TableFunctionRedis.h deleted file mode 100644 index a7fc0df0a15..00000000000 --- a/src/TableFunctions/TableFunctionRedis.h +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace DB -{ - -/* Implements Redis table function. - * Use redis(host:port, key, structure[, db_index[, password[, pool_size]]]); - */ -class TableFunctionRedis : public ITableFunction -{ -public: - static constexpr auto name = "redis"; - String getName() const override { return name; } - -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 "Redis"; } - - ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; - void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; - - RedisConfiguration configuration; - String structure; - String primary_key; -}; - -} diff --git a/src/TableFunctions/TableFunctionSQLite.cpp b/src/TableFunctions/TableFunctionSQLite.cpp index 27e6fcf1fd1..baaf0dac68f 100644 --- a/src/TableFunctions/TableFunctionSQLite.cpp +++ b/src/TableFunctions/TableFunctionSQLite.cpp @@ -1,9 +1,10 @@ -#include +#include "config.h" #if USE_SQLITE #include -#include +#include +#include #include #include "registerTableFunctions.h" @@ -12,7 +13,6 @@ #include -#include #include #include @@ -27,6 +27,28 @@ namespace ErrorCodes extern const int BAD_ARGUMENTS; } +namespace +{ + +class TableFunctionSQLite : public ITableFunction +{ +public: + static constexpr auto name = "sqlite"; + std::string getName() const override { return name; } + +private: + StoragePtr executeImpl( + const ASTPtr & ast_function, ContextPtr context, + const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; + + const char * getStorageTypeName() const override { return "SQLite"; } + + ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; + void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; + + String database_path, remote_table_name; + std::shared_ptr sqlite_db; +}; StoragePtr TableFunctionSQLite::executeImpl(const ASTPtr & /*ast_function*/, ContextPtr context, const String & table_name, ColumnsDescription /*cached_columns*/, bool /*is_insert_query*/) const @@ -69,6 +91,7 @@ void TableFunctionSQLite::parseArguments(const ASTPtr & ast_function, ContextPtr sqlite_db = openSQLiteDB(database_path, context); } +} void registerTableFunctionSQLite(TableFunctionFactory & factory) { diff --git a/src/TableFunctions/TableFunctionSQLite.h b/src/TableFunctions/TableFunctionSQLite.h deleted file mode 100644 index 74318f058a9..00000000000 --- a/src/TableFunctions/TableFunctionSQLite.h +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once -#include "config.h" - -#if USE_SQLITE -#include -#include - - -namespace DB -{ - -class TableFunctionSQLite : public ITableFunction -{ -public: - static constexpr auto name = "sqlite"; - std::string getName() const override { return name; } - -private: - StoragePtr executeImpl( - const ASTPtr & ast_function, ContextPtr context, - const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; - - const char * getStorageTypeName() const override { return "SQLite"; } - - ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; - void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; - - String database_path, remote_table_name; - std::shared_ptr sqlite_db; -}; - -} - -#endif diff --git a/src/TableFunctions/TableFunctionValues.cpp b/src/TableFunctions/TableFunctionValues.cpp index 42a19874704..7b2a61c25eb 100644 --- a/src/TableFunctions/TableFunctionValues.cpp +++ b/src/TableFunctions/TableFunctionValues.cpp @@ -9,7 +9,6 @@ #include #include -#include #include #include @@ -30,7 +29,32 @@ namespace ErrorCodes extern const int CANNOT_EXTRACT_TABLE_STRUCTURE; } -static void parseAndInsertValues(MutableColumns & res_columns, const ASTs & args, const Block & sample_block, size_t start, ContextPtr context) +namespace +{ + +/* values(structure, values...) - creates a temporary storage filling columns with values + * values is case-insensitive table function. + */ +class TableFunctionValues : public ITableFunction +{ +public: + static constexpr auto name = "values"; + std::string getName() const override { return name; } + bool hasStaticStructure() const override { return true; } +private: + StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; + const char * getStorageTypeName() const override { return "Values"; } + + ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; + void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; + + static DataTypes getTypesFromArgument(const ASTPtr & arg, ContextPtr context); + + ColumnsDescription structure; + bool has_structure_in_arguments; +}; + +void parseAndInsertValues(MutableColumns & res_columns, const ASTs & args, const Block & sample_block, size_t start, ContextPtr context) { if (res_columns.size() == 1) /// Parsing arguments as Fields { @@ -146,6 +170,8 @@ StoragePtr TableFunctionValues::executeImpl(const ASTPtr & ast_function, Context return res; } +} + void registerTableFunctionValues(TableFunctionFactory & factory) { factory.registerFunction({.documentation = {}, .allow_readonly = true}, TableFunctionFactory::CaseInsensitive); diff --git a/src/TableFunctions/TableFunctionValues.h b/src/TableFunctions/TableFunctionValues.h deleted file mode 100644 index 7c87bff835e..00000000000 --- a/src/TableFunctions/TableFunctionValues.h +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once - -#include - -namespace DB -{ -/* values(structure, values...) - creates a temporary storage filling columns with values - * values is case-insensitive table function. - */ -class TableFunctionValues : public ITableFunction -{ -public: - static constexpr auto name = "values"; - std::string getName() const override { return name; } - bool hasStaticStructure() const override { return true; } -private: - StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; - const char * getStorageTypeName() const override { return "Values"; } - - ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; - void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; - - static DataTypes getTypesFromArgument(const ASTPtr & arg, ContextPtr context); - - ColumnsDescription structure; - bool has_structure_in_arguments; -}; - - -} diff --git a/src/TableFunctions/TableFunctionViewIfPermitted.cpp b/src/TableFunctions/TableFunctionViewIfPermitted.cpp index d7944df1b28..b1691803988 100644 --- a/src/TableFunctions/TableFunctionViewIfPermitted.cpp +++ b/src/TableFunctions/TableFunctionViewIfPermitted.cpp @@ -3,12 +3,13 @@ #include #include #include +#include +#include #include #include #include #include #include -#include #include #include #include "registerTableFunctions.h" @@ -16,6 +17,7 @@ namespace DB { + namespace ErrorCodes { extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; @@ -23,11 +25,37 @@ namespace ErrorCodes extern const int ACCESS_DENIED; } - -const ASTSelectWithUnionQuery & TableFunctionViewIfPermitted::getSelectQuery() const +namespace { - return *create.select; -} + +/* viewIfPermitted(query ELSE null('structure')) + * Works as "view(query)" if the current user has the permissions required to execute "query"; works as "null('structure')" otherwise. + */ +class TableFunctionViewIfPermitted : public ITableFunction +{ +public: + static constexpr auto name = "viewIfPermitted"; + + std::string getName() const override { return name; } + +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 "ViewIfPermitted"; } + + std::vector skipAnalysisForArguments(const QueryTreeNodePtr & query_node_table_function, ContextPtr context) const override; + + void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; + + ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; + + bool isPermitted(const ContextPtr & context, const ColumnsDescription & else_columns) const; + + ASTCreateQuery create; + ASTPtr else_ast; + TableFunctionPtr else_table_function; +}; + std::vector TableFunctionViewIfPermitted::skipAnalysisForArguments(const QueryTreeNodePtr &, ContextPtr) const { @@ -118,6 +146,8 @@ bool TableFunctionViewIfPermitted::isPermitted(const ContextPtr & context, const return true; } +} + void registerTableFunctionViewIfPermitted(TableFunctionFactory & factory) { factory.registerFunction({.documentation = {}, .allow_readonly = true}); diff --git a/src/TableFunctions/TableFunctionViewIfPermitted.h b/src/TableFunctions/TableFunctionViewIfPermitted.h deleted file mode 100644 index bee4e15bfa5..00000000000 --- a/src/TableFunctions/TableFunctionViewIfPermitted.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace DB -{ - -/* viewIfPermitted(query ELSE null('structure')) - * Works as "view(query)" if the current user has the permissions required to execute "query"; works as "null('structure')" otherwise. - */ -class TableFunctionViewIfPermitted : public ITableFunction -{ -public: - static constexpr auto name = "viewIfPermitted"; - - std::string getName() const override { return name; } - - const ASTSelectWithUnionQuery & getSelectQuery() const; - -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 "ViewIfPermitted"; } - - std::vector skipAnalysisForArguments(const QueryTreeNodePtr & query_node_table_function, ContextPtr context) const override; - - void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; - - ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; - - bool isPermitted(const ContextPtr & context, const ColumnsDescription & else_columns) const; - - ASTCreateQuery create; - ASTPtr else_ast; - TableFunctionPtr else_table_function; -}; - -} diff --git a/src/TableFunctions/TableFunctionZeros.cpp b/src/TableFunctions/TableFunctionZeros.cpp index eb93626590e..f23b6540959 100644 --- a/src/TableFunctions/TableFunctionZeros.cpp +++ b/src/TableFunctions/TableFunctionZeros.cpp @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -15,9 +14,32 @@ namespace DB namespace ErrorCodes { -extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; + extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; } +namespace +{ + +/* zeros(limit), zeros_mt(limit) + * - the same as SELECT zero FROM system.zeros LIMIT limit. + * Used for testing purposes, as a simple example of table function. + */ +template +class TableFunctionZeros : public ITableFunction +{ +public: + static constexpr auto name = multithreaded ? "zeros_mt" : "zeros"; + std::string getName() const override { return name; } + bool hasStaticStructure() const override { return true; } +private: + StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; + const char * getStorageTypeName() const override { return "SystemZeros"; } + + UInt64 evaluateArgument(ContextPtr context, ASTPtr & argument) const; + + ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; +}; + template ColumnsDescription TableFunctionZeros::getActualTableStructure(ContextPtr /*context*/, bool /*is_insert_query*/) const @@ -46,6 +68,14 @@ StoragePtr TableFunctionZeros::executeImpl(const ASTPtr & ast_fun throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Table function '{}' requires 'limit'.", getName()); } +template +UInt64 TableFunctionZeros::evaluateArgument(ContextPtr context, ASTPtr & argument) const +{ + return checkAndGetLiteralArgument(evaluateConstantExpressionOrIdentifierAsLiteral(argument, context), "length"); +} + +} + void registerTableFunctionZeros(TableFunctionFactory & factory) { factory.registerFunction>({.documentation = { @@ -71,13 +101,7 @@ void registerTableFunctionZeros(TableFunctionFactory & factory) See also the `system.zeros` table. )", .examples={{"1", "SELECT count() FROM zeros_mt(1000000000) WHERE NOT ignore(randomPrintableASCII(10))", ""}} -}}); -} - -template -UInt64 TableFunctionZeros::evaluateArgument(ContextPtr context, ASTPtr & argument) const -{ - return checkAndGetLiteralArgument(evaluateConstantExpressionOrIdentifierAsLiteral(argument, context), "length"); + }}); } } diff --git a/src/TableFunctions/TableFunctionZeros.h b/src/TableFunctions/TableFunctionZeros.h deleted file mode 100644 index 07d523ee37c..00000000000 --- a/src/TableFunctions/TableFunctionZeros.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include -#include - - -namespace DB -{ - -/* zeros(limit), zeros_mt(limit) - * - the same as SELECT zero FROM system.zeros LIMIT limit. - * Used for testing purposes, as a simple example of table function. - */ -template -class TableFunctionZeros : public ITableFunction -{ -public: - static constexpr auto name = multithreaded ? "zeros_mt" : "zeros"; - std::string getName() const override { return name; } - bool hasStaticStructure() const override { return true; } -private: - StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override; - const char * getStorageTypeName() const override { return "SystemZeros"; } - - UInt64 evaluateArgument(ContextPtr context, ASTPtr & argument) const; - - ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override; -}; - - -}