mirror of
https://github.com/lingble/clickhouse.git
synced 2026-03-20 03:53:16 +00:00
fix(index.js): add test and fix for query with totals
This commit is contained in:
@@ -188,6 +188,8 @@ const result = await rs.pipe(tf).pipe(ws).exec();
|
||||
***
|
||||
|
||||
**Changelogs**:
|
||||
* 2020-04-17 (v2.1.0)
|
||||
- Fix query with totals. For json formats work perfect, but for another - doesn't
|
||||
* 2019-02-13
|
||||
- Add compatibility with user and username options
|
||||
* 2019-02-07
|
||||
|
||||
29
index.js
29
index.js
@@ -542,27 +542,27 @@ class QueryCursor {
|
||||
}
|
||||
|
||||
try {
|
||||
let data = me.getBodyParser()(res.body);
|
||||
const data = me.getBodyParser()(res.body);
|
||||
|
||||
if (me.format === 'json') {
|
||||
data = data.data;
|
||||
if (me.useTotals) {
|
||||
return cb(null, data);
|
||||
}
|
||||
|
||||
return cb(null, data.data);
|
||||
}
|
||||
|
||||
if (me.useTotals) {
|
||||
const totals = JSON.parse(res.headers['x-clickhouse-summary']);
|
||||
return cb(
|
||||
null,
|
||||
{
|
||||
meta: {},
|
||||
data: data,
|
||||
totals,
|
||||
rows: data.length,
|
||||
statistics: {},
|
||||
}
|
||||
);
|
||||
return cb(null, {
|
||||
meta: {},
|
||||
data,
|
||||
totals: {},
|
||||
rows: {},
|
||||
statistics: {},
|
||||
});
|
||||
}
|
||||
|
||||
cb(null, data);
|
||||
return cb(null, data);
|
||||
} catch (err) {
|
||||
cb(err);
|
||||
}
|
||||
@@ -603,6 +603,7 @@ class QueryCursor {
|
||||
|
||||
withTotals() {
|
||||
this.useTotals = true;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -92,5 +92,5 @@
|
||||
"test": "mocha --bail --timeout 60000 --slow 5000"
|
||||
},
|
||||
"types": "index.d.ts",
|
||||
"version": "2.0.2"
|
||||
"version": "2.1.0"
|
||||
}
|
||||
|
||||
29
test/test.js
29
test/test.js
@@ -665,17 +665,16 @@ describe('Exec system queries', () => {
|
||||
describe('Select and WITH TOTALS statement', () => {
|
||||
[false, true].forEach(withTotals => {
|
||||
it(`is ${withTotals}`, async () => {
|
||||
const query = clickhouse.query(
|
||||
`SELECT
|
||||
number % 3 AS i,
|
||||
groupArray(number) as kList
|
||||
FROM (
|
||||
SELECT number FROM system.numbers LIMIT 14
|
||||
)
|
||||
GROUP BY i ${withTotals ? '' : 'WITH TOTALS'}
|
||||
FORMAT TabSeparatedWithNames
|
||||
`
|
||||
);
|
||||
const query = clickhouse.query(`
|
||||
SELECT
|
||||
number % 3 AS i,
|
||||
groupArray(number) as kList
|
||||
FROM (
|
||||
SELECT number FROM system.numbers LIMIT 14
|
||||
)
|
||||
GROUP BY i ${withTotals ? '' : 'WITH TOTALS'}
|
||||
FORMAT TabSeparatedWithNames
|
||||
`);
|
||||
|
||||
if (withTotals) {
|
||||
query.withTotals();
|
||||
@@ -692,6 +691,8 @@ describe('Select and WITH TOTALS statement', () => {
|
||||
});
|
||||
|
||||
it('WITH TOTALS #2', async () => {
|
||||
const LIMIT_COUNT = 10;
|
||||
|
||||
const result = await clickhouse.query(`
|
||||
SELECT
|
||||
rowNumberInAllBlocks() AS i,
|
||||
@@ -703,13 +704,15 @@ describe('Select and WITH TOTALS statement', () => {
|
||||
system.numbers
|
||||
LIMIT 1000
|
||||
)
|
||||
GROUP BY i WITH TOTALS LIMIT 10
|
||||
GROUP BY i WITH TOTALS
|
||||
LIMIT ${LIMIT_COUNT}
|
||||
`).toPromise();
|
||||
|
||||
expect(result).to.have.key('meta');
|
||||
expect(result).to.have.key('data');
|
||||
expect(result).to.have.key('totals');
|
||||
expect(result).to.have.key('rows');
|
||||
expect(result.rows).to.be(LIMIT_COUNT);
|
||||
expect(result).to.have.key('statistics');
|
||||
})
|
||||
});
|
||||
@@ -738,7 +741,7 @@ describe('Abort query', () => {
|
||||
setTimeout(() => cb(), 4 * 1000);
|
||||
})
|
||||
.on('end', () => {
|
||||
cb(new Error('no way!'));
|
||||
cb(new Error('no way! May be stream very quick!'));
|
||||
});
|
||||
|
||||
setTimeout(() => $q.destroy(), 10 * 1000);
|
||||
|
||||
Reference in New Issue
Block a user