Skip to content

Commit

Permalink
add support of IPv4 and IPv6, fix #56
Browse files Browse the repository at this point in the history
  • Loading branch information
ildus committed Nov 27, 2020
1 parent 5ef4e42 commit a482ab2
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/binary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdexcept>

#include "clickhouse/columns/date.h"
#include "clickhouse/columns/ip4.h"
#include "clickhouse/columns/lowcardinality.h"
#include "clickhouse/columns/nullable.h"
#include "clickhouse/columns/factory.h"
Expand All @@ -18,6 +19,7 @@ extern "C" {
#include "postgres.h"
#include "pgtime.h"
#include "funcapi.h"
#include "fmgr.h"
#include "access/htup_details.h"
#include "access/tupdesc.h"
#include "catalog/pg_type_d.h"
Expand Down Expand Up @@ -922,8 +924,20 @@ static Datum make_datum(clickhouse::ColumnRef col, size_t row, Oid * valtype, bo
*valtype = TEXTOID;
}
break;
case Type::Code::IPv4: {
auto item = col->As<ColumnIPv4>()->AsString(row);
ret = DirectFunctionCall1(inet_in, CStringGetDatum(item.c_str()));
*valtype = INETOID;
}
break;
case Type::Code::IPv6: {
auto item = col->As<ColumnIPv6>()->AsString(row);
ret = DirectFunctionCall1(inet_in, CStringGetDatum(item.c_str()));
*valtype = INETOID;
}
break;
default:
throw std::runtime_error("clickhouse_fdw: unsupported type");
throw std::runtime_error("unsupported type in binary protocol");
}

return ret;
Expand Down
4 changes: 3 additions & 1 deletion src/pglink.c
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,8 @@ static char *str_types_map[][2] = {
{"DateTime", "TIMESTAMP"},
{"Date", "DATE"}, // important that this one is after other Date types
{"UUID", "UUID"},
{"IPv4", "inet"},
{"IPv6", "inet"},
{NULL, NULL},
};

Expand Down Expand Up @@ -835,7 +837,7 @@ parse_type(char *colname, char *typepart, bool *is_nullable, List **options)
}

