ClickHouse/src/TableFunctions/ITableFunctionXDBC.cpp

113 lines
4.1 KiB
C++
Raw Normal View History

2018-09-25 00:21:20 +00:00
#include <type_traits>
#include <ext/scope_guard.h>
#include <Core/Defines.h>
2018-09-25 00:21:20 +00:00
#include <DataTypes/DataTypeFactory.h>
#include <IO/ReadHelpers.h>
#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>
#include <Common/typeid_cast.h>
#include <Poco/NumberFormatter.h>
2019-12-15 06:34:43 +00:00
#include "registerTableFunctions.h"
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)
throw Exception("Table function '" + getName() + "' must have arguments.", ErrorCodes::LOGICAL_ERROR);
2019-03-15 17:09:14 +00:00
ASTs & args = args_func.arguments->children;
if (args.size() != 2 && args.size() != 3)
throw Exception("Table function '" + getName() + "' requires 2 or 3 arguments: " + getName() + "('DSN', table) or " + getName()
+ "('DSN', schema, table)",
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
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)
{
/// Have to const_cast, because bridges store their commands inside context
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);
/* 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);
const auto use_nulls = context->getSettingsRef().external_table_functions_use_nulls;
columns_info_uri.addQueryParameter("external_table_functions_use_nulls",
Poco::NumberFormatter::format(use_nulls));
2020-06-16 19:14:58 +00:00
ReadWriteBufferFromHTTP buf(columns_info_uri, Poco::Net::HTTPRequest::HTTP_POST, {}, ConnectionTimeouts::getHTTPTimeouts(context));
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);
auto result = std::make_shared<StorageXDBC>(StorageID(getDatabaseName(), table_name), schema_name, remote_table_name, columns, 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
}
}