-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathclickhouse.ts
514 lines (469 loc) · 14.9 KB
/
clickhouse.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
import type {
BaseResultSet,
DataFormat,
ResponseHeaders,
ResponseJSON,
} from '@clickhouse/client-common';
import { isSuccessfulResponse } from '@clickhouse/client-common';
import * as SQLParser from 'node-sql-parser';
import { SQLInterval } from '@/types';
import { hashCode, isBrowser, isNode, timeBucketByGranularity } from '@/utils';
export enum JSDataType {
Array = 'array',
Date = 'date',
Map = 'map',
Number = 'number',
String = 'string',
Bool = 'bool',
JSON = 'json',
Dynamic = 'dynamic', // json type will store anything as Dynamic type by default
}
export const getResponseHeaders = (response: Response): ResponseHeaders => {
const headers: ResponseHeaders = {};
response.headers.forEach((value, key) => {
headers[key] = value;
});
return headers;
};
export const convertCHDataTypeToJSType = (
dataType: string,
): JSDataType | null => {
if (dataType.startsWith('Date')) {
return JSDataType.Date;
} else if (dataType.startsWith('Map')) {
return JSDataType.Map;
} else if (dataType.startsWith('Array')) {
return JSDataType.Array;
} else if (
dataType.startsWith('Int') ||
dataType.startsWith('UInt') ||
dataType.startsWith('Float') ||
// Nullable types are possible (charts)
dataType.startsWith('Nullable(Int') ||
dataType.startsWith('Nullable(UInt') ||
dataType.startsWith('Nullable(Float')
) {
return JSDataType.Number;
} else if (
dataType.startsWith('String') ||
dataType.startsWith('FixedString') ||
dataType.startsWith('Enum') ||
dataType.startsWith('UUID') ||
dataType.startsWith('IPv4') ||
dataType.startsWith('IPv6')
) {
return JSDataType.String;
} else if (dataType === 'Bool') {
return JSDataType.Bool;
} else if (dataType.startsWith('JSON')) {
return JSDataType.JSON;
} else if (dataType.startsWith('Dynamic')) {
return JSDataType.Dynamic;
} else if (dataType.startsWith('LowCardinality')) {
return convertCHDataTypeToJSType(dataType.slice(15, -1));
}
return null;
};
export const convertCHTypeToPrimitiveJSType = (dataType: string) => {
const jsType = convertCHDataTypeToJSType(dataType);
if (jsType === JSDataType.Map || jsType === JSDataType.Array) {
throw new Error('Map type is not a primitive type');
} else if (jsType === JSDataType.Date) {
return JSDataType.Number;
}
return jsType;
};
const hash = (input: string | number) => Math.abs(hashCode(`${input}`));
const paramHash = (str: string | number) => {
return `HYPERDX_PARAM_${hash(str)}`;
};
export type ChSql = {
sql: string;
params: Record<string, any>;
};
type ParamTypes =
| ChSql
| ChSql[]
| { Identifier: string }
| { String: string }
| { Float32: number }
| { Float64: number }
| { Int32: number }
| { Int64: number }
| { UNSAFE_RAW_SQL: string }
| string; // TODO: Deprecate raw string interpolation
export const chSql = (
strings: TemplateStringsArray,
...values: ParamTypes[]
): ChSql => {
const sql = strings
.map((str, i) => {
const value = values[i];
// if (typeof value === 'string') {
// console.error('Unsafe string detected', value, 'in', strings, values);
// }
return (
str +
(value == null
? ''
: typeof value === 'string'
? value // If it's just a string sql literal
: 'UNSAFE_RAW_SQL' in value
? value.UNSAFE_RAW_SQL
: Array.isArray(value)
? value.map(v => v.sql).join('')
: 'sql' in value
? value.sql
: 'Identifier' in value
? `{${paramHash(value.Identifier)}:Identifier}`
: 'String' in value
? `{${paramHash(value.String)}:String}`
: 'Float32' in value
? `{${paramHash(value.Float32)}:Float32}`
: 'Float64' in value
? `{${paramHash(value.Float64)}:Float64}`
: 'Int32' in value
? `{${paramHash(value.Int32)}:Int32}`
: 'Int64' in value
? `{${paramHash(value.Int64)}:Int64}`
: '')
);
})
.join('');
return {
sql,
params: values.reduce((acc, value) => {
return {
...acc,
...(value == null ||
typeof value === 'string' ||
'UNSAFE_RAW_SQL' in value
? {}
: Array.isArray(value)
? value.reduce((acc, v) => {
Object.assign(acc, v.params);
return acc;
}, {})
: 'params' in value
? value.params
: 'Identifier' in value
? { [paramHash(value.Identifier)]: value.Identifier }
: 'String' in value
? { [paramHash(value.String)]: value.String }
: 'Float32' in value
? { [paramHash(value.Float32)]: value.Float32 }
: 'Float64' in value
? { [paramHash(value.Float64)]: value.Float64 }
: 'Int32' in value
? { [paramHash(value.Int32)]: value.Int32 }
: 'Int64' in value
? { [paramHash(value.Int64)]: value.Int64 }
: {}),
};
}, {}),
};
};
export const concatChSql = (sep: string, ...args: (ChSql | ChSql[])[]) => {
return args.reduce(
(acc: ChSql, arg) => {
if (Array.isArray(arg)) {
if (arg.length === 0) {
return acc;
}
acc.sql +=
(acc.sql.length > 0 ? sep : '') +
arg
.map(a => a.sql)
.filter(Boolean) // skip empty string expressions
.join(sep);
acc.params = arg.reduce((acc, a) => {
Object.assign(acc, a.params);
return acc;
}, acc.params);
} else if (arg.sql.length > 0) {
acc.sql += `${acc.sql.length > 0 ? sep : ''}${arg.sql}`;
Object.assign(acc.params, arg.params);
}
return acc;
},
{ sql: '', params: {} },
);
};
const isChSqlEmpty = (chSql: ChSql | ChSql[]) => {
if (Array.isArray(chSql)) {
return chSql.every(c => c.sql.length === 0);
}
return chSql.sql.length === 0;
};
export const wrapChSqlIfNotEmpty = (
sql: ChSql | ChSql[],
left: string,
right: string,
): ChSql | [] => {
if (isChSqlEmpty(sql)) {
return [];
}
return chSql`${left}${sql}${right}`;
};
export class ClickHouseQueryError extends Error {
constructor(
message: string,
public query: string,
) {
super(message);
this.name = 'ClickHouseQueryError';
}
}
export function extractColumnReference(
sql: string,
maxIterations = 10,
): string | null {
let iterations = 0;
// Loop until we remove all function calls and get just the column, with a maximum limit
while (/\w+\([^()]*\)/.test(sql) && iterations < maxIterations) {
// Replace the outermost function with its content
sql = sql.replace(/\w+\(([^()]*)\)/, '$1');
iterations++;
}
// If we reached the max iterations without resolving, return null to indicate an issue
return iterations < maxIterations ? sql.trim() : null;
}
export type ClickhouseClientOptions = {
host: string;
username?: string;
password?: string;
};
export class ClickhouseClient {
private readonly host: string;
private readonly username?: string;
private readonly password?: string;
constructor({ host, username, password }: ClickhouseClientOptions) {
this.host = host;
this.username = username;
this.password = password;
}
// https://github.com/ClickHouse/clickhouse-js/blob/1ebdd39203730bb99fad4c88eac35d9a5e96b34a/packages/client-web/src/connection/web_connection.ts#L151
async query<T extends DataFormat>({
query,
format = 'JSON',
query_params = {},
abort_signal,
clickhouse_settings,
connectionId,
queryId,
}: {
query: string;
format?: string;
abort_signal?: AbortSignal;
query_params?: Record<string, any>;
clickhouse_settings?: Record<string, any>;
connectionId?: string;
queryId?: string;
}): Promise<BaseResultSet<any, T>> {
const isLocalMode = this.username != null && this.password != null;
const includeCredentials = !isLocalMode;
const includeCorsHeader = isLocalMode;
const _connectionId = isLocalMode ? undefined : connectionId;
const searchParams = new URLSearchParams([
...(includeCorsHeader ? [['add_http_cors_header', '1']] : []),
...(_connectionId ? [['hyperdx_connection_id', _connectionId]] : []),
['query', query],
['default_format', format],
['date_time_output_format', 'iso'],
['wait_end_of_query', '0'],
['cancel_http_readonly_queries_on_client_close', '1'],
...(this.username ? [['user', this.username]] : []),
...(this.password ? [['password', this.password]] : []),
...(queryId ? [['query_id', queryId]] : []),
...Object.entries(query_params).map(([key, value]) => [
`param_${key}`,
value,
]),
...Object.entries(clickhouse_settings ?? {}).map(([key, value]) => [
key,
value,
]),
]);
let debugSql = '';
try {
debugSql = parameterizedQueryToSql({ sql: query, params: query_params });
} catch (e) {
debugSql = query;
}
// eslint-disable-next-line no-console
console.log('--------------------------------------------------------');
// eslint-disable-next-line no-console
console.log('Sending Query:', debugSql);
// eslint-disable-next-line no-console
console.log('--------------------------------------------------------');
if (isBrowser) {
// TODO: check if we can use the client-web directly
const { ResultSet } = await import('@clickhouse/client-web');
// https://github.com/ClickHouse/clickhouse-js/blob/1ebdd39203730bb99fad4c88eac35d9a5e96b34a/packages/client-web/src/connection/web_connection.ts#L200C7-L200C23
const response = await fetch(`${this.host}/?${searchParams.toString()}`, {
...(includeCredentials ? { credentials: 'include' } : {}),
signal: abort_signal,
method: 'GET',
});
// TODO: Send command to CH to cancel query on abort_signal
if (!response.ok) {
if (!isSuccessfulResponse(response.status)) {
const text = await response.text();
throw new ClickHouseQueryError(`${text}`, debugSql);
}
}
if (response.body == null) {
// TODO: Handle empty responses better?
throw new Error('Unexpected empty response from ClickHouse');
}
return new ResultSet<T>(
response.body,
format as T,
queryId ?? '',
getResponseHeaders(response),
);
} else if (isNode) {
const { createClient } = await import('@clickhouse/client');
const _client = createClient({
url: this.host,
username: this.username,
password: this.password,
clickhouse_settings: {
date_time_output_format: 'iso',
wait_end_of_query: 0,
cancel_http_readonly_queries_on_client_close: 1,
},
});
// TODO: Custom error handling
return _client.query({
query,
query_params,
format: format as T,
abort_signal,
clickhouse_settings,
query_id: queryId,
}) as unknown as BaseResultSet<any, T>;
} else {
throw new Error(
'ClickhouseClient is only supported in the browser or node environment',
);
}
}
}
export const testLocalConnection = async ({
host,
username,
password,
}: {
host: string;
username: string;
password: string;
}): Promise<boolean> => {
try {
const client = new ClickhouseClient({ host, username, password });
const result = await client.query({
query: 'SELECT 1',
format: 'TabSeparatedRaw',
});
return result.text().then(text => text.trim() === '1');
} catch (e) {
console.warn('Failed to test local connection', e);
return false;
}
};
export const tableExpr = ({
database,
table,
}: {
database: string;
table: string;
}) => {
return chSql`${{ Identifier: database }}.${{ Identifier: table }}`;
};
/**
* SELECT
* aggFnIf(fieldToColumn(field), where),
* timeBucketing(Granularity, timeConversion(fieldToColumn(field))),
* FROM db.table
* WHERE where
* GROUP BY timeBucketing, fieldToColumn(groupBy)
* ORDER BY orderBy
*/
export function parameterizedQueryToSql({
sql,
params,
}: {
sql: string;
params: Record<string, any>;
}) {
return Object.entries(params).reduce((acc, [key, value]) => {
return acc.replace(new RegExp(`{${key}:\\w+}`, 'g'), value);
}, sql);
}
export function chSqlToAliasMap(
chSql: ChSql | undefined,
): Record<string, string> {
const aliasMap: Record<string, string> = {};
if (chSql == null) {
return aliasMap;
}
try {
const sql = parameterizedQueryToSql(chSql);
const parser = new SQLParser.Parser();
const ast = parser.astify(sql, {
database: 'Postgresql',
parseOptions: { includeLocations: true },
}) as SQLParser.Select;
if (ast.columns != null) {
ast.columns.forEach(column => {
if (column.as != null) {
if (column.type === 'expr' && column.expr.type === 'column_ref') {
aliasMap[column.as] =
column.expr.array_index && column.expr.array_index[0]?.brackets
? // alias with brackets, ex: ResourceAttributes['service.name'] as service_name
`${column.expr.column.expr.value}['${column.expr.array_index[0].index.value}']`
: // normal alias
column.expr.column.expr.value;
} else if (column.expr.loc != null) {
aliasMap[column.as] = sql.slice(
column.expr.loc.start.offset,
column.expr.loc.end.offset,
);
} else {
console.error('Unknown alias column type', column);
}
}
});
}
} catch (e) {
console.error('Error parsing alias map', e, 'for query', chSql);
}
return aliasMap;
}
export type ColumnMetaType = { name: string; type: string };
export function filterColumnMetaByType(
meta: Array<ColumnMetaType>,
types: JSDataType[],
): Array<ColumnMetaType> | undefined {
return meta.filter(column =>
types.includes(convertCHDataTypeToJSType(column.type) as JSDataType),
);
}
export function inferTimestampColumn(
// from: https://github.com/ClickHouse/clickhouse-js/blob/442392c83834f313a964f9e5bd7ff44474631755/packages/client-common/src/clickhouse_types.ts#L8C3-L8C47
meta: Array<ColumnMetaType>,
) {
return filterColumnMetaByType(meta, [JSDataType.Date])?.[0];
}
export function inferNumericColumn(meta: Array<ColumnMetaType>) {
return filterColumnMetaByType(meta, [JSDataType.Number]);
}
export type ColumnMeta = {
codec_expression: string;
comment: string;
default_expression: string;
default_type: string;
name: string;
ttl_expression: string;
type: string;
};