diff --git a/src/duckdb.js b/src/duckdb.js index b0ec8e31..52b7199e 100644 --- a/src/duckdb.js +++ b/src/duckdb.js @@ -2,6 +2,7 @@ import {getArrowTableSchema, isArrowTable, loadArrow} from "./arrow.js"; import {duckdb} from "./dependencies.js"; import {FileAttachment} from "./fileAttachment.js"; import {cdn} from "./require.js"; +import {isArqueroTable} from "./table.js"; // Adapted from https://observablehq.com/@cmudig/duckdb-client // Copyright 2021 CMU Data Interaction Group @@ -134,6 +135,8 @@ export class DuckDBClient { await insertArrowTable(db, name, source); } else if (Array.isArray(source)) { // bare array of objects await insertArray(db, name, source); + } else if (isArqueroTable(source)) { + await insertArqueroTable(db, name, source); } else if ("data" in source) { // data + options const {data, ...options} = source; if (isArrowTable(data)) { @@ -215,6 +218,14 @@ async function insertArrowTable(database, name, table, options) { } } +async function insertArqueroTable(database, name, source) { + // TODO When we have stdlib versioning and can upgrade Arquero to version 5, + // we can then call source.toArrow() directly, with insertArrowTable() + const arrow = await loadArrow(); + const table = arrow.tableFromIPC(source.toArrowBuffer()); + return await insertArrowTable(database, name, table); +} + async function insertArray(database, name, array, options) { const arrow = await loadArrow(); const table = arrow.tableFromJSON(array); diff --git a/src/index.js b/src/index.js index 29751825..55994f54 100644 --- a/src/index.js +++ b/src/index.js @@ -7,5 +7,6 @@ export { arrayIsPrimitive, isDataArray, isDatabaseClient, + isArqueroTable, __table as applyDataTableOperations } from "./table.js"; diff --git a/src/table.js b/src/table.js index 247fd4fc..fc07f90b 100644 --- a/src/table.js +++ b/src/table.js @@ -141,6 +141,11 @@ function isTypedArray(value) { ); } +export function isArqueroTable(value) { + // Arquero tables have a `toArrowBuffer` function + return value && typeof value.toArrowBuffer === "function"; +} + // __query is used by table cells; __query.sql is used by SQL cells. export const __query = Object.assign( async (source, operations, invalidation, name) => { @@ -198,7 +203,7 @@ const loadTableDataSource = sourceCache(async (source, name) => { if (/\.(arrow|parquet)$/i.test(source.name)) return loadDuckDBClient(source, name); throw new Error(`unsupported file type: ${source.mimeType}`); } - if (isArrowTable(source)) return loadDuckDBClient(source, name); + if (isArrowTable(source) || isArqueroTable(source)) return loadDuckDBClient(source, name); return source; }); @@ -214,7 +219,7 @@ const loadSqlDataSource = sourceCache(async (source, name) => { throw new Error(`unsupported file type: ${source.mimeType}`); } if (isDataArray(source)) return loadDuckDBClient(await asArrowTable(source, name), name); - if (isArrowTable(source)) return loadDuckDBClient(source, name); + if (isArrowTable(source) || isArqueroTable(source)) return loadDuckDBClient(source, name); return source; });