2018-09-25 00:21:20 +00:00
|
|
|
#include <type_traits>
|
|
|
|
#include <ext/scope_guard.h>
|
|
|
|
|
2018-10-03 10:44:43 +00:00
|
|
|
#include <Core/Defines.h>
|
2018-09-25 00:21:20 +00:00
|
|
|
#include <DataTypes/DataTypeFactory.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <IO/ReadWriteBufferFromHTTP.h>
|
2020-12-10 22:05:02 +00:00
|
|
|
#include <IO/ConnectionTimeoutsContext.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>
|
|
|
|
#include <Common/typeid_cast.h>
|
2019-10-28 11:01:09 +00:00
|
|
|
#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
|
|
|
|
{
|
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)
|
2018-10-03 10:44:43 +00:00
|
|
|
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;
|
2018-10-03 10:44:43 +00:00
|
|
|
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);
|
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)
|
|
|
|
{
|
|
|
|
/// Have to const_cast, because bridges store their commands inside context
|
2021-04-10 23:33:54 +00:00
|
|
|
helper = createBridgeHelper(context, context->getSettingsRef().http_receive_timeout.value, connection_string);
|
2021-03-22 14:09:38 +00:00
|
|
|
helper->startBridgeSync();
|
|
|
|
}
|
2020-10-14 12:19:29 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
ColumnsDescription ITableFunctionXDBC::getActualTableStructure(ContextPtr context) 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
|
|
|
|
2020-10-14 12:19:29 +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
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
const auto use_nulls = context->getSettingsRef().external_table_functions_use_nulls;
|
2019-10-28 11:01:09 +00:00
|
|
|
columns_info_uri.addQueryParameter("external_table_functions_use_nulls",
|
2020-10-14 12:19:29 +00:00
|
|
|
Poco::NumberFormatter::format(use_nulls));
|
2019-10-28 11:01:09 +00:00
|
|
|
|
2020-06-16 19:14:58 +00:00
|
|
|
ReadWriteBufferFromHTTP buf(columns_info_uri, Poco::Net::HTTPRequest::HTTP_POST, {}, ConnectionTimeouts::getHTTPTimeouts(context));
|
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
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
StoragePtr ITableFunctionXDBC::executeImpl(const ASTPtr & /*ast_function*/, ContextPtr context, const std::string & table_name, ColumnsDescription /*cached_columns*/) const
|
2020-10-14 12:19:29 +00:00
|
|
|
{
|
2021-03-22 14:09:38 +00:00
|
|
|
startBridgeIfNot(context);
|
2020-10-14 12:19:29 +00:00
|
|
|
auto columns = getActualTableStructure(context);
|
|
|
|
auto result = std::make_shared<StorageXDBC>(StorageID(getDatabaseName(), table_name), schema_name, remote_table_name, columns, 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
|
|
|
}
|