Skip to content

Commit 40f6c75

Browse files
committed
Merge branch 'DBTOOLS-1895_clickhouse_add_json' into 'master'
DBTOOLS-1895 added json rules for clickhouse See merge request codekeeper/pgcodekeeper-core!284
2 parents 2b57658 + a0fe2fd commit 40f6c75

9 files changed

Lines changed: 231 additions & 170 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Added
1111

12+
- Improved parser rules for working with JSON formats in ClikHouse.
13+
1214
### Changed
1315

1416
### Fixed

CHANGELOG.ru.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
### Добавлено
1111

12+
- Улучшены правела парсера для работы с форматами JSON в ClikHouse.
13+
1214
### Изменено
1315

1416
### Исправлено

src/main/antlr4/org/pgcodekeeper/core/database/ch/parser/generated/CHParser.g4

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -945,14 +945,25 @@ move_stmt
945945
;
946946

947947
json_pair
948-
: json_value COLON json_value
948+
: json_key COLON json_value
949949
;
950950

951-
json_value
951+
json_key
952952
: DOUBLE_QUOTED_IDENTIFIER
953953
| literal
954954
;
955955

956+
json_value
957+
: LBRACE (json_pair (COMMA json_pair)*)? RBRACE
958+
| LBRACKET (json_value (COMMA json_value)*)? RBRACKET
959+
| DOUBLE_QUOTED_IDENTIFIER
960+
| signed_number_literal
961+
| STRING_LITERAL
962+
| NULL
963+
| BINARY_LITERAL
964+
| IDENTIFIER
965+
;
966+
956967
insert_stmt
957968
: with_clause? INSERT INTO TABLE? (qualified_name | FUNCTION table_function_expr) settings_clause? columns_clause? data_clause
958969
;
@@ -964,7 +975,7 @@ columns_clause
964975

965976
data_clause
966977
: FROM INFILE STRING_LITERAL (COMPRESSION identifier)? settings_clause?
967-
| FORMAT identifier
978+
| FORMAT identifier (json_value (COMMA? json_value)*)?
968979
| VALUES values_values (COMMA values_values)*
969980
| select_stmt
970981
;

src/test/resources/org/pgcodekeeper/core/it/parser/ch/ch_insert.sql

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,21 @@ INSERT INTO test_table FROM INFILE '/path/to/file.csv' FORMAT CSV;
358358
INSERT INTO test_table FROM INFILE '/path/to/file.csv' COMPRESSION 'gzip';
359359
INSERT INTO test_table FROM INFILE '/path/to/file.csv' COMPRESSION 'gzip' FORMAT CSV;
360360
INSERT INTO test_table FROM INFILE '/path/to/file.csv' SETTINGS max_threads = 4;
361-
INSERT INTO test_table FROM INFILE '/path/to/file.csv' SETTINGS max_threads = 4 FORMAT CSV;
361+
INSERT INTO test_table FROM INFILE '/path/to/file.csv' SETTINGS max_threads = 4 FORMAT CSV;
362+
INSERT INTO test FORMAT JSONEachRow {"json" : {"a" : 42}}, {"json" : {"b" : "Hello"}}, {"json" : {"a" : [1, 2, 3], "c" : "2020-01-01"}};
363+
INSERT INTO t_json_desc FORMAT JSONAsObject {"k1": 10};
364+
INSERT INTO t_json_desc FORMAT JSONAsObject
365+
[
366+
{"k1": 10, "k2": "value1"},
367+
{"k1": 20, "k2": "value2"},
368+
{"k1": 30, "k2": "value3"}
369+
];
370+
INSERT INTO t_json_desc FORMAT JSONAsObject
371+
{"k1": 10, "k2": "value1"}
372+
{"k1": 20, "k2": "value2"}
373+
{"k1": 30, "k2": "value3"};
374+
INSERT INTO test FORMAT JSONEachRow {"b": true, "n": false, "z": null, "neg": -3.14, "exp": 1.5e3, "s": "txt", "empty_obj": {}, "empty_arr": []};
375+
INSERT INTO test FORMAT JSONEachRow {"nested": {"arr": [{"x": 1}, {"x": -2}], "flag": true, "deep": {"a": {"b": [null, "v"]}}}};
376+
INSERT INTO test FORMAT JSONEachRow {"a": 1}{"a": 2}{"a": 3};
377+
INSERT INTO test FORMAT JSONAsObject [{"k": 1}, {"k": 2}];
378+
INSERT INTO test FORMAT JSONEachRow {"mixed": [1, "two", true, null, [3, 4], {"k": "v"}]};

