Skip to content
Merged
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
62 changes: 61 additions & 1 deletion lib/req_ch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,67 @@ defmodule ReqCH do
end

defp prepare_params(params) do
Enum.map(params, fn {key, value} -> {"param_#{key}", value} end)
Enum.map(params, fn {key, value} -> {"param_#{key}", prepare_param_value(value)} end)
end

defp prepare_param_value(text) when is_binary(text) do
escapes = [{"\\", "\\\\"}, {"\t", "\\\t"}, {"\n", "\\\n"}]

Enum.reduce(escapes, text, fn {pattern, replacement}, text ->
String.replace(text, pattern, replacement)
end)
end

defp prepare_param_value(%DateTime{} = datetime) do
unix_microseconds =
datetime
|> DateTime.shift_zone!("Etc/UTC")
|> DateTime.to_unix(:microsecond)

unix_seconds = unix_microseconds / 1_000_000
unix_seconds_trunc = trunc(unix_seconds)

if unix_seconds_trunc == unix_seconds do
unix_seconds_trunc
else
:erlang.float_to_binary(unix_seconds, decimals: 6)
end
end

defp prepare_param_value(array) when is_list(array) do
elements = Enum.map(array, &prepare_array_param_value/1)
IO.iodata_to_binary([?[, Enum.intersperse(elements, ?,), ?]])
end

defp prepare_param_value(tuple) when is_tuple(tuple) do
elements = Enum.map(Tuple.to_list(tuple), &prepare_array_param_value/1)
IO.iodata_to_binary([?(, Enum.intersperse(elements, ?,), ?)])
end

defp prepare_param_value(struct) when is_struct(struct), do: to_string(struct)

defp prepare_param_value(map) when is_map(map) do
elements = Enum.map(Map.to_list(map), &prepare_map_param_value/1)
IO.iodata_to_binary([?{, Enum.intersperse(elements, ?,), ?}])
end

defp prepare_param_value(other), do: to_string(other)

defp prepare_array_param_value(text) when is_binary(text) do
text = prepare_param_value(text)
[?', String.replace(text, "'", "''"), ?']
end

defp prepare_array_param_value(%s{} = param) when s in [Date, NaiveDateTime] do
[?', to_string(param), ?']
end

defp prepare_array_param_value(other), do: prepare_param_value(other)

defp prepare_map_param_value({key, value}) do
key = prepare_array_param_value(key)
value = prepare_array_param_value(value)
[key, ?:, value]
end

@valid_formats [:tsv, :csv, :json, :explorer]
Expand Down
158 changes: 158 additions & 0 deletions test/req_ch_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,164 @@ defmodule ReqCHTest do
"""
end

test "a query with Date(Time) params" do
utc_now = ~U[2024-12-20 05:44:53.855679Z]
utc_now_seconds = DateTime.truncate(utc_now, :second)
utc_today = DateTime.to_date(utc_now)

params = [
utc_now: utc_now,
utc_now_seconds: utc_now_seconds,
utc_today: utc_today
]

response =
ReqCH.query!(
ReqCH.new(),
"""
SELECT
{utc_now:DateTime64(6)},
{utc_now_seconds:DateTime},
{utc_today:Date}
FORMAT
JSONCompact
""",
params
)

assert response.status == 200

assert response.body["data"] == [
[
"2024-12-20 05:44:53.855679",
"2024-12-20 05:44:53",
"2024-12-20"
]
]
end

# https://clickhouse.com/docs/en/interfaces/http#tabs-in-url-parameters
# https://clickhouse.com/docs/en/interfaces/formats#tabseparated-data-formatting
test "a query with tabs and newlines" do
params = [
tab: "a\tb",
newline: "c\nd",
both: "a\tb\nc\t\nd"
]

response =
ReqCH.query!(
ReqCH.new(),
"SELECT {tab:String}, {newline:String}, {both:String} FORMAT JSONCompact",
params
)

assert response.status == 200
assert response.body["data"] == [["a\tb", "c\nd", "a\tb\nc\t\nd"]]
end

test "a query with arrays" do
params = [
array: ["a", "b", "c", "a\tb\nc\t\nd"],
empty_array: [],
nested_array: [["a", "b"], ["c", "d"], ["a\tb\nc\t\nd"]],
date_array: [~D[2024-12-20], ~D[2024-12-21]]
]

response =
ReqCH.query!(
ReqCH.new(),
"""
SELECT
{array:Array(String)},
{empty_array:Array(String)},
{nested_array:Array(Array(String))},
{date_array:Array(Date)}
FORMAT
JSONCompact
""",
params
)

assert response.status == 200

assert response.body["data"] == [
[
["a", "b", "c", "a\tb\nc\t\nd"],
[],
[["a", "b"], ["c", "d"], ["a\tb\nc\t\nd"]],
["2024-12-20", "2024-12-21"]
]
]
end

test "a query with tuples" do
params = [
tuple: {1, "a", ~D[2024-12-20]},
empty_tuple: {},
nested_tuple: {{1, "a"}, {2, "b"}},
date_tuple: {~D[2024-12-20], ~D[2024-12-21]}
]

response =
ReqCH.query!(
ReqCH.new(),
"""
SELECT
{tuple:Tuple(UInt8, String, Date)},
{empty_tuple:Tuple()},
{nested_tuple:Tuple(Tuple(UInt8, String), Tuple(UInt8, String))},
{date_tuple:Tuple(Date, Date)}
""",
params
)

assert response.status == 200

assert response.body ==
"""
(1,'a','2024-12-20')\t\
()\t\
((1,'a'),(2,'b'))\t\
('2024-12-20','2024-12-21')
"""
end

test "a query with maps" do
params = [
map: %{"a" => 1, "b" => 2},
empty_map: %{},
nested_map: %{"a" => %{"b" => 1}, "c" => %{"d" => 2}},
date_map: %{"a" => ~D[2024-12-20], "b" => ~D[2024-12-21]}
]

response =
ReqCH.query!(
ReqCH.new(),
"""
SELECT
{map:Map(String, UInt8)},
{empty_map:Map(String, UInt8)},
{nested_map:Map(String, Map(String, UInt8))},
{date_map:Map(String, Date)}
FORMAT
JSONCompact
""",
params
)

assert response.status == 200

assert response.body["data"] == [
[
%{"a" => 1, "b" => 2},
%{},
%{"a" => %{"b" => 1}, "c" => %{"d" => 2}},
%{"a" => "2024-12-20", "b" => "2024-12-21"}
]
]
end

test "a query with unknown database" do
assert {:ok, %Req.Response{} = response} =
ReqCH.query(
Expand Down
Loading