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

Add Ecto support based on ClickhouseEcto #69

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

Zarathustra2
Copy link
Contributor

This is based on ClickhouseEcto.

Currently parameters are not being supported that needs to be added as well.

Also needs some tests...

Example:

defmodule Repo do
  use Ecto.Repo,
  adapter: Pillar.Ecto,
  loggers: [Ecto.LogEntry],
  hostname: "localhost",
  port: 8123,
  timeout: 60_000,
  pool_timeout: 60_000,
  ownership_timeout: 60_000,
  pool_size: 30,
  otp_app: :pillar
end


defmodule Trade do
  use Ecto.Schema

  @primary_key false
  schema "trades" do
    field :ticker, :string
    field :size, :integer
  end
end

import Ecto.Query

Repo.start_link()

q = from(t in Trade, select: %{ticker: t.ticker, vol: sum(t.size)}, group_by: t.ticker)
IO.inspect(Ecto.Adapters.SQL.to_sql(:all, Repo, q))

Repo.all(q) |> IO.inspect()

use DBConnection

def connect(_) do
conn = Pillar.Connection.new("http://localhost:8123")
Copy link
Member

Choose a reason for hiding this comment

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

For all Ecto requests will be used 1 connection?
Is pool needed here? Like in BulkInsertBuffer module

lib/pillar/ecto.ex Outdated Show resolved Hide resolved
lib/pillar.ex Outdated Show resolved Hide resolved
@@ -0,0 +1,371 @@
defmodule Pillar.Ecto.QueryBuilder do
Copy link
Member

Choose a reason for hiding this comment

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

At all it looks great for me, thanks for this improvement! ❤️

I would like to see some tests, for example as you provided in description.

Could you add

  1. insert test
  2. select test
  3. join test

and add into 'readme.md' information, that there is experimental support of Ecto.Schema's

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I also want to add some more macros for custom clickhouse functions

@Zarathustra2
Copy link
Contributor Author

OMG @sofakingworld I got a first version working which also supports parameterised query - so no fear for SQL injection!

Parameterised like this: https://clickhouse.com/docs/en/interfaces/http#cli-queries-with-parameters

@Zarathustra2
Copy link
Contributor Author

DateTime mapping back to ecto model does not work yet, looking into it

@Zarathustra2
Copy link
Contributor Author

DateTime works now!

end)
end)
|> List.flatten()
|> Enum.reverse()
Copy link

@ruslandoga ruslandoga Apr 14, 2023

Choose a reason for hiding this comment

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

👋 @Zarathustra2

I wonder if you considered using execute/5 callback to build the query instead of prepare/5 to avoid reconstructing params? That's how plausible/chto ended up working.

I would be interested to see where this approach leads though :) I tried it only briefly and gave up before making it work...

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 did not, I didn't even know plausible/chto existed :O

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gonna try chto now, maybe I can drop/close my work here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ruslandoga was chto always public or has it been made public recently? I am wondering how I missed it

Copy link

@ruslandoga ruslandoga Apr 14, 2023

Choose a reason for hiding this comment

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

It's always been public in https://github.com/ruslandoga/chto and moved under Plausible org recently. However "always" in this case is just a few months.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

damn, how did it never come up for me, arg.

Just tested it briefly and seems to work with out issues so far for me. (I am currently struggling with some fragments implementations in this PR).

I am not sure how my work would compare to your library, feels like your stuff is way more reliable & major than what I have done so far. (I have never worked with Ecto under the hood prior to this PR)

Choose a reason for hiding this comment

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

way more reliable & major

The only known user so far is Plausible. So I'm not sure if it's indeed reliable for use cases outside of https://github.com/plausible/analytics ...

It would be great to have more users :)

Choose a reason for hiding this comment

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

It would be great to have more users :)

I'm willing to use it. It would be nice if we have a place to chat, maybe on ClickHouse's Slack. I've seen @Zarathustra2 there - where I've found this MR - but I'm not sure if you are there too, @ruslandoga.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants