SET datestyle = 'ISO'; CREATE SERVER binary_json_loopback FOREIGN DATA WRAPPER clickhouse_fdw OPTIONS(dbname 'json_test', driver 'binary'); CREATE SERVER http_json_loopback FOREIGN DATA WRAPPER clickhouse_fdw OPTIONS(dbname 'json_test', driver 'http'); CREATE USER MAPPING FOR CURRENT_USER SERVER binary_json_loopback; CREATE USER MAPPING FOR CURRENT_USER SERVER http_json_loopback; SELECT clickhouse_raw_query('DROP DATABASE IF EXISTS json_test'); clickhouse_raw_query ---------------------- (1 row) SELECT clickhouse_raw_query('CREATE DATABASE json_test'); clickhouse_raw_query ---------------------- (1 row) SELECT clickhouse_raw_query($$ CREATE TABLE json_test.things ( id Int32 NOT NULL, data JSON NOT NULL ) ENGINE = MergeTree PARTITION BY id ORDER BY (id); $$); clickhouse_raw_query ---------------------- (1 row) CREATE SCHEMA json_bin; CREATE SCHEMA json_http; IMPORT FOREIGN SCHEMA "json_test" FROM SERVER binary_json_loopback INTO json_bin; ERROR: pg_clickhouse: could not map things.data type <'json')> \d json_bin.things IMPORT FOREIGN SCHEMA "json_test" FROM SERVER http_json_loopback INTO json_http; ERROR: pg_clickhouse: could not map things.data type <'json')> \d json_http.things -- Fails pending https://github.com/ClickHouse/clickhouse-cpp/issues/422 INSERT INTO json_bin.things VALUES (1, '{"id": 1, "name": "widget", "size": "large", "stocked": true}'), (2, '{"id": 2, "name": "sprocket", "size": "small", "stocked": true}') ; ERROR: relation "json_bin.things" does not exist LINE 1: INSERT INTO json_bin.things VALUES ^ INSERT INTO json_http.things VALUES (1, '{"id": 1, "name": "widget", "size": "large", "stocked": true}'), (2, '{"id": 2, "name": "sprocket", "size": "small", "stocked": true}'), (3, '{"id": 3, "name": "gizmo", "size": "medium", "stocked": true}'), (4, '{"id": 4, "name": "doodad", "size": "large", "stocked": false}') ; ERROR: relation "json_http.things" does not exist LINE 1: INSERT INTO json_http.things VALUES ^ SELECT * FROM json_bin.things ORDER BY id; ERROR: relation "json_bin.things" does not exist LINE 1: SELECT * FROM json_bin.things ORDER BY id; ^ SELECT * FROM json_http.things ORDER BY id; ERROR: relation "json_http.things" does not exist LINE 1: SELECT * FROM json_http.things ORDER BY id; ^ -- Subscript access on JSON columns must not be pushed down to ClickHouse. -- ClickHouse JSON does not support the jsonb `column['key']` syntax (it -- requires dot notation), so subscripts must be evaluated locally by -- PostgreSQL. EXPLAIN (VERBOSE, COSTS OFF) SELECT data['name'] FROM json_http.things; ERROR: relation "json_http.things" does not exist LINE 2: SELECT data['name'] FROM json_http.things; ^ SELECT data['name'] FROM json_http.things ORDER BY id; ERROR: relation "json_http.things" does not exist LINE 1: SELECT data['name'] FROM json_http.things ORDER BY id; ^ -- DISTINCT forces an ORDER BY or HashAgg; the subscript must stay local. EXPLAIN (VERBOSE, COSTS OFF) SELECT DISTINCT data['size'] FROM json_http.things; ERROR: relation "json_http.things" does not exist LINE 2: SELECT DISTINCT data['size'] FROM json_http.things; ^ SELECT DISTINCT data['size'] FROM json_http.things; ERROR: relation "json_http.things" does not exist LINE 1: SELECT DISTINCT data['size'] FROM json_http.things; ^ -- GROUP BY with a JSON subscript expression. EXPLAIN (VERBOSE, COSTS OFF) SELECT data['size'], count(*) FROM json_http.things GROUP BY data['size']; ERROR: relation "json_http.things" does not exist LINE 2: SELECT data['size'], count(*) FROM json_http.things GROUP BY... ^ SELECT data['size'], count(*) FROM json_http.things GROUP BY data['size']; ERROR: relation "json_http.things" does not exist LINE 1: SELECT data['size'], count(*) FROM json_http.things GROUP BY... ^ -- The jsonb ->> operator is pushed down in WHERE / ORDER BY clauses, but -- target-list expressions are evaluated locally (PostgreSQL fetches the whole -- column and applies the operator after). This query runs -> locally. -- N.B.: Binary driver JSON data not yet supported. EXPLAIN (VERBOSE, COSTS OFF) SELECT data ->> 'name' FROM json_http.things; ERROR: relation "json_http.things" does not exist LINE 2: SELECT data ->> 'name' FROM json_http.things; ^ SELECT data ->> 'name' FROM json_http.things ORDER BY id; ERROR: relation "json_http.things" does not exist LINE 1: SELECT data ->> 'name' FROM json_http.things ORDER BY id; ^ -- WHERE clause with ->> equality must be pushed down. EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM json_http.things WHERE data ->> 'name' = 'widget'; ERROR: relation "json_http.things" does not exist LINE 2: SELECT * FROM json_http.things WHERE data ->> 'name' = 'widg... ^ SELECT * FROM json_http.things WHERE data ->> 'name' = 'widget'; ERROR: relation "json_http.things" does not exist LINE 1: SELECT * FROM json_http.things WHERE data ->> 'name' = 'widg... ^ -- WHERE clause with ->> and LIKE. EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM json_http.things WHERE data ->> 'name' LIKE 'wid%'; ERROR: relation "json_http.things" does not exist LINE 2: SELECT * FROM json_http.things WHERE data ->> 'name' LIKE 'w... ^ -- WHERE with multiple ->> conditions (AND). EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM json_http.things WHERE data ->> 'size' = 'large' AND data ->> 'stocked' = 'true'; ERROR: relation "json_http.things" does not exist LINE 2: SELECT * FROM json_http.things ^ -- WHERE with ->> in an OR condition. EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM json_http.things WHERE data ->> 'name' = 'widget' OR data ->> 'name' = 'gizmo'; ERROR: relation "json_http.things" does not exist LINE 2: SELECT * FROM json_http.things ^ -- ORDER BY with ->> pushdown. EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM json_http.things ORDER BY data ->> 'size'; ERROR: relation "json_http.things" does not exist LINE 2: SELECT * FROM json_http.things ORDER BY data ->> 'size'; ^ -- The jsonb -> operator: target-list expressions are evaluated locally -- (same as ->>). This query evaluates `->` locally. EXPLAIN (VERBOSE, COSTS OFF) SELECT data -> 'name' FROM json_http.things; ERROR: relation "json_http.things" does not exist LINE 2: SELECT data -> 'name' FROM json_http.things; ^ SELECT data -> 'name' FROM json_http.things ORDER BY id; ERROR: relation "json_http.things" does not exist LINE 1: SELECT data -> 'name' FROM json_http.things ORDER BY id; ^ -- WHERE clause with -> equality must be pushed down. EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM json_http.things WHERE data -> 'name' = '"widget"'; ERROR: relation "json_http.things" does not exist LINE 2: SELECT * FROM json_http.things WHERE data -> 'name' = '"widg... ^ SELECT * FROM json_http.things WHERE data -> 'name' = '"widget"'; ERROR: relation "json_http.things" does not exist LINE 1: SELECT * FROM json_http.things WHERE data -> 'name' = '"widg... ^ -- WHERE clause with -> JSON boolean literal must push down. EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM json_http.things WHERE data -> 'stocked' = 'true'::jsonb; ERROR: relation "json_http.things" does not exist LINE 2: SELECT * FROM json_http.things WHERE data -> 'stocked' = 'tr... ^ SELECT * FROM json_http.things WHERE data -> 'stocked' = 'true'::jsonb ORDER BY id; ERROR: relation "json_http.things" does not exist LINE 1: SELECT * FROM json_http.things WHERE data -> 'stocked' = 'tr... ^ -- WHERE clause with -> wraps the dot notation in toJSONString() so that the -- result is a proper JSON value (-> returns jsonb, not text like ->>). EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM json_http.things WHERE data -> 'name' = '"widget"'::jsonb; ERROR: relation "json_http.things" does not exist LINE 2: SELECT * FROM json_http.things WHERE data -> 'name' = '"widg... ^ -- Edge cases: JSON keys that require identifier quoting. SELECT clickhouse_raw_query($$ CREATE TABLE json_test.special_keys ( id Int32 NOT NULL, data JSON NOT NULL ) ENGINE = MergeTree ORDER BY (id); $$); clickhouse_raw_query ---------------------- (1 row) CREATE FOREIGN TABLE json_http.special_keys (id integer NOT NULL, data jsonb NOT NULL) SERVER http_json_loopback OPTIONS (database 'json_test', table_name 'special_keys'); INSERT INTO json_http.special_keys VALUES (1, '{"my field": "hello", "CamelCase": "world", "select": "reserved"}'), (2, E'{"The \\"meaning\\" of life": 42, "back\\\\slash": "bs", "dotted.key": "dot", "it''s": "apos", "key/with!special@chars#": "special", "123numeric": "num"}'); -- Key with a space: must be quoted in the remote SQL. EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM json_http.special_keys WHERE data ->> 'my field' = 'hello'; QUERY PLAN ----------------------------------------------------------------------------------------------- Foreign Scan on json_http.special_keys Output: id, data Remote SQL: SELECT id, data FROM json_test.special_keys WHERE ((data."my field" = 'hello')) (3 rows) SELECT data ->> 'my field' FROM json_http.special_keys; ERROR: invalid input syntax for type json DETAIL: Token "(" is invalid. CONTEXT: JSON data, line 1: (... -- Key with mixed case. EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM json_http.special_keys WHERE data ->> 'CamelCase' = 'world'; QUERY PLAN ------------------------------------------------------------------------------------------------ Foreign Scan on json_http.special_keys Output: id, data Remote SQL: SELECT id, data FROM json_test.special_keys WHERE ((data."CamelCase" = 'world')) (3 rows) SELECT data ->> 'CamelCase' FROM json_http.special_keys; ERROR: invalid input syntax for type json DETAIL: Token "(" is invalid. CONTEXT: JSON data, line 1: (... -- Key that is a SQL reserved word. EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM json_http.special_keys WHERE data ->> 'select' = 'reserved'; QUERY PLAN ------------------------------------------------------------------------------------------------ Foreign Scan on json_http.special_keys Output: id, data Remote SQL: SELECT id, data FROM json_test.special_keys WHERE ((data."select" = 'reserved')) (3 rows) SELECT data ->> 'select' FROM json_http.special_keys; ERROR: invalid input syntax for type json DETAIL: Token "(" is invalid. CONTEXT: JSON data, line 1: (... -- Key containing embedded double quotes. EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM json_http.special_keys WHERE data ->> 'The "meaning" of life' = '42'; QUERY PLAN ----------------------------------------------------------------------------------------------------------- Foreign Scan on json_http.special_keys Output: id, data Remote SQL: SELECT id, data FROM json_test.special_keys WHERE ((data."The ""meaning"" of life" = '42')) (3 rows) SELECT data ->> 'The "meaning" of life' FROM json_http.special_keys WHERE id = 2; ERROR: invalid input syntax for type json DETAIL: Token "(" is invalid. CONTEXT: JSON data, line 1: (... -- Key containing a backslash. EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM json_http.special_keys WHERE data ->> 'back\slash' = 'bs'; QUERY PLAN ---------------------------------------------------------------------------------------------- Foreign Scan on json_http.special_keys Output: id, data Remote SQL: SELECT id, data FROM json_test.special_keys WHERE ((data."back\slash" = 'bs')) (3 rows) SELECT data ->> 'back\slash' FROM json_http.special_keys WHERE id = 2; ERROR: invalid input syntax for type json DETAIL: Token "(" is invalid. CONTEXT: JSON data, line 1: (... -- Key containing a dot (must not be confused with nested access). EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM json_http.special_keys WHERE data ->> 'dotted.key' = 'dot'; QUERY PLAN ----------------------------------------------------------------------------------------------- Foreign Scan on json_http.special_keys Output: id, data Remote SQL: SELECT id, data FROM json_test.special_keys WHERE ((data."dotted.key" = 'dot')) (3 rows) SELECT data ->> 'dotted.key' FROM json_http.special_keys WHERE id = 2; ERROR: invalid input syntax for type json DETAIL: Token "(" is invalid. CONTEXT: JSON data, line 1: (... -- Key containing an apostrophe / single quote. EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM json_http.special_keys WHERE data ->> 'it''s' = 'apos'; QUERY PLAN ------------------------------------------------------------------------------------------ Foreign Scan on json_http.special_keys Output: id, data Remote SQL: SELECT id, data FROM json_test.special_keys WHERE ((data."it's" = 'apos')) (3 rows) SELECT data ->> 'it''s' FROM json_http.special_keys WHERE id = 2; ERROR: invalid input syntax for type json DETAIL: Token "(" is invalid. CONTEXT: JSON data, line 1: (... -- Key with slashes, bangs, at-signs, etc. EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM json_http.special_keys WHERE data ->> 'key/with!special@chars#' = 'special'; QUERY PLAN ---------------------------------------------------------------------------------------------------------------- Foreign Scan on json_http.special_keys Output: id, data Remote SQL: SELECT id, data FROM json_test.special_keys WHERE ((data."key/with!special@chars#" = 'special')) (3 rows) -- Key that starts with a digit. EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM json_http.special_keys WHERE data ->> '123numeric' = 'num'; QUERY PLAN ----------------------------------------------------------------------------------------------- Foreign Scan on json_http.special_keys Output: id, data Remote SQL: SELECT id, data FROM json_test.special_keys WHERE ((data."123numeric" = 'num')) (3 rows) -- ======================================================================= -- jsonb_extract_path_text / jsonb_extract_path pushdown -- ======================================================================= -- Create a table with nested JSON for multi-level path tests. SELECT clickhouse_raw_query($$ CREATE TABLE json_test.events ( id UInt32, event_name String, props JSON ) ENGINE = MergeTree ORDER BY (event_name, id); $$); clickhouse_raw_query ---------------------- (1 row) SELECT clickhouse_raw_query($$ INSERT INTO json_test.events VALUES (1, 'order', '{"customerId": "C100", "address": {"city": "Paris", "zip": "75001"}}'), (2, 'order', '{"customerId": "C200", "address": {"city": "London", "zip": "SW1A"}}'); $$); clickhouse_raw_query ---------------------- (1 row) CREATE FOREIGN TABLE json_http_events ( id integer, event_name text, props jsonb ) SERVER http_json_loopback OPTIONS (table_name 'events'); -- Target-list: jsonb_extract_path_text is evaluated locally (like -> / ->>). EXPLAIN (VERBOSE, COSTS OFF) SELECT jsonb_extract_path_text(props, 'customerId') FROM json_http_events; QUERY PLAN --------------------------------------------------------------------------- Foreign Scan on public.json_http_events Output: jsonb_extract_path_text(props, VARIADIC '{customerId}'::text[]) Remote SQL: SELECT props FROM json_test.events (3 rows) SELECT jsonb_extract_path_text(props, 'customerId') FROM json_http_events ORDER BY id; ERROR: invalid input syntax for type json DETAIL: Token "(" is invalid. CONTEXT: JSON data, line 1: (... -- Target-list: multi-level path, still evaluated locally. EXPLAIN (VERBOSE, COSTS OFF) SELECT jsonb_extract_path_text(props, 'address', 'city') FROM json_http_events; QUERY PLAN ----------------------------------------------------------------------------- Foreign Scan on public.json_http_events Output: jsonb_extract_path_text(props, VARIADIC '{address,city}'::text[]) Remote SQL: SELECT props FROM json_test.events (3 rows) SELECT jsonb_extract_path_text(props, 'address', 'city') FROM json_http_events ORDER BY id; ERROR: invalid input syntax for type json DETAIL: Token "(" is invalid. CONTEXT: JSON data, line 1: (... -- Target-list: jsonb_extract_path (returns jsonb, not text), evaluated locally. EXPLAIN (VERBOSE, COSTS OFF) SELECT jsonb_extract_path(props, 'address') FROM json_http_events; QUERY PLAN ------------------------------------------------------------------- Foreign Scan on public.json_http_events Output: jsonb_extract_path(props, VARIADIC '{address}'::text[]) Remote SQL: SELECT props FROM json_test.events (3 rows) -- WHERE: single-level jsonb_extract_path_text pushes down as dot notation. EXPLAIN (VERBOSE, COSTS OFF) SELECT id FROM json_http_events WHERE jsonb_extract_path_text(props, 'customerId') = 'C100'; QUERY PLAN ------------------------------------------------------------------------------------- Foreign Scan on public.json_http_events Output: id Remote SQL: SELECT id FROM json_test.events WHERE ((props."customerId" = 'C100')) (3 rows) SELECT id FROM json_http_events WHERE jsonb_extract_path_text(props, 'customerId') = 'C100'; id ---- 1 (1 row) -- WHERE: multi-level jsonb_extract_path_text pushes down as dot notation. EXPLAIN (VERBOSE, COSTS OFF) SELECT id FROM json_http_events WHERE jsonb_extract_path_text(props, 'address', 'city') = 'Paris'; QUERY PLAN -------------------------------------------------------------------------------------- Foreign Scan on public.json_http_events Output: id Remote SQL: SELECT id FROM json_test.events WHERE ((props.address.city = 'Paris')) (3 rows) SELECT id FROM json_http_events WHERE jsonb_extract_path_text(props, 'address', 'city') = 'Paris'; id ---- 1 (1 row) -- WHERE: jsonb_extract_path pushes down with toJSONString wrapping. EXPLAIN (VERBOSE, COSTS OFF) SELECT id FROM json_http_events WHERE jsonb_extract_path(props, 'address', 'city') = '"Paris"'::jsonb; QUERY PLAN ------------------------------------------------------------------------------------------------------ Foreign Scan on public.json_http_events Output: id Remote SQL: SELECT id FROM json_test.events WHERE ((toJSONString(props.address.city) = '"Paris"')) (3 rows) DROP FOREIGN TABLE json_http_events; SELECT clickhouse_raw_query('DROP DATABASE json_test'); clickhouse_raw_query ---------------------- (1 row) DROP USER MAPPING FOR CURRENT_USER SERVER binary_json_loopback; DROP USER MAPPING FOR CURRENT_USER SERVER http_json_loopback; DROP SERVER binary_json_loopback CASCADE; DROP SERVER http_json_loopback CASCADE; NOTICE: drop cascades to foreign table json_http.special_keys