Skip to content

Commit 51a61fc

Browse files
Support Arquero tables (#332)
1 parent e3dab1b commit 51a61fc

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

src/duckdb.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {getArrowTableSchema, isArrowTable, loadArrow} from "./arrow.js";
22
import {duckdb} from "./dependencies.js";
33
import {FileAttachment} from "./fileAttachment.js";
44
import {cdn} from "./require.js";
5+
import {isArqueroTable} from "./table.js";
56

67
// Adapted from https://observablehq.com/@cmudig/duckdb-client
78
// Copyright 2021 CMU Data Interaction Group
@@ -134,6 +135,8 @@ export class DuckDBClient {
134135
await insertArrowTable(db, name, source);
135136
} else if (Array.isArray(source)) { // bare array of objects
136137
await insertArray(db, name, source);
138+
} else if (isArqueroTable(source)) {
139+
await insertArqueroTable(db, name, source);
137140
} else if ("data" in source) { // data + options
138141
const {data, ...options} = source;
139142
if (isArrowTable(data)) {
@@ -215,6 +218,14 @@ async function insertArrowTable(database, name, table, options) {
215218
}
216219
}
217220

221+
async function insertArqueroTable(database, name, source) {
222+
// TODO When we have stdlib versioning and can upgrade Arquero to version 5,
223+
// we can then call source.toArrow() directly, with insertArrowTable()
224+
const arrow = await loadArrow();
225+
const table = arrow.tableFromIPC(source.toArrowBuffer());
226+
return await insertArrowTable(database, name, table);
227+
}
228+
218229
async function insertArray(database, name, array, options) {
219230
const arrow = await loadArrow();
220231
const table = arrow.tableFromJSON(array);

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ export {
77
arrayIsPrimitive,
88
isDataArray,
99
isDatabaseClient,
10+
isArqueroTable,
1011
__table as applyDataTableOperations
1112
} from "./table.js";

src/table.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ function isTypedArray(value) {
141141
);
142142
}
143143

144+
export function isArqueroTable(value) {
145+
// Arquero tables have a `toArrowBuffer` function
146+
return value && typeof value.toArrowBuffer === "function";
147+
}
148+
144149
// __query is used by table cells; __query.sql is used by SQL cells.
145150
export const __query = Object.assign(
146151
async (source, operations, invalidation, name) => {
@@ -198,7 +203,7 @@ const loadTableDataSource = sourceCache(async (source, name) => {
198203
if (/\.(arrow|parquet)$/i.test(source.name)) return loadDuckDBClient(source, name);
199204
throw new Error(`unsupported file type: ${source.mimeType}`);
200205
}
201-
if (isArrowTable(source)) return loadDuckDBClient(source, name);
206+
if (isArrowTable(source) || isArqueroTable(source)) return loadDuckDBClient(source, name);
202207
return source;
203208
});
204209

@@ -214,7 +219,7 @@ const loadSqlDataSource = sourceCache(async (source, name) => {
214219
throw new Error(`unsupported file type: ${source.mimeType}`);
215220
}
216221
if (isDataArray(source)) return loadDuckDBClient(await asArrowTable(source, name), name);
217-
if (isArrowTable(source)) return loadDuckDBClient(source, name);
222+
if (isArrowTable(source) || isArqueroTable(source)) return loadDuckDBClient(source, name);
218223
return source;
219224
});
220225

0 commit comments

Comments
 (0)