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

Adds withClickhouseSettings to @effect/sql-clickhouse #3859

Merged
merged 2 commits into from
Nov 1, 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
5 changes: 5 additions & 0 deletions .changeset/four-lions-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/sql-clickhouse": minor
---

Exposes clickhouse_settings parameter
5 changes: 4 additions & 1 deletion packages/sql-clickhouse/examples/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ Effect.gen(function*() {

yield* sql`SELECT * FROM clickhouse_js_example_cloud_table ORDER BY id`.stream.pipe(
Stream.runForEach(Effect.log),
sql.withQueryId("select")
sql.withQueryId("select"),
sql.withClickhouseSettings({
log_comment: "Some comment to be stored in the query log"
})
)
}).pipe(
Effect.provide(ClickhouseLive),
Expand Down
51 changes: 42 additions & 9 deletions packages/sql-clickhouse/src/ClickhouseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ export interface ClickhouseClient extends Client.SqlClient {
(queryId: string): <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>
<A, E, R>(effect: Effect.Effect<A, E, R>, queryId: string): Effect.Effect<A, E, R>
}
readonly withClickhouseSettings: {
(
settings: NonNullable<Clickhouse.BaseQueryParams["clickhouse_settings"]>
): <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>
<A, E, R>(
effect: Effect.Effect<A, E, R>,
settings: NonNullable<Clickhouse.BaseQueryParams["clickhouse_settings"]>
): Effect.Effect<A, E, R>
}
}

/**
Expand Down Expand Up @@ -106,13 +115,15 @@ export const make = (
const method = fiber.getFiberRef(currentClientMethod)
return Effect.async<Clickhouse.ResultSet<"JSON"> | Clickhouse.CommandResult, SqlError>((resume) => {
const queryId = fiber.getFiberRef(currentQueryId) ?? Crypto.randomUUID()
const settings = fiber.getFiberRef(currentClickhouseSettings) ?? {}
const controller = new AbortController()
if (method === "command") {
this.conn.command({
query: sql,
query_params: paramsObj,
abort_signal: controller.signal,
query_id: queryId
query_id: queryId,
clickhouse_settings: settings
}).then(
(result) => resume(Effect.succeed(result)),
(cause) => resume(Effect.fail(new SqlError({ cause, message: "Failed to execute statement" })))
Expand All @@ -123,6 +134,7 @@ export const make = (
query_params: paramsObj,
abort_signal: controller.signal,
query_id: queryId,
clickhouse_settings: settings,
format
}).then(
(result) => resume(Effect.succeed(result)),
Expand Down Expand Up @@ -231,28 +243,38 @@ export const make = (
readonly values: Clickhouse.InsertValues<Readable, T>
readonly format?: Clickhouse.DataFormat
}) {
return FiberRef.getWith(currentQueryId, (queryId_) =>
tim-smart marked this conversation as resolved.
Show resolved Hide resolved
Effect.async<Clickhouse.InsertResult, SqlError>((resume) => {
const queryId = queryId_ ?? Crypto.randomUUID()
return Effect.withFiberRuntime<Clickhouse.InsertResult, SqlError>((fiber) =>
Effect.async((resume) => {
const queryId = fiber.getFiberRef(currentQueryId) ?? Crypto.randomUUID()
const settings = fiber.getFiberRef(currentClickhouseSettings)
const controller = new AbortController()
client.insert({
format: "JSONEachRow",
...options,
abort_signal: controller.signal,
query_id: queryId
query_id: queryId,
clickhouse_settings: settings
}).then(
(result) =>
resume(Effect.succeed(result)),
(result) => resume(Effect.succeed(result)),
(cause) => resume(Effect.fail(new SqlError({ cause, message: "Failed to insert data" })))
)
return Effect.suspend(() => {
controller.abort()
return Effect.promise(() => client.command({ query: `KILL QUERY WHERE query_id = '${queryId}'` }))
})
}))
})
)
},
withQueryId: dual(2, <A, E, R>(effect: Effect.Effect<A, E, R>, queryId: string) =>
Effect.locally(effect, currentQueryId, queryId))
Effect.locally(effect, currentQueryId, queryId)),
withClickhouseSettings: dual(
2,
<A, E, R>(
effect: Effect.Effect<A, E, R>,
settings: NonNullable<Clickhouse.BaseQueryParams["clickhouse_settings"]>
) =>
Effect.locally(effect, currentClickhouseSettings, settings)
)
}
)
})
Expand All @@ -275,6 +297,17 @@ export const currentQueryId: FiberRef.FiberRef<string | undefined> = globalValue
() => FiberRef.unsafeMake<string | undefined>(undefined)
)

/**
* @category fiber refs
* @since 1.0.0
*/
export const currentClickhouseSettings: FiberRef.FiberRef<
NonNullable<Clickhouse.BaseQueryParams["clickhouse_settings"]>
> = globalValue(
"@effect/sql-clickhouse/ClickhouseClient/currentClickhouseSettings",
() => FiberRef.unsafeMake({})
)

/**
* @category layers
* @since 1.0.0
Expand Down