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

Db clickhouse support #82

Merged
merged 2 commits into from
Oct 28, 2024
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
84 changes: 84 additions & 0 deletions lib/assets/connection_cell/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,83 @@ export function init(ctx, info) {
`,
};

const ClickhouseForm = {
name: "ClickhouseForm",

components: {
BaseInput: BaseInput,
BaseSwitch: BaseSwitch,
BaseSecret: BaseSecret,
BaseSelect: BaseSelect,
},

props: {
fields: {
type: Object,
default: {},
},
},

template: `
<div class="row">
<BaseInput
name="schema"
label="Schema"
type="text"
v-model="fields.schema"
inputClass="input"
:grow
/>
<BaseInput
name="hostname"
label="Hostname"
type="text"
v-model="fields.hostname"
inputClass="input"
:grow
:required
/>
<BaseInput
name="port"
label="Port"
type="number"
v-model="fields.port"
inputClass="input input--xs input--number"
:grow
:required
/>
</div>
<div class="row">
<BaseInput
name="database"
label="Database"
type="text"
v-model="fields.database"
inputClass="input"
:grow
/>
<BaseInput
name="username"
label="User name"
type="text"
v-model="fields.username"
inputClass="input"
:grow
/>
<BaseSecret
textInputName="password"
secretInputName="password_secret"
toggleInputName="use_password_secret"
label="Password"
v-model:textInputValue="fields.password"
v-model:secretInputValue="fields.password_secret"
v-model:toggleInputValue="fields.use_password_secret"
modalTitle="Set password"
/>
</div>
`,
};

const app = Vue.createApp({
components: {
BaseInput: BaseInput,
Expand All @@ -813,6 +890,7 @@ export function init(ctx, info) {
SQLServerForm: SQLServerForm,
BigQueryForm: BigQueryForm,
AthenaForm: AthenaForm,
ClickhouseForm: ClickhouseForm,
},

template: `
Expand Down Expand Up @@ -847,6 +925,7 @@ export function init(ctx, info) {
<SQLiteForm v-bind:fields="fields" v-if="isSQLite" />
<DuckDBForm v-bind:fields="fields" v-if="isDuckDB" />
<SnowflakeForm v-bind:fields="fields" v-if="isSnowflake" />
<ClickhouseForm v-bind:fields="fields" v-if="isClickhouse" />
<BigQueryForm v-bind:fields="fields" v-bind:helpBox="helpBox" v-if="isBigQuery" />
<AthenaForm v-bind:fields="fields" v-bind:helpBox="helpBox" v-bind:hasAwsCredentials="hasAwsCredentials" v-if="isAthena" />
<SQLServerForm v-bind:fields="fields" v-if="isSQLServer" />
Expand All @@ -870,6 +949,7 @@ export function init(ctx, info) {
{ label: "Google BigQuery", value: "bigquery" },
{ label: "AWS Athena", value: "athena" },
{ label: "Snowflake", value: "snowflake" },
{ label: "Clickhouse", value: "clickhouse" },
{ label: "SQL Server", value: "sqlserver" },
],
};
Expand All @@ -888,6 +968,10 @@ export function init(ctx, info) {
return this.fields.type === "snowflake";
},

isClickhouse() {
return this.fields.type === "clickhouse";
},

isBigQuery() {
return this.fields.type === "bigquery";
},
Expand Down
3 changes: 2 additions & 1 deletion lib/assets/sql_cell/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ export function init(ctx, payload) {
athena: "AWS Athena",
snowflake: "Snowflake",
sqlserver: "SQL Server",
duckdb: "DuckDB"
duckdb: "DuckDB",
clickhouse: "Clickhouse"
},
};
},
Expand Down
26 changes: 26 additions & 0 deletions lib/kino_db/connection_cell.ex
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ defmodule KinoDB.ConnectionCell do
else:
~w|database hostname port use_ipv6 username password use_ssl cacertfile instance|

"clickhouse" ->
~w|scheme username password_secret hostname port database|

type when type in ["postgres", "mysql"] ->
if fields["use_password_secret"],
do: ~w|database hostname port use_ipv6 use_ssl cacertfile username password_secret|,
Expand Down Expand Up @@ -189,6 +192,9 @@ defmodule KinoDB.ConnectionCell do
"sqlserver" ->
~w|hostname port|

"clickhouse" ->
~w|hostname port|

type when type in ["postgres", "mysql"] ->
~w|hostname port|
end
Expand Down Expand Up @@ -323,6 +329,14 @@ defmodule KinoDB.ConnectionCell do
end
end

defp to_quoted(%{"type" => "clickhouse"} = attrs) do
quote do
opts = unquote(trim_opts(shared_options(attrs) ++ clickhouse_options(attrs)))

{:ok, unquote(quoted_var(attrs["variable"]))} = Kino.start_child({Ch, opts})
end
end

defp quoted_access_key(%{"secret_access_key" => password}), do: password

defp quoted_access_key(%{"secret_access_key_secret" => ""}), do: ""
Expand Down Expand Up @@ -423,6 +437,12 @@ defmodule KinoDB.ConnectionCell do
end
end

defp clickhouse_options(attrs) do
[
scheme: attrs["scheme"] || "http"
]
end

