@malloydata/db-duckdb 0.0.41-dev230615144150 → 0.0.41-dev230615181401

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.
@@ -5,11 +5,11 @@ declare type RemoteFileCallback = (tableName: string) => Promise<Uint8Array | un
5
5
  export declare abstract class DuckDBWASMConnection extends DuckDBCommon {
6
6
  readonly name: string;
7
7
  private databasePath;
8
- private workingDirectory;
8
+ protected workingDirectory: string;
9
9
  connecting: Promise<void>;
10
10
  protected _connection: duckdb.AsyncDuckDBConnection | null;
11
11
  protected _database: duckdb.AsyncDuckDB | null;
12
- protected isSetup: boolean;
12
+ protected isSetup: Promise<void> | undefined;
13
13
  private worker;
14
14
  private remoteFileCallbacks;
15
15
  private remoteFileStatus;
@@ -18,6 +18,7 @@ export declare abstract class DuckDBWASMConnection extends DuckDBCommon {
18
18
  abstract getBundles(): duckdb.DuckDBBundles;
19
19
  get connection(): duckdb.AsyncDuckDBConnection | null;
20
20
  get database(): duckdb.AsyncDuckDB | null;
21
+ loadExtension(ext: string): Promise<void>;
21
22
  protected setup(): Promise<void>;
22
23
  protected runDuckDBQuery(sql: string): Promise<{
23
24
  rows: QueryDataRow[];
@@ -54,7 +54,8 @@ const web_worker_1 = __importDefault(require("web-worker"));
54
54
  const malloy_1 = require("@malloydata/malloy");
55
55
  const apache_arrow_1 = require("apache-arrow");
56
56
  const duckdb_common_1 = require("./duckdb_common");
57
- const TABLE_MATCH = /FROM\s*'(.*)'/gi;
57
+ const TABLE_MATCH = /FROM\s*('([^']*)'|"([^"]*)")/gi;
58
+ const TABLE_FUNCTION_MATCH = /FROM\s+[a-z0-9_]+\(('([^']*)'|"([^"]*)")/gi;
58
59
  /**
59
60
  * Arrow's toJSON() doesn't really do what I'd expect, since
60
61
  * it still includes Arrow objects like DecimalBigNums and Vectors,
@@ -94,7 +95,9 @@ const unwrapArrow = (value) => {
94
95
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
95
96
  const result = {};
96
97
  for (const key in obj) {
97
- result[key] = unwrapArrow(obj[key]);
98
+ if (Object.prototype.hasOwnProperty.call(obj, key)) {
99
+ result[key] = unwrapArrow(obj[key]);
100
+ }
98
101
  }
99
102
  return result;
100
103
  }
@@ -126,7 +129,6 @@ class DuckDBWASMConnection extends duckdb_common_1.DuckDBCommon {
126
129
  this.workingDirectory = workingDirectory;
127
130
  this._connection = null;
128
131
  this._database = null;
129
- this.isSetup = false;
130
132
  this.worker = null;
131
133
  this.remoteFileCallbacks = [];
132
134
  this.remoteFileStatus = {};
@@ -164,8 +166,31 @@ class DuckDBWASMConnection extends duckdb_common_1.DuckDBCommon {
164
166
  get database() {
165
167
  return this._database;
166
168
  }
169
+ async loadExtension(ext) {
170
+ try {
171
+ await this.runDuckDBQuery(`INSTALL '${ext}'`);
172
+ await this.runDuckDBQuery(`LOAD '${ext}'`);
173
+ }
174
+ catch (error) {
175
+ // eslint-disable-next-line no-console
176
+ console.error(`Unable to load ${ext} extension`, error);
177
+ }
178
+ }
167
179
  async setup() {
180
+ const doSetup = async () => {
181
+ if (this.workingDirectory) {
182
+ await this.runDuckDBQuery(`SET FILE_SEARCH_PATH='${this.workingDirectory}'`);
183
+ }
184
+ // Not quite ready for prime time
185
+ // for (const ext of ['json', 'httpfs', 'icu']) {
186
+ // await this.loadExtension(ext);
187
+ // }
188
+ };
168
189
  await this.connecting;
190
+ if (!this.isSetup) {
191
+ this.isSetup = doSetup();
192
+ }
193
+ await this.isSetup;
169
194
  }
170
195
  async runDuckDBQuery(sql) {
171
196
  var _a;
@@ -234,7 +259,10 @@ class DuckDBWASMConnection extends duckdb_common_1.DuckDBCommon {
234
259
  async fetchSchemaForSQLBlock(sqlRef) {
235
260
  const tables = [];
236
261
  for (const match of sqlRef.selectStr.matchAll(TABLE_MATCH)) {
237
- tables.push(match[1]);
262
+ tables.push(match[2] || match[3]);
263
+ }
264
+ for (const match of sqlRef.selectStr.matchAll(TABLE_FUNCTION_MATCH)) {
265
+ tables.push(match[2] || match[3]);
238
266
  }
239
267
  await this.findTables(tables);
240
268
  return super.fetchSchemaForSQLBlock(sqlRef);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,92 @@
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_wasm_connection_node_1 = require("./duckdb_wasm_connection_node");
27
+ describe('DuckDBWasmConnection', () => {
28
+ let connection;
29
+ let findTables;
30
+ beforeAll(async () => {
31
+ connection = new duckdb_wasm_connection_node_1.DuckDBWASMConnection('duckdb');
32
+ await connection.runSQL('SELECT 1');
33
+ });
34
+ afterAll(async () => {
35
+ await connection.close();
36
+ await new Promise(resolve => setTimeout(resolve, 10000));
37
+ });
38
+ beforeEach(() => {
39
+ jest
40
+ .spyOn(duckdb_common_1.DuckDBCommon.prototype, 'fetchSchemaForSQLBlock')
41
+ .mockResolvedValue({
42
+ error: 'mocked',
43
+ });
44
+ findTables = jest.spyOn(connection, 'findTables');
45
+ });
46
+ afterEach(() => {
47
+ jest.resetAllMocks();
48
+ });
49
+ it('finds simple tables in SQL', async () => {
50
+ await connection.fetchSchemaForSQLBlock({
51
+ selectStr: `
52
+ SELECT
53
+ created_at,
54
+ sale_price,
55
+ inventory_item_id
56
+ FROM 'order_items.parquet'
57
+ SELECT
58
+ id,
59
+ product_department,
60
+ product_category,
61
+ created_at AS inventory_items_created_at
62
+ FROM "inventory_items.parquet"
63
+ `,
64
+ });
65
+ expect(findTables).toHaveBeenCalledWith([
66
+ 'order_items.parquet',
67
+ 'inventory_items.parquet',
68
+ ]);
69
+ });
70
+ it('finds table functions in SQL', async () => {
71
+ await connection.fetchSchemaForSQLBlock({
72
+ selectStr: `
73
+ SELECT
74
+ created_at,
75
+ sale_price,
76
+ inventory_item_id
77
+ FROM read_parquet('order_items2.parquet', arg='value')
78
+ SELECT
79
+ id,
80
+ product_department,
81
+ product_category,
82
+ created_at AS inventory_items_created_at
83
+ FROM read_parquet("inventory_items2.parquet")
84
+ `,
85
+ });
86
+ expect(findTables).toHaveBeenCalledWith([
87
+ 'order_items2.parquet',
88
+ 'inventory_items2.parquet',
89
+ ]);
90
+ });
91
+ });
92
+ //# 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.41-dev230615144150",
3
+ "version": "0.0.41-dev230615181401",
4
4
  "license": "MIT",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -40,10 +40,10 @@
40
40
  "prepublishOnly": "npm run build"
41
41
  },
42
42
  "dependencies": {
43
- "@malloydata/duckdb-wasm": "0.0.1",
44
- "@malloydata/malloy": "^0.0.41-dev230615144150",
43
+ "@malloydata/duckdb-wasm": "0.0.2",
44
+ "@malloydata/malloy": "^0.0.40",
45
45
  "apache-arrow": "^11.0.0",
46
- "duckdb": "0.7.1",
46
+ "duckdb": "0.8.1",
47
47
  "web-worker": "^1.2.0"
48
48
  }
49
49
  }