Skip to content

Commit 26c3d7a

Browse files
committed
Yield rows individually
1 parent 266bde9 commit 26c3d7a

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

packages/datasources/duckdb/index.cjs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ const runQuery = async (queryString, database, batchSize = 100000) => {
125125
} else if (database.directory) {
126126
// Local database stored in source directory
127127
filename = path.join(database.directory, database.filename);
128+
} else {
129+
// filename provided without a directory; use as given (may be absolute or relative)
130+
filename = database.filename;
128131
}
129132
}
130133

@@ -185,7 +188,10 @@ const runQuery = async (queryString, database, batchSize = 100000) => {
185188
await reader.readUntil(target);
186189
const allRows = reader.getRowObjectsJS();
187190
const newRows = allRows.slice(prev).map(standardizeRow);
188-
if (newRows.length > 0) yield newRows;
191+
// yield rows one-by-one (asyncIterableToBatchedAsyncGenerator expects single-row yields)
192+
for (const r of newRows) {
193+
yield r;
194+
}
189195
prev = reader.currentRowCount;
190196
}
191197
} finally {

packages/datasources/duckdb/test/test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
import { test } from 'uvu';
22
import * as assert from 'uvu/assert';
3+
import path from 'path';
4+
import { fileURLToPath } from 'url';
35
import runQuery from '../index.cjs';
46
import { batchedAsyncGeneratorToArray, TypeFidelity } from '@evidence-dev/db-commons';
57
import 'dotenv/config';
68

9+
test('basic select from needful_things.duckdb', async () => {
10+
// Resolve the database file path relative to the repository root (from this test file)
11+
const __filename = fileURLToPath(import.meta.url);
12+
const __dirname = path.dirname(__filename);
13+
const dbPath = path.join(__dirname, '..', '..', '..', '..', 'needful_things.duckdb');
14+
// Select the rows from sqlite_master; this test database contains 7 entries
15+
const query = 'SELECT name FROM sqlite_master';
16+
const { rows: rowGen, expectedRowCount } = await runQuery(query, { filename: dbPath });
17+
const rows = await batchedAsyncGeneratorToArray(rowGen);
18+
assert.instance(rows, Array);
19+
assert.type(rows[0], 'object');
20+
// Expect exactly 7 rows in this test database
21+
assert.equal(rows.length, 7);
22+
});
23+
724
// Types to test
825
// BOOLEAN
926
// TINYINT

0 commit comments

Comments
 (0)