@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
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { abortReason } from './abort.js';
|
|
2
|
+
import { openStreamReader } from './streaming-helpers.js';
|
|
3
|
+
import { EXCEPTION_BLOCK_MAX_BYTES, endsWith, findUntaggedStreamException, splitStreamException, } from './stream-exception.js';
|
|
4
|
+
/**
|
|
5
|
+
* Exposes a raw response body (Arrow IPC, Parquet, …) as a web byte stream.
|
|
6
|
+
*
|
|
7
|
+
* With an exception tag, the last {@link EXCEPTION_BLOCK_MAX_BYTES} bytes are
|
|
8
|
+
* held until the body ends, so a mid-stream ClickHouse failure reaches the
|
|
9
|
+
* consumer as ClickHouse's own error rather than as the exception text spliced
|
|
10
|
+
* into the data, which a decoder would misreport or, worse, partly accept.
|
|
11
|
+
* With an end marker, the same held bytes show whether the body is complete.
|
|
12
|
+
*/
|
|
13
|
+
export function createByteStream(source, options = {}) {
|
|
14
|
+
const { exceptionTag, endMarker, abortSignal } = options;
|
|
15
|
+
const holdsTail = exceptionTag !== undefined || endMarker !== undefined;
|
|
16
|
+
let readerPromise;
|
|
17
|
+
let onAbort;
|
|
18
|
+
let held = new Uint8Array(0);
|
|
19
|
+
const ensureReader = () => (readerPromise ??= openStreamReader(source));
|
|
20
|
+
const fail = (controller, failure) => {
|
|
21
|
+
if (failure.data.length)
|
|
22
|
+
controller.enqueue(failure.data);
|
|
23
|
+
controller.error(failure.error);
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* ClickHouse's error in a body known to be cut short: the tagged block, or,
|
|
27
|
+
* from servers before 25.11, the plain exception text they append.
|
|
28
|
+
*/
|
|
29
|
+
const exceptionInCutBody = () => {
|
|
30
|
+
if (exceptionTag)
|
|
31
|
+
return splitStreamException(held, exceptionTag);
|
|
32
|
+
return endMarker ? findUntaggedStreamException(held) : undefined;
|
|
33
|
+
};
|
|
34
|
+
/** Why a body that has ended is not a complete result, if it is not. */
|
|
35
|
+
const incompleteBody = () => {
|
|
36
|
+
const tagged = exceptionTag ? splitStreamException(held, exceptionTag) : undefined;
|
|
37
|
+
if (tagged || !endMarker || endsWith(held, endMarker))
|
|
38
|
+
return tagged;
|
|
39
|
+
const untagged = exceptionTag ? undefined : findUntaggedStreamException(held);
|
|
40
|
+
return untagged ?? {
|
|
41
|
+
data: held,
|
|
42
|
+
error: new Error('The ClickHouse response ended before the end of the result, so the result is incomplete.' +
|
|
43
|
+
(exceptionTag ? '' : ' ClickHouse may have failed mid-stream; servers before 25.11 do not report why.')),
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
const detachAbort = () => {
|
|
47
|
+
if (onAbort) {
|
|
48
|
+
abortSignal?.removeEventListener('abort', onAbort);
|
|
49
|
+
onAbort = undefined;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
return new ReadableStream({
|
|
53
|
+
start(controller) {
|
|
54
|
+
if (!abortSignal)
|
|
55
|
+
return;
|
|
56
|
+
// The clients stop honoring the signal once headers arrive, so aborting
|
|
57
|
+
// must both error the consumer and drop the source.
|
|
58
|
+
const abort = () => {
|
|
59
|
+
detachAbort();
|
|
60
|
+
controller.error(abortReason(abortSignal));
|
|
61
|
+
ensureReader().then(reader => reader.close()).catch(() => undefined);
|
|
62
|
+
};
|
|
63
|
+
if (abortSignal.aborted) {
|
|
64
|
+
abort();
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
onAbort = abort;
|
|
68
|
+
abortSignal.addEventListener('abort', onAbort, { once: true });
|
|
69
|
+
},
|
|
70
|
+
async pull(controller) {
|
|
71
|
+
try {
|
|
72
|
+
const reader = await ensureReader();
|
|
73
|
+
// A pull that enqueues nothing is not called again, so read until
|
|
74
|
+
// there is something to release or the body has ended.
|
|
75
|
+
for (;;) {
|
|
76
|
+
const { done, value } = await reader.readNext();
|
|
77
|
+
if (done) {
|
|
78
|
+
detachAbort();
|
|
79
|
+
const failure = incompleteBody();
|
|
80
|
+
if (failure) {
|
|
81
|
+
fail(controller, failure);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (held.length)
|
|
85
|
+
controller.enqueue(held);
|
|
86
|
+
controller.close();
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const bytes = toBytes(value);
|
|
90
|
+
if (!holdsTail) {
|
|
91
|
+
if (!bytes.length)
|
|
92
|
+
continue;
|
|
93
|
+
controller.enqueue(bytes);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const joined = concat(held, bytes);
|
|
97
|
+
const release = joined.length - EXCEPTION_BLOCK_MAX_BYTES;
|
|
98
|
+
if (release <= 0) {
|
|
99
|
+
held = joined;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
controller.enqueue(joined.subarray(0, release));
|
|
103
|
+
held = joined.slice(release);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
detachAbort();
|
|
109
|
+
const reader = await ensureReader();
|
|
110
|
+
await reader.close();
|
|
111
|
+
// ClickHouse resets the connection right after the exception block,
|
|
112
|
+
// so the read fails with a bare socket error once the block is in.
|
|
113
|
+
const exception = abortSignal?.aborted ? undefined : exceptionInCutBody();
|
|
114
|
+
if (exception) {
|
|
115
|
+
fail(controller, exception);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
async cancel() {
|
|
122
|
+
detachAbort();
|
|
123
|
+
const reader = await ensureReader();
|
|
124
|
+
await reader.close();
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
function toBytes(chunk) {
|
|
129
|
+
if (chunk instanceof Uint8Array)
|
|
130
|
+
return chunk;
|
|
131
|
+
if (chunk instanceof ArrayBuffer)
|
|
132
|
+
return new Uint8Array(chunk);
|
|
133
|
+
if (typeof chunk === 'string')
|
|
134
|
+
return new TextEncoder().encode(chunk);
|
|
135
|
+
throw new TypeError('Expected a byte chunk from the response stream.');
|
|
136
|
+
}
|
|
137
|
+
function concat(a, b) {
|
|
138
|
+
if (!a.length)
|
|
139
|
+
return b;
|
|
140
|
+
const joined = new Uint8Array(a.length + b.length);
|
|
141
|
+
joined.set(a, 0);
|
|
142
|
+
joined.set(b, a.length);
|
|
143
|
+
return joined;
|
|
144
|
+
}
|
|
@@ -7,7 +7,7 @@ export function createClickHouseClient(config) {
|
|
|
7
7
|
return config.client;
|
|
8
8
|
}
|
|
9
9
|
const clientModule = getAutoClientModule();
|
|
10
|
-
const { integerJsonEncoding:
|
|
10
|
+
const { integerJsonEncoding: _integerOption, arrowCompression: _arrowOption, ...clientConfig } = config;
|
|
11
11
|
return clientModule.createClient(toNodeClientConfig(clientConfig));
|
|
12
12
|
}
|
|
13
13
|
export function deriveClickHouseNamespace(config) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compound-parameter.d.ts","sourceRoot":"","sources":["../../../src/core/utils/compound-parameter.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"compound-parameter.d.ts","sourceRoot":"","sources":["../../../src/core/utils/compound-parameter.ts"],"names":[],"mappings":"AAqDA;;;GAGG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAQhF"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { escapeValue } from '../utils.js';
|
|
2
|
+
import { dateToSqlText } from './date-literal.js';
|
|
2
3
|
import { parseParameterType, parseTupleField } from './parameter-type.js';
|
|
3
4
|
import { isPlainRecord } from './record-guards.js';
|
|
4
5
|
function invalidValue(type) {
|
|
@@ -43,7 +44,10 @@ function serializeValue(value, type) {
|
|
|
43
44
|
return invalidValue(type);
|
|
44
45
|
return `{${entries.map(([key, item]) => `${serializeValue(key, args[0])}:${serializeValue(item, args[1])}`).join(',')}}`;
|
|
45
46
|
}
|
|
46
|
-
|
|
47
|
+
// Elements are parsed as text inside CAST, where an ISO string is read correctly.
|
|
48
|
+
if (value instanceof Date)
|
|
49
|
+
return dateToSqlText(value);
|
|
50
|
+
if (typeof value === 'object')
|
|
47
51
|
return invalidValue(type);
|
|
48
52
|
// UInt64 values and object-map keys may be strings; text readers for numeric
|
|
49
53
|
// types expect unquoted numbers. Validate before including them in the text.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQL renderings of a JavaScript `Date`, which is a UTC instant.
|
|
3
|
+
*
|
|
4
|
+
* A quoted ISO string (`'2026-03-01T00:00:00.000Z'`) or any string with
|
|
5
|
+
* fractional seconds is rejected with TYPE_MISMATCH when ClickHouse compares it
|
|
6
|
+
* with a `DateTime` sorting-key column, and a bare `'2026-03-01 00:00:00'` is
|
|
7
|
+
* read in the server's time zone, not UTC. A conversion function with an
|
|
8
|
+
* explicit `'UTC'` avoids both and still lets the primary index prune.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* A ClickHouse expression for the instant. Whole seconds in the `DateTime`
|
|
12
|
+
* range render as `toDateTime(..., 'UTC')`, so they also fit `IN` lists over
|
|
13
|
+
* `DateTime` columns; anything else renders as `toDateTime64(..., 3, 'UTC')`.
|
|
14
|
+
*/
|
|
15
|
+
export declare function dateToSqlExpression(value: Date): string;
|
|
16
|
+
/**
|
|
17
|
+
* The instant as a quoted ISO 8601 string, for values ClickHouse parses as
|
|
18
|
+
* text (the elements of an array, tuple or map parameter read through CAST).
|
|
19
|
+
*/
|
|
20
|
+
export declare function dateToSqlText(value: Date): string;
|
|
21
|
+
//# sourceMappingURL=date-literal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"date-literal.d.ts","sourceRoot":"","sources":["../../../src/core/utils/date-literal.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAgBH;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,IAAI,GAAG,MAAM,CAQvD;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,IAAI,GAAG,MAAM,CAGjD"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQL renderings of a JavaScript `Date`, which is a UTC instant.
|
|
3
|
+
*
|
|
4
|
+
* A quoted ISO string (`'2026-03-01T00:00:00.000Z'`) or any string with
|
|
5
|
+
* fractional seconds is rejected with TYPE_MISMATCH when ClickHouse compares it
|
|
6
|
+
* with a `DateTime` sorting-key column, and a bare `'2026-03-01 00:00:00'` is
|
|
7
|
+
* read in the server's time zone, not UTC. A conversion function with an
|
|
8
|
+
* explicit `'UTC'` avoids both and still lets the primary index prune.
|
|
9
|
+
*/
|
|
10
|
+
// DateTime covers 1970-01-01 00:00:00 to 2106-02-07 06:28:15 UTC.
|
|
11
|
+
const DATETIME_MAX_MS = 4294967295 * 1000;
|
|
12
|
+
function assertValidDate(value) {
|
|
13
|
+
if (!Number.isFinite(value.getTime())) {
|
|
14
|
+
throw new Error('Cannot render an invalid Date as a SQL parameter');
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** `2026-03-01T00:00:00.123Z` → `2026-03-01 00:00:00.123` (UTC). */
|
|
18
|
+
function utcText(value) {
|
|
19
|
+
return value.toISOString().replace('T', ' ').replace('Z', '');
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* A ClickHouse expression for the instant. Whole seconds in the `DateTime`
|
|
23
|
+
* range render as `toDateTime(..., 'UTC')`, so they also fit `IN` lists over
|
|
24
|
+
* `DateTime` columns; anything else renders as `toDateTime64(..., 3, 'UTC')`.
|
|
25
|
+
*/
|
|
26
|
+
export function dateToSqlExpression(value) {
|
|
27
|
+
assertValidDate(value);
|
|
28
|
+
const ms = value.getTime();
|
|
29
|
+
const text = utcText(value);
|
|
30
|
+
if (ms % 1000 === 0 && ms >= 0 && ms <= DATETIME_MAX_MS) {
|
|
31
|
+
return `toDateTime('${text.slice(0, 19)}', 'UTC')`;
|
|
32
|
+
}
|
|
33
|
+
return `toDateTime64('${text}', 3, 'UTC')`;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The instant as a quoted ISO 8601 string, for values ClickHouse parses as
|
|
37
|
+
* text (the elements of an array, tuple or map parameter read through CAST).
|
|
38
|
+
*/
|
|
39
|
+
export function dateToSqlText(value) {
|
|
40
|
+
assertValidDate(value);
|
|
41
|
+
return `'${value.toISOString()}'`;
|
|
42
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ends a statement with `FORMAT <format>`, so the format is part of the query
|
|
3
|
+
* rather than a setting a `readonly = 1` user may not change.
|
|
4
|
+
*
|
|
5
|
+
* Trailing semicolons, whitespace and comments are dropped first, because a
|
|
6
|
+
* clause appended after them would open a second statement or be commented
|
|
7
|
+
* out. Strings, quoted identifiers, heredocs and nested block comments are
|
|
8
|
+
* skipped as ClickHouse's lexer does, so markers inside them are not mistaken
|
|
9
|
+
* for the end of the statement.
|
|
10
|
+
*
|
|
11
|
+
* A statement that already ends in `FORMAT <format>` is returned trimmed.
|
|
12
|
+
* Nothing else is treated as a format clause: a trailing `format DESC` is a
|
|
13
|
+
* sort on a column named `format`. A statement with a different `FORMAT`
|
|
14
|
+
* clause gets a second one, which ClickHouse rejects as a syntax error.
|
|
15
|
+
*/
|
|
16
|
+
export declare function withFormatClause(sql: string, format: string): string;
|
|
17
|
+
//# sourceMappingURL=format-clause.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format-clause.d.ts","sourceRoot":"","sources":["../../../src/core/utils/format-clause.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAMpE"}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ends a statement with `FORMAT <format>`, so the format is part of the query
|
|
3
|
+
* rather than a setting a `readonly = 1` user may not change.
|
|
4
|
+
*
|
|
5
|
+
* Trailing semicolons, whitespace and comments are dropped first, because a
|
|
6
|
+
* clause appended after them would open a second statement or be commented
|
|
7
|
+
* out. Strings, quoted identifiers, heredocs and nested block comments are
|
|
8
|
+
* skipped as ClickHouse's lexer does, so markers inside them are not mistaken
|
|
9
|
+
* for the end of the statement.
|
|
10
|
+
*
|
|
11
|
+
* A statement that already ends in `FORMAT <format>` is returned trimmed.
|
|
12
|
+
* Nothing else is treated as a format clause: a trailing `format DESC` is a
|
|
13
|
+
* sort on a column named `format`. A statement with a different `FORMAT`
|
|
14
|
+
* clause gets a second one, which ClickHouse rejects as a syntax error.
|
|
15
|
+
*/
|
|
16
|
+
export function withFormatClause(sql, format) {
|
|
17
|
+
const { end, trailingFormat } = scanStatementEnd(sql);
|
|
18
|
+
const statement = sql.slice(0, end);
|
|
19
|
+
if (trailingFormat?.toLowerCase() === format.toLowerCase())
|
|
20
|
+
return statement;
|
|
21
|
+
return `${statement}\nFORMAT ${format}`;
|
|
22
|
+
}
|
|
23
|
+
function scanStatementEnd(sql) {
|
|
24
|
+
let end = 0;
|
|
25
|
+
let last;
|
|
26
|
+
let beforeLast;
|
|
27
|
+
const significant = (token, tokenEnd) => {
|
|
28
|
+
beforeLast = last;
|
|
29
|
+
last = token;
|
|
30
|
+
end = tokenEnd;
|
|
31
|
+
};
|
|
32
|
+
let i = 0;
|
|
33
|
+
while (i < sql.length) {
|
|
34
|
+
const char = sql[i];
|
|
35
|
+
const next = sql[i + 1];
|
|
36
|
+
if (/\s/.test(char) || char === ';') {
|
|
37
|
+
i++;
|
|
38
|
+
}
|
|
39
|
+
else if ((char === '-' && next === '-') || (char === '#' && (next === ' ' || next === '!'))) {
|
|
40
|
+
const newline = sql.indexOf('\n', i);
|
|
41
|
+
i = newline < 0 ? sql.length : newline + 1;
|
|
42
|
+
}
|
|
43
|
+
else if (char === '/' && next === '*') {
|
|
44
|
+
const close = blockCommentEnd(sql, i);
|
|
45
|
+
// An unclosed comment is left for ClickHouse to report.
|
|
46
|
+
if (close < 0) {
|
|
47
|
+
significant({ kind: 'other', text: sql.slice(i) }, sql.length);
|
|
48
|
+
i = sql.length;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
i = close;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
else if (char === "'" || char === '"' || char === '`') {
|
|
55
|
+
const close = quotedEnd(sql, i);
|
|
56
|
+
const text = sql.slice(i + 1, close - 1);
|
|
57
|
+
significant({ kind: char === "'" ? 'other' : 'quoted-identifier', text }, close);
|
|
58
|
+
i = close;
|
|
59
|
+
}
|
|
60
|
+
else if (char === '$' && heredocEnd(sql, i) > 0) {
|
|
61
|
+
const close = heredocEnd(sql, i);
|
|
62
|
+
significant({ kind: 'other', text: sql.slice(i, close) }, close);
|
|
63
|
+
i = close;
|
|
64
|
+
}
|
|
65
|
+
else if (isWordChar(char)) {
|
|
66
|
+
let close = i + 1;
|
|
67
|
+
while (close < sql.length && isWordChar(sql[close]))
|
|
68
|
+
close++;
|
|
69
|
+
significant({ kind: 'word', text: sql.slice(i, close) }, close);
|
|
70
|
+
i = close;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
significant({ kind: 'other', text: char }, i + 1);
|
|
74
|
+
i++;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const endsInFormat = beforeLast?.kind === 'word'
|
|
78
|
+
&& beforeLast.text.toUpperCase() === 'FORMAT'
|
|
79
|
+
&& (last?.kind === 'word' || last?.kind === 'quoted-identifier');
|
|
80
|
+
return { end, trailingFormat: endsInFormat ? last.text : undefined };
|
|
81
|
+
}
|
|
82
|
+
function isWordChar(char) {
|
|
83
|
+
return /[A-Za-z0-9_$]/.test(char) || char.charCodeAt(0) > 0x7f;
|
|
84
|
+
}
|
|
85
|
+
/** Index past a nested block comment opened at `start`, or -1 when it is not closed. */
|
|
86
|
+
function blockCommentEnd(sql, start) {
|
|
87
|
+
let depth = 0;
|
|
88
|
+
let i = start;
|
|
89
|
+
while (i + 1 < sql.length) {
|
|
90
|
+
if (sql[i] === '/' && sql[i + 1] === '*') {
|
|
91
|
+
depth++;
|
|
92
|
+
i += 2;
|
|
93
|
+
}
|
|
94
|
+
else if (sql[i] === '*' && sql[i + 1] === '/') {
|
|
95
|
+
depth--;
|
|
96
|
+
i += 2;
|
|
97
|
+
if (depth === 0)
|
|
98
|
+
return i;
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
i++;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return -1;
|
|
105
|
+
}
|
|
106
|
+
/** Index past a quoted string or identifier opened at `start`; the end of `sql` when unclosed. */
|
|
107
|
+
function quotedEnd(sql, start) {
|
|
108
|
+
const quote = sql[start];
|
|
109
|
+
let i = start + 1;
|
|
110
|
+
while (i < sql.length) {
|
|
111
|
+
const char = sql[i];
|
|
112
|
+
if (char === '\\') {
|
|
113
|
+
i += 2;
|
|
114
|
+
}
|
|
115
|
+
else if (char === quote) {
|
|
116
|
+
// A doubled quote is an escaped quote, not the end.
|
|
117
|
+
if (sql[i + 1] === quote) {
|
|
118
|
+
i += 2;
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
return i + 1;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
i++;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return sql.length;
|
|
129
|
+
}
|
|
130
|
+
/** Index past a `$tag$…$tag$` heredoc opened at `start`, or -1 when there is none. */
|
|
131
|
+
function heredocEnd(sql, start) {
|
|
132
|
+
const match = /^\$[A-Za-z0-9_]*\$/.exec(sql.slice(start));
|
|
133
|
+
if (!match)
|
|
134
|
+
return -1;
|
|
135
|
+
const delimiter = match[0];
|
|
136
|
+
const close = sql.indexOf(delimiter, start + delimiter.length);
|
|
137
|
+
return close < 0 ? -1 : close + delimiter.length;
|
|
138
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"integer-json-encoding.d.ts","sourceRoot":"","sources":["../../../src/core/utils/integer-json-encoding.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,oCAAoC,CAAC;AAC7E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"integer-json-encoding.d.ts","sourceRoot":"","sources":["../../../src/core/utils/integer-json-encoding.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,oCAAoC,CAAC;AAC7E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAK/D,MAAM,WAAW,0BAA0B;IACzC,QAAQ,EAAE,kBAAkB,CAAC;IAC7B,qBAAqB,EAAE,OAAO,CAAC;CAChC;AAMD;;;GAGG;AACH,wBAAgB,wBAAwB,CACtC,QAAQ,EAAE,mBAAmB,EAC7B,cAAc,CAAC,EAAE,kBAAkB,EACnC,cAAc,CAAC,EAAE,kBAAkB,GAClC,0BAA0B,CAa5B;AAED;;;;GAIG;AACH,wBAAgB,8BAA8B,CAC5C,KAAK,EAAE,OAAO,EACd,qBAAqB,EAAE,OAAO,GAC7B,KAAK,GAAG,SAAS,CAanB"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isReadonlySettingError } from './readonly-setting-error.js';
|
|
1
2
|
const QUOTE_64BIT = 'output_format_json_quote_64bit_integers';
|
|
2
3
|
function hasQuote64BitSetting(settings) {
|
|
3
4
|
return settings !== undefined && Object.prototype.hasOwnProperty.call(settings, QUOTE_64BIT);
|
|
@@ -25,23 +26,13 @@ export function buildIntegerJsonSettings(encoding, configSettings, optionSetting
|
|
|
25
26
|
* operations, whose original errors must be preserved.
|
|
26
27
|
*/
|
|
27
28
|
export function createReadonlyIntegerJsonError(error, adapterDefaultApplied) {
|
|
28
|
-
if (!adapterDefaultApplied || !
|
|
29
|
+
if (!adapterDefaultApplied || !isReadonlySettingError(error, QUOTE_64BIT))
|
|
29
30
|
return undefined;
|
|
30
31
|
return new Error(`ClickHouse rejected HypeQuery's precision-safe setting (${QUOTE_64BIT}=1) ` +
|
|
31
|
-
`because this connection uses readonly = 1.
|
|
32
|
-
`
|
|
33
|
-
`
|
|
34
|
-
`
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (!error || typeof error !== 'object')
|
|
38
|
-
return false;
|
|
39
|
-
const { code, type } = error;
|
|
40
|
-
const message = error.message;
|
|
41
|
-
if (typeof message !== 'string' || !message.includes(QUOTE_64BIT))
|
|
42
|
-
return false;
|
|
43
|
-
return code === '164'
|
|
44
|
-
|| code === 164
|
|
45
|
-
|| type === 'READONLY'
|
|
46
|
-
|| /in readonly mode/i.test(message);
|
|
32
|
+
`because this connection uses readonly = 1, whose profile does not already set it. ` +
|
|
33
|
+
`To keep Int64 and wider values exact, either add ${QUOTE_64BIT} = 1 to the ` +
|
|
34
|
+
`user's settings profile (readonly = 1 accepts a setting that matches the profile) ` +
|
|
35
|
+
`or use readonly = 2. Otherwise set integerJsonEncoding: 'server-default' in the ` +
|
|
36
|
+
`HypeQuery adapter or query-builder configuration to omit it; the server may then ` +
|
|
37
|
+
`return Int64 and wider values as JSON numbers, which lose precision beyond 2^53.`, { cause: error });
|
|
47
38
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether ClickHouse rejected `setting` because the connection uses
|
|
3
|
+
* `readonly = 1`. Code 164 also covers other settings and forbidden
|
|
4
|
+
* operations, so the message must name this setting.
|
|
5
|
+
*/
|
|
6
|
+
export declare function isReadonlySettingError(error: unknown, setting: string): boolean;
|
|
7
|
+
//# sourceMappingURL=readonly-setting-error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"readonly-setting-error.d.ts","sourceRoot":"","sources":["../../../src/core/utils/readonly-setting-error.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAU/E"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether ClickHouse rejected `setting` because the connection uses
|
|
3
|
+
* `readonly = 1`. Code 164 also covers other settings and forbidden
|
|
4
|
+
* operations, so the message must name this setting.
|
|
5
|
+
*/
|
|
6
|
+
export function isReadonlySettingError(error, setting) {
|
|
7
|
+
if (!error || typeof error !== 'object')
|
|
8
|
+
return false;
|
|
9
|
+
const { code, type } = error;
|
|
10
|
+
const message = error.message;
|
|
11
|
+
if (typeof message !== 'string' || !message.includes(setting))
|
|
12
|
+
return false;
|
|
13
|
+
return code === '164'
|
|
14
|
+
|| code === 164
|
|
15
|
+
|| type === 'READONLY'
|
|
16
|
+
|| /in readonly mode/i.test(message);
|
|
17
|
+
}
|
|
@@ -4,6 +4,11 @@
|
|
|
4
4
|
* Scanners use it so markers inside such a region stay data.
|
|
5
5
|
*/
|
|
6
6
|
export declare function skipNonCode(sql: string, start: number): number | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* The SQL with every non-code region (literals, quoted identifiers, heredocs,
|
|
9
|
+
* comments) replaced by a space, so pattern checks only see SQL code.
|
|
10
|
+
*/
|
|
11
|
+
export declare function maskNonCode(sql: string): string;
|
|
7
12
|
/**
|
|
8
13
|
* Split on positional parameter markers that occur in SQL code. Question marks
|
|
9
14
|
* inside literals, quoted identifiers, heredocs, or comments are data, not
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sql-parens.d.ts","sourceRoot":"","sources":["../../../src/core/utils/sql-parens.ts"],"names":[],"mappings":"AAuEA;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAqB1E;AASD;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,CAqB1D;AAED;;;;GAIG;AACH,wBAAgB,4BAA4B,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAchE;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CA8B/D;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAM3D;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAuBzD"}
|
|
1
|
+
{"version":3,"file":"sql-parens.d.ts","sourceRoot":"","sources":["../../../src/core/utils/sql-parens.ts"],"names":[],"mappings":"AAuEA;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAqB1E;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAc/C;AASD;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,CAqB1D;AAED;;;;GAIG;AACH,wBAAgB,4BAA4B,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAchE;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CA8B/D;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAM3D;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAuBzD"}
|
|
@@ -92,6 +92,25 @@ export function skipNonCode(sql, start) {
|
|
|
92
92
|
}
|
|
93
93
|
return undefined;
|
|
94
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* The SQL with every non-code region (literals, quoted identifiers, heredocs,
|
|
97
|
+
* comments) replaced by a space, so pattern checks only see SQL code.
|
|
98
|
+
*/
|
|
99
|
+
export function maskNonCode(sql) {
|
|
100
|
+
let code = '';
|
|
101
|
+
let i = 0;
|
|
102
|
+
while (i < sql.length) {
|
|
103
|
+
const skippedTo = skipNonCode(sql, i);
|
|
104
|
+
if (skippedTo !== undefined) {
|
|
105
|
+
code += ' ';
|
|
106
|
+
i = skippedTo;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
code += sql[i];
|
|
110
|
+
i += 1;
|
|
111
|
+
}
|
|
112
|
+
return code;
|
|
113
|
+
}
|
|
95
114
|
function matchingCloser(ch) {
|
|
96
115
|
if (ch === '(')
|
|
97
116
|
return ')';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The header ClickHouse (25.11+) sends before any data, naming the tag it will
|
|
3
|
+
* wrap a mid-stream exception in.
|
|
4
|
+
*/
|
|
5
|
+
export declare const EXCEPTION_TAG_HEADER = "x-clickhouse-exception-tag";
|
|
6
|
+
/**
|
|
7
|
+
* Upper bound on the exception block a byte stream holds back. ClickHouse's
|
|
8
|
+
* messages are a few hundred bytes; a longer one still fails the stream, only
|
|
9
|
+
* without its text.
|
|
10
|
+
*/
|
|
11
|
+
export declare const EXCEPTION_BLOCK_MAX_BYTES: number;
|
|
12
|
+
export interface StreamException {
|
|
13
|
+
/** The bytes before the exception block: the part of the result that was sent. */
|
|
14
|
+
data: Uint8Array;
|
|
15
|
+
error: Error;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Splits a failed response's trailing exception block from the data before it.
|
|
19
|
+
*
|
|
20
|
+
* ClickHouse cannot change the status once data has been sent, so it ends the
|
|
21
|
+
* body with:
|
|
22
|
+
*
|
|
23
|
+
* \r\n__exception__\r\n<tag>\r\n<message>\n<length> <tag>\r\n__exception__\r\n
|
|
24
|
+
*
|
|
25
|
+
* Returns `undefined` when `bytes` does not end that way, so a body that merely
|
|
26
|
+
* contains the marker is never mistaken for a failure.
|
|
27
|
+
*/
|
|
28
|
+
export declare function splitStreamException(bytes: Uint8Array, tag: string): StreamException | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Finds the plain exception text a ClickHouse server older than 25.11 appends
|
|
31
|
+
* when a query fails after data was sent. Those servers mark nothing, so this
|
|
32
|
+
* is only meaningful once the body is already known to be incomplete: binary
|
|
33
|
+
* data could contain the same text by chance.
|
|
34
|
+
*/
|
|
35
|
+
export declare function findUntaggedStreamException(bytes: Uint8Array): StreamException | undefined;
|
|
36
|
+
export declare function endsWith(bytes: Uint8Array, suffix: Uint8Array): boolean;
|
|
37
|
+
//# sourceMappingURL=stream-exception.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-exception.d.ts","sourceRoot":"","sources":["../../../src/core/utils/stream-exception.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,eAAO,MAAM,oBAAoB,+BAA+B,CAAC;AAEjE;;;;GAIG;AACH,eAAO,MAAM,yBAAyB,QAAY,CAAC;AAQnD,MAAM,WAAW,eAAe;IAC9B,kFAAkF;IAClF,IAAI,EAAE,UAAU,CAAC;IACjB,KAAK,EAAE,KAAK,CAAC;CACd;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,UAAU,EACjB,GAAG,EAAE,MAAM,GACV,eAAe,GAAG,SAAS,CAoB7B;AAED;;;;;GAKG;AACH,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,UAAU,GAAG,eAAe,GAAG,SAAS,CAS1F;AAED,wBAAgB,QAAQ,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU,GAAG,OAAO,CAOvE"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { parseError } from '@clickhouse/client-common';
|
|
2
|
+
/**
|
|
3
|
+
* The header ClickHouse (25.11+) sends before any data, naming the tag it will
|
|
4
|
+
* wrap a mid-stream exception in.
|
|
5
|
+
*/
|
|
6
|
+
export const EXCEPTION_TAG_HEADER = 'x-clickhouse-exception-tag';
|
|
7
|
+
/**
|
|
8
|
+
* Upper bound on the exception block a byte stream holds back. ClickHouse's
|
|
9
|
+
* messages are a few hundred bytes; a longer one still fails the stream, only
|
|
10
|
+
* without its text.
|
|
11
|
+
*/
|
|
12
|
+
export const EXCEPTION_BLOCK_MAX_BYTES = 16 * 1024;
|
|
13
|
+
/** The start of the exception text servers before 25.11 append to a failed body. */
|
|
14
|
+
const UNTAGGED_EXCEPTION_RE = /Code: \d+\. DB::Exception: /g;
|
|
15
|
+
const encoder = new TextEncoder();
|
|
16
|
+
const decoder = new TextDecoder('utf-8');
|
|
17
|
+
/**
|
|
18
|
+
* Splits a failed response's trailing exception block from the data before it.
|
|
19
|
+
*
|
|
20
|
+
* ClickHouse cannot change the status once data has been sent, so it ends the
|
|
21
|
+
* body with:
|
|
22
|
+
*
|
|
23
|
+
* \r\n__exception__\r\n<tag>\r\n<message>\n<length> <tag>\r\n__exception__\r\n
|
|
24
|
+
*
|
|
25
|
+
* Returns `undefined` when `bytes` does not end that way, so a body that merely
|
|
26
|
+
* contains the marker is never mistaken for a failure.
|
|
27
|
+
*/
|
|
28
|
+
export function splitStreamException(bytes, tag) {
|
|
29
|
+
const end = encoder.encode(` ${tag}\r\n__exception__\r\n`);
|
|
30
|
+
if (!endsWith(bytes, end)) {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
const start = encoder.encode(`\r\n__exception__\r\n${tag}\r\n`);
|
|
34
|
+
const startAt = lastIndexOf(bytes, start);
|
|
35
|
+
if (startAt < 0) {
|
|
36
|
+
return {
|
|
37
|
+
data: new Uint8Array(0),
|
|
38
|
+
error: new Error('ClickHouse failed mid-stream; its error message was too long to recover.'),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
// "<message>\n<length>": the length hint follows the message's last newline.
|
|
42
|
+
const body = decoder.decode(bytes.subarray(startAt + start.length, bytes.length - end.length));
|
|
43
|
+
const hintAt = body.lastIndexOf('\n');
|
|
44
|
+
const message = (hintAt >= 0 ? body.slice(0, hintAt) : body).trim();
|
|
45
|
+
return { data: bytes.subarray(0, startAt), error: parseError(message) };
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Finds the plain exception text a ClickHouse server older than 25.11 appends
|
|
49
|
+
* when a query fails after data was sent. Those servers mark nothing, so this
|
|
50
|
+
* is only meaningful once the body is already known to be incomplete: binary
|
|
51
|
+
* data could contain the same text by chance.
|
|
52
|
+
*/
|
|
53
|
+
export function findUntaggedStreamException(bytes) {
|
|
54
|
+
// windows-1252 decodes one character per byte, so indices stay byte offsets.
|
|
55
|
+
const text = new TextDecoder('windows-1252').decode(bytes);
|
|
56
|
+
let startAt = -1;
|
|
57
|
+
for (const match of text.matchAll(UNTAGGED_EXCEPTION_RE))
|
|
58
|
+
startAt = match.index;
|
|
59
|
+
if (startAt < 0)
|
|
60
|
+
return undefined;
|
|
61
|
+
const message = decoder.decode(bytes.subarray(startAt)).trim();
|
|
62
|
+
return { data: bytes.subarray(0, startAt), error: parseError(message) };
|
|
63
|
+
}
|
|
64
|
+
export function endsWith(bytes, suffix) {
|
|
65
|
+
if (bytes.length < suffix.length)
|
|
66
|
+
return false;
|
|
67
|
+
const offset = bytes.length - suffix.length;
|
|
68
|
+
for (let i = 0; i < suffix.length; i++) {
|
|
69
|
+
if (bytes[offset + i] !== suffix[i])
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
function lastIndexOf(bytes, needle) {
|
|
75
|
+
outer: for (let at = bytes.length - needle.length; at >= 0; at--) {
|
|
76
|
+
for (let i = 0; i < needle.length; i++) {
|
|
77
|
+
if (bytes[at + i] !== needle[i])
|
|
78
|
+
continue outer;
|
|
79
|
+
}
|
|
80
|
+
return at;
|
|
81
|
+
}
|
|
82
|
+
return -1;
|
|
83
|
+
}
|