Skip to content

Commit 6a2aacb

Browse files
committed
[WIP] feat(clickhouse): Use ClickHouse query with parameters via HTTP interface
1 parent 8e815c2 commit 6a2aacb

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

packages/cubejs-clickhouse-driver/src/ClickHouseDriver.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,33 @@ export class ClickHouseDriver extends BaseDriver implements DriverInterface {
227227
}
228228

229229
protected queryResponse(query: string, values: unknown[]) {
230-
const formattedQuery = sqlstring.format(query, values);
230+
// const formattedQuery = sqlstring.format(query, values);
231+
const formattedQuery = query;
232+
233+
// TODO override param
234+
235+
// `queryOptions` object will be merged into query params via querystring.stringify
236+
// https://github.com/cube-js/apla-node-clickhouse/blob/5a6577fc97ba6911171753fc65b2cd2f6170f2f7/src/clickhouse.js#L347-L348
237+
// https://github.com/cube-js/apla-node-clickhouse/blob/5a6577fc97ba6911171753fc65b2cd2f6170f2f7/src/clickhouse.js#L265-L266
238+
// https://github.com/cube-js/apla-node-clickhouse/blob/5a6577fc97ba6911171753fc65b2cd2f6170f2f7/src/clickhouse.js#L336-L338
239+
// https://github.com/cube-js/apla-node-clickhouse/blob/5a6577fc97ba6911171753fc65b2cd2f6170f2f7/src/clickhouse.js#L173-L175
240+
241+
// We can use `toSearchParams` or `formatQueryParams` from `@clickhouse/client-common` to prepare params, and extract only interesting ones
242+
// Beware - these functions marked as "For implementations usage only - should not be re-exported", so, probably, ot could be moved or disappear completely
243+
// https://github.com/ClickHouse/clickhouse-js/blob/a15cce93545c792852e34c05ce31954c75d11486/packages/client-common/src/utils/url.ts#L57-L61
244+
245+
// HTTP interface itself is documented, so it should be fine
246+
// https://clickhouse.com/docs/en/interfaces/cli#cli-queries-with-parameters
247+
// https://clickhouse.com/docs/en/interfaces/http#cli-queries-with-parameters
248+
249+
// TODO prepare values
250+
const paramsValues = Object.fromEntries(values.map((value, idx) => {
251+
const paramName = `p${idx}`;
252+
const paramKey = `param_${paramName}`;
253+
// TODO prepare value
254+
const preparedValue = value;
255+
return [paramKey, value];
256+
}));
231257

232258
return this.withConnection((connection, queryId) => connection.querying(formattedQuery, {
233259
dataObjects: true,
@@ -241,6 +267,9 @@ export class ClickHouseDriver extends BaseDriver implements DriverInterface {
241267
//
242268
//
243269
...(this.readOnlyMode ? {} : { join_use_nulls: 1 }),
270+
271+
// Add parameter values to query string
272+
...paramsValues,
244273
}
245274
}));
246275
}

packages/cubejs-schema-compiler/src/adapter/ClickHouseQuery.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ export class ClickHouseQuery extends BaseQuery {
268268

269269
public sqlTemplates() {
270270
const templates = super.sqlTemplates();
271+
// TODO not every param is a string
272+
// TODO override timeStampParam
273+
templates.params.param = '{p{{ param_index }}:String}';
271274
templates.functions.DATETRUNC = 'DATE_TRUNC({{ args_concat }})';
272275
// TODO: Introduce additional filter in jinja? or parseDateTimeBestEffort?
273276
// https://github.com/ClickHouse/ClickHouse/issues/19351

0 commit comments

Comments
 (0)