Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 191 additions & 0 deletions lib/ch/row_binary.ex
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ defmodule Ch.RowBinary do
:ipv4,
:ipv6,
:point,
:json,
:nothing
],
do: t
Expand Down Expand Up @@ -754,6 +755,193 @@ defmodule Ch.RowBinary do
end
end

for {pattern, count} <- varints do
defp decode_json_decode_rows(
<<unquote(pattern), bin::bytes>>,
types_rest,
row,
rows,
types
) do
decode_json_continue(bin, unquote(count), [], types_rest, row, rows, types)
end
end

for {pattern, size} <- varints do
defp decode_json_continue(
<<unquote(pattern), name::size(unquote(size))-bytes, bin::bytes>>,
count,
acc,
types_rest,
row,
rows,
types
)
when count > 0 do
case bin do
# Int64
<<0x0A, i64::64-little-signed, rest::bytes>> ->
decode_json_continue(rest, count - 1, [{name, i64} | acc], types_rest, row, rows, types)

# Float64
<<0x0E, f64::64-little-float, rest::bytes>> ->
decode_json_continue(rest, count - 1, [{name, f64} | acc], types_rest, row, rows, types)

# Boolean
<<0x2D, b, rest::bytes>> ->
b =
case b do
0 -> false
1 -> true
end

decode_json_continue(rest, count - 1, [{name, b} | acc], types_rest, row, rows, types)

# String
<<0x15, rest::bytes>> ->
decode_json_string(rest, name, count, acc, types_rest, row, rows, types)

# Array of nullable strings
<<0x1E, 0x23, 0x15, rest::bytes>> ->
decode_json_nullable_array(
rest,
:string,
name,
count,
acc,
types_rest,
row,
rows,
types
)

_ ->
raise "oops, what is " <>
Enum.join(for(<<b <- bin>>, do: "0x" <> Integer.to_string(b, 16)), " ")
end
end
end

defp decode_json_continue(bin, _count = 0, acc, types_rest, row, rows, types) do
decode_rows(types_rest, bin, [Map.new(acc) | row], rows, types)
end

for {pattern, size} <- varints do
defp decode_json_string(
<<unquote(pattern), s::size(unquote(size))-bytes, rest::bytes>>,
name,
count,
acc,
types_rest,
row,
rows,
types
) do
decode_json_continue(rest, count - 1, [{name, s} | acc], types_rest, row, rows, types)
end
end

for {pattern, array_count} <- varints do
defp decode_json_nullable_array(
<<unquote(pattern), rest::bytes>>,
type,
name,
json_count,
json_acc,
types_rest,
row,
rows,
types
) do
case type do
:string ->
decode_json_nullable_string_array(
rest,
unquote(array_count),
[],
name,
json_count,
json_acc,
types_rest,
row,
rows,
types
)
end
end
end

defp decode_json_nullable_string_array(
<<1, rest::bytes>>,
array_count,
array_acc,
name,
json_count,
json_acc,
types_rest,
row,
rows,
types
) do
decode_json_nullable_string_array(
rest,
array_count - 1,
[nil | array_acc],
name,
json_count,
json_acc,
types_rest,
row,
rows,
types
)
end

for {pattern, size} <- varints do
defp decode_json_nullable_string_array(
<<0, unquote(pattern), s::size(unquote(size))-bytes, rest::bytes>>,
array_count,
array_acc,
name,
json_count,
json_acc,
types_rest,
row,
rows,
types
)
when array_count > 0 do
decode_json_nullable_string_array(
rest,
array_count - 1,
[s | array_acc],
name,
json_count,
json_acc,
types_rest,
row,
rows,
types
)
end
end

defp decode_json_nullable_string_array(
<<rest::bytes>>,
0,
array_acc,
name,
json_count,
json_acc,
types_rest,
row,
rows,
types
) do
acc = [{name, :lists.reverse(array_acc)} | json_acc]
decode_json_continue(rest, json_count - 1, acc, types_rest, row, rows, types)
end

@compile inline: [decode_array_decode_rows: 6]
defp decode_array_decode_rows(<<0, bin::bytes>>, _type, types_rest, row, rows, types) do
decode_rows(types_rest, bin, [[] | row], rows, types)
Expand Down Expand Up @@ -884,6 +1072,9 @@ defmodule Ch.RowBinary do
:binary ->
decode_binary_decode_rows(bin, types_rest, row, rows, types)

