@hypequery/clickhouse 2.13.0 → 2.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/bin.js +1 -1
- package/dist/cli/describe-relation.js +57 -0
- package/dist/cli/generate-types.js +14 -11
- package/dist/cli/type-parsing.js +9 -0
- package/dist/core/adapters/clickhouse-adapter.d.ts +20 -1
- package/dist/core/adapters/clickhouse-adapter.d.ts.map +1 -1
- package/dist/core/adapters/clickhouse-adapter.js +51 -7
- package/dist/core/adapters/database-adapter.d.ts +6 -0
- package/dist/core/adapters/database-adapter.d.ts.map +1 -1
- package/dist/core/cross-filter.d.ts.map +1 -1
- package/dist/core/cross-filter.js +3 -12
- package/dist/core/features/aggregations.d.ts +5 -1
- package/dist/core/features/aggregations.d.ts.map +1 -1
- package/dist/core/features/aggregations.js +17 -3
- package/dist/core/features/executor.d.ts +4 -0
- package/dist/core/features/executor.d.ts.map +1 -1
- package/dist/core/features/executor.js +38 -8
- package/dist/core/query-builder.d.ts +83 -9
- package/dist/core/query-builder.d.ts.map +1 -1
- package/dist/core/query-builder.js +74 -0
- package/dist/core/tests/integration/setup.d.ts +2 -0
- package/dist/core/tests/integration/setup.d.ts.map +1 -1
- package/dist/core/tests/test-utils.d.ts +2 -0
- package/dist/core/tests/test-utils.d.ts.map +1 -1
- package/dist/core/types/aggregate-types.d.ts +54 -0
- package/dist/core/types/aggregate-types.d.ts.map +1 -0
- package/dist/core/types/aggregate-types.js +1 -0
- package/dist/core/utils/aggregate-detection.d.ts +3 -0
- package/dist/core/utils/aggregate-detection.d.ts.map +1 -0
- package/dist/core/utils/aggregate-detection.js +26 -0
- package/dist/core/utils/arrow-compression.d.ts +18 -0
- package/dist/core/utils/arrow-compression.d.ts.map +1 -0
- package/dist/core/utils/arrow-compression.js +37 -0
- package/dist/core/utils/byte-stream.d.ts +22 -0
- package/dist/core/utils/byte-stream.d.ts.map +1 -0
- package/dist/core/utils/byte-stream.js +144 -0
- package/dist/core/utils/clickhouse-adapter-config.js +1 -1
- package/dist/core/utils/compound-parameter.d.ts.map +1 -1
- package/dist/core/utils/compound-parameter.js +5 -1
- package/dist/core/utils/date-literal.d.ts +21 -0
- package/dist/core/utils/date-literal.d.ts.map +1 -0
- package/dist/core/utils/date-literal.js +42 -0
- package/dist/core/utils/format-clause.d.ts +17 -0
- package/dist/core/utils/format-clause.d.ts.map +1 -0
- package/dist/core/utils/format-clause.js +138 -0
- package/dist/core/utils/integer-json-encoding.d.ts.map +1 -1
- package/dist/core/utils/integer-json-encoding.js +8 -17
- package/dist/core/utils/readonly-setting-error.d.ts +7 -0
- package/dist/core/utils/readonly-setting-error.d.ts.map +1 -0
- package/dist/core/utils/readonly-setting-error.js +17 -0
- package/dist/core/utils/sql-parens.d.ts +5 -0
- package/dist/core/utils/sql-parens.d.ts.map +1 -1
- package/dist/core/utils/sql-parens.js +19 -0
- package/dist/core/utils/stream-exception.d.ts +37 -0
- package/dist/core/utils/stream-exception.d.ts.map +1 -0
- package/dist/core/utils/stream-exception.js +83 -0
- package/dist/core/utils/streaming-helpers.d.ts +11 -0
- package/dist/core/utils/streaming-helpers.d.ts.map +1 -1
- package/dist/core/utils/streaming-helpers.js +36 -23
- package/dist/core/utils.d.ts.map +1 -1
- package/dist/core/utils.js +2 -4
- package/dist/datasets.d.ts.map +1 -1
- package/dist/datasets.js +3 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/types/clickhouse-types.d.ts +8 -2
- package/dist/types/clickhouse-types.d.ts.map +1 -1
- package/package.json +12 -12
package/dist/cli/bin.js
CHANGED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Column introspection that also covers parameterized views. `DESCRIBE TABLE`
|
|
3
|
+
* returns no columns for a view whose query has `{name:Type}` parameters;
|
|
4
|
+
* describing a call with each parameter's default value returns them, and
|
|
5
|
+
* `DESCRIBE` analyses the query without running it. Mirrors
|
|
6
|
+
* `@hypequery/cli`'s `typegen/describe-relation.ts`.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
// Scanned left to right: a `{name:Type}` placeholder in code, or a region that
|
|
10
|
+
// is not code (string literal, quoted identifier, heredoc, comment) to skip, so
|
|
11
|
+
// placeholder-shaped text inside a literal or comment is never read as a
|
|
12
|
+
// parameter. A type may itself contain a quoted string: DateTime64(3, 'UTC').
|
|
13
|
+
const PARAMETER_OR_NON_CODE = new RegExp([
|
|
14
|
+
/\{\s*([A-Za-z_][A-Za-z0-9_]*)\s*:\s*((?:[^{}'"]|'(?:[^'\\]|\\.)*')+?)\s*\}/.source,
|
|
15
|
+
/'(?:[^'\\]|\\.|'')*'|"(?:[^"\\]|\\.|"")*"|`(?:[^`\\]|\\.|``)*`/.source,
|
|
16
|
+
/\$(?<tag>[A-Za-z0-9_]*)\$[\s\S]*?\$\k<tag>\$|--[^\n]*|\/\*[\s\S]*?\*\//.source,
|
|
17
|
+
].join('|'), 'g');
|
|
18
|
+
|
|
19
|
+
/** @returns {Array<{ name: string, type: string }>} */
|
|
20
|
+
export function parseViewParameters(createQuery) {
|
|
21
|
+
const seen = new Map();
|
|
22
|
+
for (const match of createQuery.matchAll(PARAMETER_OR_NON_CODE)) {
|
|
23
|
+
const [, name, type] = match;
|
|
24
|
+
if (name && type && !seen.has(name)) seen.set(name, type);
|
|
25
|
+
}
|
|
26
|
+
return [...seen].map(([name, type]) => ({ name, type }));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const quoteIdentifier = name => `\`${name.replace(/\\/g, '\\\\').replace(/`/g, '\\`')}\``;
|
|
30
|
+
const quoteString = value => `'${value.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`;
|
|
31
|
+
|
|
32
|
+
export function describeParameterizedViewQuery(viewName, parameters) {
|
|
33
|
+
const args = parameters
|
|
34
|
+
.map(({ name, type }) => `${quoteIdentifier(name)} = defaultValueOfTypeName(${quoteString(type)})`)
|
|
35
|
+
.join(', ');
|
|
36
|
+
return `DESCRIBE (SELECT * FROM ${quoteIdentifier(viewName)}(${args}))`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function rows(client, query) {
|
|
40
|
+
const result = await client.query({ query, format: 'JSONEachRow' });
|
|
41
|
+
return result.json();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** @returns {Promise<{ columns: Array<Record<string, string>>, parameters?: Array<{ name: string, type: string }> }>} */
|
|
45
|
+
export async function describeRelation(client, tableName) {
|
|
46
|
+
const columns = await rows(client, `DESCRIBE TABLE ${tableName}`);
|
|
47
|
+
if (columns.length > 0) return { columns };
|
|
48
|
+
|
|
49
|
+
const [definition] = await rows(
|
|
50
|
+
client,
|
|
51
|
+
`SELECT create_table_query FROM system.tables WHERE database = currentDatabase() AND name = ${quoteString(tableName)}`,
|
|
52
|
+
);
|
|
53
|
+
const parameters = parseViewParameters(definition?.create_table_query ?? '');
|
|
54
|
+
if (parameters.length === 0) return { columns };
|
|
55
|
+
|
|
56
|
+
return { columns: await rows(client, describeParameterizedViewQuery(tableName, parameters)), parameters };
|
|
57
|
+
}
|
|
@@ -2,12 +2,13 @@ import { ClickHouseConnection } from '../core/connection.js';
|
|
|
2
2
|
import fs from 'fs/promises';
|
|
3
3
|
import path from 'path';
|
|
4
4
|
import dotenv from 'dotenv';
|
|
5
|
+
import { describeRelation } from './describe-relation.js';
|
|
5
6
|
import { clickhouseToTsType } from './type-parsing.js';
|
|
6
7
|
|
|
7
8
|
export { clickhouseToTsType } from './type-parsing.js';
|
|
8
9
|
|
|
9
10
|
// Load environment variables from the current directory
|
|
10
|
-
dotenv.config();
|
|
11
|
+
dotenv.config({ quiet: true });
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* @typedef {Object} ColumnInfo
|
|
@@ -86,13 +87,19 @@ export async function generateTypeDefinitions(client, options = {}) {
|
|
|
86
87
|
*/
|
|
87
88
|
export interface IntrospectedSchema {`;
|
|
88
89
|
|
|
90
|
+
// Describe each table once; parameterized views need a described call.
|
|
91
|
+
const described = new Map();
|
|
92
|
+
for (const table of tables) {
|
|
93
|
+
described.set(table.name, await describeRelation(client, table.name));
|
|
94
|
+
}
|
|
95
|
+
|
|
89
96
|
// Get columns for each table
|
|
90
97
|
for (const table of tables) {
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
98
|
+
const { columns, parameters } = described.get(table.name);
|
|
99
|
+
if (parameters) {
|
|
100
|
+
const signature = parameters.map(({ name, type }) => `${name}: ${type}`).join(', ');
|
|
101
|
+
typeDefinitions += `\n /** Parameterized view: select from ${table.name}(${signature}). */`;
|
|
102
|
+
}
|
|
96
103
|
typeDefinitions += `\n ${table.name}: {`;
|
|
97
104
|
for (const column of columns) {
|
|
98
105
|
typeDefinitions += `\n '${column.name}': ${JSON.stringify(column.type)};`;
|
|
@@ -105,11 +112,7 @@ export interface IntrospectedSchema {`;
|
|
|
105
112
|
// Also generate a type-safe record type for each table
|
|
106
113
|
typeDefinitions += `\n// Type-safe record types for each table\n`;
|
|
107
114
|
for (const table of tables) {
|
|
108
|
-
const
|
|
109
|
-
query: `DESCRIBE TABLE ${table.name}`,
|
|
110
|
-
format: 'JSONEachRow'
|
|
111
|
-
});
|
|
112
|
-
const columns = await columnsQuery.json();
|
|
115
|
+
const { columns } = described.get(table.name);
|
|
113
116
|
|
|
114
117
|
typeDefinitions += `export interface ${capitalizeFirstLetter(table.name)}Record {`;
|
|
115
118
|
for (const column of columns) {
|
package/dist/cli/type-parsing.js
CHANGED
|
@@ -194,6 +194,15 @@ export const clickhouseToTsType = (type) => {
|
|
|
194
194
|
return clickhouseToTsType(wrappedLowCardinalityType);
|
|
195
195
|
}
|
|
196
196
|
|
|
197
|
+
// SimpleAggregateFunction(fn, T) values are plain T values.
|
|
198
|
+
const wrappedSimpleAggregate = unwrapType(normalizedType, 'SimpleAggregateFunction');
|
|
199
|
+
if (wrappedSimpleAggregate) {
|
|
200
|
+
const simpleAggregateArgs = splitTopLevelArgs(wrappedSimpleAggregate);
|
|
201
|
+
if (simpleAggregateArgs.length === 2) {
|
|
202
|
+
return clickhouseToTsType(simpleAggregateArgs[1]);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
197
206
|
const wrappedTupleType = unwrapType(normalizedType, 'Tuple');
|
|
198
207
|
if (wrappedTupleType) {
|
|
199
208
|
const tupleParts = splitTopLevelArgs(wrappedTupleType);
|
|
@@ -7,15 +7,34 @@ export declare class ClickHouseAdapter implements DatabaseAdapter {
|
|
|
7
7
|
/** Connection-level settings, so they can outrank the adapter's own defaults. */
|
|
8
8
|
private readonly configSettings?;
|
|
9
9
|
private readonly integerJsonEncoding;
|
|
10
|
+
private readonly arrowCompression;
|
|
10
11
|
constructor(config: ClickHouseAdapterConfig);
|
|
11
12
|
/**
|
|
12
|
-
* Converts only rejections of
|
|
13
|
+
* Converts only rejections of an adapter-owned default setting into an
|
|
13
14
|
* actionable configuration error. Caller-owned settings and unrelated
|
|
14
15
|
* readonly failures retain their original error identity.
|
|
15
16
|
*/
|
|
16
17
|
private withReadonlyGuidance;
|
|
18
|
+
private jsonSettings;
|
|
17
19
|
query<T>(sql: string, params?: unknown[], options?: QueryExecutionOptions): Promise<T[]>;
|
|
18
20
|
stream<T>(sql: string, params?: unknown[], options?: QueryExecutionOptions): Promise<ReadableStream<T[]>>;
|
|
21
|
+
/**
|
|
22
|
+
* Streams the result as uncompressed Arrow IPC stream bytes. Batches are
|
|
23
|
+
* uncompressed by default because JavaScript Arrow readers ship without the
|
|
24
|
+
* LZ4 codec ClickHouse would otherwise use; a connection-level
|
|
25
|
+
* `output_format_arrow_compression_method` outranks that default, a per-call
|
|
26
|
+
* one outranks both, and `arrowCompression: 'server-default'` omits it. Use
|
|
27
|
+
* HTTP response compression to shrink the transfer instead.
|
|
28
|
+
*
|
|
29
|
+
* The format is requested with a `FORMAT ArrowStream` clause rather than the
|
|
30
|
+
* `default_format` setting, which `readonly = 1` users may not change; a
|
|
31
|
+
* trailing `;` or comment is dropped first.
|
|
32
|
+
*
|
|
33
|
+
* A body that ends without Arrow's end-of-stream marker fails the stream, so
|
|
34
|
+
* a mid-stream failure is caught even on servers before 25.11, which send no
|
|
35
|
+
* exception tag; ClickHouse's own error is recovered where it can be.
|
|
36
|
+
*/
|
|
37
|
+
streamArrow(sql: string, params?: unknown[], options?: QueryExecutionOptions): Promise<ReadableStream<Uint8Array>>;
|
|
19
38
|
insert<T extends Record<string, unknown>>(table: string, rows: T[], options?: InsertExecutionOptions): Promise<InsertResultSummary>;
|
|
20
39
|
close(): Promise<void>;
|
|
21
40
|
render(sql: string, params?: unknown[]): string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clickhouse-adapter.d.ts","sourceRoot":"","sources":["../../../src/core/adapters/clickhouse-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EACf,qBAAqB,EACrB,sBAAsB,EACtB,mBAAmB,EACpB,MAAM,uBAAuB,CAAC;AAG/B,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"clickhouse-adapter.d.ts","sourceRoot":"","sources":["../../../src/core/adapters/clickhouse-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EACf,qBAAqB,EACrB,sBAAsB,EACtB,mBAAmB,EACpB,MAAM,uBAAuB,CAAC;AAG/B,OAAO,KAAK,EAEV,uBAAuB,EAExB,MAAM,qBAAqB,CAAC;AAiD7B,qBAAa,iBAAkB,YAAW,eAAe;IACvD,QAAQ,CAAC,IAAI,gBAAgB;IAC7B,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,OAAO,CAAC,MAAM,CAAmB;IACjC,iFAAiF;IACjF,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAqB;IACrD,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAsB;IAC1D,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAmB;gBAExC,MAAM,EAAE,uBAAuB;IAQ3C;;;;OAIG;YACW,oBAAoB;IAYlC,OAAO,CAAC,YAAY;IAId,KAAK,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,GAAE,OAAO,EAAO,EAAE,OAAO,CAAC,EAAE,qBAAqB,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC;IAyB5F,MAAM,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,GAAE,OAAO,EAAO,EAAE,OAAO,CAAC,EAAE,qBAAqB,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC,EAAE,CAAC,CAAC;IAmBnH;;;;;;;;;;;;;;;OAeG;IACG,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,GAAE,OAAO,EAAO,EAAE,OAAO,CAAC,EAAE,qBAAqB,GAAG,OAAO,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC;IA6BtH,MAAM,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5C,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,CAAC,EAAE,EACT,OAAO,CAAC,EAAE,sBAAsB,GAC/B,OAAO,CAAC,mBAAmB,CAAC;IAyBzB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAI5B,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,GAAE,OAAO,EAAO,GAAG,MAAM;CAGpD;AAED,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,uBAAuB,GAAG,eAAe,CAExF"}
|
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
import { substituteParameters } from '../utils.js';
|
|
2
2
|
import { createJsonEachRowStream } from '../utils/streaming-helpers.js';
|
|
3
|
+
import { createByteStream } from '../utils/byte-stream.js';
|
|
4
|
+
import { EXCEPTION_TAG_HEADER } from '../utils/stream-exception.js';
|
|
5
|
+
import { withFormatClause } from '../utils/format-clause.js';
|
|
6
|
+
import { buildArrowStreamSettings, createReadonlyArrowCompressionError, } from '../utils/arrow-compression.js';
|
|
3
7
|
import { assertSafeInsertIdentifiers } from '../utils/insert-identifiers.js';
|
|
4
8
|
import { consumeResultWithAbort } from '../utils/abortable-result.js';
|
|
5
9
|
import { throwIfAborted } from '../utils/abort.js';
|
|
6
10
|
import { createClickHouseClient, deriveClickHouseNamespace, } from '../utils/clickhouse-adapter-config.js';
|
|
7
11
|
import { buildIntegerJsonSettings, createReadonlyIntegerJsonError, } from '../utils/integer-json-encoding.js';
|
|
8
12
|
import { toClientSettings } from '../utils/clickhouse-client-compat.js';
|
|
13
|
+
/** Arrow's end-of-stream marker: a continuation token and a zero length. */
|
|
14
|
+
const ARROW_END_OF_STREAM = new Uint8Array([0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0]);
|
|
9
15
|
export class ClickHouseAdapter {
|
|
10
16
|
name = 'clickhouse';
|
|
11
17
|
namespace;
|
|
@@ -13,32 +19,35 @@ export class ClickHouseAdapter {
|
|
|
13
19
|
/** Connection-level settings, so they can outrank the adapter's own defaults. */
|
|
14
20
|
configSettings;
|
|
15
21
|
integerJsonEncoding;
|
|
22
|
+
arrowCompression;
|
|
16
23
|
constructor(config) {
|
|
17
24
|
this.namespace = deriveClickHouseNamespace(config);
|
|
18
25
|
this.client = createClickHouseClient(config);
|
|
19
26
|
this.configSettings = config.clickhouse_settings;
|
|
20
27
|
this.integerJsonEncoding = config.integerJsonEncoding ?? 'quoted';
|
|
28
|
+
this.arrowCompression = config.arrowCompression ?? 'none';
|
|
21
29
|
}
|
|
22
30
|
/**
|
|
23
|
-
* Converts only rejections of
|
|
31
|
+
* Converts only rejections of an adapter-owned default setting into an
|
|
24
32
|
* actionable configuration error. Caller-owned settings and unrelated
|
|
25
33
|
* readonly failures retain their original error identity.
|
|
26
34
|
*/
|
|
27
|
-
async withReadonlyGuidance(
|
|
28
|
-
const attempt = buildIntegerJsonSettings(this.integerJsonEncoding, this.configSettings, optionSettings);
|
|
35
|
+
async withReadonlyGuidance(attempt, guidance, send) {
|
|
29
36
|
try {
|
|
30
37
|
return await send(attempt.settings);
|
|
31
38
|
}
|
|
32
39
|
catch (error) {
|
|
33
|
-
|
|
34
|
-
throw guidanceError ?? error;
|
|
40
|
+
throw guidance(error, attempt.adapterDefaultApplied) ?? error;
|
|
35
41
|
}
|
|
36
42
|
}
|
|
43
|
+
jsonSettings(optionSettings) {
|
|
44
|
+
return buildIntegerJsonSettings(this.integerJsonEncoding, this.configSettings, optionSettings);
|
|
45
|
+
}
|
|
37
46
|
async query(sql, params = [], options) {
|
|
38
47
|
// The ClickHouse clients never check an already-aborted signal, so fail before sending anything.
|
|
39
48
|
throwIfAborted(options?.abortSignal);
|
|
40
49
|
const finalSQL = substituteParameters(sql, params);
|
|
41
|
-
const result = await this.withReadonlyGuidance(options?.clickhouseSettings, clickhouseSettings => this.client.query({
|
|
50
|
+
const result = await this.withReadonlyGuidance(this.jsonSettings(options?.clickhouseSettings), createReadonlyIntegerJsonError, clickhouseSettings => this.client.query({
|
|
42
51
|
query: finalSQL,
|
|
43
52
|
format: 'JSONEachRow',
|
|
44
53
|
clickhouse_settings: toClientSettings(clickhouseSettings),
|
|
@@ -52,7 +61,7 @@ export class ClickHouseAdapter {
|
|
|
52
61
|
async stream(sql, params = [], options) {
|
|
53
62
|
throwIfAborted(options?.abortSignal);
|
|
54
63
|
const finalSQL = substituteParameters(sql, params);
|
|
55
|
-
const result = await this.withReadonlyGuidance(options?.clickhouseSettings, clickhouseSettings => this.client.query({
|
|
64
|
+
const result = await this.withReadonlyGuidance(this.jsonSettings(options?.clickhouseSettings), createReadonlyIntegerJsonError, clickhouseSettings => this.client.query({
|
|
56
65
|
query: finalSQL,
|
|
57
66
|
format: 'JSONEachRow',
|
|
58
67
|
clickhouse_settings: toClientSettings(clickhouseSettings),
|
|
@@ -62,6 +71,41 @@ export class ClickHouseAdapter {
|
|
|
62
71
|
const stream = result.stream();
|
|
63
72
|
return createJsonEachRowStream(stream, options?.abortSignal);
|
|
64
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* Streams the result as uncompressed Arrow IPC stream bytes. Batches are
|
|
76
|
+
* uncompressed by default because JavaScript Arrow readers ship without the
|
|
77
|
+
* LZ4 codec ClickHouse would otherwise use; a connection-level
|
|
78
|
+
* `output_format_arrow_compression_method` outranks that default, a per-call
|
|
79
|
+
* one outranks both, and `arrowCompression: 'server-default'` omits it. Use
|
|
80
|
+
* HTTP response compression to shrink the transfer instead.
|
|
81
|
+
*
|
|
82
|
+
* The format is requested with a `FORMAT ArrowStream` clause rather than the
|
|
83
|
+
* `default_format` setting, which `readonly = 1` users may not change; a
|
|
84
|
+
* trailing `;` or comment is dropped first.
|
|
85
|
+
*
|
|
86
|
+
* A body that ends without Arrow's end-of-stream marker fails the stream, so
|
|
87
|
+
* a mid-stream failure is caught even on servers before 25.11, which send no
|
|
88
|
+
* exception tag; ClickHouse's own error is recovered where it can be.
|
|
89
|
+
*/
|
|
90
|
+
async streamArrow(sql, params = [], options) {
|
|
91
|
+
throwIfAborted(options?.abortSignal);
|
|
92
|
+
const finalSQL = withFormatClause(substituteParameters(sql, params), 'ArrowStream');
|
|
93
|
+
// A caller-supplied client does not carry the adapter's connection-level
|
|
94
|
+
// settings, so they are merged here, as query() and stream() do.
|
|
95
|
+
const attempt = buildArrowStreamSettings(this.arrowCompression, this.configSettings, options?.clickhouseSettings);
|
|
96
|
+
const result = await this.withReadonlyGuidance(attempt, createReadonlyArrowCompressionError, clickhouseSettings => this.client.exec({
|
|
97
|
+
query: finalSQL,
|
|
98
|
+
clickhouse_settings: toClientSettings(clickhouseSettings),
|
|
99
|
+
query_id: options?.queryId,
|
|
100
|
+
abort_signal: options?.abortSignal,
|
|
101
|
+
}));
|
|
102
|
+
const tag = result.response_headers[EXCEPTION_TAG_HEADER];
|
|
103
|
+
return createByteStream(result.stream, {
|
|
104
|
+
exceptionTag: typeof tag === 'string' ? tag : undefined,
|
|
105
|
+
endMarker: ARROW_END_OF_STREAM,
|
|
106
|
+
abortSignal: options?.abortSignal,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
65
109
|
async insert(table, rows, options) {
|
|
66
110
|
throwIfAborted(options?.abortSignal);
|
|
67
111
|
assertSafeInsertIdentifiers(table, options?.columns);
|
|
@@ -26,6 +26,12 @@ export interface DatabaseAdapter {
|
|
|
26
26
|
/** Releases connections held by the adapter. Callers must not reuse it afterwards. */
|
|
27
27
|
close?(): Promise<void>;
|
|
28
28
|
stream?<T>(sql: string, params?: unknown[], options?: QueryExecutionOptions): Promise<ReadableStream<T[]>>;
|
|
29
|
+
/**
|
|
30
|
+
* Streams the result as Arrow IPC stream bytes (ClickHouse `ArrowStream`),
|
|
31
|
+
* for any Arrow reader to decode. Optional: callers reject with a clear error
|
|
32
|
+
* on adapters without it.
|
|
33
|
+
*/
|
|
34
|
+
streamArrow?(sql: string, params?: unknown[], options?: QueryExecutionOptions): Promise<ReadableStream<Uint8Array>>;
|
|
29
35
|
/**
|
|
30
36
|
* Inserts rows that have already been normalized for JSONEachRow-compatible
|
|
31
37
|
* execution. The executor filters explicit empty batches before calling the
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"database-adapter.d.ts","sourceRoot":"","sources":["../../../src/core/adapters/database-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,oCAAoC,CAAC;AAE7E,MAAM,WAAW,qBAAqB;IACpC,kBAAkB,CAAC,EAAE,kBAAkB,CAAC;IACxC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,WAAW,CAAC;CAC3B;AAED,MAAM,WAAW,sBAAsB;IACrC,kBAAkB,CAAC,EAAE,kBAAkB,CAAC;IACxC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,gGAAgG;IAChG,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;C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,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,GACtE,IAAI;IAQP;;;;OAIG;IACH,QAAQ,CACN,eAAe,EAAE,KAAK,CACpB,oBAAoB,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,SAAS,CAAC,CAAC,GAAG,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,SAAS,CAAC,CAAC,CAC9F,EACD,QAAQ,EAAE,KAAK,GAAG,IAAI,GACrB,IAAI;IAYP;;OAEG;IACH,aAAa,IAAI,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,SAAS,CAAC,CAAC;IAIvD;;;;OAIG;IACH,OAAO,CAAC,aAAa;IAerB;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAazB;;OAEG;IACH,OAAO,CAAC,aAAa;IAqBrB;;OAEG;IACH,OAAO,CAAC,OAAO;IAMf;;;;;OAKG;IACH,IAAI,CAAC,CAAC,SAAS,MAAM,MAAM,CAAC,SAAS,CAAC,EACpC,WAAW,EAAE,CAAC,EACd,CAAC,EAAE,MAAM,EACT,OAAO,GAAE,MAAM,GAAG,KAAc,GAC/B,IAAI;CAmBR"}
|
|
@@ -23,18 +23,9 @@ export class CrossFilter {
|
|
|
23
23
|
const columnType = this.getColumnType(String(condition.column));
|
|
24
24
|
this.validateValueType(columnType, condition.value, String(condition.column), condition.operator);
|
|
25
25
|
}
|
|
26
|
-
//
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
value = value.map(v => v instanceof Date ? v.toISOString() : v);
|
|
30
|
-
}
|
|
31
|
-
else if (value instanceof Date) {
|
|
32
|
-
value = value.toISOString();
|
|
33
|
-
}
|
|
34
|
-
this.rootGroup.conditions.push({
|
|
35
|
-
...condition,
|
|
36
|
-
value
|
|
37
|
-
});
|
|
26
|
+
// Dates stay Date objects: the SQL renderer turns them into a UTC
|
|
27
|
+
// DateTime expression, which ClickHouse accepts for sorting-key columns.
|
|
28
|
+
this.rootGroup.conditions.push({ ...condition });
|
|
38
29
|
return this;
|
|
39
30
|
}
|
|
40
31
|
/**
|
|
@@ -4,9 +4,12 @@ import type { SelectQueryNode } from '../../types/index.js';
|
|
|
4
4
|
export declare class AggregationFeature<Schema extends SchemaDefinition<Schema>, State extends BuilderState<Schema, string, any, keyof Schema, Partial<Record<string, keyof Schema>>, any, any>> {
|
|
5
5
|
private builder;
|
|
6
6
|
private static readonly TRAILING_ALIAS_PATTERN;
|
|
7
|
-
private static readonly LEADING_AGGREGATE_CALL_PATTERN;
|
|
8
7
|
constructor(builder: QueryBuilder<Schema, State>);
|
|
9
8
|
private stripTrailingAlias;
|
|
9
|
+
/**
|
|
10
|
+
* An aggregate anywhere in the expression (`round(avg(x), 2)`) or a window
|
|
11
|
+
* function makes it invalid as a GROUP BY key, not only a leading call.
|
|
12
|
+
*/
|
|
10
13
|
private isAggregateSelection;
|
|
11
14
|
private createAggregateSelection;
|
|
12
15
|
private shouldInferGroupByFromSelection;
|
|
@@ -30,5 +33,6 @@ export declare class AggregationFeature<Schema extends SchemaDefinition<Schema>,
|
|
|
30
33
|
stddev(column: string, alias: string): SelectQueryNode<State["output"], Schema>;
|
|
31
34
|
variance(column: string, alias: string): SelectQueryNode<State["output"], Schema>;
|
|
32
35
|
approxCountDistinct(column: string, alias: string): SelectQueryNode<State["output"], Schema>;
|
|
36
|
+
mergeState(stateFunction: string, column: string, alias: string): SelectQueryNode<State["output"], Schema>;
|
|
33
37
|
}
|
|
34
38
|
//# sourceMappingURL=aggregations.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aggregations.d.ts","sourceRoot":"","sources":["../../../src/core/features/aggregations.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAChF,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,KAAK,EAAE,eAAe,EAAiB,MAAM,sBAAsB,CAAC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AE,MAAM;IAI3C,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IAIvD,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IAIvD,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IAOrD,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IAIpC,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IAItC,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IAIjD,UAAU,CAAC,aAAa,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;CAIhE"}
|
|
@@ -1,16 +1,19 @@
|
|
|
1
|
+
import { isAggregateOrWindowExpression } from '../utils/aggregate-detection.js';
|
|
1
2
|
export class AggregationFeature {
|
|
2
3
|
builder;
|
|
3
4
|
static TRAILING_ALIAS_PATTERN = /\s+AS\s+[A-Za-z_][A-Za-z0-9_]*$/i;
|
|
4
|
-
static LEADING_AGGREGATE_CALL_PATTERN = /^(COUNT|SUM|AVG|MIN|MAX|argMax|argMin|quantile|stddevSamp|varSamp|uniq)\s*\(/i;
|
|
5
5
|
constructor(builder) {
|
|
6
6
|
this.builder = builder;
|
|
7
7
|
}
|
|
8
8
|
stripTrailingAlias(selection) {
|
|
9
9
|
return selection.replace(AggregationFeature.TRAILING_ALIAS_PATTERN, '').trim();
|
|
10
10
|
}
|
|
11
|
+
/**
|
|
12
|
+
* An aggregate anywhere in the expression (`round(avg(x), 2)`) or a window
|
|
13
|
+
* function makes it invalid as a GROUP BY key, not only a leading call.
|
|
14
|
+
*/
|
|
11
15
|
isAggregateSelection(selection) {
|
|
12
|
-
|
|
13
|
-
return AggregationFeature.LEADING_AGGREGATE_CALL_PATTERN.test(expressionWithoutAlias);
|
|
16
|
+
return isAggregateOrWindowExpression(this.stripTrailingAlias(selection));
|
|
14
17
|
}
|
|
15
18
|
createAggregateSelection(selection) {
|
|
16
19
|
return {
|
|
@@ -101,4 +104,15 @@ export class AggregationFeature {
|
|
|
101
104
|
approxCountDistinct(column, alias) {
|
|
102
105
|
return this.appendAggregateSelection(`uniq(${column}) AS ${alias}`);
|
|
103
106
|
}
|
|
107
|
+
mergeState(stateFunction, column, alias) {
|
|
108
|
+
assertStateFunctionName(stateFunction);
|
|
109
|
+
return this.appendAggregateSelection(`${stateFunction}Merge(${column}) AS ${alias}`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const STATE_FUNCTION_NAME = /^[A-Za-z][A-Za-z0-9]*$/;
|
|
113
|
+
/** The name is spliced into SQL as `<name>Merge`, so it must be a bare function name. */
|
|
114
|
+
function assertStateFunctionName(stateFunction) {
|
|
115
|
+
if (typeof stateFunction !== 'string' || !STATE_FUNCTION_NAME.test(stateFunction) || /(Merge|State|If|OrNull|OrDefault)$/.test(stateFunction)) {
|
|
116
|
+
throw new Error(`Invalid aggregate state function "${String(stateFunction)}": expected a bare function name such as "uniq" or "sum".`);
|
|
117
|
+
}
|
|
104
118
|
}
|
|
@@ -16,9 +16,13 @@ export declare class ExecutorFeature<Schema extends SchemaDefinition<Schema>, St
|
|
|
16
16
|
sql: string;
|
|
17
17
|
parameters: any[];
|
|
18
18
|
};
|
|
19
|
+
toSQLWithPlaceholders(placeholder: (value: unknown) => string): string;
|
|
19
20
|
toSQL(): string;
|
|
20
21
|
execute(options?: ExecutorRunOptions): Promise<State['output'][]>;
|
|
21
22
|
stream(options?: ExecutorStreamOptions): Promise<ReadableStream<State['output'][]>>;
|
|
23
|
+
streamArrow(options?: ExecutorStreamOptions): Promise<ReadableStream<Uint8Array>>;
|
|
24
|
+
/** Opens a result stream with the builder's settings, logging the request. */
|
|
25
|
+
private openStream;
|
|
22
26
|
private compileQuery;
|
|
23
27
|
}
|
|
24
28
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"executor.d.ts","sourceRoot":"","sources":["../../../src/core/features/executor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAChF,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAU,KAAK,QAAQ,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"executor.d.ts","sourceRoot":"","sources":["../../../src/core/features/executor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAChF,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAU,KAAK,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAM3D,UAAU,kBAAkB;IAC1B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,UAAU,CAAC,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;CAChC;AAED,UAAU,qBAAqB;IAC7B,WAAW,CAAC,EAAE,WAAW,CAAC;CAC3B;AAED,qBAAa,eAAe,CAC1B,MAAM,SAAS,gBAAgB,CAAC,MAAM,CAAC,EACvC,KAAK,SAAS,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,MAAM,CAAC,CAAC,EAAE,GAAG,EAAE,GAAG,CAAC;IAElG,OAAO,CAAC,OAAO;gBAAP,OAAO,EAAE,YAAY,CAAC,MAAM,EAAE,KAAK,CAAC;IAExD,eAAe,IAAI;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,GAAG,EAAE,CAAA;KAAE;IAKrD,qBAAqB,CAAC,WAAW,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,MAAM,GAAG,MAAM;IAatE,KAAK,IAAI,MAAM;IAMT,OAAO,CAAC,OAAO,CAAC,EAAE,kBAAkB,GAAG,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC;IAsDjE,MAAM,CAAC,OAAO,CAAC,EAAE,qBAAqB,GAAG,OAAO,CAAC,cAAc,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IAYnF,WAAW,CAAC,OAAO,CAAC,EAAE,qBAAqB,GAAG,OAAO,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC;IAYvF,8EAA8E;YAChE,UAAU;IAiDxB,OAAO,CAAC,YAAY;CAMrB"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { logger } from '../utils/logger.js';
|
|
2
2
|
import { substituteParameters } from '../utils.js';
|
|
3
|
+
import { splitSqlPlaceholders } from '../utils/sql-parens.js';
|
|
3
4
|
export class ExecutorFeature {
|
|
4
5
|
builder;
|
|
5
6
|
constructor(builder) {
|
|
@@ -9,6 +10,18 @@ export class ExecutorFeature {
|
|
|
9
10
|
const compiled = this.compileQuery();
|
|
10
11
|
return { sql: compiled.query, parameters: [...compiled.parameters] };
|
|
11
12
|
}
|
|
13
|
+
toSQLWithPlaceholders(placeholder) {
|
|
14
|
+
const { sql, parameters } = this.toSQLWithParams();
|
|
15
|
+
const parts = splitSqlPlaceholders(sql);
|
|
16
|
+
if (parts.length - 1 !== parameters.length) {
|
|
17
|
+
throw new Error(`Mismatch between placeholders and parameters. Found ${parts.length - 1} placeholders but ${parameters.length} parameters.`);
|
|
18
|
+
}
|
|
19
|
+
let rendered = parts[0];
|
|
20
|
+
for (let i = 0; i < parameters.length; i++) {
|
|
21
|
+
rendered += placeholder(parameters[i]) + parts[i + 1];
|
|
22
|
+
}
|
|
23
|
+
return rendered;
|
|
24
|
+
}
|
|
12
25
|
toSQL() {
|
|
13
26
|
const { sql, parameters } = this.toSQLWithParams();
|
|
14
27
|
const adapter = this.builder.getAdapter();
|
|
@@ -65,6 +78,29 @@ export class ExecutorFeature {
|
|
|
65
78
|
}
|
|
66
79
|
}
|
|
67
80
|
async stream(options) {
|
|
81
|
+
return this.openStream((adapter, sql, parameters, clickhouseSettings) => {
|
|
82
|
+
if (!adapter.stream) {
|
|
83
|
+
throw new Error(`Streaming is not supported by adapter "${adapter.name}".`);
|
|
84
|
+
}
|
|
85
|
+
return adapter.stream(sql, parameters, {
|
|
86
|
+
clickhouseSettings,
|
|
87
|
+
abortSignal: options?.abortSignal,
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
async streamArrow(options) {
|
|
92
|
+
return this.openStream((adapter, sql, parameters, clickhouseSettings) => {
|
|
93
|
+
if (!adapter.streamArrow) {
|
|
94
|
+
throw new Error(`Arrow streaming is not supported by adapter "${adapter.name}".`);
|
|
95
|
+
}
|
|
96
|
+
return adapter.streamArrow(sql, parameters, {
|
|
97
|
+
clickhouseSettings,
|
|
98
|
+
abortSignal: options?.abortSignal,
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
/** Opens a result stream with the builder's settings, logging the request. */
|
|
103
|
+
async openStream(open) {
|
|
68
104
|
const adapter = this.builder.getAdapter();
|
|
69
105
|
const queryNode = this.builder.toQueryNode();
|
|
70
106
|
const { sql, parameters } = this.toSQLWithParams();
|
|
@@ -76,13 +112,7 @@ export class ExecutorFeature {
|
|
|
76
112
|
status: 'started'
|
|
77
113
|
});
|
|
78
114
|
try {
|
|
79
|
-
|
|
80
|
-
throw new Error(`Streaming is not supported by adapter "${adapter.name}".`);
|
|
81
|
-
}
|
|
82
|
-
const webStream = await adapter.stream(sql, parameters, {
|
|
83
|
-
clickhouseSettings: queryNode.settings,
|
|
84
|
-
abortSignal: options?.abortSignal,
|
|
85
|
-
});
|
|
115
|
+
const stream = await open(adapter, sql, parameters, queryNode.settings);
|
|
86
116
|
const endTime = Date.now();
|
|
87
117
|
logger.logQuery({
|
|
88
118
|
query: sql,
|
|
@@ -92,7 +122,7 @@ export class ExecutorFeature {
|
|
|
92
122
|
duration: endTime - startTime,
|
|
93
123
|
status: 'completed'
|
|
94
124
|
});
|
|
95
|
-
return
|
|
125
|
+
return stream;
|
|
96
126
|
}
|
|
97
127
|
catch (error) {
|
|
98
128
|
const endTime = Date.now();
|