Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse named tuples #197

Merged
merged 4 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
47 changes: 42 additions & 5 deletions lib/ch/types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule Ch.Types do
{"Float#{size}", :"f#{size}", []}
end,
{"Array", :array, [:type]},
{"Tuple", :tuple, [:type]},
{"Tuple", :tuple, [:maybe_named_column]},
{"Map", :map, [:type]},
{"FixedString", :fixed_string, [:int]},
{"Nullable", :nullable, [:type]},
Expand Down Expand Up @@ -392,6 +392,31 @@ defmodule Ch.Types do
decode_identifier(rest, 0, rest, stack, acc)
end

for {encoded, decoded, [_ | _] = args} <- types do
defp decode([:maybe_named_column | stack], unquote(encoded) <> rest, acc) do
[:close, {:tuple, [:maybe_named_column]} | stack] = stack

decode(
[:open | unquote(args)] ++
[:close, {unquote(decoded), unquote(args)}, acc, :close, {:tuple, [:type]} | stack],
rest,
[]
)
end
end

for {encoded, decoded, []} <- types do
defp decode([:maybe_named_column | stack], unquote(encoded) <> rest, acc) do
[:close, {:tuple, [:maybe_named_column]} | stack] = stack
decode([:close, {:tuple, [:type]} | stack], rest, [unquote(decoded) | acc])
end
end

defp decode([:maybe_named_column | stack], <<rest::bytes>>, acc) do
[:close, {:tuple, [:maybe_named_column]} | stack] = stack
decode([:identifier, :type, :close, {:tuple, [:identifier, :type]} | stack], rest, acc)
end

defp decode([:eq | stack], <<?=, rest::bytes>>, acc) do
decode(stack, rest, acc)
end
Expand All @@ -402,7 +427,7 @@ defmodule Ch.Types do
defp close([_ | stack]), do: close(stack)

defp build_type(:array = a, [t]), do: {a, t}
defp build_type(:tuple = t, ts), do: {t, :lists.reverse(ts)}
defp build_type(:tuple = t, ts), do: {t, build_tuple(ts)}
defp build_type(:fixed_string = fs, [n]), do: {fs, n}
defp build_type(:datetime = d, [tz]), do: {d, tz}
defp build_type(:datetime64 = d, [precision]), do: {d, precision}
Expand All @@ -423,6 +448,18 @@ defmodule Ch.Types do
mapping |> :lists.reverse() |> Enum.chunk_every(2) |> Enum.map(fn [k, v] -> {k, v} end)
end

defp build_tuple([type, name | rest]) when is_binary(name) do
named_columns_to_types(rest, [type])
end

defp build_tuple(types), do: :lists.reverse(types)

defp named_columns_to_types([type, _name | rest], acc) do
named_columns_to_types(rest, [type | acc])
end

defp named_columns_to_types([], acc), do: acc

# TODO '', \'

defp decode_string(<<?', rest::bytes>>, len, original, stack, acc) do
Expand All @@ -441,8 +478,10 @@ defmodule Ch.Types do
defp utf8_size(codepoint) when codepoint <= 0x10FFFF, do: 4

defguardp is_alpha(a) when (a >= ?a and a <= ?z) or (a >= ?A and a <= ?Z)
defguardp is_numeric(char) when char >= ?0 and char <= ?9

defp decode_identifier(<<a, rest::bytes>>, len, original, stack, acc) when is_alpha(a) do
defp decode_identifier(<<a, rest::bytes>>, len, original, stack, acc)
when is_alpha(a) or is_numeric(a) do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming identifiers are alphanumeric for now. But it seems like ClickHouse allows more than that. These work:

select tuple(1 as a$, 2 as b);
select tuple(1 as a_, 2 as b);

I'll try to find out what is allowed in identifiers and update it later.

decode_identifier(rest, len + 1, original, stack, acc)
end

Expand All @@ -451,8 +490,6 @@ defmodule Ch.Types do
decode(stack, rest, [:binary.copy(part) | acc])
end

defguardp is_numeric(char) when char >= ?0 and char <= ?9

defp decode_int(<<?-, i, rest::bytes>>, stack, outer_acc) when is_numeric(i) do
decode_int_cont(rest, -(i - ?0), stack, outer_acc)
end
Expand Down
1 change: 1 addition & 0 deletions test/ch/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ defmodule Ch.ConnectionTest do
}} = Ch.query(conn, "SELECT * FROM t_uuid ORDER BY y")
end

@tag :skip
Copy link
Contributor Author

@ruslandoga ruslandoga Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Latest ClickHouse now has "new JSON" https://clickhouse.com/docs/en/sql-reference/data-types/newjson, I'll add support for it later.

For now the test for "old JSON" is skipped to prevent CI errors.

test "json", %{conn: conn} do
settings = [allow_experimental_object_type: 1]

Expand Down
5 changes: 5 additions & 0 deletions test/ch/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ defmodule Ch.QueryTest do
assert [[[[0]]]] = Ch.query!(conn, "SELECT [[0]]").rows
end

test "decode tuples", %{conn: conn} do
assert [[{"Hello", 123}]] = Ch.query!(conn, "select ('Hello', 123)").rows
assert [[{"Hello", 123}]] = Ch.query!(conn, "select ('Hello' as a, 123 as b)").rows
end

test "decode network types", %{conn: conn} do
assert [[{127, 0, 0, 1} = ipv4]] = Ch.query!(conn, "SELECT '127.0.0.1'::inet4").rows
assert :inet.ntoa(ipv4) == ~c"127.0.0.1"
Expand Down
17 changes: 17 additions & 0 deletions test/ch/types_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ defmodule Ch.TypesTest do
]}
end

test "named tuple" do
assert decode("Tuple(a String, b UInt8)") == {:tuple, [:string, :u8]}
assert decode(" Tuple ( a String , b UInt8 ) ") == {:tuple, [:string, :u8]}

assert decode(
" Tuple ( a Array( String ) , t Tuple ( a String , b UInt64 ), d DateTime, d64 DateTime64(3), f FixedString(3) ) "
) ==
{:tuple,
[
{:array, :string},
{:tuple, [:string, :u64]},
:datetime,
{:datetime64, 3},
{:fixed_string, 3}
]}
end

test "datetime" do
assert decode("DateTime") == :datetime
assert decode("DateTime('UTC')") == {:datetime, "UTC"}
Expand Down