:json ->
decode_json_decode_rows(bin, types_rest, row, rows, types)

# TODO utf8?
{:fixed_string, size} ->
<<s::size(size)-bytes, bin::bytes>> = bin
Expand Down
2 changes: 2 additions & 0 deletions lib/ch/types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ defmodule Ch.Types do
{"Time", :time, []},
{"Date32", :date32, []},
{"Date", :date, []},
{"JSON", :json, []},
{"LowCardinality", :low_cardinality, [:type]},
for size <- [32, 64, 128, 256] do
{"Decimal#{size}", :"decimal#{size}", [:int]}
Expand Down Expand Up @@ -324,6 +325,7 @@ defmodule Ch.Types do
end

def decode("DateTime"), do: :datetime
def decode("JSON" <> _), do: :json

def decode(type) do
try do
Expand Down
73 changes: 73 additions & 0 deletions test/ch/json_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
defmodule Ch.JSONTest do
use ExUnit.Case, async: true

@moduletag :json

@table "json_test"

setup do
conn = start_supervised!({Ch, database: Ch.Test.database(), settings: [enable_json_type: 1]})

on_exit(fn ->
Ch.Test.query("DROP TABLE IF EXISTS #{@table}", [], database: Ch.Test.database())
end)

{:ok, conn: conn}
end

test "simple json", %{conn: conn} do
assert Ch.query!(conn, ~s|select '{"a":"b","c":"d"}'::json|).rows == [
[%{"a" => "b", "c" => "d"}]
]

assert Ch.query!(conn, ~s|select '{"a":42}'::json|).rows == [[%{"a" => 42}]]
assert Ch.query!(conn, ~s|select '{}'::json|).rows == [[%{}]]
assert Ch.query!(conn, ~s|select '{"a":null}'::json|).rows == [[%{}]]
assert Ch.query!(conn, ~s|select '{"a":3.14}'::json|).rows == [[%{"a" => 3.14}]]
assert Ch.query!(conn, ~s|select '{"a":true}'::json|).rows == [[%{"a" => true}]]
assert Ch.query!(conn, ~s|select '{"a":false}'::json|).rows == [[%{"a" => false}]]

assert Ch.query!(conn, ~s|select '{"a":{"b":"c"}}'::json|).rows == [
[%{"a.b" => "c"}]
]

assert Ch.query!(conn, ~s|select '{"a":[]}'::json|).rows == [
[%{"a" => []}]
]

assert Ch.query!(conn, ~s|select '{"a":[null]}'::json|).rows == [
[%{"a" => [nil]}]
]

assert Ch.query!(conn, ~s|select '{"a":[1,3.14,"hello",null]}'::json|).rows == [
[%{"a" => ["1", "3.14", "hello", nil]}]
]

# # <<31, 4, 35, 10, 35, 14, 35, 21, 48, 0, 128, 2, 16, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 10, 215, 163, 112, 61, 10, 1, 64, 0, 1, 115, 1, 1, 97, 21, 1, 98>>
# assert Ch.query!(conn, ~s|select '{"a":[1,2.13,"s",{"a":"b"}]}'::json|).rows == [
# [%{"a.b" => 42}]
# ]
end

# TODO
@tag :skip
test "creating json", %{conn: conn} do
Ch.query!(conn, "CREATE TABLE #{@table} (json JSON) ENGINE = Memory")

Ch.query!(conn, """
INSERT INTO #{@table} VALUES
('{"a" : {"b" : 42}, "c" : [1, 2, 3]}'),
('{"f" : "Hello, World!"}'),
('{"a" : {"b" : 43, "e" : 10}, "c" : [4, 5, 6]}')
""")

assert Ch.query!(
conn,
"SELECT json FROM #{@table}"
).rows == [
[%{"a" => %{"b" => "42"}, "c" => ["1", "2", "3"]}],
[%{"f" => "Hello, World!"}],
[%{"a" => %{"b" => "43", "e" => "10"}, "c" => ["4", "5", "6"]}]
]
end
end
4 changes: 2 additions & 2 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ extra_exclude =
if ch_version >= "25" do
[]
else
# Time type is not supported in ClickHouse < 25
[:time]
# Time and JSON types are not supported in ClickHouse < 25
[:time, :json]
end

ExUnit.start(exclude: [:slow | extra_exclude])