@@ -568,20 +568,86 @@ defmodule Ch.ConnectionTest do
568
568
} } = Ch . query ( conn , "SELECT * FROM t_uuid ORDER BY y" )
569
569
end
570
570
571
+ # TODO non utf8
572
+ test "read json as string" , % { conn: conn } do
573
+ assert Ch . query! ( conn , ~s| select '{"a":42}'::JSON| , [ ] ,
574
+ settings: [
575
+ enable_json_type: 1 ,
576
+ output_format_binary_write_json_as_string: 1
577
+ ]
578
+ ) . rows == [ [ % { "a" => "42" } ] ]
579
+ end
580
+
581
+ test "write->read json as string" , % { conn: conn } do
582
+ Ch . query! ( conn , "CREATE TABLE test_write_json(json JSON) ENGINE = Memory" , [ ] ,
583
+ settings: [
584
+ enable_json_type: 1
585
+ ]
586
+ )
587
+
588
+ rowbinary =
589
+ Ch.RowBinary . encode_rows (
590
+ [
591
+ [ Jason . encode_to_iodata! ( % { "a" => 42 } ) ] ,
592
+ [ Jason . encode_to_iodata! ( % { "b" => 10 } ) ]
593
+ ] ,
594
+ _types = [ :string ]
595
+ )
596
+
597
+ Ch . query! ( conn , [ "insert into test_write_json(json) format RowBinary\n " | rowbinary ] , [ ] ,
598
+ settings: [
599
+ enable_json_type: 1 ,
600
+ input_format_binary_read_json_as_string: 1
601
+ ]
602
+ )
603
+
604
+ assert Ch . query! ( conn , "select json from test_write_json" , [ ] ,
605
+ settings: [
606
+ enable_json_type: 1 ,
607
+ output_format_binary_write_json_as_string: 1
608
+ ]
609
+ ) . rows ==
610
+ [ [ % { "a" => "42" } ] , [ % { "b" => "10" } ] ]
611
+ end
612
+
613
+ # https://clickhouse.com/docs/en/sql-reference/data-types/newjson
614
+ # https://clickhouse.com/docs/en/integrations/data-formats/json/overview
615
+ # https://clickhouse.com/blog/a-new-powerful-json-data-type-for-clickhouse
616
+ # https://clickhouse.com/blog/json-bench-clickhouse-vs-mongodb-elasticsearch-duckdb-postgresql
617
+ # https://github.com/ClickHouse/ClickHouse/pull/70288
618
+ # https://github.com/ClickHouse/ClickHouse/blob/master/src/Core/TypeId.h
571
619
@ tag :skip
572
620
test "json" , % { conn: conn } do
573
- settings = [ allow_experimental_object_type : 1 ]
621
+ settings = [ enable_json_type : 1 ]
574
622
575
- Ch . query! ( conn , "CREATE TABLE json(o JSON) ENGINE = Memory" , [ ] , settings: settings )
623
+ assert Ch . query! (
624
+ conn ,
625
+ ~s| select '{"a":42,"b":10}'::JSON| ,
626
+ [ ] ,
627
+ settings: settings ,
628
+ decode: false ,
629
+ format: "RowBinary"
630
+ ) . rows == [
631
+ << 2 , 1 , 97 , 10 , 42 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 98 , 10 , 10 , 0 , 0 , 0 , 0 , 0 , 0 , 0 >>
632
+ ]
576
633
577
- Ch . query! ( conn , ~s | INSERT INTO json VALUES ('{"a": 1, "b": { "c": 2, "d": [1, 2, 3] }}') | )
634
+ # Ch.query!(conn, "CREATE TABLE test_json( json JSON) ENGINE = Memory", [], settings: settings )
578
635
579
- assert Ch . query! ( conn , "SELECT o.a, o.b.c, o.b.d[3] FROM json" ) . rows == [ [ 1 , 2 , 3 ] ]
636
+ # Ch.query!(
637
+ # conn,
638
+ # ~s|INSERT INTO test_json VALUES ('{"a" : {"b" : 42}, "c" : [1, 2, 3]}'), ('{"f" : "Hello, World!"}'), ('{"a" : {"b" : 43, "e" : 10}, "c" : [4, 5, 6]}')|
639
+ # )
580
640
581
- # named tuples are not supported yet
582
- assert_raise ArgumentError , fn -> Ch . query! ( conn , "SELECT o FROM json" ) end
641
+ # assert Ch.query!(conn, "SELECT json FROM test_json") == :asdf
642
+
643
+ # assert Ch.query!(conn, "SELECT json.a, json.b.c, json.b.d[3] FROM test_json").rows == [
644
+ # [1, 2, 3]
645
+ # ]
583
646
end
584
647
648
+ # TODO variant (is there?)
649
+ # TODO dynamic
650
+
585
651
# TODO enum16
586
652
587
653
test "enum8" , % { conn: conn } do
0 commit comments