size_t i = 0;
while (str_types_map[i] != NULL)
while (str_types_map[i][0] != NULL)
{
if (strncmp(str_types_map[i][0], typepart, strlen(str_types_map[i][0])) == 0)
{
Expand Down
60 changes: 59 additions & 1 deletion tests/expected/import_schema.out
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,27 @@ SELECT clickhousedb_raw_query('INSERT INTO regression.types2 SELECT

(1 row)

SELECT clickhousedb_raw_query('CREATE TABLE regression.ip (
c1 IPv4,
c2 IPv6
) ENGINE = MergeTree PARTITION BY c1 ORDER BY (c1);
');
clickhousedb_raw_query
------------------------

(1 row)

SELECT clickhousedb_raw_query($$
INSERT INTO regression.ip VALUES
('116.106.34.242', '2001:44c8:129:2632:33:0:252:2'),
('116.106.34.243', '2a02:e980:1e::1'),
('116.106.34.244', '::1');
$$);
clickhousedb_raw_query
------------------------

(1 row)

-- array types
SELECT clickhousedb_raw_query('CREATE TABLE regression.arrays (
c1 Array(Int), c2 Array(String)
Expand Down Expand Up @@ -254,6 +275,15 @@ FDW options: (database 'regression', table_name 'tuples', engine 'MergeTree')
Server: loopback
FDW options: (database 'regression', table_name 'timezones', engine 'MergeTree')

\d+ clickhouse.ip;
Foreign table "clickhouse.ip"
Column | Type | Collation | Nullable | Default | FDW options | Storage | Stats target | Description
--------+------+-----------+----------+---------+-------------+---------+--------------+-------------
c1 | inet | | not null | | | main | |
c2 | inet | | not null | | | main | |
Server: loopback
FDW options: (database 'regression', table_name 'ip', engine 'MergeTree')

SELECT * FROM clickhouse.ints ORDER BY c1 DESC LIMIT 4;
c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8 | c9 | c10
----+----+----+----+----+----+----+----+------+------
Expand Down Expand Up @@ -298,6 +328,14 @@ SELECT * FROM clickhouse.timezones ORDER BY t1 LIMIT 2;
2020-01-01 12:00:00 | 2020-01-01 12:00:00 | 2020-01-01 12:00:00 | 2020-01-01 12:00:00
(2 rows)

SELECT * FROM clickhouse.ip ORDER BY c1;
c1 | c2
----------------+-------------------------------
116.106.34.242 | 2001:44c8:129:2632:33:0:252:2
116.106.34.243 | 2a02:e980:1e::1
116.106.34.244 | ::1
(3 rows)

IMPORT FOREIGN SCHEMA "regression" FROM SERVER loopback_bin INTO clickhouse_bin;
NOTICE: clickhouse_fdw: ClickHouse <UInt8> type was translated to <INT2> type for column "c5", change it to BOOLEAN if needed
NOTICE: clickhouse_fdw: ClickHouse <Tuple> type was translated to <TEXT> type for column "c2", please create composite type and alter the column if needed
Expand Down Expand Up @@ -374,6 +412,15 @@ FDW options: (database 'regression', table_name 'tuples', engine 'MergeTree')
Server: loopback_bin
FDW options: (database 'regression', table_name 'timezones', engine 'MergeTree')

\d+ clickhouse_bin.ip;
Foreign table "clickhouse_bin.ip"
Column | Type | Collation | Nullable | Default | FDW options | Storage | Stats target | Description
--------+------+-----------+----------+---------+-------------+---------+--------------+-------------
c1 | inet | | not null | | | main | |
c2 | inet | | not null | | | main | |
Server: loopback_bin
FDW options: (database 'regression', table_name 'ip', engine 'MergeTree')

SELECT * FROM clickhouse_bin.ints ORDER BY c1 DESC LIMIT 4;
c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8 | c9 | c10
----+----+----+----+----+----+----+----+------+------
Expand Down Expand Up @@ -413,6 +460,14 @@ SELECT * FROM clickhouse_bin.timezones ORDER BY t1 LIMIT 2;
2020-01-01 12:00:00 | 2020-01-01 11:00:00 | 2020-01-01 11:00:00 | 2020-01-01 12:00:00
(2 rows)

SELECT * FROM clickhouse.ip ORDER BY c1;
c1 | c2
----------------+-------------------------------
116.106.34.242 | 2001:44c8:129:2632:33:0:252:2
116.106.34.243 | 2a02:e980:1e::1
116.106.34.244 | ::1
(3 rows)

IMPORT FOREIGN SCHEMA "regression" LIMIT TO (ints, types) FROM SERVER loopback INTO clickhouse_limit;
NOTICE: clickhouse_fdw: ClickHouse <UInt8> type was translated to <INT2> type for column "c5", change it to BOOLEAN if needed
\d+ clickhouse_limit.ints;
Expand Down Expand Up @@ -515,24 +570,27 @@ SELECT clickhousedb_raw_query('DROP DATABASE regression_2');
(1 row)

DROP EXTENSION clickhouse_fdw CASCADE;
NOTICE: drop cascades to 21 other objects
NOTICE: drop cascades to 24 other objects
DETAIL: drop cascades to server loopback
drop cascades to foreign table clickhouse.arrays
drop cascades to foreign table clickhouse.ints
drop cascades to foreign table clickhouse.ip
drop cascades to foreign table clickhouse.timezones
drop cascades to foreign table clickhouse.tuples
drop cascades to foreign table clickhouse.types
drop cascades to foreign table clickhouse.types2
drop cascades to foreign table clickhouse_limit.ints
drop cascades to foreign table clickhouse_limit.types
drop cascades to foreign table clickhouse_except.arrays
drop cascades to foreign table clickhouse_except.ip
drop cascades to foreign table clickhouse_except.timezones
drop cascades to foreign table clickhouse_except.tuples
drop cascades to foreign table clickhouse_except.types2
drop cascades to foreign table clickhouse.custom_option
drop cascades to server loopback_bin
drop cascades to foreign table clickhouse_bin.arrays
drop cascades to foreign table clickhouse_bin.ints
drop cascades to foreign table clickhouse_bin.ip
drop cascades to foreign table clickhouse_bin.timezones
drop cascades to foreign table clickhouse_bin.tuples
drop cascades to foreign table clickhouse_bin.types
Expand Down
16 changes: 16 additions & 0 deletions tests/sql/import_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ SELECT clickhousedb_raw_query('INSERT INTO regression.types2 SELECT
format(''cardinal {0}'', toString(number + 1))
FROM numbers(10);');

SELECT clickhousedb_raw_query('CREATE TABLE regression.ip (
c1 IPv4,
c2 IPv6
) ENGINE = MergeTree PARTITION BY c1 ORDER BY (c1);
');
SELECT clickhousedb_raw_query($$
INSERT INTO regression.ip VALUES
('116.106.34.242', '2001:44c8:129:2632:33:0:252:2'),
('116.106.34.243', '2a02:e980:1e::1'),
('116.106.34.244', '::1');
$$);

-- array types
SELECT clickhousedb_raw_query('CREATE TABLE regression.arrays (
c1 Array(Int), c2 Array(String)
Expand Down Expand Up @@ -111,13 +123,15 @@ IMPORT FOREIGN SCHEMA "regression" FROM SERVER loopback INTO clickhouse;
\d+ clickhouse.arrays;
\d+ clickhouse.tuples;
\d+ clickhouse.timezones;
\d+ clickhouse.ip;

SELECT * FROM clickhouse.ints ORDER BY c1 DESC LIMIT 4;
SELECT * FROM clickhouse.types ORDER BY c1 LIMIT 2;
SELECT * FROM clickhouse.types2 ORDER BY c1 LIMIT 2;
SELECT * FROM clickhouse.arrays ORDER BY c1 LIMIT 2;
SELECT * FROM clickhouse.tuples ORDER BY c1 LIMIT 2;
SELECT * FROM clickhouse.timezones ORDER BY t1 LIMIT 2;
SELECT * FROM clickhouse.ip ORDER BY c1;

IMPORT FOREIGN SCHEMA "regression" FROM SERVER loopback_bin INTO clickhouse_bin;

Expand All @@ -127,13 +141,15 @@ IMPORT FOREIGN SCHEMA "regression" FROM SERVER loopback_bin INTO clickhouse_bin;
\d+ clickhouse_bin.arrays;
\d+ clickhouse_bin.tuples;
\d+ clickhouse_bin.timezones;
\d+ clickhouse_bin.ip;

SELECT * FROM clickhouse_bin.ints ORDER BY c1 DESC LIMIT 4;
SELECT * FROM clickhouse_bin.types ORDER BY c1 LIMIT 2;
SELECT * FROM clickhouse_bin.types2 ORDER BY c1 LIMIT 2;
SELECT * FROM clickhouse_bin.arrays ORDER BY c1 LIMIT 2;
SELECT * FROM clickhouse_bin.tuples ORDER BY c1 LIMIT 2;
SELECT * FROM clickhouse_bin.timezones ORDER BY t1 LIMIT 2;
SELECT * FROM clickhouse.ip ORDER BY c1;

IMPORT FOREIGN SCHEMA "regression" LIMIT TO (ints, types) FROM SERVER loopback INTO clickhouse_limit;

Expand Down

0 comments on commit a482ab2

Please sign in to comment.