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>
|
2023-02-03 10:54:49 +00:00
|
|
|
#include <IO/ConnectionTimeouts.h>
|
2018-10-03 10:44:43 +00:00
|
|
|
#include <Interpreters/evaluateConstantExpression.h>
|
2018-09-25 00:21:20 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/parseQuery.h>
|
2018-10-03 10:44:43 +00:00
|
|
|
#include <Storages/StorageXDBC.h>
|
2018-09-25 00:21:20 +00:00
|
|
|
#include <TableFunctions/ITableFunction.h>
|
|
|
|
#include <TableFunctions/ITableFunctionXDBC.h>
|
|
|
|
#include <TableFunctions/TableFunctionFactory.h>
|
2018-10-03 10:44:43 +00:00
|
|
|
#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
|
|
|
|
2022-02-12 19:49:01 +00:00
|
|
|
|
2018-09-25 00:21:20 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2018-10-03 10:44:43 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
2018-10-03 12:10:57 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2018-10-03 10:44:43 +00:00
|
|
|
}
|
2018-09-25 00:21:20 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
void ITableFunctionXDBC::parseArguments(const ASTPtr & ast_function, ContextPtr context)
|
2018-10-03 10:44:43 +00:00
|
|
|
{
|
2019-03-15 17:09:14 +00:00
|
|
|
const auto & args_func = ast_function->as<ASTFunction &>();
|
2018-10-03 10:44:43 +00:00
|
|
|
|
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());
|
2018-10-03 10:44:43 +00:00
|
|
|
|
2019-03-15 17:09:14 +00:00
|
|
|
ASTs & args = args_func.arguments->children;
|
2018-10-03 10:44:43 +00:00
|
|
|
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);
|
2018-10-03 10:44:43 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2018-10-03 10:44:43 +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
|
|
|
}
|
2018-10-03 10:44:43 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
void ITableFunctionXDBC::startBridgeIfNot(ContextPtr context) const
|
2021-03-22 14:09:38 +00:00
|
|
|
{
|
|
|
|
if (!helper)
|
|
|
|
{
|
2023-01-30 16:05:35 +00:00
|
|
|
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();
|
|
|
|
}
|
2020-10-14 12:19:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-06 08:56:07 +00:00
|
|
|
ColumnsDescription ITableFunctionXDBC::getActualTableStructure(ContextPtr context, bool /*is_insert_query*/) const
|
2020-10-14 12:19:29 +00:00
|
|
|
{
|
2021-03-22 14:09:38 +00:00
|
|
|
startBridgeIfNot(context);
|
2018-10-03 10:44:43 +00:00
|
|
|
|
2022-02-12 19:49:01 +00:00
|
|
|
/// Infer external table structure.
|
2018-10-03 10:44:43 +00:00
|
|
|
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);
|
2018-10-03 10:44:43 +00:00
|
|
|
|
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));
|
2019-10-28 11:01:09 +00:00
|
|
|
|
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(),
|
|
|
|
{context->getConfigRef().getUInt("keep_alive_timeout", DEFAULT_HTTP_KEEP_ALIVE_TIMEOUT), 0}),
|
|
|
|
credentials);
|
2018-10-03 10:44:43 +00:00
|
|
|
|
|
|
|
std::string columns_info;
|
|
|
|
readStringBinary(columns_info, buf);
|
|
|
|
NamesAndTypesList columns = NamesAndTypesList::parse(columns_info);
|
|
|
|
|
2020-10-14 12:19:29 +00:00
|
|
|
return ColumnsDescription{columns};
|
|
|
|
}
|
2020-08-26 20:56:30 +00:00
|
|
|
|
2023-07-06 08:56:07 +00:00
|
|
|
StoragePtr ITableFunctionXDBC::executeImpl(const ASTPtr & /*ast_function*/, ContextPtr context, const std::string & table_name, ColumnsDescription /*cached_columns*/, bool is_insert_query) const
|
2020-10-14 12:19:29 +00:00
|
|
|
{
|
2021-03-22 14:09:38 +00:00
|
|
|
startBridgeIfNot(context);
|
2023-07-06 08:56:07 +00:00
|
|
|
auto columns = getActualTableStructure(context, is_insert_query);
|
2021-04-23 12:18:23 +00:00
|
|
|
auto result = std::make_shared<StorageXDBC>(
|
2022-02-12 10:50:05 +00:00
|
|
|
StorageID(getDatabaseName(), table_name), schema_name, remote_table_name, columns, ConstraintsDescription{}, String{}, context, helper);
|
2018-10-03 10:44:43 +00:00
|
|
|
result->startup();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerTableFunctionJDBC(TableFunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<TableFunctionJDBC>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerTableFunctionODBC(TableFunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<TableFunctionODBC>();
|
2018-09-25 00:21:20 +00:00
|
|
|
}
|
2018-11-22 15:59:00 +00:00
|
|
|
}
|