Skip to content

Commit 263e564

Browse files
authored
Merge pull request #39 from apla/fix-format-clause
Fix request body serializing with FORMAT clause
2 parents dd366fb + 5df0726 commit 263e564

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

src/clickhouse.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,13 @@ ClickHouse.prototype.query = function (chQuery, options, cb) {
304304

305305
reqData.finalized = false;
306306

307+
// Newline is recomended https://clickhouse.yandex/docs/en/query_language/insert_into/#insert
308+
formatEnding = '\n';
307309
if (!chQuery.match (/FORMAT/i)) {
308310
// simplest format to use, only need to escape \t, \\ and \n
309311
options.format = options.format || 'TabSeparated';
310-
formatEnding = ' '; // clickhouse don't like data immediately after format name
311312
} else {
312-
313+
options.omitFormat = true;
313314
}
314315
}
315316
} else {
@@ -323,7 +324,8 @@ ClickHouse.prototype.query = function (chQuery, options, cb) {
323324
queryObject.query = chQuery + ((options.omitFormat) ? '' : ' FORMAT ' + options.format + formatEnding);
324325
reqParams.method = 'GET';
325326
} else {
326-
reqData.query = chQuery + (options.omitFormat ? '' : ' FORMAT ' + options.format + formatEnding);
327+
// Trimmed query still may require `formatEnding` when FORMAT clause specified in query
328+
reqData.query = chQuery + (options.omitFormat ? '' : ' FORMAT ' + options.format) + formatEnding;
327329
reqParams.method = 'POST';
328330
}
329331

src/streams.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ RecordStream.prototype._write = function _write (chunk, enc, cb) {
147147
};
148148

149149
RecordStream.prototype._destroy = function _destroy (err, cb) {
150-
150+
151151
process.nextTick (function () {
152152
RecordStream.super_.prototype._destroy.call (this, err, function (destroyErr) {
153153
this.req.destroy (err);

test/05-insert.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,59 @@ describe ("insert data", function () {
327327
stream.end ();
328328
});
329329

330+
it ("creates a table 5", function () {
331+
var ch = new ClickHouse ({host: host, port: port, queryOptions: {database: dbName}});
332+
return ch.querying ("CREATE TABLE t5 (a UInt8, b Float32, x Nullable(String), z DateTime) ENGINE = Memory");
333+
});
334+
335+
it ("inserts csv with FORMAT clause", function (done) {
336+
var ch = new ClickHouse ({host: host, port: port});
337+
var stream = ch.query ("INSERT INTO t5 FORMAT CSV", {queryOptions: {database: dbName}}, function (err, result) {
338+
assert (!err, err);
339+
340+
ch.query ("SELECT * FROM t5", {syncParser: true, queryOptions: {database: dbName}}, function (err, result) {
341+
342+
assert.equal (result.data[0][0], 0);
343+
assert.equal (result.data[0][1], 0);
344+
assert.equal (result.data[0][2], null);
345+
assert.equal (result.data[0][3], '1970-01-02 00:00:00');
346+
assert.equal (result.data[1][0], 1);
347+
assert.equal (result.data[1][1], 1.5);
348+
assert.equal (result.data[1][2], '1');
349+
assert.equal (result.data[1][3], '2050-01-01 00:00:00');
350+
351+
done ();
352+
353+
});
354+
});
355+
stream.write('0,0,\\N,"1970-01-02 00:00:00"\n1,1.5,"1","2050-01-01 00:00:00"')
356+
stream.end ();
357+
});
358+
359+
it ("select data with FORMAT clause", function () {
360+
var ch = new ClickHouse ({host: host, port: port});
361+
return ch.querying("SELECT * FROM t5 FORMAT Values", {queryOptions: {database: dbName}})
362+
.then((data) => {
363+
assert.equal (data, `(0,0,NULL,'1970-01-02 00:00:00'),(1,1.5,'1','2050-01-01 00:00:00')`)
364+
})
365+
});
366+
367+
it ("select data with GET method and FORMAT clause", function () {
368+
var ch = new ClickHouse ({host: host, port: port, useQueryString: true});
369+
return ch.querying("SELECT * FROM t5 FORMAT Values", {queryOptions: {database: dbName}})
370+
.then((data) => {
371+
assert.equal (data, `(0,0,NULL,'1970-01-02 00:00:00'),(1,1.5,'1','2050-01-01 00:00:00')`)
372+
})
373+
});
374+
375+
it ("select data with GET method and format option", function () {
376+
var ch = new ClickHouse ({host: host, port: port, useQueryString: true});
377+
return ch.querying("SELECT * FROM t5", {queryOptions: {database: dbName}, format: 'Values'})
378+
.then((data) => {
379+
assert.equal (data, `(0,0,NULL,'1970-01-02 00:00:00'),(1,1.5,'1','2050-01-01 00:00:00')`)
380+
})
381+
});
382+
330383
after (function (done) {
331384

332385
if (!dbCreated)

0 commit comments

Comments
 (0)