ClickHouse/src/TableFunctions/ITableFunctionXDBC.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

108 lines
3.8 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>
#include <IO/ConnectionTimeoutsContext.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/ITableFunctionXDBC.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
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
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);
2021-03-22 14:09:38 +00:00
helper->startBridgeSync();
}
}
ColumnsDescription ITableFunctionXDBC::getActualTableStructure(ContextPtr context) 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{};
ReadWriteBufferFromHTTP buf(columns_info_uri, Poco::Net::HTTPRequest::HTTP_POST, {}, ConnectionTimeouts::getHTTPTimeouts(context), 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*/) const
{
2021-03-22 14:09:38 +00:00
startBridgeIfNot(context);
auto columns = getActualTableStructure(context);
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;
}
void registerTableFunctionJDBC(TableFunctionFactory & factory)
{
factory.registerFunction<TableFunctionJDBC>();
}
void registerTableFunctionODBC(TableFunctionFactory & factory)
{
factory.registerFunction<TableFunctionODBC>();
2018-09-25 00:21:20 +00:00
}
}