-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathresult.ts
More file actions
180 lines (167 loc) · 5.82 KB
/
Copy pathresult.ts
File metadata and controls
180 lines (167 loc) · 5.82 KB
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
import type {
ProgressRow,
ResponseHeaders,
ResponseJSON,
SpecialEventRow,
} from './clickhouse_types'
import type {
DataFormat,
RawDataFormat,
RecordsJSONFormat,
SingleDocumentJSONFormat,
StreamableDataFormat,
StreamableJSONDataFormat,
} from './data_formatter'
export type RowOrProgress<T> = { row: T } | ProgressRow | SpecialEventRow<T>
export type ResultStream<Format extends DataFormat | unknown, Stream> =
// JSON*EachRow (except JSONObjectEachRow), CSV, TSV etc.
Format extends StreamableDataFormat
? Stream
: // JSON formats represented as an object { data, meta, statistics, ... }
Format extends SingleDocumentJSONFormat
? never
: // JSON formats represented as a Record<string, T>
Format extends RecordsJSONFormat
? never
: // If we fail to infer the literal type, allow getting the stream
Stream
export type ResultJSONType<T, F extends DataFormat | unknown> =
// Emits either a { row: T } or an object with progress
F extends 'JSONEachRowWithProgress'
? RowOrProgress<T>[]
: // JSON*EachRow formats except JSONObjectEachRow
F extends StreamableJSONDataFormat
? T[]
: // JSON formats with known layout { data, meta, statistics, ... }
F extends SingleDocumentJSONFormat
? ResponseJSON<T>
: // JSON formats represented as a Record<string, T>
F extends RecordsJSONFormat
? Record<string, T>
: // CSV, TSV, etc. - cannot be represented as JSON
F extends RawDataFormat
? never
: // happens only when Format could not be inferred from a literal
T[] | Record<string, T> | ResponseJSON<T>
export type RowJSONType<T, F extends DataFormat | unknown> =
// Emits either a { row: T } or an object with progress
F extends 'JSONEachRowWithProgress'
? RowOrProgress<T>
: // JSON*EachRow formats
F extends StreamableJSONDataFormat
? T
: // CSV, TSV, non-streamable JSON formats - cannot be streamed as JSON
F extends RawDataFormat | SingleDocumentJSONFormat | RecordsJSONFormat
? never
: T // happens only when Format could not be inferred from a literal
export interface Row<
JSONType = unknown,
Format extends DataFormat | unknown = unknown,
> {
/** A string representation of a row. */
text: string
/**
* Returns a JSON representation of a row.
* The method will throw if called on a response in JSON incompatible format.
* It is safe to call this method multiple times.
*/
json<T = JSONType>(): RowJSONType<T, Format>
}
export interface BaseResultSet<
Stream,
Format extends DataFormat | unknown,
RawStream = Stream,
> {
/**
* The method waits for all the rows to be fully loaded
* and returns the result as a string.
*
* It is possible to call this method for all supported formats.
*
* The method should throw if the underlying stream was already consumed
* by calling the other methods.
*/
text(): Promise<string>
/**
* The method waits for the all the rows to be fully loaded.
* When the response is received in full, it will be decoded to return JSON.
*
* Should be called only for JSON* formats family.
*
* The method should throw if the underlying stream was already consumed
* by calling the other methods, or if it is called for non-JSON formats,
* such as CSV, TSV, etc.
*/
json<T = unknown>(): Promise<ResultJSONType<T, Format>>
/**
* Returns a readable stream for responses that can be streamed.
*
* Formats that CAN be streamed ({@link StreamableDataFormat}):
* * JSONEachRow
* * JSONStringsEachRow
* * JSONCompactEachRow
* * JSONCompactStringsEachRow
* * JSONCompactEachRowWithNames
* * JSONCompactEachRowWithNamesAndTypes
* * JSONCompactStringsEachRowWithNames
* * JSONCompactStringsEachRowWithNamesAndTypes
* * CSV
* * CSVWithNames
* * CSVWithNamesAndTypes
* * TabSeparated
* * TabSeparatedRaw
* * TabSeparatedWithNames
* * TabSeparatedWithNamesAndTypes
* * CustomSeparated
* * CustomSeparatedWithNames
* * CustomSeparatedWithNamesAndTypes
* * Parquet
*
* Formats that CANNOT be streamed (the method returns "never" in TS):
* * JSON
* * JSONStrings
* * JSONCompact
* * JSONCompactStrings
* * JSONColumnsWithMetadata
* * JSONObjectEachRow
*
* Every iteration provides an array of {@link Row} instances
* for {@link StreamableDataFormat} format.
*
* Should be called only once.
*
* The method should throw if called on a response in non-streamable format,
* and if the underlying stream was already consumed
* by calling the other methods.
*/
stream(): ResultStream<Format, Stream>
/**
* Returns a readable stream of the raw response body bytes, without splitting
* it into rows.
*
* This is the correct way to consume binary formats such as `Parquet`,
* `ORC`, `Arrow`, `ArrowStream`, or `Native`, where the row-oriented
* {@link stream} method would corrupt the payload (it splits the body on
* newline characters, which are meaningless in binary data and can be
* misinterpreted as row or exception boundaries).
*
* Unlike calling {@link ClickHouseClient.exec} directly, this method still
* detects a mid-stream exception appended by the server and propagates it as
* a stream error.
*
* Each iteration provides a chunk of raw bytes (`Buffer` in Node.js,
* `Uint8Array` in the Web client).
*
* Should be called only once.
*
* The method should throw if the underlying stream was already consumed
* by calling the other methods.
*/
rawStream(): RawStream
/** Close the underlying stream. */
close(): void
/** ClickHouse server QueryID. */
query_id: string
/** Response headers. */
response_headers: ResponseHeaders
}