src/test/resources/org/pgcodekeeper/core/it/parser/ch/ch_insert_refs.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,13 @@ Reference: action = INSERT, line number = 357, charPositionInLine = 0
358358
Reference: action = INSERT, line number = 358, charPositionInLine = 0
359359
Reference: action = INSERT, line number = 359, charPositionInLine = 0
360360
Reference: action = INSERT, line number = 360, charPositionInLine = 0
361-
Reference: action = INSERT, line number = 361, charPositionInLine = 0
361+
Reference: action = INSERT, line number = 361, charPositionInLine = 0
362+
Reference: action = INSERT, line number = 362, charPositionInLine = 0
363+
Reference: action = INSERT, line number = 363, charPositionInLine = 0
364+
Reference: action = INSERT, line number = 364, charPositionInLine = 0
365+
Reference: action = INSERT, line number = 370, charPositionInLine = 0
366+
Reference: action = INSERT, line number = 374, charPositionInLine = 0
367+
Reference: action = INSERT, line number = 375, charPositionInLine = 0
368+
Reference: action = INSERT, line number = 376, charPositionInLine = 0
369+
Reference: action = INSERT, line number = 377, charPositionInLine = 0
370+
Reference: action = INSERT, line number = 378, charPositionInLine = 0

src/test/resources/org/pgcodekeeper/core/it/parser/ch/ch_select.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,4 +1408,10 @@ select arrayMap( x -> x % 3 ? 1 : null, range(3)) as k from system.numbers_mt li
14081408
SELECT () as empty_tuple;
14091409
SELECT ((), 1, 'test');
14101410
SELECT * FROM t WHERE x = ();
1411+
SELECT json, JSONAllPathsWithTypes(json) FROM test;
1412+
SELECT JSONExtract('{"a": "hello", "b": [-100, 200.0, 300]}', 'Tuple(String, Array(Float64))') AS res;
1413+
SELECT JSONHas('{"a": "hello", "b": [-100, 200.0, 300]}', -2);
1414+
SELECT JSON_QUERY('{"array":[[0, 1, 2, 3, 4, 5], [0, -1, -2, -3, -4, -5]]}', '$.array[*][0 to 2, 4]');
1415+
SELECT JSON_VALUE('{"hello":"world"}', '$.b') settings function_json_value_return_type_allow_nullable=true;
1416+
SELECT toJSONString(tuple(1.25, NULL, NaN, +inf, -inf, [])) SETTINGS output_format_json_quote_denormals = 1;
14111417
SELECT ifNull(x, ()) FROM t;

src/test/resources/org/pgcodekeeper/core/it/parser/ch/ch_select_refs.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1326,4 +1326,10 @@ Reference: action = SELECT, line number = 1407, charPositionInLine = 0
13261326
Reference: action = SELECT, line number = 1408, charPositionInLine = 0
13271327
Reference: action = SELECT, line number = 1409, charPositionInLine = 0
13281328
Reference: action = SELECT, line number = 1410, charPositionInLine = 0
1329-
Reference: action = SELECT, line number = 1411, charPositionInLine = 0
1329+
Reference: action = SELECT, line number = 1411, charPositionInLine = 0
1330+
Reference: action = SELECT, line number = 1412, charPositionInLine = 0
1331+
Reference: action = SELECT, line number = 1413, charPositionInLine = 0
1332+
Reference: action = SELECT, line number = 1414, charPositionInLine = 0
1333+
Reference: action = SELECT, line number = 1415, charPositionInLine = 0
1334+
Reference: action = SELECT, line number = 1416, charPositionInLine = 0
1335+
Reference: action = SELECT, line number = 1417, charPositionInLine = 0

src/test/resources/org/pgcodekeeper/core/it/parser/ch/ch_table.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,10 @@ CREATE TABLE test (json JSON) ENGINE = Memory;
994994
CREATE TABLE test (json JSON(a.b UInt32)) ENGINE = Memory;
995995
CREATE TABLE test (json JSON(a.b UInt32, SKIP a.e)) ENGINE = Memory;
996996
CREATE TABLE test (id UInt64, json JSON(max_dynamic_paths=3)) ENGINE=Memory ORDER BY id;
997+
CREATE TABLE test (json JSON(max_dynamic_paths=1)) ENGINE = Memory;
998+
CREATE TABLE test (json JSON(max_dynamic_types=8)) ENGINE = Memory;
999+
CREATE TABLE test (json JSON(SKIP REGEXP 'path.to.skip.*')) ENGINE = Memory;
1000+
CREATE TABLE test (json JSON(max_dynamic_paths=10, max_dynamic_types=8, a.b UInt32, a.b.c String, SKIP a.e, SKIP REGEXP 'paths.*')) ENGINE = Memory;
9971001
CREATE TABLE test (id UInt32, vec QBit(Float32, 8)) ENGINE = Memory;
9981002

9991003
--Buffer

0 commit comments

Comments
 (0)