Skip to content

Commit 1c53576

Browse files
committed
Map ClickHouse JSON type to PostgreSQL JSONB
Add support for JSONB to `INSERT` statements and, when importing tables, map the ClickHouse JSON type to PostgreSQL JSONB. This increases the what users can do with these values; they can still use JSON for manually-created tables. Does not yet include binary engine support for JSON, which will requires support from clickhouse-cpp (ClickHouse/clickhouse-cpp#450).
1 parent bb0b395 commit 1c53576

File tree

4 files changed

+20
-19
lines changed

4 files changed

+20
-19
lines changed

doc/pg_clickhouse.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ types:
310310
Int32 | integer |
311311
Int64 | bigint |
312312
Int8 | smallint |
313-
JSON | json | HTTP engine only
313+
JSON | jsonb | HTTP engine only
314314
String | text |
315315
UInt16 | integer |
316316
UInt32 | bigint |

src/pglink.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ extend_insert_query(ch_http_insert_state * state, TupleTableSlot * slot)
400400
case VARCHAROID:
401401
case TEXTOID:
402402
case JSONOID:
403+
case JSONBOID:
403404
case NAMEOID:
404405
case BITOID:
405406
case BYTEAOID:
@@ -785,7 +786,7 @@ binary_insert_tuple(void *istate, TupleTableSlot * slot)
785786
('UUID', 'uuid', ''),
786787
('IPv4', 'inet', ''),
787788
('IPv6', 'inet', ''),
788-
('JSON', 'json', 'HTTP engine only')
789+
('JSON', 'jsonb', 'HTTP engine only')
789790
) AS v("ClickHouse", "PostgreSQL", "Notes")
790791
ORDER BY "ClickHouse";
791792
@@ -810,7 +811,7 @@ static char *str_types_map[][2] = {
810811
{"UUID", "UUID"},
811812
{"IPv4", "inet"},
812813
{"IPv6", "inet"},
813-
{"JSON", "JSON"},
814+
{"JSON", "JSONB"},
814815
{NULL, NULL},
815816
};
816817

test/expected/json.out

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ IMPORT FOREIGN SCHEMA "json_test" FROM SERVER binary_json_loopback INTO json_bin
3434
Column | Type | Collation | Nullable | Default | FDW options
3535
--------+---------+-----------+----------+---------+-------------
3636
id | integer | | not null | |
37-
data | json | | not null | |
37+
data | jsonb | | not null | |
3838
Server: binary_json_loopback
3939
FDW options: (database 'json_test', table_name 'things', engine 'MergeTree')
4040

@@ -44,7 +44,7 @@ IMPORT FOREIGN SCHEMA "json_test" FROM SERVER http_json_loopback INTO json_http;
4444
Column | Type | Collation | Nullable | Default | FDW options
4545
--------+---------+-----------+----------+---------+-------------
4646
id | integer | | not null | |
47-
data | json | | not null | |
47+
data | jsonb | | not null | |
4848
Server: http_json_loopback
4949
FDW options: (database 'json_test', table_name 'things', engine 'MergeTree')
5050

@@ -64,12 +64,12 @@ SELECT * FROM json_bin.things ORDER BY id;
6464
ERROR: pg_clickhouse: unsupported column type: JSON
6565
DETAIL: Remote Query: SELECT id, data FROM json_test.things ORDER BY id ASC NULLS LAST
6666
SELECT * FROM json_http.things ORDER BY id;
67-
id | data
68-
----+----------------------------------------------------------
69-
1 | {"id":1,"name":"widget","size":"large","stocked":true}
70-
2 | {"id":2,"name":"sprocket","size":"small","stocked":true}
71-
3 | {"id":3,"name":"gizmo","size":"medium","stocked":true}
72-
4 | {"id":4,"name":"doodad","size":"large","stocked":false}
67+
id | data
68+
----+-----------------------------------------------------------------
69+
1 | {"id": 1, "name": "widget", "size": "large", "stocked": true}
70+
2 | {"id": 2, "name": "sprocket", "size": "small", "stocked": true}
71+
3 | {"id": 3, "name": "gizmo", "size": "medium", "stocked": true}
72+
4 | {"id": 4, "name": "doodad", "size": "large", "stocked": false}
7373
(4 rows)
7474

7575
SELECT clickhouse_raw_query('DROP DATABASE json_test');

test/expected/json_1.out

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ IMPORT FOREIGN SCHEMA "json_test" FROM SERVER binary_json_loopback INTO json_bin
3434
Column | Type | Collation | Nullable | Default | FDW options
3535
--------+---------+-----------+----------+---------+-------------
3636
id | integer | | not null | |
37-
data | json | | not null | |
37+
data | jsonb | | not null | |
3838
Server: binary_json_loopback
3939
FDW options: (database 'json_test', table_name 'things', engine 'MergeTree')
4040

@@ -44,7 +44,7 @@ IMPORT FOREIGN SCHEMA "json_test" FROM SERVER http_json_loopback INTO json_http;
4444
Column | Type | Collation | Nullable | Default | FDW options
4545
--------+---------+-----------+----------+---------+-------------
4646
id | integer | | not null | |
47-
data | json | | not null | |
47+
data | jsonb | | not null | |
4848
Server: http_json_loopback
4949
FDW options: (database 'json_test', table_name 'things', engine 'MergeTree')
5050

@@ -64,12 +64,12 @@ SELECT * FROM json_bin.things ORDER BY id;
6464
ERROR: pg_clickhouse: unsupported column type: JSON
6565
DETAIL: Remote Query: SELECT id, data FROM json_test.things ORDER BY id ASC NULLS LAST
6666
SELECT * FROM json_http.things ORDER BY id;
67-
id | data
68-
----+------------------------------------------------------------
69-
1 | {"id":"1","name":"widget","size":"large","stocked":true}
70-
2 | {"id":"2","name":"sprocket","size":"small","stocked":true}
71-
3 | {"id":"3","name":"gizmo","size":"medium","stocked":true}
72-
4 | {"id":"4","name":"doodad","size":"large","stocked":false}
67+
id | data
68+
----+-------------------------------------------------------------------
69+
1 | {"id": "1", "name": "widget", "size": "large", "stocked": true}
70+
2 | {"id": "2", "name": "sprocket", "size": "small", "stocked": true}
71+
3 | {"id": "3", "name": "gizmo", "size": "medium", "stocked": true}
72+
4 | {"id": "4", "name": "doodad", "size": "large", "stocked": false}
7373
(4 rows)
7474

7575
SELECT clickhouse_raw_query('DROP DATABASE json_test');

0 commit comments

Comments
 (0)