defp quoted_var(string), do: {String.to_atom(string), [], nil}

defp quoted_pass(%{"password" => password}), do: password
Expand Down Expand Up @@ -497,6 +517,12 @@ defmodule KinoDB.ConnectionCell do
end
end

defp missing_dep(%{"type" => "clickhouse"}) do
unless Code.ensure_loaded?(Ch) do
~s|{:ch, "~> 0.2"}|
end
end

defp missing_dep(_ctx), do: nil

defp join_quoted(quoted_blocks) do
Expand Down
4 changes: 4 additions & 0 deletions lib/kino_db/sql_cell.ex
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ defmodule KinoDB.SQLCell do
to_quoted(attrs, quote(do: Tds), fn n -> "@#{n}" end)
end

defp to_quoted(%{"connection" => %{"type" => "clickhouse"}} = attrs) do
to_quoted(attrs, quote(do: Ch), fn n -> "{$#{n}:String}" end)
end

# Explorer-based
defp to_quoted(%{"connection" => %{"type" => "snowflake"}} = attrs) do
to_explorer_quoted(attrs, fn n -> "?#{n}" end)
Expand Down
14 changes: 14 additions & 0 deletions test/kino_db/connection_cell_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,19 @@ defmodule KinoDB.ConnectionCellTest do
{:ok, db} = Kino.start_child({Adbc.Database, driver: :snowflake, uri: uri})
{:ok, conn} = Kino.start_child({Adbc.Connection, database: db})\
'''

assert ConnectionCell.to_source(put_in(attrs["type"], "clickhouse")) == ~s'''
opts = [
hostname: "localhost",
port: 4444,
username: "admin",
password: "pass",
database: "default",
scheme: "http"
]

{:ok, conn} = Kino.start_child({Ch, opts})\
'''
end

test "generates empty source code when required fields are missing" do
Expand All @@ -216,6 +229,7 @@ defmodule KinoDB.ConnectionCellTest do
assert ConnectionCell.to_source(put_in(@empty_required_fields["type"], "bigquery")) == ""
assert ConnectionCell.to_source(put_in(@empty_required_fields["type"], "athena")) == ""
assert ConnectionCell.to_source(put_in(@empty_required_fields["type"], "snowflake")) == ""
assert ConnectionCell.to_source(put_in(@empty_required_fields["type"], "clickhouse")) == ""
end

test "generates empty source code when all conditional fields are missing" do
Expand Down
46 changes: 46 additions & 0 deletions test/kino_db/sql_cell_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ defmodule KinoDB.SQLCellTest do
result = Explorer.DataFrame.from_query!(conn, ~S"SELECT id FROM users", [])\
"""

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "clickhouse")) == """
result = Ch.query!(conn, ~S"SELECT id FROM users", [])\
"""

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "sqlserver")) == """
result = Tds.query!(conn, ~S"SELECT id FROM users", [])\
"""
Expand Down Expand Up @@ -206,6 +210,18 @@ defmodule KinoDB.SQLCellTest do
)\
'''

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "clickhouse")) == ~s'''
result =
Ch.query!(
conn,
~S"""
SELECT id FROM users
WHERE last_name = 'Sherlock'
""",
[]
)\
'''

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "sqlserver")) == ~s'''
result =
Tds.query!(
Expand Down Expand Up @@ -277,6 +293,15 @@ defmodule KinoDB.SQLCellTest do
)\
'''

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "clickhouse")) == ~s'''
result =
Ch.query!(
conn,
~S"SELECT id FROM users WHERE id {$1:String} AND name LIKE {$2:String}",
[user_id, search <> \"%\"]
)\
'''

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "sqlserver")) == ~s'''
result =
Tds.query!(conn, ~S"SELECT id FROM users WHERE id @1 AND name LIKE @2", [
Expand Down Expand Up @@ -375,6 +400,19 @@ defmodule KinoDB.SQLCellTest do
)\
'''

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "clickhouse")) == ~s'''
result =
Ch.query!(
conn,
~S"""
SELECT id from users
-- WHERE id = {{user_id1}}
/* WHERE id = {{user_id2}} */ WHERE id = {$1:String}
""",
[user_id3]
)\
'''

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "sqlserver")) == ~s'''
result =
Tds.query!(
Expand Down Expand Up @@ -422,6 +460,10 @@ defmodule KinoDB.SQLCellTest do
result = Explorer.DataFrame.from_query!(conn, ~S"SELECT id FROM users", [])\
"""

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "clickhouse")) == """
result = Ch.query!(conn, ~S"SELECT id FROM users", [])\
"""

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "sqlserver")) == """
result = Tds.query!(conn, ~S"SELECT id FROM users", [], timeout: 30000)\
"""
Expand Down Expand Up @@ -452,6 +494,10 @@ defmodule KinoDB.SQLCellTest do
result = DF.from_query!(conn, ~S"SELECT id FROM users", [])\
"""

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "clickhouse")) == """
result = Ch.query!(conn, ~S"SELECT id FROM users", [])\
"""

assert SQLCell.to_source(put_in(attrs["connection"]["type"], "bigquery")) == """
result = Req.post!(conn, bigquery: {~S"SELECT id FROM users", []}).body\
"""
Expand Down
Loading