Skip to content

Commit

Permalink
Merge pull request #123 from malthe/fix-select-many-result-corruption…
Browse files Browse the repository at this point in the history
…-issue

Fix result corruption issue when selecting many rows
  • Loading branch information
malthe authored Jun 15, 2024
2 parents ecd9bd9 + 747c231 commit a42d947
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
In next release ...

- Fix issue where larger results would sometimes have duplicates (#122).

- Fixed an issue where the result row array type would use the record generic
as the value type instead of `any` which was incorrect.

Expand Down
11 changes: 5 additions & 6 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -920,10 +920,7 @@ export class ClientImpl {
);

const remaining = bytes + read - size;
if (remaining <= 0) {
callback(row);
row = null;
} else {
if (remaining > 0) {
const offset = startRowData + end;
buffer.writeInt8(mtype, offset - 7);
buffer.writeInt32BE(bytes - end - 1, offset - 6);
Expand All @@ -933,8 +930,12 @@ export class ClientImpl {
return read + end;
}

callback(row);
row = null;

// Keep track of how much data we've consumed.
frame += bytes;
read += bytes;

// If the next message header doesn't fit, we
// break out and wait for more data to arrive.
Expand All @@ -943,8 +944,6 @@ export class ClientImpl {
this.expect = 5;
return read;
}

read += bytes;
}

this.activeRow = null;
Expand Down
16 changes: 16 additions & 0 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,22 @@ describe('Query', () => {
deepEqual(result.rows, [[1]]);
});

test('Select many', async ({ client }) => {
const queryRunsCount = 10;
const idsCount = 10_000;
for (let i = 0; i < queryRunsCount; ++i) {
const result = await client.query(
'select id, id + 1, id + 2 from generate_series(1, $1) id',
[idsCount],
);
deepEqual(result.rows.length, idsCount);
for (const [i, j, k] of result.rows) {
deepEqual(i + 1, j);
deepEqual(i + 2, k);
}
}
});

test('Stream', async ({ client }) => {
const s = 'abcdefghijklmnopqrstuvxyz'.repeat(Math.pow(2, 17));
const buffer = Buffer.from(s);
Expand Down

0 comments on commit a42d947

Please sign in to comment.