@malloydata/db-duckdb 0.0.96-dev231025212541 → 0.0.96-dev231025215824
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/duckdb.spec.d.ts +1 -0
- package/dist/duckdb.spec.js +116 -0
- package/dist/duckdb_common.d.ts +3 -2
- package/dist/duckdb_common.js +12 -6
- package/dist/duckdb_wasm_connection.d.ts +3 -2
- package/dist/duckdb_wasm_connection.js +11 -10
- package/dist/duckdb_wasm_connection.spec.js +4 -10
- package/package.json +3 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright 2023 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
* a copy of this software and associated documentation files
|
|
7
|
+
* (the "Software"), to deal in the Software without restriction,
|
|
8
|
+
* including without limitation the rights to use, copy, modify, merge,
|
|
9
|
+
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
10
|
+
* and to permit persons to whom the Software is furnished to do so,
|
|
11
|
+
* subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be
|
|
14
|
+
* included in all copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
20
|
+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
22
|
+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
+
const duckdb_common_1 = require("./duckdb_common");
|
|
26
|
+
const duckdb_connection_1 = require("./duckdb_connection");
|
|
27
|
+
/*
|
|
28
|
+
* !IMPORTANT
|
|
29
|
+
*
|
|
30
|
+
* The connection is reused for each test, so if you do not name your tables
|
|
31
|
+
* and keys uniquely for each test you will see cross test interactions.
|
|
32
|
+
*/
|
|
33
|
+
describe('DuckDBConnection', () => {
|
|
34
|
+
let connection;
|
|
35
|
+
let runRawSQL;
|
|
36
|
+
beforeAll(async () => {
|
|
37
|
+
connection = new duckdb_connection_1.DuckDBConnection('duckdb');
|
|
38
|
+
await connection.runSQL('SELECT 1');
|
|
39
|
+
});
|
|
40
|
+
afterAll(async () => {
|
|
41
|
+
await connection.close();
|
|
42
|
+
});
|
|
43
|
+
beforeEach(async () => {
|
|
44
|
+
runRawSQL = jest
|
|
45
|
+
.spyOn(duckdb_common_1.DuckDBCommon.prototype, 'runRawSQL')
|
|
46
|
+
.mockResolvedValue({ rows: [], totalRows: 0 });
|
|
47
|
+
});
|
|
48
|
+
afterEach(() => {
|
|
49
|
+
jest.resetAllMocks();
|
|
50
|
+
});
|
|
51
|
+
it('caches table schema', async () => {
|
|
52
|
+
await connection.fetchSchemaForTables({ 'test1': 'table1' }, {});
|
|
53
|
+
expect(runRawSQL).toBeCalledTimes(1);
|
|
54
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
55
|
+
await connection.fetchSchemaForTables({ 'test1': 'table1' }, {});
|
|
56
|
+
expect(runRawSQL).toBeCalledTimes(1);
|
|
57
|
+
});
|
|
58
|
+
it('refreshes table schema', async () => {
|
|
59
|
+
await connection.fetchSchemaForTables({ 'test2': 'table2' }, {});
|
|
60
|
+
expect(runRawSQL).toBeCalledTimes(1);
|
|
61
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
62
|
+
await connection.fetchSchemaForTables({ 'test2': 'table2' }, { refreshTimestamp: Date.now() });
|
|
63
|
+
expect(runRawSQL).toBeCalledTimes(2);
|
|
64
|
+
});
|
|
65
|
+
it('caches sql schema', async () => {
|
|
66
|
+
await connection.fetchSchemaForSQLBlock(SQL_BLOCK_1, {});
|
|
67
|
+
expect(runRawSQL).toBeCalledTimes(1);
|
|
68
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
69
|
+
await connection.fetchSchemaForSQLBlock(SQL_BLOCK_1, {});
|
|
70
|
+
expect(runRawSQL).toBeCalledTimes(1);
|
|
71
|
+
});
|
|
72
|
+
it('refreshes sql schema', async () => {
|
|
73
|
+
await connection.fetchSchemaForSQLBlock(SQL_BLOCK_2, {});
|
|
74
|
+
expect(runRawSQL).toBeCalledTimes(1);
|
|
75
|
+
await new Promise(resolve => setTimeout(resolve));
|
|
76
|
+
await connection.fetchSchemaForSQLBlock(SQL_BLOCK_2, {
|
|
77
|
+
refreshTimestamp: Date.now(),
|
|
78
|
+
});
|
|
79
|
+
expect(runRawSQL).toBeCalledTimes(2);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
const SQL_BLOCK_1 = {
|
|
83
|
+
type: 'sqlBlock',
|
|
84
|
+
name: 'block1',
|
|
85
|
+
selectStr: `
|
|
86
|
+
SELECT
|
|
87
|
+
created_at,
|
|
88
|
+
sale_price,
|
|
89
|
+
inventory_item_id
|
|
90
|
+
FROM 'order_items.parquet'
|
|
91
|
+
SELECT
|
|
92
|
+
id,
|
|
93
|
+
product_department,
|
|
94
|
+
product_category,
|
|
95
|
+
created_at AS inventory_items_created_at
|
|
96
|
+
FROM "inventory_items.parquet"
|
|
97
|
+
`,
|
|
98
|
+
};
|
|
99
|
+
const SQL_BLOCK_2 = {
|
|
100
|
+
type: 'sqlBlock',
|
|
101
|
+
name: 'block2',
|
|
102
|
+
selectStr: `
|
|
103
|
+
SELECT
|
|
104
|
+
created_at,
|
|
105
|
+
sale_price,
|
|
106
|
+
inventory_item_id
|
|
107
|
+
FROM read_parquet('order_items2.parquet', arg='value')
|
|
108
|
+
SELECT
|
|
109
|
+
id,
|
|
110
|
+
product_department,
|
|
111
|
+
product_category,
|
|
112
|
+
created_at AS inventory_items_created_at
|
|
113
|
+
FROM read_parquet("inventory_items2.parquet")
|
|
114
|
+
`,
|
|
115
|
+
};
|
|
116
|
+
//# sourceMappingURL=duckdb.spec.js.map
|
package/dist/duckdb_common.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { MalloyQueryData, PersistSQLResults, PooledConnection, QueryDataRow, QueryRunStats, RunSQLOptions, SQLBlock, StreamingConnection, StructDef, TestableConnection } from '@malloydata/malloy';
|
|
2
|
+
import { FetchSchemaOptions } from '@malloydata/malloy-interfaces';
|
|
2
3
|
export interface DuckDBQueryOptions {
|
|
3
4
|
rowLimit: number;
|
|
4
5
|
}
|
|
@@ -42,14 +43,14 @@ export declare abstract class DuckDBCommon implements TestableConnection, Persis
|
|
|
42
43
|
private stringToTypeMap;
|
|
43
44
|
private fillStructDefFromTypeMap;
|
|
44
45
|
private schemaFromQuery;
|
|
45
|
-
fetchSchemaForSQLBlock(sqlRef: SQLBlock): Promise<{
|
|
46
|
+
fetchSchemaForSQLBlock(sqlRef: SQLBlock, { refreshTimestamp }: FetchSchemaOptions): Promise<{
|
|
46
47
|
structDef: StructDef;
|
|
47
48
|
error?: undefined;
|
|
48
49
|
} | {
|
|
49
50
|
error: string;
|
|
50
51
|
structDef?: undefined;
|
|
51
52
|
}>;
|
|
52
|
-
fetchSchemaForTables(tables: Record<string, string
|
|
53
|
+
fetchSchemaForTables(tables: Record<string, string>, { refreshTimestamp }: FetchSchemaOptions): Promise<{
|
|
53
54
|
schemas: Record<string, StructDef>;
|
|
54
55
|
errors: Record<string, string>;
|
|
55
56
|
}>;
|
package/dist/duckdb_common.js
CHANGED
|
@@ -230,37 +230,43 @@ class DuckDBCommon {
|
|
|
230
230
|
}
|
|
231
231
|
this.fillStructDefFromTypeMap(structDef, typeMap);
|
|
232
232
|
}
|
|
233
|
-
async fetchSchemaForSQLBlock(sqlRef) {
|
|
233
|
+
async fetchSchemaForSQLBlock(sqlRef, { refreshTimestamp }) {
|
|
234
234
|
const key = sqlRef.name;
|
|
235
235
|
let inCache = this.sqlSchemaCache.get(key);
|
|
236
|
-
if (!inCache
|
|
236
|
+
if (!inCache ||
|
|
237
|
+
(refreshTimestamp && refreshTimestamp > inCache.timestamp)) {
|
|
238
|
+
const timestamp = refreshTimestamp !== null && refreshTimestamp !== void 0 ? refreshTimestamp : Date.now();
|
|
237
239
|
try {
|
|
238
240
|
inCache = {
|
|
239
241
|
structDef: await this.getSQLBlockSchema(sqlRef),
|
|
242
|
+
timestamp,
|
|
240
243
|
};
|
|
241
244
|
}
|
|
242
245
|
catch (error) {
|
|
243
|
-
inCache = { error: error.message };
|
|
246
|
+
inCache = { error: error.message, timestamp };
|
|
244
247
|
}
|
|
245
248
|
this.sqlSchemaCache.set(key, inCache);
|
|
246
249
|
}
|
|
247
250
|
return inCache;
|
|
248
251
|
}
|
|
249
|
-
async fetchSchemaForTables(tables) {
|
|
252
|
+
async fetchSchemaForTables(tables, { refreshTimestamp }) {
|
|
250
253
|
const schemas = {};
|
|
251
254
|
const errors = {};
|
|
252
255
|
for (const tableKey in tables) {
|
|
253
256
|
let inCache = this.schemaCache.get(tableKey);
|
|
254
|
-
if (!inCache
|
|
257
|
+
if (!inCache ||
|
|
258
|
+
(refreshTimestamp && refreshTimestamp > inCache.timestamp)) {
|
|
259
|
+
const timestamp = refreshTimestamp !== null && refreshTimestamp !== void 0 ? refreshTimestamp : Date.now();
|
|
255
260
|
const tablePath = tables[tableKey];
|
|
256
261
|
try {
|
|
257
262
|
inCache = {
|
|
258
263
|
schema: await this.getTableSchema(tableKey, tablePath),
|
|
264
|
+
timestamp,
|
|
259
265
|
};
|
|
260
266
|
this.schemaCache.set(tableKey, inCache);
|
|
261
267
|
}
|
|
262
268
|
catch (error) {
|
|
263
|
-
inCache = { error: error.message };
|
|
269
|
+
inCache = { error: error.message, timestamp };
|
|
264
270
|
}
|
|
265
271
|
}
|
|
266
272
|
if (inCache.schema !== undefined) {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as duckdb from '@malloydata/duckdb-wasm';
|
|
2
2
|
import { QueryDataRow, RunSQLOptions, StructDef, SQLBlock } from '@malloydata/malloy';
|
|
3
3
|
import { DuckDBCommon, QueryOptionsReader } from './duckdb_common';
|
|
4
|
+
import { FetchSchemaOptions } from '@malloydata/malloy-interfaces';
|
|
4
5
|
type RemoteFileCallback = (tableName: string) => Promise<Uint8Array | undefined>;
|
|
5
6
|
export declare abstract class DuckDBWASMConnection extends DuckDBCommon {
|
|
6
7
|
readonly name: string;
|
|
@@ -26,14 +27,14 @@ export declare abstract class DuckDBWASMConnection extends DuckDBCommon {
|
|
|
26
27
|
}>;
|
|
27
28
|
runSQLStream(sql: string, _options?: RunSQLOptions): AsyncIterableIterator<QueryDataRow>;
|
|
28
29
|
private findTables;
|
|
29
|
-
fetchSchemaForSQLBlock(sqlRef: SQLBlock): Promise<{
|
|
30
|
+
fetchSchemaForSQLBlock(sqlRef: SQLBlock, options: FetchSchemaOptions): Promise<{
|
|
30
31
|
structDef: StructDef;
|
|
31
32
|
error?: undefined;
|
|
32
33
|
} | {
|
|
33
34
|
error: string;
|
|
34
35
|
structDef?: undefined;
|
|
35
36
|
}>;
|
|
36
|
-
fetchSchemaForTables(missing: Record<string, string
|
|
37
|
+
fetchSchemaForTables(missing: Record<string, string>, options: FetchSchemaOptions): Promise<{
|
|
37
38
|
schemas: Record<string, StructDef>;
|
|
38
39
|
errors: Record<string, string>;
|
|
39
40
|
}>;
|
|
@@ -226,7 +226,7 @@ class DuckDBWASMConnection extends duckdb_common_1.DuckDBCommon {
|
|
|
226
226
|
}
|
|
227
227
|
}
|
|
228
228
|
}
|
|
229
|
-
async findTables(tables) {
|
|
229
|
+
async findTables(tables, { refreshTimestamp }) {
|
|
230
230
|
const fetchRemoteFile = async (tablePath) => {
|
|
231
231
|
var _a;
|
|
232
232
|
for (const callback of this.remoteFileCallbacks) {
|
|
@@ -236,7 +236,7 @@ class DuckDBWASMConnection extends duckdb_common_1.DuckDBCommon {
|
|
|
236
236
|
break;
|
|
237
237
|
}
|
|
238
238
|
}
|
|
239
|
-
return
|
|
239
|
+
return refreshTimestamp !== null && refreshTimestamp !== void 0 ? refreshTimestamp : Date.now();
|
|
240
240
|
};
|
|
241
241
|
await this.setup();
|
|
242
242
|
for (const tablePath of tables) {
|
|
@@ -248,14 +248,15 @@ class DuckDBWASMConnection extends duckdb_common_1.DuckDBCommon {
|
|
|
248
248
|
continue;
|
|
249
249
|
}
|
|
250
250
|
// If we're not trying to fetch start trying
|
|
251
|
-
|
|
251
|
+
const mapped = this.remoteFileStatus[tablePath];
|
|
252
|
+
if (!mapped || (refreshTimestamp && refreshTimestamp > (await mapped))) {
|
|
252
253
|
this.remoteFileStatus[tablePath] = fetchRemoteFile(tablePath);
|
|
253
254
|
}
|
|
254
255
|
// Wait for response
|
|
255
256
|
await this.remoteFileStatus[tablePath];
|
|
256
257
|
}
|
|
257
258
|
}
|
|
258
|
-
async fetchSchemaForSQLBlock(sqlRef) {
|
|
259
|
+
async fetchSchemaForSQLBlock(sqlRef, options) {
|
|
259
260
|
const tables = [];
|
|
260
261
|
for (const match of sqlRef.selectStr.matchAll(TABLE_MATCH)) {
|
|
261
262
|
tables.push(match[2] || match[3]);
|
|
@@ -263,13 +264,13 @@ class DuckDBWASMConnection extends duckdb_common_1.DuckDBCommon {
|
|
|
263
264
|
for (const match of sqlRef.selectStr.matchAll(TABLE_FUNCTION_MATCH)) {
|
|
264
265
|
tables.push(match[2] || match[3]);
|
|
265
266
|
}
|
|
266
|
-
await this.findTables(tables);
|
|
267
|
-
return super.fetchSchemaForSQLBlock(sqlRef);
|
|
267
|
+
await this.findTables(tables, options);
|
|
268
|
+
return super.fetchSchemaForSQLBlock(sqlRef, options);
|
|
268
269
|
}
|
|
269
|
-
async fetchSchemaForTables(missing) {
|
|
270
|
+
async fetchSchemaForTables(missing, options) {
|
|
270
271
|
const tables = Object.values(missing);
|
|
271
|
-
await this.findTables(tables);
|
|
272
|
-
return super.fetchSchemaForTables(missing);
|
|
272
|
+
await this.findTables(tables, options);
|
|
273
|
+
return super.fetchSchemaForTables(missing, options);
|
|
273
274
|
}
|
|
274
275
|
async close() {
|
|
275
276
|
if (this._connection) {
|
|
@@ -290,7 +291,7 @@ class DuckDBWASMConnection extends duckdb_common_1.DuckDBCommon {
|
|
|
290
291
|
}
|
|
291
292
|
async registerRemoteTable(tableName, url) {
|
|
292
293
|
var _a;
|
|
293
|
-
this.remoteFileStatus[tableName] = Promise.resolve(
|
|
294
|
+
this.remoteFileStatus[tableName] = Promise.resolve(Number.MIN_SAFE_INTEGER);
|
|
294
295
|
(_a = this.database) === null || _a === void 0 ? void 0 : _a.registerFileURL(tableName, url, duckdb.DuckDBDataProtocol.HTTP, true);
|
|
295
296
|
}
|
|
296
297
|
}
|
|
@@ -62,11 +62,8 @@ id,
|
|
|
62
62
|
created_at AS inventory_items_created_at
|
|
63
63
|
FROM "inventory_items.parquet"
|
|
64
64
|
`,
|
|
65
|
-
});
|
|
66
|
-
expect(findTables).toHaveBeenCalledWith([
|
|
67
|
-
'order_items.parquet',
|
|
68
|
-
'inventory_items.parquet',
|
|
69
|
-
]);
|
|
65
|
+
}, {});
|
|
66
|
+
expect(findTables).toHaveBeenCalledWith(['order_items.parquet', 'inventory_items.parquet'], {});
|
|
70
67
|
});
|
|
71
68
|
it('finds table functions in SQL', async () => {
|
|
72
69
|
await connection.fetchSchemaForSQLBlock({
|
|
@@ -83,11 +80,8 @@ id,
|
|
|
83
80
|
created_at AS inventory_items_created_at
|
|
84
81
|
FROM read_parquet("inventory_items2.parquet")
|
|
85
82
|
`,
|
|
86
|
-
});
|
|
87
|
-
expect(findTables).toHaveBeenCalledWith([
|
|
88
|
-
'order_items2.parquet',
|
|
89
|
-
'inventory_items2.parquet',
|
|
90
|
-
]);
|
|
83
|
+
}, {});
|
|
84
|
+
expect(findTables).toHaveBeenCalledWith(['order_items2.parquet', 'inventory_items2.parquet'], {});
|
|
91
85
|
});
|
|
92
86
|
});
|
|
93
87
|
//# sourceMappingURL=duckdb_wasm_connection.spec.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@malloydata/db-duckdb",
|
|
3
|
-
"version": "0.0.96-
|
|
3
|
+
"version": "0.0.96-dev231025215824",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -41,7 +41,8 @@
|
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@malloydata/duckdb-wasm": "0.0.2-rev3",
|
|
44
|
-
"@malloydata/malloy": "^0.0.96-
|
|
44
|
+
"@malloydata/malloy": "^0.0.96-dev231025215824",
|
|
45
|
+
"@malloydata/malloy-interfaces": "^0.0.96-dev231025215824",
|
|
45
46
|
"apache-arrow": "^11.0.0",
|
|
46
47
|
"duckdb": "0.8.1",
|
|
47
48
|
"web-worker": "^1.2.0"
|