Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/ubuntu/clickhouse.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ for pkg in clickhouse-common-static clickhouse-server; do
# Prior to v22, the server package supported all architectures.
ARCH=all
fi
echo "${base_url}/${pkg}_${CH_VERSION}_${ARCH}.deb"
curl -sLo "${pkg}.deb" "${base_url}/${pkg}_${CH_VERSION}_${ARCH}.deb"
dpkg -i "${pkg}.deb"
rm "${pkg}.deb"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)

# We'll need the clickhouse-cpp library and rpath so it can be found.
SHLIB_LINK += -Wl,-rpath,$(pkglibdir)/ $(CH_CPP_LIB)
SHLIB_LINK += -Wl,-rpath,$(pkglibdir)/

# PostgresSQL 15 and earlier violate a C++ v17 storage specifier error.
ifeq ($(shell test $(MAJORVERSION) -lt 16; echo $$?),0)
Expand Down
9 changes: 8 additions & 1 deletion src/binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ void * exc_palloc0(Size size)
return ret;
}

#define CLICKHOUSE_SECURE_PORT 9440

ch_binary_connection_t * ch_binary_connect(
char * host, int port, char * database, char * user, char * password, char ** error)
{
Expand All @@ -113,8 +115,11 @@ ch_binary_connection_t * ch_binary_connect(
options = new ClientOptions();
options->SetPingBeforeQuery(true);

if (host)
if (host) {
options->SetHost(std::string(host));
if (!port && ch_is_cloud_host(host))
options->SetPort(CLICKHOUSE_SECURE_PORT);
}
if (port)
options->SetPort(port);
if (database)
Expand All @@ -123,6 +128,8 @@ ch_binary_connection_t * ch_binary_connect(
options->SetUser(std::string(user));
if (password)
options->SetPassword(std::string(password));
if (options->port == CLICKHOUSE_SECURE_PORT)
options->SetSSLOptions(ClientOptions::SSLOptions());

//options->SetRethrowException(false);
conn = new ch_binary_connection_t();
Expand Down
5 changes: 1 addition & 4 deletions src/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ clickhouse_connect(ForeignServer *server, UserMapping *user)
char *driver = "http";

/* default settings */
ch_connection_details details = {"127.0.0.1", 8123, NULL, NULL, "default"};
ch_connection_details details = {"127.0.0.1", 0, NULL, NULL, "default"};

chfdw_extract_options(server->options, &driver, &details.host,
&details.port, &details.dbname, &details.username, &details.password);
Expand All @@ -58,9 +58,6 @@ clickhouse_connect(ForeignServer *server, UserMapping *user)
}
else if (strcmp(driver, "binary") == 0)
{
if (details.port == 8123)
details.port = 9000;

return chfdw_binary_connect(&details);
}
else
Expand Down
15 changes: 12 additions & 3 deletions src/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ static size_t write_data(void *contents, size_t size, size_t nmemb, void *userp)
return realsize;
}

#define CLICKHOUSE_PORT 8123
#define CLICKHOUSE_TLS_PORT 8443
#define HTTP_TLS_PORT 443

ch_http_connection_t *ch_http_connection(char *host, int port, char *username, char *password)
{
int n;
Expand All @@ -65,6 +69,9 @@ ch_http_connection_t *ch_http_connection(char *host, int port, char *username, c
if (!conn->curl)
goto cleanup;

if (!port)
port = ch_is_cloud_host(host) ? CLICKHOUSE_TLS_PORT : CLICKHOUSE_PORT;

len += strlen(host) + snprintf(NULL, 0, "%d", port);

if (username) {
Expand All @@ -81,19 +88,21 @@ ch_http_connection_t *ch_http_connection(char *host, int port, char *username, c
if (!connstring)
goto cleanup;

char *scheme = port == CLICKHOUSE_TLS_PORT || port == HTTP_TLS_PORT ? "https" : "http";

if (username && password)
{
n = snprintf(connstring, len, "http://%s:%s@%s:%d/", username, password, host, port);
n = snprintf(connstring, len, "%s://%s:%s@%s:%d/", scheme, username, password, host, port);
curl_free(username);
curl_free(password);
}
else if (username)
{
n = snprintf(connstring, len, "http://%s@%s:%d/", username, host, port);
n = snprintf(connstring, len, "%s://%s@%s:%d/", scheme, username, host, port);
curl_free(username);
}
else
n = snprintf(connstring, len, "http://%s:%d/", host, port);
n = snprintf(connstring, len, "%s://%s:%d/", scheme, host, port);

if (n < 0)
goto cleanup;
Expand Down
6 changes: 6 additions & 0 deletions src/include/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ typedef struct ch_binary_connection_t
char *error;
} ch_binary_connection_t;

/*
* Check whether the given string matches a ClickHouse Cloud host name.
*/
extern int ch_is_cloud_host(const char *host);
int ends_with(const char *s, const char *suffix);

#endif /* CLICKHOUSE_INTERNAL_H */
19 changes: 19 additions & 0 deletions src/internal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <string.h>
#include <internal.h>

int ends_with(const char *s, const char *suffix) {
size_t slen = strlen(s);
size_t suffix_len = strlen(suffix);
return suffix_len <= slen && !strcmp(s + slen - suffix_len, suffix);
}

/*
* Check whether the given string matches a ClickHouse Cloud host name.
*/
int ch_is_cloud_host(const char *host)
{
if (!host) return 0;
return ends_with(host, ".clickhouse.cloud")
|| ends_with(host, ".clickhouse-staging.com")
|| ends_with(host, ".clickhouse-dev.com");
}
3 changes: 1 addition & 2 deletions src/option.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

static char *DEFAULT_DBNAME = "default";


/*
* Describes the valid options for objects that this wrapper uses.
*/
Expand All @@ -46,7 +45,7 @@ typedef struct ChFdwOption
static ChFdwOption *clickhouse_fdw_options;

/*
* Valid options for clickhouseclient.
* Valid options for clickhouse client.
* Allocated and filled in InitChFdwOptions.
*/
static const ChFdwOption ch_options[] =
Expand Down