-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathclickhouse.js
More file actions
388 lines (293 loc) · 10.1 KB
/
Copy pathclickhouse.js
File metadata and controls
388 lines (293 loc) · 10.1 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
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
var http = require ('http');
var https = require('https');
var url = require ('url');
var qs = require ('querystring');
var util = require ('util');
var zlib = require('zlib');
// var debug = require ('debug')('clickhouse');
require ('./legacy-support');
var RecordStream = require ('./streams').RecordStream;
var JSONStream = require ('./streams').JSONStream;
var parseError = require ('./parse-error');
var LIBRARY_SPECIFIC_OPTIONS = require ('./consts').LIBRARY_SPECIFIC_OPTIONS;
function httpResponseHandler (stream, reqParams, reqData, cb, response) {
var str;
var error;
if (response.statusCode === 200) {
str = Buffer.alloc ? Buffer.alloc (0) : new Buffer (0);
} else {
error = Buffer.alloc ? Buffer.alloc (0) : new Buffer (0);
}
function errorHandler (e) {
var err = parseError (e);
// user should define callback or add event listener for the error event
if (!cb || (cb && stream.listeners ('error').length))
stream.emit ('error', err);
return cb && cb (err);
}
// In case of error, we're just throw away data
response.on ('error', errorHandler);
// TODO: use streaming interface
// from https://github.com/jimhigson/oboe.js
// or https://www.npmjs.com/package/stream-json or
// or https://github.com/creationix/jsonparse
// or implement it youself
var jsonParser = new JSONStream (stream);
var symbolsTransferred = 0;
//another chunk of data has been received, so append it to `str`
response.on ('data', function (chunk) {
symbolsTransferred += chunk.length;
// JSON response
if (
response.headers['content-type']
&& response.headers['content-type'].indexOf ('application/json') === 0
&& !reqData.syncParser
&& chunk.lastIndexOf ("\n") !== -1
&& str
) {
// store in buffer anything after
var newLinePos = chunk.lastIndexOf ("\n");
var remains = chunk.slice (newLinePos + 1);
Buffer.concat([str, chunk.slice (0, newLinePos)])
.toString ('utf8')
.split ("\n")
.forEach (jsonParser);
jsonParser.rows.forEach (function (row) {
// write to readable stream
stream.push (row);
});
jsonParser.rows = [];
str = remains;
// plaintext response
} else if (str) {
str = Buffer.concat ([str, chunk]);
} else {
error = Buffer.concat ([error, chunk]);
}
});
//the whole response has been received, so we just print it out here
response.on('end', function () {
// debug (response.headers);
if (error) {
return errorHandler (error);
}
var data;
var contentType = response.headers['content-type'];
// Early return and stream end in case when content-type means empty body
if (response.statusCode === 200 && (
!contentType
|| contentType.indexOf ('text/plain') === 0
|| contentType.indexOf ('text/html') === 0 // WTF: xenial - no content-type, precise - text/html
)) {
// probably this is a ping response or any other successful response with *empty* body
stream.push (null);
cb && cb (null, str.toString ('utf8'));
return;
}
var supplemental = {};
// we already pushed all the data
if (jsonParser.columns.length) {
try {
supplemental = JSON.parse (jsonParser.supplementalString + str.toString ('utf8'));
} catch (e) {
// TODO
}
stream.supplemental = supplemental;
// end stream
stream.push (null);
cb && cb (null, Object.assign ({}, supplemental, {
meta: jsonParser.columns,
transferred: symbolsTransferred
}));
return;
}
// one shot data parsing, should be much faster for smaller datasets
try {
if (response.headers['content-encoding'] === 'gzip') {
str = zlib.gunzipSync(str)
}
data = JSON.parse (str.toString ('utf8'));
data.transferred = symbolsTransferred;
if (data.meta) {
stream.emit ('metadata', data.meta);
}
if (data.data) {
// no highWatermark support
data.data.forEach (function (row) {
stream.push (row);
});
}
} catch (e) {
if (!reqData.format || !reqData.format.match (/^(JSON|JSONCompact)$/)) {
data = str.toString ('utf8');
} else {
return errorHandler (e);
}
} finally {
if (!stream.readableEnded) {
stream.push (null);
cb && cb (null, data);
}
}
});
}
function httpRequest (reqParams, reqData, cb) {
if (reqParams.query) {
reqParams.path = (reqParams.pathname || reqParams.path) + '?' + qs.stringify (reqParams.query);
}
var stream = new RecordStream ({
format: reqData.format
});
var requestInstance = reqParams.protocol === 'https:' ? https : http;
var req = requestInstance.request (reqParams, httpResponseHandler.bind (
this, stream, reqParams, reqData, cb
));
req.on ('error', function (e) {
// user should define callback or add event listener for the error event
if (!cb || (cb && stream.listeners ('error').length))
stream.emit ('error', e);
return cb && cb (e);
});
req.on('timeout', function (e) {
req.abort();
})
stream.req = req;
if (reqData.query)
req.write (reqData.query);
if (reqData.finalized) {
req.end();
}
return stream;
}
function ClickHouse (options) {
if (!options) {
console.error ('You must provide at least host name to query ClickHouse');
return null;
}
if (options.constructor === String) {
options = {host: options};
}
this.options = options;
}
ClickHouse.prototype.getReqParams = function () {
var urlObject = {};
// avoid to set defaults - node http module is not happy
for (var name of Object.keys(this.options)) {
if (!LIBRARY_SPECIFIC_OPTIONS.has(name)) {
urlObject[name] = this.options[name];
}
}
if (this.options.hasOwnProperty('user') || this.options.hasOwnProperty('password')) {
urlObject.auth = (this.options.user || 'default')
+ ':'
+ (this.options.password || '')
}
urlObject.method = 'POST';
urlObject.path = urlObject.path || '/';
urlObject.port = urlObject.port || 8123;
return urlObject;
}
ClickHouse.prototype.query = function (chQuery, options, cb) {
chQuery = chQuery.trim ();
if (cb === undefined && options && options.constructor === Function) {
cb = options;
options = undefined;
}
if (!options)
options = {
queryOptions: {}
};
options.omitFormat = options.omitFormat || this.options.omitFormat || false;
options.dataObjects = options.dataObjects || this.options.dataObjects || false;
options.format = options.format || this.options.format || null;
options.readonly = options.readonly || this.options.readonly || this.options.useQueryString || false;
// we're adding `queryOptions` passed for constructor if any
var queryObject = Object.assign ({}, this.options.queryOptions, options.queryOptions);
var formatRegexp = /FORMAT\s+(BlockTabSeparated|CSV|CSVWithNames|JSON|JSONCompact|JSONEachRow|Native|Null|Pretty|PrettyCompact|PrettyCompactMonoBlock|PrettyNoEscapes|PrettyCompactNoEscapes|PrettySpaceNoEscapes|PrettySpace|RowBinary|TabSeparated|TabSeparatedRaw|TabSeparatedWithNames|TabSeparatedWithNamesAndTypes|TSKV|Values|Vertical|XML)/i;
var formatMatch = chQuery.match (formatRegexp);
if (!options.omitFormat && formatMatch) {
options.format = formatMatch[1];
options.omitFormat = true;
}
var reqData = {
syncParser: options.syncParser || this.options.syncParser || false,
finalized: true, // allows to write records into connection stream
};
var reqParams = this.getReqParams ();
var formatEnding = '';
// format should be added for data queries
if (chQuery.match (/^(?:SELECT|WITH|SHOW|DESC|DESCRIBE|EXISTS\s+TABLE)/i)) {
if (!options.format)
options.format = options.dataObjects ? 'JSON' : 'JSONCompact';
} else if (chQuery.match (/^INSERT/i)) {
// There is some variants according to the documentation:
// 1. Values already available in the query: INSERT INTO t VALUES (1),(2),(3)
// 2. Values must me provided with POST data: INSERT INTO t VALUES
// 3. Same as previous but without VALUES keyword: INSERT INTO t FORMAT Values
// 4. Insert from SELECT: INSERT INTO t SELECT…
// we need to handle 2 and 3 and http stream must stay open in that cases
if (chQuery.match (/\s+VALUES\b/i)) {
if (chQuery.match (/\s+VALUES\s*$/i))
reqData.finalized = false;
options.format = 'Values';
options.omitFormat = true;
} else if (chQuery.match (/INSERT\s+INTO\s+\S+\s+(?:\([^\)]+\)\s+)?SELECT/mi)) {
reqData.finalized = true;
options.omitFormat = true;
} else {
reqData.finalized = false;
// Newline is recomended https://clickhouse.yandex/docs/en/query_language/insert_into/#insert
formatEnding = '\n';
if (!chQuery.match (/FORMAT/i)) {
// simplest format to use, only need to escape \t, \\ and \n
options.format = options.format || 'TabSeparated';
} else {
options.omitFormat = true;
}
}
} else {
options.omitFormat = true;
}
reqData.format = options.format;
// use query string to submit ClickHouse query — useful to mock CH server
if (options.readonly) {
queryObject.query = chQuery + ((options.omitFormat) ? '' : ' FORMAT ' + options.format + formatEnding);
reqParams.method = 'GET';
} else {
// Trimmed query still may require `formatEnding` when FORMAT clause specified in query
reqData.query = chQuery + (options.omitFormat ? '' : ' FORMAT ' + options.format) + formatEnding;
reqParams.method = 'POST';
}
reqParams.query = queryObject;
var stream = httpRequest (reqParams, reqData, cb);
return stream;
}
ClickHouse.prototype.querying = function (chQuery, options) {
return new Promise (function (resolve, reject) {
// Force override `syncParser` option when using promise api
const queryOptions = Object.assign ({}, options, {syncParser: true})
var stream = this.query (chQuery, queryOptions, function (err, data) {
if (err)
return reject (err);
resolve (data);
});
}.bind (this));
}
ClickHouse.prototype.ping = function (cb) {
var reqParams = this.getReqParams ();
reqParams.method = 'GET';
var stream = httpRequest (reqParams, {finalized: true}, cb);
return stream;
}
ClickHouse.prototype.pinging = function () {
return new Promise (function (resolve, reject) {
var reqParams = this.getReqParams ();
reqParams.method = 'GET';
httpRequest (reqParams, {finalized: true}, function (err, data) {
if (err)
return reject (err);
resolve (data);
});
}.bind (this));
}
module.exports = ClickHouse;