Skip to content

Commit a42d947

Browse files
authored
Merge pull request #123 from malthe/fix-select-many-result-corruption-issue
Fix result corruption issue when selecting many rows
2 parents ecd9bd9 + 747c231 commit a42d947

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
In next release ...
22

3+
- Fix issue where larger results would sometimes have duplicates (#122).
4+
35
- Fixed an issue where the result row array type would use the record generic
46
as the value type instead of `any` which was incorrect.
57

src/client.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -920,10 +920,7 @@ export class ClientImpl {
920920
);
921921

922922
const remaining = bytes + read - size;
923-
if (remaining <= 0) {
924-
callback(row);
925-
row = null;
926-
} else {
923+
if (remaining > 0) {
927924
const offset = startRowData + end;
928925
buffer.writeInt8(mtype, offset - 7);
929926
buffer.writeInt32BE(bytes - end - 1, offset - 6);
@@ -933,8 +930,12 @@ export class ClientImpl {
933930
return read + end;
934931
}
935932

933+
callback(row);
934+
row = null;
935+
936936
// Keep track of how much data we've consumed.
937937
frame += bytes;
938+
read += bytes;
938939

939940
// If the next message header doesn't fit, we
940941
// break out and wait for more data to arrive.
@@ -943,8 +944,6 @@ export class ClientImpl {
943944
this.expect = 5;
944945
return read;
945946
}
946-
947-
read += bytes;
948947
}
949948

950949
this.activeRow = null;

test/client.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,22 @@ describe('Query', () => {
301301
deepEqual(result.rows, [[1]]);
302302
});
303303

304+
test('Select many', async ({ client }) => {
305+
const queryRunsCount = 10;
306+
const idsCount = 10_000;
307+
for (let i = 0; i < queryRunsCount; ++i) {
308+
const result = await client.query(
309+
'select id, id + 1, id + 2 from generate_series(1, $1) id',
310+
[idsCount],
311+
);
312+
deepEqual(result.rows.length, idsCount);
313+
for (const [i, j, k] of result.rows) {
314+
deepEqual(i + 1, j);
315+
deepEqual(i + 2, k);
316+
}
317+
}
318+
});
319+
304320
test('Stream', async ({ client }) => {
305321
const s = 'abcdefghijklmnopqrstuvxyz'.repeat(Math.pow(2, 17));
306322
const buffer = Buffer.from(s);

0 commit comments

Comments
 (0)