@loaders.gl/sql 5.0.0-alpha.1
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/LICENSE +41 -0
- package/README.md +27 -0
- package/dist/duckdb-sql-source.d.ts +84 -0
- package/dist/duckdb-sql-source.d.ts.map +1 -0
- package/dist/duckdb-sql-source.js +298 -0
- package/dist/duckdb-sql-source.js.map +1 -0
- package/dist/index.cjs +759 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/snowflake-sql-source.d.ts +84 -0
- package/dist/snowflake-sql-source.d.ts.map +1 -0
- package/dist/snowflake-sql-source.js +209 -0
- package/dist/snowflake-sql-source.js.map +1 -0
- package/dist/sql-source.d.ts +45 -0
- package/dist/sql-source.d.ts.map +1 -0
- package/dist/sql-source.js +146 -0
- package/dist/sql-source.js.map +1 -0
- package/dist/sql-types.d.ts +107 -0
- package/dist/sql-types.d.ts.map +1 -0
- package/dist/sql-types.js +5 -0
- package/dist/sql-types.js.map +1 -0
- package/dist/sql-utils.d.ts +19 -0
- package/dist/sql-utils.d.ts.map +1 -0
- package/dist/sql-utils.js +108 -0
- package/dist/sql-utils.js.map +1 -0
- package/package.json +57 -0
- package/src/duckdb-sql-source.ts +368 -0
- package/src/index.ts +22 -0
- package/src/snowflake-sql-source.ts +296 -0
- package/src/sql-source.ts +193 -0
- package/src/sql-types.ts +119 -0
- package/src/sql-utils.ts +137 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,759 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// dist/index.js
|
|
31
|
+
var dist_exports = {};
|
|
32
|
+
__export(dist_exports, {
|
|
33
|
+
DuckDBSQLDataSource: () => DuckDBSQLDataSource,
|
|
34
|
+
DuckDBSQLSource: () => DuckDBSQLSource,
|
|
35
|
+
SQLDataSource: () => SQLDataSource,
|
|
36
|
+
SnowflakeSQLDataSource: () => SnowflakeSQLDataSource,
|
|
37
|
+
SnowflakeSQLSource: () => SnowflakeSQLSource,
|
|
38
|
+
getSQLAdapterFactory: () => getSQLAdapterFactory,
|
|
39
|
+
registerSQLAdapter: () => registerSQLAdapter
|
|
40
|
+
});
|
|
41
|
+
module.exports = __toCommonJS(dist_exports);
|
|
42
|
+
|
|
43
|
+
// dist/sql-source.js
|
|
44
|
+
var import_loader_utils = require("@loaders.gl/loader-utils");
|
|
45
|
+
|
|
46
|
+
// dist/sql-utils.js
|
|
47
|
+
var arrow = __toESM(require("apache-arrow"), 1);
|
|
48
|
+
var import_schema_utils = require("@loaders.gl/schema-utils");
|
|
49
|
+
function isNodeRuntime() {
|
|
50
|
+
var _a;
|
|
51
|
+
return typeof process !== "undefined" && Boolean((_a = process.versions) == null ? void 0 : _a.node);
|
|
52
|
+
}
|
|
53
|
+
function escapeSqlString(value) {
|
|
54
|
+
return value.replace(/'/g, "''");
|
|
55
|
+
}
|
|
56
|
+
function quoteSqlIdentifier(identifier) {
|
|
57
|
+
return `"${identifier.replace(/"/g, '""')}"`;
|
|
58
|
+
}
|
|
59
|
+
function convertRowsToArrowTable(rows) {
|
|
60
|
+
const table = rows.length > 0 ? (0, import_schema_utils.makeTableFromData)(rows) : { shape: "object-row-table", schema: { fields: [], metadata: {} }, data: rows };
|
|
61
|
+
const schema = deducePrimitiveAwareSchema(table);
|
|
62
|
+
if (table.data.length === 0 || schema.fields.length === 0) {
|
|
63
|
+
return {
|
|
64
|
+
shape: "arrow-table",
|
|
65
|
+
schema,
|
|
66
|
+
data: new arrow.Table(new arrow.Schema([]))
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
shape: "arrow-table",
|
|
71
|
+
schema,
|
|
72
|
+
data: arrow.tableFromJSON(table.data)
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
function convertSQLColumnsToSchema(columns) {
|
|
76
|
+
return {
|
|
77
|
+
metadata: {},
|
|
78
|
+
fields: columns.sort((left, right) => (left.ordinalPosition || 0) - (right.ordinalPosition || 0)).map((column) => ({
|
|
79
|
+
name: column.columnName,
|
|
80
|
+
type: mapSQLTypeToDataType(column.sqlType),
|
|
81
|
+
nullable: column.nullable,
|
|
82
|
+
metadata: {
|
|
83
|
+
sqlType: column.sqlType
|
|
84
|
+
}
|
|
85
|
+
}))
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
function mapSQLTypeToDataType(sqlType) {
|
|
89
|
+
const normalizedType = sqlType.toUpperCase();
|
|
90
|
+
if (normalizedType.includes("BIGINT") || normalizedType === "HUGEINT" || normalizedType === "LONG") {
|
|
91
|
+
return "int64";
|
|
92
|
+
}
|
|
93
|
+
if (normalizedType.includes("INT") || normalizedType === "INTEGER" || normalizedType === "SMALLINT" || normalizedType === "TINYINT") {
|
|
94
|
+
return "int32";
|
|
95
|
+
}
|
|
96
|
+
if (normalizedType.includes("DOUBLE") || normalizedType.includes("FLOAT8") || normalizedType.includes("REAL")) {
|
|
97
|
+
return "float64";
|
|
98
|
+
}
|
|
99
|
+
if (normalizedType.includes("FLOAT") || normalizedType.includes("DECIMAL") || normalizedType.includes("NUMERIC")) {
|
|
100
|
+
return "float64";
|
|
101
|
+
}
|
|
102
|
+
if (normalizedType.includes("BOOL")) {
|
|
103
|
+
return "bool";
|
|
104
|
+
}
|
|
105
|
+
if (normalizedType.includes("DATE")) {
|
|
106
|
+
return "date-day";
|
|
107
|
+
}
|
|
108
|
+
if (normalizedType.includes("TIME") || normalizedType.includes("TIMESTAMP")) {
|
|
109
|
+
return "timestamp-millisecond";
|
|
110
|
+
}
|
|
111
|
+
if (normalizedType.includes("BLOB") || normalizedType.includes("BINARY")) {
|
|
112
|
+
return "binary";
|
|
113
|
+
}
|
|
114
|
+
return "utf8";
|
|
115
|
+
}
|
|
116
|
+
function deducePrimitiveAwareSchema(table) {
|
|
117
|
+
return {
|
|
118
|
+
...table.schema,
|
|
119
|
+
fields: table.schema.fields.map((field) => {
|
|
120
|
+
if (field.type !== "null") {
|
|
121
|
+
return field;
|
|
122
|
+
}
|
|
123
|
+
const columnValues = table.data.map((row) => row[field.name]);
|
|
124
|
+
const inferredType = (0, import_schema_utils.getDataTypeFromArray)(columnValues);
|
|
125
|
+
return { ...field, type: inferredType.type, nullable: inferredType.nullable };
|
|
126
|
+
})
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
function getQualifiedTableName(options) {
|
|
130
|
+
const identifiers = [options.catalogName, options.schemaName, options.tableName].filter((value) => Boolean(value));
|
|
131
|
+
return identifiers.map((identifier) => quoteSqlIdentifier(identifier)).join(".");
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// dist/sql-source.js
|
|
135
|
+
var SQL_ADAPTER_FACTORIES = /* @__PURE__ */ new Map();
|
|
136
|
+
function registerSQLAdapter(type, factory) {
|
|
137
|
+
SQL_ADAPTER_FACTORIES.set(type, factory);
|
|
138
|
+
}
|
|
139
|
+
function getSQLAdapterFactory(type) {
|
|
140
|
+
return SQL_ADAPTER_FACTORIES.get(type);
|
|
141
|
+
}
|
|
142
|
+
var SQL_SOURCE_DEFAULT_OPTIONS = {
|
|
143
|
+
sql: {
|
|
144
|
+
resultFormat: "auto",
|
|
145
|
+
adapterOptions: {},
|
|
146
|
+
browserAdapterFactory: void 0,
|
|
147
|
+
nodeAdapterFactory: void 0
|
|
148
|
+
},
|
|
149
|
+
duckdb: {
|
|
150
|
+
accessMode: "auto",
|
|
151
|
+
bundles: void 0,
|
|
152
|
+
databasePath: void 0,
|
|
153
|
+
remoteUrl: void 0,
|
|
154
|
+
workerUrl: void 0
|
|
155
|
+
},
|
|
156
|
+
snowflake: {
|
|
157
|
+
account: void 0,
|
|
158
|
+
database: void 0,
|
|
159
|
+
schema: void 0,
|
|
160
|
+
token: void 0,
|
|
161
|
+
warehouse: void 0,
|
|
162
|
+
role: void 0,
|
|
163
|
+
authenticator: void 0
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
var SQLDataSource = class extends import_loader_utils.DataSource {
|
|
167
|
+
sourceType;
|
|
168
|
+
adapterFactory;
|
|
169
|
+
adapterPromise = null;
|
|
170
|
+
metadataPromise = null;
|
|
171
|
+
constructor(data, options, sourceType, defaultOptions = SQL_SOURCE_DEFAULT_OPTIONS, adapterFactory, coreApi) {
|
|
172
|
+
super(data, options, defaultOptions, coreApi);
|
|
173
|
+
this.sourceType = sourceType;
|
|
174
|
+
this.adapterFactory = adapterFactory || getSQLAdapterFactory(sourceType);
|
|
175
|
+
}
|
|
176
|
+
/** Returns cached high-level metadata for the SQL source. */
|
|
177
|
+
async getMetadata() {
|
|
178
|
+
if (!this.metadataPromise) {
|
|
179
|
+
this.metadataPromise = this.loadMetadata();
|
|
180
|
+
}
|
|
181
|
+
return this.metadataPromise;
|
|
182
|
+
}
|
|
183
|
+
/** Lists available catalogs from the connected database. */
|
|
184
|
+
async listCatalogs() {
|
|
185
|
+
const adapter = await this.getAdapter();
|
|
186
|
+
return await adapter.listCatalogs();
|
|
187
|
+
}
|
|
188
|
+
/** Lists available schemas from the connected database. */
|
|
189
|
+
async listSchemas(catalogName) {
|
|
190
|
+
const adapter = await this.getAdapter();
|
|
191
|
+
return await adapter.listSchemas(catalogName);
|
|
192
|
+
}
|
|
193
|
+
/** Lists available tables from the connected database. */
|
|
194
|
+
async listTables(options) {
|
|
195
|
+
const adapter = await this.getAdapter();
|
|
196
|
+
return await adapter.listTables(options);
|
|
197
|
+
}
|
|
198
|
+
/** Returns the schema for a specific SQL table. */
|
|
199
|
+
async getTableSchema(options) {
|
|
200
|
+
const adapter = await this.getAdapter();
|
|
201
|
+
return await adapter.getTableSchema(options);
|
|
202
|
+
}
|
|
203
|
+
/** Executes a SQL query and returns object rows. */
|
|
204
|
+
async queryRows(sqlText, options = {}) {
|
|
205
|
+
const adapter = await this.getAdapter();
|
|
206
|
+
return await adapter.executeRows(sqlText, options);
|
|
207
|
+
}
|
|
208
|
+
/** Executes a SQL query and returns a loaders.gl Arrow table. */
|
|
209
|
+
async queryArrow(sqlText, options = {}) {
|
|
210
|
+
const adapter = await this.getAdapter();
|
|
211
|
+
if (adapter.executeArrow) {
|
|
212
|
+
return await adapter.executeArrow(sqlText, options);
|
|
213
|
+
}
|
|
214
|
+
const rows = await adapter.executeRows(sqlText, options);
|
|
215
|
+
return convertRowsToArrowTable(rows);
|
|
216
|
+
}
|
|
217
|
+
/** Closes any underlying adapter connection. */
|
|
218
|
+
async close() {
|
|
219
|
+
if (!this.adapterPromise) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
const adapter = await this.adapterPromise;
|
|
223
|
+
await adapter.close();
|
|
224
|
+
this.adapterPromise = null;
|
|
225
|
+
this.metadataPromise = null;
|
|
226
|
+
}
|
|
227
|
+
async getAdapter() {
|
|
228
|
+
if (!this.adapterPromise) {
|
|
229
|
+
this.adapterPromise = this.loadAdapter();
|
|
230
|
+
}
|
|
231
|
+
return await this.adapterPromise;
|
|
232
|
+
}
|
|
233
|
+
async loadAdapter() {
|
|
234
|
+
var _a, _b;
|
|
235
|
+
try {
|
|
236
|
+
const runtimeFactory = isNodeRuntime() ? (_a = this.options.sql) == null ? void 0 : _a.nodeAdapterFactory : (_b = this.options.sql) == null ? void 0 : _b.browserAdapterFactory;
|
|
237
|
+
const factory = runtimeFactory || this.adapterFactory;
|
|
238
|
+
if (!factory) {
|
|
239
|
+
throw new Error(`No SQL adapter is registered for source type "${this.sourceType}".`);
|
|
240
|
+
}
|
|
241
|
+
const adapter = await factory({
|
|
242
|
+
coreApi: this.coreApi,
|
|
243
|
+
options: this.options,
|
|
244
|
+
sourceType: this.sourceType,
|
|
245
|
+
url: this.url
|
|
246
|
+
});
|
|
247
|
+
await adapter.connect();
|
|
248
|
+
return adapter;
|
|
249
|
+
} catch (error) {
|
|
250
|
+
throw this.reportError(error, `Failed to create SQL adapter for ${this.sourceType} from ${this.url}`);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
async loadMetadata() {
|
|
254
|
+
const adapter = await this.getAdapter();
|
|
255
|
+
const [catalogs, schemas, tables] = await Promise.all([
|
|
256
|
+
adapter.listCatalogs(),
|
|
257
|
+
adapter.listSchemas(),
|
|
258
|
+
adapter.listTables()
|
|
259
|
+
]);
|
|
260
|
+
return {
|
|
261
|
+
type: this.sourceType,
|
|
262
|
+
capabilities: adapter.capabilities,
|
|
263
|
+
catalogs,
|
|
264
|
+
schemas,
|
|
265
|
+
tables
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
// dist/duckdb-sql-source.js
|
|
271
|
+
var import_schema_utils2 = require("@loaders.gl/schema-utils");
|
|
272
|
+
var VERSION = true ? "5.0.0-alpha.1" : "latest";
|
|
273
|
+
var DuckDBSQLSource = {
|
|
274
|
+
dataType: null,
|
|
275
|
+
batchType: null,
|
|
276
|
+
name: "DuckDBSQLSource",
|
|
277
|
+
id: "duckdb-sql",
|
|
278
|
+
module: "sql",
|
|
279
|
+
version: VERSION,
|
|
280
|
+
extensions: ["duckdb", "db"],
|
|
281
|
+
mimeTypes: [],
|
|
282
|
+
type: "duckdb-sql",
|
|
283
|
+
fromUrl: true,
|
|
284
|
+
fromBlob: false,
|
|
285
|
+
options: {
|
|
286
|
+
sql: {
|
|
287
|
+
resultFormat: "auto",
|
|
288
|
+
adapterOptions: {},
|
|
289
|
+
browserAdapterFactory: void 0,
|
|
290
|
+
nodeAdapterFactory: void 0
|
|
291
|
+
},
|
|
292
|
+
duckdb: {
|
|
293
|
+
accessMode: "auto",
|
|
294
|
+
bundles: void 0,
|
|
295
|
+
databasePath: void 0,
|
|
296
|
+
remoteUrl: void 0,
|
|
297
|
+
workerUrl: void 0
|
|
298
|
+
},
|
|
299
|
+
snowflake: {
|
|
300
|
+
account: void 0,
|
|
301
|
+
database: void 0,
|
|
302
|
+
schema: void 0,
|
|
303
|
+
token: void 0,
|
|
304
|
+
warehouse: void 0,
|
|
305
|
+
role: void 0,
|
|
306
|
+
authenticator: void 0
|
|
307
|
+
}
|
|
308
|
+
},
|
|
309
|
+
defaultOptions: SQL_SOURCE_DEFAULT_OPTIONS,
|
|
310
|
+
testURL: (url) => /^duckdb:\/\//i.test(url),
|
|
311
|
+
createDataSource: (data, options, coreApi) => new DuckDBSQLDataSource(data, options, coreApi)
|
|
312
|
+
};
|
|
313
|
+
var DuckDBSQLDataSource = class extends SQLDataSource {
|
|
314
|
+
constructor(data, options, coreApi) {
|
|
315
|
+
super(data, options, DuckDBSQLSource.type, SQL_SOURCE_DEFAULT_OPTIONS, createDuckDBAdapter, coreApi);
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
async function createDuckDBAdapter(context) {
|
|
319
|
+
var _a;
|
|
320
|
+
if ((_a = context.options.duckdb) == null ? void 0 : _a.remoteUrl) {
|
|
321
|
+
throw new Error("Remote DuckDB adapters are not implemented yet.");
|
|
322
|
+
}
|
|
323
|
+
if (isNodeRuntime()) {
|
|
324
|
+
return await createDuckDBNodeAdapter(context);
|
|
325
|
+
}
|
|
326
|
+
return await createDuckDBBrowserAdapter(context);
|
|
327
|
+
}
|
|
328
|
+
async function createDuckDBNodeAdapter(context) {
|
|
329
|
+
var _a;
|
|
330
|
+
const duckdb = await import("@duckdb/node-api");
|
|
331
|
+
const databasePath = getDuckDBDatabasePath(context.url, context.options);
|
|
332
|
+
const configuration = ((_a = context.options.duckdb) == null ? void 0 : _a.accessMode) && context.options.duckdb.accessMode !== "auto" ? { access_mode: context.options.duckdb.accessMode } : void 0;
|
|
333
|
+
let connection;
|
|
334
|
+
const executeRows = async (sqlText, options = {}) => {
|
|
335
|
+
if (!connection) {
|
|
336
|
+
const instance = await duckdb.DuckDBInstance.create(databasePath, configuration);
|
|
337
|
+
connection = await instance.connect();
|
|
338
|
+
}
|
|
339
|
+
const reader = await connection.runAndReadAll(sqlText, options.parameters);
|
|
340
|
+
return reader.getRowObjectsJson();
|
|
341
|
+
};
|
|
342
|
+
return {
|
|
343
|
+
capabilities: {
|
|
344
|
+
supportsArrow: true,
|
|
345
|
+
supportsMetadata: true,
|
|
346
|
+
runtime: "node",
|
|
347
|
+
isDynamic: true
|
|
348
|
+
},
|
|
349
|
+
async connect() {
|
|
350
|
+
if (!connection) {
|
|
351
|
+
const instance = await duckdb.DuckDBInstance.create(databasePath, configuration);
|
|
352
|
+
connection = await instance.connect();
|
|
353
|
+
}
|
|
354
|
+
},
|
|
355
|
+
async close() {
|
|
356
|
+
if (connection == null ? void 0 : connection.closeSync) {
|
|
357
|
+
connection.closeSync();
|
|
358
|
+
}
|
|
359
|
+
connection = null;
|
|
360
|
+
},
|
|
361
|
+
async listCatalogs() {
|
|
362
|
+
const rows = await executeRows(`
|
|
363
|
+
SELECT DISTINCT catalog_name
|
|
364
|
+
FROM information_schema.schemata
|
|
365
|
+
ORDER BY catalog_name
|
|
366
|
+
`);
|
|
367
|
+
return rows.map((row) => ({ catalogName: String(row.catalog_name) }));
|
|
368
|
+
},
|
|
369
|
+
async listSchemas(catalogName) {
|
|
370
|
+
const filter = catalogName ? `WHERE catalog_name = '${escapeSqlString(catalogName)}'` : "";
|
|
371
|
+
const rows = await executeRows(`
|
|
372
|
+
SELECT catalog_name, schema_name
|
|
373
|
+
FROM information_schema.schemata
|
|
374
|
+
${filter}
|
|
375
|
+
ORDER BY catalog_name, schema_name
|
|
376
|
+
`);
|
|
377
|
+
return rows.map((row) => ({
|
|
378
|
+
catalogName: row.catalog_name ? String(row.catalog_name) : void 0,
|
|
379
|
+
schemaName: String(row.schema_name)
|
|
380
|
+
}));
|
|
381
|
+
},
|
|
382
|
+
async listTables(options) {
|
|
383
|
+
const filters = [
|
|
384
|
+
(options == null ? void 0 : options.catalogName) ? `table_catalog = '${escapeSqlString(options.catalogName)}'` : void 0,
|
|
385
|
+
(options == null ? void 0 : options.schemaName) ? `table_schema = '${escapeSqlString(options.schemaName)}'` : void 0,
|
|
386
|
+
`table_schema NOT IN ('information_schema', 'pg_catalog')`
|
|
387
|
+
].filter(Boolean);
|
|
388
|
+
const whereClause = filters.length ? `WHERE ${filters.join(" AND ")}` : "";
|
|
389
|
+
const rows = await executeRows(`
|
|
390
|
+
SELECT table_catalog, table_schema, table_name, table_type
|
|
391
|
+
FROM information_schema.tables
|
|
392
|
+
${whereClause}
|
|
393
|
+
ORDER BY table_catalog, table_schema, table_name
|
|
394
|
+
`);
|
|
395
|
+
return rows.map((row) => ({
|
|
396
|
+
catalogName: row.table_catalog ? String(row.table_catalog) : void 0,
|
|
397
|
+
schemaName: row.table_schema ? String(row.table_schema) : void 0,
|
|
398
|
+
tableName: String(row.table_name),
|
|
399
|
+
tableType: row.table_type ? String(row.table_type) : void 0
|
|
400
|
+
}));
|
|
401
|
+
},
|
|
402
|
+
async getTableSchema(options) {
|
|
403
|
+
const filters = [
|
|
404
|
+
options.catalogName ? `table_catalog = '${escapeSqlString(options.catalogName)}'` : void 0,
|
|
405
|
+
options.schemaName ? `table_schema = '${escapeSqlString(options.schemaName)}'` : void 0,
|
|
406
|
+
`table_name = '${escapeSqlString(options.tableName)}'`
|
|
407
|
+
].filter(Boolean);
|
|
408
|
+
const rows = await executeRows(`
|
|
409
|
+
SELECT table_catalog, table_schema, table_name, column_name, data_type, is_nullable, ordinal_position
|
|
410
|
+
FROM information_schema.columns
|
|
411
|
+
WHERE ${filters.join(" AND ")}
|
|
412
|
+
ORDER BY ordinal_position
|
|
413
|
+
`);
|
|
414
|
+
const columns = rows.map((row) => ({
|
|
415
|
+
catalogName: row.table_catalog ? String(row.table_catalog) : void 0,
|
|
416
|
+
schemaName: row.table_schema ? String(row.table_schema) : void 0,
|
|
417
|
+
tableName: row.table_name ? String(row.table_name) : void 0,
|
|
418
|
+
columnName: String(row.column_name),
|
|
419
|
+
sqlType: String(row.data_type),
|
|
420
|
+
nullable: row.is_nullable === "YES",
|
|
421
|
+
ordinalPosition: Number(row.ordinal_position)
|
|
422
|
+
}));
|
|
423
|
+
return convertSQLColumnsToSchema(columns);
|
|
424
|
+
},
|
|
425
|
+
async executeRows(sqlText, options = {}) {
|
|
426
|
+
return await executeRows(sqlText, options);
|
|
427
|
+
},
|
|
428
|
+
async executeArrow(sqlText, options = {}) {
|
|
429
|
+
const rows = await executeRows(sqlText, options);
|
|
430
|
+
return convertRowsToArrowTable(rows);
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
async function createDuckDBBrowserAdapter(context) {
|
|
435
|
+
var _a, _b;
|
|
436
|
+
const duckdb = await import("@duckdb/duckdb-wasm");
|
|
437
|
+
const logger = new duckdb.ConsoleLogger();
|
|
438
|
+
const selectedBundles = await duckdb.selectBundle(duckdb.getJsDelivrBundles());
|
|
439
|
+
const bundles = {
|
|
440
|
+
...selectedBundles,
|
|
441
|
+
...(_a = context.options.duckdb) == null ? void 0 : _a.bundles
|
|
442
|
+
};
|
|
443
|
+
const workerUrl = ((_b = context.options.duckdb) == null ? void 0 : _b.workerUrl) || selectedBundles.mainWorker;
|
|
444
|
+
if (!workerUrl) {
|
|
445
|
+
throw new Error("DuckDBSQLSource could not resolve a worker bundle URL.");
|
|
446
|
+
}
|
|
447
|
+
const worker = await duckdb.createWorker(workerUrl);
|
|
448
|
+
const database = new duckdb.AsyncDuckDB(logger, worker);
|
|
449
|
+
await database.instantiate(bundles.mainModule, bundles.pthreadWorker);
|
|
450
|
+
const connection = await database.connect();
|
|
451
|
+
const executeRows = async (sqlText) => {
|
|
452
|
+
const result = await connection.query(sqlText);
|
|
453
|
+
return (0, import_schema_utils2.convertArrowToTable)(result, "object-row-table").data;
|
|
454
|
+
};
|
|
455
|
+
return {
|
|
456
|
+
capabilities: {
|
|
457
|
+
supportsArrow: true,
|
|
458
|
+
supportsMetadata: true,
|
|
459
|
+
runtime: "browser",
|
|
460
|
+
isDynamic: true
|
|
461
|
+
},
|
|
462
|
+
async connect() {
|
|
463
|
+
},
|
|
464
|
+
async close() {
|
|
465
|
+
await connection.close();
|
|
466
|
+
await database.terminate();
|
|
467
|
+
worker.terminate();
|
|
468
|
+
},
|
|
469
|
+
async listCatalogs() {
|
|
470
|
+
const rows = await executeRows(`
|
|
471
|
+
SELECT DISTINCT catalog_name
|
|
472
|
+
FROM information_schema.schemata
|
|
473
|
+
ORDER BY catalog_name
|
|
474
|
+
`);
|
|
475
|
+
return rows.map((row) => ({ catalogName: String(row.catalog_name) }));
|
|
476
|
+
},
|
|
477
|
+
async listSchemas(catalogName) {
|
|
478
|
+
const filter = catalogName ? `WHERE catalog_name = '${escapeSqlString(catalogName)}'` : "";
|
|
479
|
+
const rows = await executeRows(`
|
|
480
|
+
SELECT catalog_name, schema_name
|
|
481
|
+
FROM information_schema.schemata
|
|
482
|
+
${filter}
|
|
483
|
+
ORDER BY catalog_name, schema_name
|
|
484
|
+
`);
|
|
485
|
+
return rows.map((row) => ({
|
|
486
|
+
catalogName: row.catalog_name ? String(row.catalog_name) : void 0,
|
|
487
|
+
schemaName: String(row.schema_name)
|
|
488
|
+
}));
|
|
489
|
+
},
|
|
490
|
+
async listTables(options) {
|
|
491
|
+
const filters = [
|
|
492
|
+
(options == null ? void 0 : options.catalogName) ? `table_catalog = '${escapeSqlString(options.catalogName)}'` : void 0,
|
|
493
|
+
(options == null ? void 0 : options.schemaName) ? `table_schema = '${escapeSqlString(options.schemaName)}'` : void 0,
|
|
494
|
+
`table_schema NOT IN ('information_schema', 'pg_catalog')`
|
|
495
|
+
].filter(Boolean);
|
|
496
|
+
const whereClause = filters.length ? `WHERE ${filters.join(" AND ")}` : "";
|
|
497
|
+
const rows = await executeRows(`
|
|
498
|
+
SELECT table_catalog, table_schema, table_name, table_type
|
|
499
|
+
FROM information_schema.tables
|
|
500
|
+
${whereClause}
|
|
501
|
+
ORDER BY table_catalog, table_schema, table_name
|
|
502
|
+
`);
|
|
503
|
+
return rows.map((row) => ({
|
|
504
|
+
catalogName: row.table_catalog ? String(row.table_catalog) : void 0,
|
|
505
|
+
schemaName: row.table_schema ? String(row.table_schema) : void 0,
|
|
506
|
+
tableName: String(row.table_name),
|
|
507
|
+
tableType: row.table_type ? String(row.table_type) : void 0
|
|
508
|
+
}));
|
|
509
|
+
},
|
|
510
|
+
async getTableSchema(options) {
|
|
511
|
+
const filters = [
|
|
512
|
+
options.catalogName ? `table_catalog = '${escapeSqlString(options.catalogName)}'` : void 0,
|
|
513
|
+
options.schemaName ? `table_schema = '${escapeSqlString(options.schemaName)}'` : void 0,
|
|
514
|
+
`table_name = '${escapeSqlString(options.tableName)}'`
|
|
515
|
+
].filter(Boolean);
|
|
516
|
+
const rows = await executeRows(`
|
|
517
|
+
SELECT table_catalog, table_schema, table_name, column_name, data_type, is_nullable, ordinal_position
|
|
518
|
+
FROM information_schema.columns
|
|
519
|
+
WHERE ${filters.join(" AND ")}
|
|
520
|
+
ORDER BY ordinal_position
|
|
521
|
+
`);
|
|
522
|
+
const columns = rows.map((row) => ({
|
|
523
|
+
catalogName: row.table_catalog ? String(row.table_catalog) : void 0,
|
|
524
|
+
schemaName: row.table_schema ? String(row.table_schema) : void 0,
|
|
525
|
+
tableName: row.table_name ? String(row.table_name) : void 0,
|
|
526
|
+
columnName: String(row.column_name),
|
|
527
|
+
sqlType: String(row.data_type),
|
|
528
|
+
nullable: row.is_nullable === "YES",
|
|
529
|
+
ordinalPosition: Number(row.ordinal_position)
|
|
530
|
+
}));
|
|
531
|
+
return convertSQLColumnsToSchema(columns);
|
|
532
|
+
},
|
|
533
|
+
async executeRows(sqlText) {
|
|
534
|
+
return await executeRows(sqlText);
|
|
535
|
+
},
|
|
536
|
+
async executeArrow(sqlText) {
|
|
537
|
+
const result = await connection.query(sqlText);
|
|
538
|
+
return (0, import_schema_utils2.convertArrowToTable)(result, "arrow-table");
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
function getDuckDBDatabasePath(url, options) {
|
|
543
|
+
var _a;
|
|
544
|
+
if ((_a = options.duckdb) == null ? void 0 : _a.databasePath) {
|
|
545
|
+
return options.duckdb.databasePath;
|
|
546
|
+
}
|
|
547
|
+
const parsedUrl = new URL(url);
|
|
548
|
+
const pathname = decodeURIComponent(parsedUrl.pathname || "");
|
|
549
|
+
if (!pathname || pathname === "/") {
|
|
550
|
+
return ":memory:";
|
|
551
|
+
}
|
|
552
|
+
return pathname.slice(1) || ":memory:";
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
// dist/snowflake-sql-source.js
|
|
556
|
+
var VERSION2 = true ? "5.0.0-alpha.1" : "latest";
|
|
557
|
+
var SnowflakeSQLSource = {
|
|
558
|
+
dataType: null,
|
|
559
|
+
batchType: null,
|
|
560
|
+
name: "SnowflakeSQLSource",
|
|
561
|
+
id: "snowflake-sql",
|
|
562
|
+
module: "sql",
|
|
563
|
+
version: VERSION2,
|
|
564
|
+
extensions: [],
|
|
565
|
+
mimeTypes: [],
|
|
566
|
+
type: "snowflake-sql",
|
|
567
|
+
fromUrl: true,
|
|
568
|
+
fromBlob: false,
|
|
569
|
+
options: {
|
|
570
|
+
sql: {
|
|
571
|
+
resultFormat: "auto",
|
|
572
|
+
adapterOptions: {},
|
|
573
|
+
browserAdapterFactory: void 0,
|
|
574
|
+
nodeAdapterFactory: void 0
|
|
575
|
+
},
|
|
576
|
+
duckdb: {
|
|
577
|
+
accessMode: "auto",
|
|
578
|
+
bundles: void 0,
|
|
579
|
+
databasePath: void 0,
|
|
580
|
+
remoteUrl: void 0,
|
|
581
|
+
workerUrl: void 0
|
|
582
|
+
},
|
|
583
|
+
snowflake: {
|
|
584
|
+
account: void 0,
|
|
585
|
+
database: void 0,
|
|
586
|
+
schema: void 0,
|
|
587
|
+
token: void 0,
|
|
588
|
+
warehouse: void 0,
|
|
589
|
+
role: void 0,
|
|
590
|
+
authenticator: void 0
|
|
591
|
+
}
|
|
592
|
+
},
|
|
593
|
+
defaultOptions: SQL_SOURCE_DEFAULT_OPTIONS,
|
|
594
|
+
testURL: (url) => /^(sql\+snowflake|snowflake):\/\//i.test(url),
|
|
595
|
+
createDataSource: (data, options, coreApi) => new SnowflakeSQLDataSource(data, options, coreApi)
|
|
596
|
+
};
|
|
597
|
+
var SnowflakeSQLDataSource = class extends SQLDataSource {
|
|
598
|
+
constructor(data, options, coreApi) {
|
|
599
|
+
super(data, options, SnowflakeSQLSource.type, SQL_SOURCE_DEFAULT_OPTIONS, createSnowflakeAdapter, coreApi);
|
|
600
|
+
}
|
|
601
|
+
};
|
|
602
|
+
async function createSnowflakeAdapter(context) {
|
|
603
|
+
var _a;
|
|
604
|
+
const baseStatementUrl = getSnowflakeStatementsUrl(context.url, context.options);
|
|
605
|
+
const authorization = (_a = context.options.snowflake) == null ? void 0 : _a.token;
|
|
606
|
+
if (!authorization) {
|
|
607
|
+
throw new Error("SnowflakeSQLSource requires snowflake.token.");
|
|
608
|
+
}
|
|
609
|
+
const executeRows = async (sqlText, options = {}) => {
|
|
610
|
+
var _a2, _b;
|
|
611
|
+
const initialResponse = await executeSnowflakeStatement(context, baseStatementUrl, authorization, sqlText, options);
|
|
612
|
+
const responses = [initialResponse];
|
|
613
|
+
const partitionInfo = ((_a2 = initialResponse.resultSetMetaData) == null ? void 0 : _a2.partitionInfo) || [];
|
|
614
|
+
for (let partitionIndex = 1; partitionIndex < partitionInfo.length; partitionIndex++) {
|
|
615
|
+
responses.push(await fetchSnowflakeJson(context, `${baseStatementUrl}/${initialResponse.statementHandle}?partition=${partitionIndex}`, authorization, { method: "GET", signal: options.signal }));
|
|
616
|
+
}
|
|
617
|
+
const rowType = ((_b = initialResponse.resultSetMetaData) == null ? void 0 : _b.rowType) || [];
|
|
618
|
+
const rowNames = rowType.map((row) => row.name);
|
|
619
|
+
return responses.flatMap((response) => (response.data || []).map((values) => {
|
|
620
|
+
const row = {};
|
|
621
|
+
rowNames.forEach((name, index) => {
|
|
622
|
+
row[name] = values[index];
|
|
623
|
+
});
|
|
624
|
+
return row;
|
|
625
|
+
}));
|
|
626
|
+
};
|
|
627
|
+
return {
|
|
628
|
+
capabilities: {
|
|
629
|
+
supportsArrow: false,
|
|
630
|
+
supportsMetadata: true,
|
|
631
|
+
runtime: "both",
|
|
632
|
+
isDynamic: false
|
|
633
|
+
},
|
|
634
|
+
async connect() {
|
|
635
|
+
if (!authorization) {
|
|
636
|
+
throw new Error("SnowflakeSQLSource requires snowflake.token.");
|
|
637
|
+
}
|
|
638
|
+
},
|
|
639
|
+
async close() {
|
|
640
|
+
},
|
|
641
|
+
async listCatalogs() {
|
|
642
|
+
const rows = await executeRows("SHOW DATABASES");
|
|
643
|
+
return rows.map((row) => ({
|
|
644
|
+
catalogName: String(row.name || row.NAME)
|
|
645
|
+
}));
|
|
646
|
+
},
|
|
647
|
+
async listSchemas() {
|
|
648
|
+
const rows = await executeRows("SHOW SCHEMAS");
|
|
649
|
+
return rows.map((row) => ({
|
|
650
|
+
catalogName: row.database_name ? String(row.database_name) : void 0,
|
|
651
|
+
schemaName: String(row.name || row.NAME)
|
|
652
|
+
}));
|
|
653
|
+
},
|
|
654
|
+
async listTables() {
|
|
655
|
+
const rows = await executeRows("SHOW TABLES");
|
|
656
|
+
return rows.map((row) => ({
|
|
657
|
+
catalogName: row.database_name ? String(row.database_name) : void 0,
|
|
658
|
+
schemaName: row.schema_name ? String(row.schema_name) : void 0,
|
|
659
|
+
tableName: String(row.name || row.NAME),
|
|
660
|
+
tableType: row.kind ? String(row.kind) : void 0
|
|
661
|
+
}));
|
|
662
|
+
},
|
|
663
|
+
async getTableSchema(options) {
|
|
664
|
+
const qualifiedTableName = getQualifiedTableName(options);
|
|
665
|
+
const rows = await executeRows(`DESCRIBE TABLE ${qualifiedTableName}`);
|
|
666
|
+
return convertSQLColumnsToSchema(rows.map((row, index) => ({
|
|
667
|
+
columnName: String(row.name || row.NAME || `column_${index + 1}`),
|
|
668
|
+
sqlType: String(row.type || row.TYPE || "VARCHAR"),
|
|
669
|
+
nullable: String(row.null || row.NULL || "").toUpperCase() === "Y",
|
|
670
|
+
ordinalPosition: index + 1
|
|
671
|
+
})));
|
|
672
|
+
},
|
|
673
|
+
async executeRows(sqlText, options = {}) {
|
|
674
|
+
return await executeRows(sqlText, options);
|
|
675
|
+
},
|
|
676
|
+
async executeArrow(sqlText, options = {}) {
|
|
677
|
+
const rows = await executeRows(sqlText, options);
|
|
678
|
+
return convertRowsToArrowTable(rows);
|
|
679
|
+
}
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
async function executeSnowflakeStatement(context, baseStatementUrl, authorization, sqlText, options) {
|
|
683
|
+
var _a, _b, _c, _d;
|
|
684
|
+
let response = await fetchSnowflakeJson(context, baseStatementUrl, authorization, {
|
|
685
|
+
method: "POST",
|
|
686
|
+
headers: {
|
|
687
|
+
"Content-Type": "application/json"
|
|
688
|
+
},
|
|
689
|
+
body: JSON.stringify({
|
|
690
|
+
statement: sqlText,
|
|
691
|
+
timeout: 60,
|
|
692
|
+
bindings: normalizeSnowflakeBindings(options.parameters),
|
|
693
|
+
warehouse: (_a = context.options.snowflake) == null ? void 0 : _a.warehouse,
|
|
694
|
+
database: (_b = context.options.snowflake) == null ? void 0 : _b.database,
|
|
695
|
+
schema: (_c = context.options.snowflake) == null ? void 0 : _c.schema,
|
|
696
|
+
role: (_d = context.options.snowflake) == null ? void 0 : _d.role
|
|
697
|
+
}),
|
|
698
|
+
signal: options.signal
|
|
699
|
+
});
|
|
700
|
+
while ((response == null ? void 0 : response.statementStatusUrl) && !(response == null ? void 0 : response.data)) {
|
|
701
|
+
const pollUrl = `${getSnowflakeBaseUrl(context.url, context.options)}${response.statementStatusUrl}`;
|
|
702
|
+
response = await fetchSnowflakeJson(context, pollUrl, authorization, {
|
|
703
|
+
method: "GET",
|
|
704
|
+
signal: options.signal
|
|
705
|
+
});
|
|
706
|
+
}
|
|
707
|
+
if ((response == null ? void 0 : response.code) || (response == null ? void 0 : response.message)) {
|
|
708
|
+
throw new Error(response.message || `Snowflake SQL API error ${response.code}`);
|
|
709
|
+
}
|
|
710
|
+
return response;
|
|
711
|
+
}
|
|
712
|
+
async function fetchSnowflakeJson(context, url, authorization, requestInit) {
|
|
713
|
+
var _a, _b, _c;
|
|
714
|
+
const customFetch = (_c = (_b = (_a = context.options.core) == null ? void 0 : _a.loadOptions) == null ? void 0 : _b.core) == null ? void 0 : _c.fetch;
|
|
715
|
+
const headers = new Headers(requestInit.headers);
|
|
716
|
+
headers.set("Authorization", authorization.startsWith("Bearer ") ? authorization : `Bearer ${authorization}`);
|
|
717
|
+
headers.set("Accept", "application/json");
|
|
718
|
+
const response = typeof customFetch === "function" ? await customFetch(url, {
|
|
719
|
+
...requestInit,
|
|
720
|
+
headers
|
|
721
|
+
}) : await context.coreApi.fetchFile(url, {
|
|
722
|
+
...requestInit,
|
|
723
|
+
headers
|
|
724
|
+
});
|
|
725
|
+
const json = await response.json();
|
|
726
|
+
if (!response.ok) {
|
|
727
|
+
throw new Error((json == null ? void 0 : json.message) || `${response.status} ${response.statusText}`);
|
|
728
|
+
}
|
|
729
|
+
return json;
|
|
730
|
+
}
|
|
731
|
+
function normalizeSnowflakeBindings(parameters) {
|
|
732
|
+
if (!parameters || Array.isArray(parameters)) {
|
|
733
|
+
return void 0;
|
|
734
|
+
}
|
|
735
|
+
const bindings = {};
|
|
736
|
+
for (const [key, value] of Object.entries(parameters)) {
|
|
737
|
+
bindings[key] = {
|
|
738
|
+
type: typeof value === "number" ? "FIXED" : "TEXT",
|
|
739
|
+
value
|
|
740
|
+
};
|
|
741
|
+
}
|
|
742
|
+
return bindings;
|
|
743
|
+
}
|
|
744
|
+
function getSnowflakeStatementsUrl(url, options) {
|
|
745
|
+
return `${getSnowflakeBaseUrl(url, options)}/api/v2/statements`;
|
|
746
|
+
}
|
|
747
|
+
function getSnowflakeBaseUrl(url, options) {
|
|
748
|
+
var _a;
|
|
749
|
+
if (/^https?:\/\//i.test(url)) {
|
|
750
|
+
return url.replace(/\/api\/v2\/statements.*$/, "");
|
|
751
|
+
}
|
|
752
|
+
const parsedUrl = new URL(url);
|
|
753
|
+
const account = ((_a = options.snowflake) == null ? void 0 : _a.account) || parsedUrl.hostname;
|
|
754
|
+
if (!account) {
|
|
755
|
+
throw new Error("SnowflakeSQLSource requires a Snowflake account URL or snowflake.account.");
|
|
756
|
+
}
|
|
757
|
+
return `https://${account}.snowflakecomputing.com`;
|
|
758
|
+
}
|
|
759
|
+
//# sourceMappingURL=index.cjs.map
|