diff --git a/tests/unit/kernel/execution.test.ts b/tests/unit/kernel/execution.test.ts index 9aae43f5..0c271ace 100644 --- a/tests/unit/kernel/execution.test.ts +++ b/tests/unit/kernel/execution.test.ts @@ -48,7 +48,7 @@ class FakeNativeStatement implements KernelStatement { // Mirrors the kernel `Statement.statementId` getter. public readonly statementId = '01ef-fake-statement-id'; - public async fetchNextBatch() { + public async fetchNextBatch(): Promise<{ ipcBytes: Buffer } | null> { return null; } @@ -1894,3 +1894,36 @@ describe('DBSQLSession / DBSQLOperation over kernel backends — Symbol.asyncDis expect(closeSpy.callCount).to.equal(1); }); }); + +describe('DBSQLSession / DBSQLOperation over kernel backends — maxRows', () => { + it('ignores executeStatement maxRows and slices fetchChunk maxRows exactly over kernel batches', async () => { + const ipc = (from: number, count: number) => + Buffer.from( + tableToIPC(tableFromArrays({ id: Int32Array.from({ length: count }, (_, i) => from + i) }), 'stream'), + ); + const batches = [ipc(0, 4096), ipc(4096, 781)]; + const connection = new FakeNativeConnection(); + connection.statementToReturn.schema = () => ({ ipcBytes: ipc(0, 0) }); + connection.statementToReturn.fetchNextBatch = async () => { + const ipcBytes = batches.shift(); + return ipcBytes ? { ipcBytes } : null; + }; + const context = makeContext(); + const session = new DBSQLSession({ backend: new KernelSessionBackend({ connection, context }), context }); + + const operation = await session.executeStatement('SELECT id FROM range(4877)', { maxRows: 1000 }); + expect(connection.lastOptions, 'maxRows is not forwarded to the kernel').to.equal(undefined); + + const sizes: number[] = []; + const ids: unknown[] = []; + do { + // eslint-disable-next-line no-await-in-loop + const chunk = (await operation.fetchChunk({ maxRows: 1000 })) as Array<{ id: unknown }>; + sizes.push(chunk.length); + ids.push(...chunk.map((row) => row.id)); + } while (await operation.hasMoreRows()); // eslint-disable-line no-await-in-loop + + expect(sizes).to.deep.equal([1000, 1000, 1000, 1000, 877]); + expect(ids).to.deep.equal(Array.from({ length: 4877 }, (_, i) => i)); + }); +});