Skip to content

Commit 29941c6

Browse files
committed
eh?
1 parent 1874e71 commit 29941c6

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

lib/ch/row_binary.ex

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,20 @@ defmodule Ch.RowBinary do
698698
defp utf8_size(codepoint) when codepoint <= 0xFFFF, do: 3
699699
defp utf8_size(codepoint) when codepoint <= 0x10FFFF, do: 4
700700

701+
@compile inline: [decode_json_decode_rows: 5]
702+
703+
for {pattern, size} <- varints do
704+
defp decode_json_decode_rows(
705+
<<unquote(pattern), s::size(unquote(size))-bytes, bin::bytes>>,
706+
types_rest,
707+
row,
708+
rows,
709+
types
710+
) do
711+
decode_rows(types_rest, bin, [:json.decode(s) | row], rows, types)
712+
end
713+
end
714+
701715
@compile inline: [decode_binary_decode_rows: 5]
702716

703717
for {pattern, size} <- varints do
@@ -867,7 +881,7 @@ defmodule Ch.RowBinary do
867881
decode_rows(types_rest, bin, [Date.add(@epoch_date, d) | row], rows, types)
868882

869883
:json ->
870-
raise ArgumentError, "JSON type is not supported for decoding: #{inspect(bin)}"
884+
decode_json_decode_rows(bin, types_rest, row, rows, types)
871885

872886
{:datetime, timezone} ->
873887
<<s::32-little, bin::bytes>> = bin

test/ch/connection_test.exs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,12 +568,55 @@ defmodule Ch.ConnectionTest do
568568
}} = Ch.query(conn, "SELECT * FROM t_uuid ORDER BY y")
569569
end
570570

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+
[:json.encode(%{"a" => 42})],
592+
[:json.encode(%{"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+
571613
# https://clickhouse.com/docs/en/sql-reference/data-types/newjson
572614
# https://clickhouse.com/docs/en/integrations/data-formats/json/overview
573615
# https://clickhouse.com/blog/a-new-powerful-json-data-type-for-clickhouse
574616
# https://clickhouse.com/blog/json-bench-clickhouse-vs-mongodb-elasticsearch-duckdb-postgresql
575617
# https://github.com/ClickHouse/ClickHouse/pull/70288
576618
# https://github.com/ClickHouse/ClickHouse/blob/master/src/Core/TypeId.h
619+
@tag :skip
577620
test "json", %{conn: conn} do
578621
settings = [enable_json_type: 1]
579622

0 commit comments

Comments
 (0)