@malloydata/db-trino 0.0.135-dev240326020414 → 0.0.135-dev240326154530
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/dist/trino_connection.d.ts +4 -1
- package/dist/trino_connection.js +55 -10
- package/package.json +1 -1
|
@@ -48,13 +48,16 @@ export declare class TrinoConnection implements Connection, PersistSQLResults {
|
|
|
48
48
|
errors: Record<string, string>;
|
|
49
49
|
}>;
|
|
50
50
|
private structDefFromTableSchema;
|
|
51
|
-
fetchSchemaForSQLBlock(
|
|
51
|
+
fetchSchemaForSQLBlock(sqlRef: SQLBlock, { refreshTimestamp }: FetchSchemaOptions): Promise<{
|
|
52
52
|
structDef: StructDef;
|
|
53
53
|
error?: undefined;
|
|
54
54
|
} | {
|
|
55
55
|
error: string;
|
|
56
56
|
structDef?: undefined;
|
|
57
57
|
}>;
|
|
58
|
+
private structDefFromSqlBlock;
|
|
59
|
+
private executeAndWait;
|
|
60
|
+
private loadSchemaForSqlBlock;
|
|
58
61
|
estimateQueryCost(_sqlCommand: string): Promise<QueryRunStats>;
|
|
59
62
|
executeSQLRaw(_sqlCommand: string): Promise<QueryData>;
|
|
60
63
|
test(): Promise<void>;
|
package/dist/trino_connection.js
CHANGED
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
25
|
exports.TrinoConnection = void 0;
|
|
26
26
|
const malloy_1 = require("@malloydata/malloy");
|
|
27
|
+
const crypto_1 = require("crypto");
|
|
27
28
|
const trino_client_1 = require("trino-client");
|
|
28
29
|
// manage access to BQ, control costs, enforce global data/API limits
|
|
29
30
|
class TrinoConnection {
|
|
@@ -254,7 +255,6 @@ class TrinoConnection {
|
|
|
254
255
|
return { schemas: schemas, errors: errors };
|
|
255
256
|
}
|
|
256
257
|
async structDefFromTableSchema(tableKey, tablePath) {
|
|
257
|
-
var _a, _b;
|
|
258
258
|
const structDef = {
|
|
259
259
|
type: 'struct',
|
|
260
260
|
name: tableKey,
|
|
@@ -269,12 +269,63 @@ class TrinoConnection {
|
|
|
269
269
|
},
|
|
270
270
|
fields: [],
|
|
271
271
|
};
|
|
272
|
+
return await this.loadSchemaForSqlBlock(`DESCRIBE ${tablePath}`, structDef, `table ${tablePath}`);
|
|
273
|
+
}
|
|
274
|
+
async fetchSchemaForSQLBlock(sqlRef,
|
|
275
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
276
|
+
{ refreshTimestamp }) {
|
|
277
|
+
const key = sqlRef.name;
|
|
278
|
+
let inCache = this.sqlSchemaCache.get(key);
|
|
279
|
+
if (!inCache ||
|
|
280
|
+
(refreshTimestamp && refreshTimestamp > inCache.timestamp)) {
|
|
281
|
+
const timestamp = refreshTimestamp !== null && refreshTimestamp !== void 0 ? refreshTimestamp : Date.now();
|
|
282
|
+
try {
|
|
283
|
+
inCache = {
|
|
284
|
+
structDef: await this.structDefFromSqlBlock(sqlRef),
|
|
285
|
+
timestamp,
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
catch (error) {
|
|
289
|
+
inCache = { error: error.message, timestamp };
|
|
290
|
+
}
|
|
291
|
+
this.sqlSchemaCache.set(key, inCache);
|
|
292
|
+
}
|
|
293
|
+
return inCache;
|
|
294
|
+
}
|
|
295
|
+
async structDefFromSqlBlock(sqlRef) {
|
|
296
|
+
const structDef = {
|
|
297
|
+
type: 'struct',
|
|
298
|
+
name: sqlRef.name,
|
|
299
|
+
dialect: this.dialectName,
|
|
300
|
+
structSource: {
|
|
301
|
+
type: 'sql',
|
|
302
|
+
method: 'subquery',
|
|
303
|
+
sqlBlock: sqlRef,
|
|
304
|
+
},
|
|
305
|
+
structRelationship: {
|
|
306
|
+
type: 'basetable',
|
|
307
|
+
connectionName: this.name,
|
|
308
|
+
},
|
|
309
|
+
fields: [],
|
|
310
|
+
};
|
|
311
|
+
const tmpQueryName = `myMalloyQuery${(0, crypto_1.randomUUID)().replace(/-/g, '')}`;
|
|
312
|
+
await this.executeAndWait(`PREPARE ${tmpQueryName} FROM ${sqlRef.selectStr}`);
|
|
313
|
+
return await this.loadSchemaForSqlBlock(`DESCRIBE OUTPUT ${tmpQueryName}`, structDef, `query ${sqlRef.selectStr.substring(0, 50)}`);
|
|
314
|
+
}
|
|
315
|
+
async executeAndWait(sqlBlock) {
|
|
316
|
+
const result = await this.trino.query(sqlBlock);
|
|
317
|
+
// TODO: make sure failure is handled correctly.
|
|
318
|
+
while (!(await result.next()).done)
|
|
319
|
+
;
|
|
320
|
+
}
|
|
321
|
+
async loadSchemaForSqlBlock(sqlBlock, structDef, element) {
|
|
322
|
+
var _a, _b;
|
|
272
323
|
try {
|
|
273
|
-
const result = await this.trino.query(
|
|
324
|
+
const result = await this.trino.query(sqlBlock);
|
|
274
325
|
const queryResult = await result.next();
|
|
275
326
|
if (queryResult.value.error) {
|
|
276
327
|
// TODO: handle.
|
|
277
|
-
throw new Error(`Failed to grab schema for
|
|
328
|
+
throw new Error(`Failed to grab schema for ${element}: ${JSON.stringify(queryResult.value.error)}`);
|
|
278
329
|
}
|
|
279
330
|
const rows = (_a = queryResult.value.data) !== null && _a !== void 0 ? _a : [];
|
|
280
331
|
for (const row of rows) {
|
|
@@ -288,16 +339,10 @@ class TrinoConnection {
|
|
|
288
339
|
}
|
|
289
340
|
}
|
|
290
341
|
catch (e) {
|
|
291
|
-
throw new Error(`Could not fetch schema for
|
|
342
|
+
throw new Error(`Could not fetch schema for ${element} ${e}`);
|
|
292
343
|
}
|
|
293
|
-
// TODO: handle repeated etc.
|
|
294
344
|
return structDef;
|
|
295
345
|
}
|
|
296
|
-
async fetchSchemaForSQLBlock(_sqlRef,
|
|
297
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
298
|
-
{ refreshTimestamp }) {
|
|
299
|
-
throw new Error('Not implemented 5');
|
|
300
|
-
}
|
|
301
346
|
/* public async downloadMalloyQuery(
|
|
302
347
|
sqlCommand: string
|
|
303
348
|
): Promise<ResourceStream<RowMetadata>> {
|