2020-11-25 21:06:33 +00:00
|
|
|
#include <TableFunctions/TableFunctionPostgreSQL.h>
|
|
|
|
|
|
|
|
#if USE_LIBPQXX
|
2021-02-11 21:59:58 +00:00
|
|
|
#include <Databases/PostgreSQL/fetchPostgreSQLTableStructure.h>
|
|
|
|
#include <Storages/StoragePostgreSQL.h>
|
|
|
|
|
2020-11-25 21:06:33 +00:00
|
|
|
#include <Interpreters/evaluateConstantExpression.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <TableFunctions/ITableFunction.h>
|
|
|
|
#include <TableFunctions/TableFunctionFactory.h>
|
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include "registerTableFunctions.h"
|
2021-03-31 14:02:51 +00:00
|
|
|
#include <Common/parseRemoteDescription.h>
|
2020-11-25 21:06:33 +00:00
|
|
|
|
2021-01-11 10:23:44 +00:00
|
|
|
|
2020-11-25 21:06:33 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
StoragePtr TableFunctionPostgreSQL::executeImpl(const ASTPtr & /*ast_function*/,
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr context, const std::string & table_name, ColumnsDescription /*cached_columns*/) const
|
2020-11-25 21:06:33 +00:00
|
|
|
{
|
|
|
|
auto columns = getActualTableStructure(context);
|
|
|
|
auto result = std::make_shared<StoragePostgreSQL>(
|
2021-04-23 12:18:23 +00:00
|
|
|
StorageID(getDatabaseName(), table_name),
|
2021-05-11 16:22:24 +00:00
|
|
|
connection_pool,
|
2021-09-01 23:17:15 +00:00
|
|
|
configuration->table,
|
2021-04-23 12:18:23 +00:00
|
|
|
columns,
|
|
|
|
ConstraintsDescription{},
|
|
|
|
String{},
|
2021-09-01 23:17:15 +00:00
|
|
|
configuration->schema,
|
|
|
|
configuration->on_conflict);
|
2020-11-25 21:06:33 +00:00
|
|
|
|
|
|
|
result->startup();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
ColumnsDescription TableFunctionPostgreSQL::getActualTableStructure(ContextPtr context) const
|
2020-11-25 21:06:33 +00:00
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
const bool use_nulls = context->getSettingsRef().external_table_functions_use_nulls;
|
2021-05-08 14:55:53 +00:00
|
|
|
auto connection_holder = connection_pool->get();
|
2022-01-08 17:44:17 +00:00
|
|
|
auto columns_info = fetchPostgreSQLTableStructure(
|
|
|
|
connection_holder->get(), configuration->table, configuration->schema, use_nulls).physical_columns;
|
2020-11-25 21:06:33 +00:00
|
|
|
|
2022-01-08 17:44:17 +00:00
|
|
|
if (!columns_info)
|
2021-10-22 10:08:38 +00:00
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Table structure not returned");
|
2021-12-27 08:03:04 +00:00
|
|
|
|
2022-01-08 17:44:17 +00:00
|
|
|
return ColumnsDescription{columns_info->columns};
|
2020-11-25 21:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
void TableFunctionPostgreSQL::parseArguments(const ASTPtr & ast_function, ContextPtr context)
|
2020-11-25 21:06:33 +00:00
|
|
|
{
|
|
|
|
const auto & func_args = ast_function->as<ASTFunction &>();
|
|
|
|
if (!func_args.arguments)
|
|
|
|
throw Exception("Table function 'PostgreSQL' must have arguments.", ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
|
2021-09-01 23:17:15 +00:00
|
|
|
configuration.emplace(StoragePostgreSQL::getConfiguration(func_args.arguments->children, context));
|
2022-06-23 21:44:59 +00:00
|
|
|
const auto & settings = context->getSettingsRef();
|
|
|
|
connection_pool = std::make_shared<postgres::PoolWithFailover>(
|
|
|
|
*configuration,
|
|
|
|
settings.postgresql_connection_pool_size,
|
|
|
|
settings.postgresql_connection_pool_wait_timeout,
|
|
|
|
POSTGRESQL_POOL_WITH_FAILOVER_DEFAULT_MAX_TRIES,
|
|
|
|
settings.postgresql_connection_pool_auto_close_connection);
|
2020-11-25 21:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void registerTableFunctionPostgreSQL(TableFunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<TableFunctionPostgreSQL>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|