ClickHouse/src/TableFunctions/ITableFunctionXDBC.cpp

194 lines
6.4 KiB
C++
Raw Normal View History

2018-09-25 00:21:20 +00:00
#include <DataTypes/DataTypeFactory.h>
#include <IO/ReadHelpers.h>
2022-02-12 19:49:01 +00:00
#include <IO/WriteHelpers.h>
2018-09-25 00:21:20 +00:00
#include <IO/ReadWriteBufferFromHTTP.h>
Fix terribly broken, fragile and potentially cyclic linking Sorry for the clickbaity title. This is about static method ConnectionTimeouts::getHTTPTimeouts(). It was be declared in header IO/ConnectionTimeouts.h, and defined in header IO/ConnectionTimeoutsContext.h (!). This is weird and caused issues with linking on s390x (##45520). There was an attempt to fix some inconsistencies (#45848) but neither did @Algunenano nor me at first really understand why the definition is in the header. Turns out that ConnectionTimeoutsContext.h is only #include'd from source files which are part of the normal server build BUT NOT part of the keeper standalone build (which must be enabled via CMake -DBUILD_STANDALONE_KEEPER=1). This dependency was not documented and as a result, some misguided workarounds were introduced earlier, e.g. https://github.com/ClickHouse/ClickHouse/pull/38475/commits/0341c6c54bd7ac77200b4ca123208b195514ef20 The deeper cause was that getHTTPTimeouts() is passed a "Context". This class is part of the "dbms" libary which is deliberately not linked by the standalone build of clickhouse-keeper. The context is only used to read the settings and the "Settings" class is part of the clickhouse_common library which is linked by clickhouse-keeper already. To resolve this mess, this PR - creates source file IO/ConnectionTimeouts.cpp and moves all ConnectionTimeouts definitions into it, including getHTTPTimeouts(). - breaks the wrong dependency by passing "Settings" instead of "Context" into getHTTPTimeouts(). - resolves the previous hacks
2023-02-03 10:54:49 +00:00
#include <IO/ConnectionTimeouts.h>
#include <Interpreters/evaluateConstantExpression.h>
2018-09-25 00:21:20 +00:00
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTLiteral.h>
#include <Parsers/parseQuery.h>
#include <Storages/StorageXDBC.h>
2018-09-25 00:21:20 +00:00
#include <TableFunctions/ITableFunction.h>
#include <TableFunctions/TableFunctionFactory.h>
#include <Poco/Net/HTTPRequest.h>
2018-09-25 00:21:20 +00:00
#include <Common/Exception.h>
2019-12-15 06:34:43 +00:00
#include "registerTableFunctions.h"
2018-09-25 00:21:20 +00:00
2023-11-06 03:33:23 +00:00
#include <Poco/Util/AbstractConfiguration.h>
#include <BridgeHelper/XDBCBridgeHelper.h>
#include "config.h"
2022-02-12 19:49:01 +00:00
2018-09-25 00:21:20 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
extern const int LOGICAL_ERROR;
}
2018-09-25 00:21:20 +00:00
2023-11-06 03:33:23 +00:00
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<XDBCBridgeHelper<JDBCBridgeMixin>>(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<XDBCBridgeHelper<ODBCBridgeMixin>>(context, http_timeout_, connection_string_, use_connection_pooling_);
}
const char * getStorageTypeName() const override { return "ODBC"; }
};
void ITableFunctionXDBC::parseArguments(const ASTPtr & ast_function, ContextPtr context)
{
2019-03-15 17:09:14 +00:00
const auto & args_func = ast_function->as<ASTFunction &>();
2019-03-15 17:09:14 +00:00
if (!args_func.arguments)
2022-02-12 19:49:01 +00:00
throw Exception(ErrorCodes::LOGICAL_ERROR, "Table function '{}' must have arguments.", getName());
2019-03-15 17:09:14 +00:00
ASTs & args = args_func.arguments->children;
if (args.size() != 2 && args.size() != 3)
2022-02-12 19:49:01 +00:00
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
"Table function '{0}' requires 2 or 3 arguments: {0}('DSN', table) or {0}('DSN', schema, table)", getName());
2018-09-25 00:21:20 +00:00
2020-03-09 02:31:05 +00:00
for (auto & arg : args)
arg = evaluateConstantExpressionOrIdentifierAsLiteral(arg, context);
if (args.size() == 3)
2018-09-25 14:29:44 +00:00
{
2019-03-15 17:09:14 +00:00
connection_string = args[0]->as<ASTLiteral &>().value.safeGet<String>();
schema_name = args[1]->as<ASTLiteral &>().value.safeGet<String>();
2019-07-19 13:28:28 +00:00
remote_table_name = args[2]->as<ASTLiteral &>().value.safeGet<String>();
2018-09-25 14:29:44 +00:00
}
else if (args.size() == 2)
2018-09-25 14:29:44 +00:00
{
2019-03-15 17:09:14 +00:00
connection_string = args[0]->as<ASTLiteral &>().value.safeGet<String>();
2019-07-19 13:28:28 +00:00
remote_table_name = args[1]->as<ASTLiteral &>().value.safeGet<String>();
2018-09-25 14:29:44 +00:00
}
2021-03-22 14:09:38 +00:00
}
void ITableFunctionXDBC::startBridgeIfNot(ContextPtr context) const
2021-03-22 14:09:38 +00:00
{
if (!helper)
{
helper = createBridgeHelper(context, context->getSettingsRef().http_receive_timeout.value, connection_string, context->getSettingsRef().odbc_bridge_use_connection_pooling.value);
2021-03-22 14:09:38 +00:00
helper->startBridgeSync();
}
}
ColumnsDescription ITableFunctionXDBC::getActualTableStructure(ContextPtr context, bool /*is_insert_query*/) const
{
2021-03-22 14:09:38 +00:00
startBridgeIfNot(context);
2022-02-12 19:49:01 +00:00
/// Infer external table structure.
Poco::URI columns_info_uri = helper->getColumnsInfoURI();
columns_info_uri.addQueryParameter("connection_string", connection_string);
if (!schema_name.empty())
columns_info_uri.addQueryParameter("schema", schema_name);
2019-07-19 13:28:28 +00:00
columns_info_uri.addQueryParameter("table", remote_table_name);
2022-02-12 19:49:01 +00:00
bool use_nulls = context->getSettingsRef().external_table_functions_use_nulls;
columns_info_uri.addQueryParameter("external_table_functions_use_nulls", toString(use_nulls));
2021-10-28 10:28:05 +00:00
Poco::Net::HTTPBasicCredentials credentials{};
2023-02-07 12:10:26 +00:00
ReadWriteBufferFromHTTP buf(
columns_info_uri,
Poco::Net::HTTPRequest::HTTP_POST,
{},
ConnectionTimeouts::getHTTPTimeouts(
context->getSettingsRef(),
2023-12-05 12:34:37 +00:00
context->getServerSettings().keep_alive_timeout),
2023-02-07 12:10:26 +00:00
credentials);
std::string columns_info;
readStringBinary(columns_info, buf);
NamesAndTypesList columns = NamesAndTypesList::parse(columns_info);
return ColumnsDescription{columns};
}
2020-08-26 20:56:30 +00:00
StoragePtr ITableFunctionXDBC::executeImpl(const ASTPtr & /*ast_function*/, ContextPtr context, const std::string & table_name, ColumnsDescription /*cached_columns*/, bool is_insert_query) const
{
2021-03-22 14:09:38 +00:00
startBridgeIfNot(context);
auto columns = getActualTableStructure(context, is_insert_query);
2021-04-23 12:18:23 +00:00
auto result = std::make_shared<StorageXDBC>(
StorageID(getDatabaseName(), table_name), schema_name, remote_table_name, columns, ConstraintsDescription{}, String{}, context, helper);
result->startup();
return result;
}
2023-11-06 03:33:23 +00:00
}
void registerTableFunctionJDBC(TableFunctionFactory & factory)
{
factory.registerFunction<TableFunctionJDBC>();
}
void registerTableFunctionODBC(TableFunctionFactory & factory)
{
factory.registerFunction<TableFunctionODBC>();
2018-09-25 00:21:20 +00:00
}
}