@hpcc-js/observablehq-compiler
Version:
hpcc-js - ObservableHQ Compiler (unoffical)
238 lines (237 loc) • 7.29 kB
JavaScript
import "./chunk-rwCFCtyI.js";
import * as e from "https://cdn.jsdelivr.net/npm/@duckdb/duckdb-wasm@1.32.0/+esm";
//#region ../../node_modules/@observablehq/notebook-kit/dist/src/runtime/stdlib/duckdb.js
var t = e.selectBundle({
mvp: {
mainModule: "https://cdn.jsdelivr.net/npm/@duckdb/duckdb-wasm@1.32.0/dist/duckdb-mvp.wasm",
mainWorker: "https://cdn.jsdelivr.net/npm/@duckdb/duckdb-wasm@1.32.0/dist/duckdb-browser-mvp.worker.js"
},
eh: {
mainModule: "https://cdn.jsdelivr.net/npm/@duckdb/duckdb-wasm@1.32.0/dist/duckdb-eh.wasm",
mainWorker: "https://cdn.jsdelivr.net/npm/@duckdb/duckdb-wasm@1.32.0/dist/duckdb-browser-eh.worker.js"
}
}), n = new e.ConsoleLogger(e.LogLevel.WARNING), r = t.then(({ mainWorker: e }) => {
let t = `importScripts(${JSON.stringify(e)});`;
return URL.createObjectURL(new Blob([t], { type: "text/javascript" }));
}), i = class DuckDBClient {
constructor(e) {
Object.defineProperties(this, { _db: { value: e } });
}
async queryStream(e, t) {
let n = await this._db.connect(), r, i;
try {
if (r = t?.length > 0 ? await (await n.prepare(e)).send(...t) : await n.send(e), i = await r.next(), i.done) throw Error("missing first batch");
} catch (e) {
throw await n.close(), e;
}
return {
schema: i.value.schema,
async *readRows() {
try {
for (; !i.done;) yield i.value.toArray(), i = await r.next();
} finally {
await n.close();
}
}
};
}
async query(e, t) {
let n = await this._db.connect(), r;
try {
r = t?.length > 0 ? await (await n.prepare(e)).query(...t) : await n.query(e);
} finally {
await n.close();
}
return r;
}
async queryRow(e, t) {
let n = (await this.queryStream(e, t)).readRows();
try {
let { done: e, value: t } = await n.next();
return e || !t.length ? null : t[0];
} finally {
await n.return();
}
}
async sql(e, ...t) {
return await this.query(e.join("?"), t);
}
queryTag(e, ...t) {
return [e.join("?"), t];
}
escape(e) {
return `"${e}"`;
}
async describeTables() {
return Array.from(await this.query("SHOW TABLES"), ({ name: e }) => ({ name: e }));
}
async describeColumns(e = {}) {
return Array.from(await this.query(`DESCRIBE ${this.escape(e.table)}`), ({ column_name: e, column_type: t, null: n }) => ({
name: e,
type: getDuckDBType(t),
nullable: n !== "NO",
databaseType: t
}));
}
static async of(e = {}, t = {}) {
let n = await createDuckDB();
return t.query?.castDecimalToDouble === void 0 && (t = {
...t,
query: {
...t.query,
castDecimalToDouble: !0
}
}), t.query?.castTimestampToDate === void 0 && (t = {
...t,
query: {
...t.query,
castTimestampToDate: !0
}
}), t.query?.castBigIntToDouble === void 0 && (t = {
...t,
query: {
...t.query,
castBigIntToDouble: !0
}
}), t.filesystem?.forceFullHTTPReads === void 0 && (t = {
...t,
filesystem: {
...t.filesystem,
forceFullHTTPReads: !0
}
}), await n.open(t), await Promise.all(Object.entries(e).map(([e, t]) => insertSource(n, e, t))), new DuckDBClient(n);
}
static sql() {
return this.of.apply(this, arguments).then((e) => e.sql.bind(e));
}
};
Object.defineProperty(i, "name", { value: "DuckDBClient" }), Object.defineProperty(i.prototype, "dialect", { value: "duckdb" });
async function insertSource(e, t, n) {
if (n = await n, isFileAttachment(n)) return insertFile(e, t, n);
if (isArrowTable(n)) return insertArrowTable(e, t, n);
if (Array.isArray(n)) return insertArray(e, t, n);
if (isArqueroTable(n)) return insertArqueroTable(e, t, n);
if (typeof n == "string") return insertUrl(e, t, n);
if (n && typeof n == "object") {
if ("data" in n) {
let { data: r, ...i } = n;
return isArrowTable(r) ? insertArrowTable(e, t, r, i) : insertArray(e, t, r, i);
}
if ("file" in n) {
let { file: r, ...i } = n;
return insertFile(e, t, r, i);
}
}
throw Error(`invalid source: ${n}`);
}
async function insertUrl(e, t, n) {
let r = await e.connect();
try {
await r.query(`CREATE VIEW '${t}' AS FROM '${n}'`);
} finally {
await r.close();
}
}
async function insertFile(e, t, n, r) {
let i = await n.url();
if (i.startsWith("blob:")) {
let t = await n.arrayBuffer();
await e.registerFileBuffer(n.name, new Uint8Array(t));
} else await e.registerFileURL(n.name, new URL(i, location).href, 4);
let a = await e.connect();
try {
switch (n.mimeType) {
case "text/csv":
case "text/tab-separated-values": return await a.insertCSVFromPath(n.name, {
name: t,
schema: "main",
...r
}).catch(async (e) => {
if (e.toString().includes("Could not convert")) return await insertUntypedCSV(a, n, t);
throw e;
});
case "application/json": return await a.insertJSONFromPath(n.name, {
name: t,
schema: "main",
...r
});
default:
if (/\.arrow$/i.test(n.name)) {
let e = new Uint8Array(await n.arrayBuffer());
return await a.insertArrowFromIPCStream(e, {
name: t,
schema: "main",
...r
});
}
if (/\.parquet$/i.test(n.name)) {
let e = n.size < 5e7 ? "TABLE" : "VIEW";
return await a.query(`CREATE ${e} '${t}' AS SELECT * FROM parquet_scan('${n.name}')`);
}
if (/\.(db|ddb|duckdb)$/i.test(n.name)) return await a.query(`ATTACH '${n.name}' AS ${t} (READ_ONLY)`);
throw Error(`unknown file type: ${n.mimeType}`);
}
} finally {
await a.close();
}
}
async function insertUntypedCSV(e, t, n) {
return await (await e.prepare(`CREATE TABLE '${n}' AS SELECT * FROM read_csv_auto(?, ALL_VARCHAR=TRUE)`)).send(t.name);
}
async function insertArrowTable(e, t, n, r) {
let i = await e.connect();
try {
await i.insertArrowTable(n, {
name: t,
schema: "main",
...r
});
} finally {
await i.close();
}
}
async function insertArqueroTable(e, t, n) {
return await insertArrowTable(e, t, (await import("https://cdn.jsdelivr.net/npm/apache-arrow@17.0.0/+esm")).tableFromIPC(n.toArrowBuffer()));
}
async function insertArray(e, t, n, r) {
return await insertArrowTable(e, t, (await import("https://cdn.jsdelivr.net/npm/apache-arrow@17.0.0/+esm")).tableFromJSON(n), r);
}
async function createDuckDB() {
let i = new Worker(await r), a = new e.AsyncDuckDB(n, i);
return await a.instantiate((await t).mainModule), a;
}
function getDuckDBType(e) {
switch (e) {
case "BIGINT":
case "HUGEINT":
case "UBIGINT": return "bigint";
case "DOUBLE":
case "REAL":
case "FLOAT": return "number";
case "INTEGER":
case "SMALLINT":
case "TINYINT":
case "USMALLINT":
case "UINTEGER":
case "UTINYINT": return "integer";
case "BOOLEAN": return "boolean";
case "DATE":
case "TIMESTAMP":
case "TIMESTAMP WITH TIME ZONE": return "date";
case "VARCHAR":
case "UUID": return "string";
default: return /^DECIMAL\(/.test(e) ? "integer" : "other";
}
}
function isFileAttachment(e) {
return e && typeof e.name == "string" && typeof e.url == "function" && typeof e.arrayBuffer == "function";
}
function isArqueroTable(e) {
return e && typeof e.toArrowBuffer == "function";
}
function isArrowTable(e) {
return e && typeof e.getChild == "function" && typeof e.toArray == "function" && e.schema && Array.isArray(e.schema.fields);
}
//#endregion
export { i as DuckDBClient };
//# sourceMappingURL=duckdb-COqa9Gou.js.map