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

Native encoder and buffer helpers #181

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .github/workflows/bench.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
- run: mix deps.get --only $MIX_ENV
- run: mix compile --warnings-as-errors
- run: mkdir results
- run: mix run bench/encode.exs | tee results/encode.txt
- run: mix run bench/insert.exs | tee results/insert.txt
- run: mix run bench/stream.exs | tee results/stream.txt
- uses: actions/upload-artifact@v4
Expand Down
35 changes: 35 additions & 0 deletions bench/encode.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
types = [Ch.Types.u64(), Ch.Types.string(), Ch.Types.array(Ch.Types.u8()), Ch.Types.datetime()]
encoding_types = Ch.RowBinary.encoding_types(types)

rows = fn count ->
Enum.map(1..count, fn i ->
[i, "Golang SQL database driver", [1, 2, 3, 4, 5, 6, 7, 8, 9], NaiveDateTime.utc_now()]
end)
end

alias Ch.{RowBinary, Native}

defmodule NativeBuffer do
def encode_row(row, buffer) do
Native.add_row(buffer, row)
end

def encode_rows(rows, types) do
rows
|> Enum.reduce(Native.new_buffer(types), &__MODULE__.encode_row/2)
|> Native.to_iodata()
end
end

Benchee.run(
%{
"control" => fn rows -> Enum.each(rows, fn _row -> [] end) end,
"RowBinary.encode_rows/2" => fn rows -> RowBinary.encode_rows(rows, types) end,
"RowBinary._encode_rows/2" => fn rows -> RowBinary._encode_rows(rows, encoding_types) end,
"Native.add_row/2" => fn rows -> NativeBuffer.encode_rows(rows, types) end
},
inputs: %{
# TODO more inputs (take some from Plausible write buffer)
"1_000_000 rows" => rows.(1_000_000)
}
)
27 changes: 27 additions & 0 deletions lib/ch/native.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule Ch.Native do
@moduledoc """
Helpers for working with ClickHouse [`Native`](https://clickhouse.com/docs/en/sql-reference/formats#native) format.
"""

@opaque buffer :: list()

@spec new_buffer([String.t()]) :: buffer
def new_buffer(types) do
encoding_types = Ch.RowBinary.encoding_types(types)
Enum.map(encoding_types, fn t -> [t | _column = []] end)
end

@spec add_row(buffer, [term]) :: buffer
def add_row(buffer, row)

def add_row([[type | column] | buffer_rest], [value | row_rest]) do
[[type | [column | Ch.RowBinary.encode(type, value)]] | add_row(buffer_rest, row_rest)]
end

def add_row([] = done, []), do: done

@spec to_iodata(buffer) :: iodata
def to_iodata(buffer) do
Enum.map(buffer, fn [_type | column] -> column end)
end
end
Loading