@malloydata/db-trino 0.0.138-dev240401194552 → 0.0.138-dev240402170711
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.
|
@@ -38,6 +38,8 @@ export declare class TrinoConnection implements Connection, PersistSQLResults {
|
|
|
38
38
|
canStream(): this is StreamingConnection;
|
|
39
39
|
get supportsNesting(): boolean;
|
|
40
40
|
manifestTemporaryTable(_sqlCommand: string): Promise<string>;
|
|
41
|
+
convertRow(structDef: StructDef, row: unknown[]): {};
|
|
42
|
+
convertNest(structDef: StructDef, dataRows: unknown[][]): {};
|
|
41
43
|
runSQL(sqlCommand: string, options?: RunSQLOptions, _rowIndex?: number): Promise<MalloyQueryData>;
|
|
42
44
|
runSQLBlockAndFetchResultSchema(_sqlBlock: SQLBlock, _options?: RunSQLOptions): Promise<{
|
|
43
45
|
data: MalloyQueryData;
|
|
@@ -57,6 +59,8 @@ export declare class TrinoConnection implements Connection, PersistSQLResults {
|
|
|
57
59
|
}>;
|
|
58
60
|
private structDefFromSqlBlock;
|
|
59
61
|
private executeAndWait;
|
|
62
|
+
malloyTypeFromTrinoType(name: string, trinoType: string): FieldAtomicTypeDef | StructDef;
|
|
63
|
+
structDefFromSchema(rows: string[][], structDef: StructDef): void;
|
|
60
64
|
private loadSchemaForSqlBlock;
|
|
61
65
|
estimateQueryCost(_sqlCommand: string): Promise<QueryRunStats>;
|
|
62
66
|
executeSQLRaw(_sqlCommand: string): Promise<QueryData>;
|
package/dist/trino_connection.js
CHANGED
|
@@ -42,8 +42,11 @@ class TrinoConnection {
|
|
|
42
42
|
'integer': { type: 'number', numberType: 'integer' },
|
|
43
43
|
'bigint': { type: 'number', numberType: 'integer' },
|
|
44
44
|
'double': { type: 'number', numberType: 'float' },
|
|
45
|
+
'decimal': { type: 'number', numberType: 'float' },
|
|
45
46
|
'string': { type: 'string' },
|
|
46
47
|
'date': { type: 'date' },
|
|
48
|
+
'timestamp': { type: 'timestamp' },
|
|
49
|
+
'boolean': { type: 'boolean' },
|
|
47
50
|
// TODO(figutierrez0): cleanup.
|
|
48
51
|
/* 'INT64': {type: 'number', numberType: 'integer'},
|
|
49
52
|
'FLOAT': {type: 'number', numberType: 'float'},
|
|
@@ -163,6 +166,25 @@ class TrinoConnection {
|
|
|
163
166
|
throw maybeRewriteError(e);
|
|
164
167
|
}
|
|
165
168
|
}*/
|
|
169
|
+
convertRow(structDef, row) {
|
|
170
|
+
const col = {};
|
|
171
|
+
for (let i = 0; i < structDef.fields.length; i++) {
|
|
172
|
+
col[structDef.fields[i].name] = row[i];
|
|
173
|
+
}
|
|
174
|
+
return col;
|
|
175
|
+
}
|
|
176
|
+
convertNest(structDef, dataRows) {
|
|
177
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
178
|
+
const ret = [];
|
|
179
|
+
if (structDef.structRelationship.type === 'nested' &&
|
|
180
|
+
!structDef.structRelationship.isArray) {
|
|
181
|
+
return this.convertRow(structDef, dataRows);
|
|
182
|
+
}
|
|
183
|
+
for (const row of dataRows) {
|
|
184
|
+
ret.push(this.convertRow(structDef, row));
|
|
185
|
+
}
|
|
186
|
+
return ret;
|
|
187
|
+
}
|
|
166
188
|
async runSQL(sqlCommand, options = {},
|
|
167
189
|
// TODO(figutierrez): Use.
|
|
168
190
|
_rowIndex = 0) {
|
|
@@ -173,6 +195,10 @@ class TrinoConnection {
|
|
|
173
195
|
// TODO: handle.
|
|
174
196
|
throw new Error(`Failed to execute sql: ${sqlCommand}. \n Error: ${JSON.stringify(queryResult.value.error)}`);
|
|
175
197
|
}
|
|
198
|
+
const malloyColumns = queryResult.value.columns.map(c => this.malloyTypeFromTrinoType(c.name, c.type));
|
|
199
|
+
// Debugging types
|
|
200
|
+
// const _x = queryResult.value.columns.map(c => console.log(c.type));
|
|
201
|
+
// console.log(JSON.stringify(malloyColumns, null, 2));
|
|
176
202
|
let maxRows = (_a = options.rowLimit) !== null && _a !== void 0 ? _a : 50;
|
|
177
203
|
const malloyRows = [];
|
|
178
204
|
while (queryResult !== null && maxRows--) {
|
|
@@ -181,9 +207,8 @@ class TrinoConnection {
|
|
|
181
207
|
const malloyRow = {};
|
|
182
208
|
for (let i = 0; i < queryResult.value.columns.length; i++) {
|
|
183
209
|
const column = queryResult.value.columns[i];
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
malloyRow[column.name] = JSON.parse(row[i]);
|
|
210
|
+
if (malloyColumns[i].type === 'struct') {
|
|
211
|
+
malloyRow[column.name] = this.convertNest(malloyColumns[i], row[i]);
|
|
187
212
|
}
|
|
188
213
|
else {
|
|
189
214
|
malloyRow[column.name] = row[i];
|
|
@@ -315,8 +340,95 @@ class TrinoConnection {
|
|
|
315
340
|
while (!(await result.next()).done)
|
|
316
341
|
;
|
|
317
342
|
}
|
|
343
|
+
malloyTypeFromTrinoType(name, trinoType) {
|
|
344
|
+
var _a;
|
|
345
|
+
let malloyType;
|
|
346
|
+
// Arrays look like `array(type)`
|
|
347
|
+
const arrayMatch = trinoType.match(/^array\((.*)\)$/);
|
|
348
|
+
// Structs look like `row(name type, name type)`
|
|
349
|
+
const structMatch = trinoType.match(/^row\((.*)\)$/);
|
|
350
|
+
if (arrayMatch) {
|
|
351
|
+
const arrayType = arrayMatch[1];
|
|
352
|
+
const innerType = this.malloyTypeFromTrinoType(name, arrayType);
|
|
353
|
+
if (innerType.type === 'struct') {
|
|
354
|
+
malloyType = innerType;
|
|
355
|
+
malloyType.structRelationship = {
|
|
356
|
+
type: 'nested',
|
|
357
|
+
fieldName: name,
|
|
358
|
+
isArray: true,
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
else {
|
|
362
|
+
malloyType = {
|
|
363
|
+
type: 'struct',
|
|
364
|
+
name,
|
|
365
|
+
dialect: this.dialectName,
|
|
366
|
+
structSource: { type: 'nested' },
|
|
367
|
+
structRelationship: {
|
|
368
|
+
type: 'nested',
|
|
369
|
+
fieldName: name,
|
|
370
|
+
isArray: true,
|
|
371
|
+
},
|
|
372
|
+
fields: [{ ...innerType, name: 'value' }],
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
else if (structMatch) {
|
|
377
|
+
// TODO: Trino doesn't quote or escape commas in field names,
|
|
378
|
+
// so some magic is going to need to be applied before we get here
|
|
379
|
+
// to avoid confusion if a field name contains a comma
|
|
380
|
+
const innerTypes = structMatch[1].split(/,\s+/);
|
|
381
|
+
malloyType = {
|
|
382
|
+
type: 'struct',
|
|
383
|
+
name,
|
|
384
|
+
dialect: this.dialectName,
|
|
385
|
+
structSource: { type: 'nested' },
|
|
386
|
+
structRelationship: {
|
|
387
|
+
type: 'nested',
|
|
388
|
+
fieldName: name,
|
|
389
|
+
isArray: false,
|
|
390
|
+
},
|
|
391
|
+
fields: [],
|
|
392
|
+
};
|
|
393
|
+
for (let innerType of innerTypes) {
|
|
394
|
+
// TODO: Handle time zone type annotation, which is an
|
|
395
|
+
// exception to the types not containing spaces assumption
|
|
396
|
+
innerType = innerType.replace(/ with time zone$/, '');
|
|
397
|
+
const parts = innerType.match(/^(.*)\s(\S+)$/);
|
|
398
|
+
if (parts) {
|
|
399
|
+
const innerName = parts[1];
|
|
400
|
+
const innerTrinoType = parts[2];
|
|
401
|
+
const innerMalloyType = this.malloyTypeFromTrinoType(innerName, innerTrinoType);
|
|
402
|
+
malloyType.fields.push({ ...innerMalloyType, name: innerName });
|
|
403
|
+
}
|
|
404
|
+
else {
|
|
405
|
+
malloyType.fields.push({
|
|
406
|
+
name: 'unknown',
|
|
407
|
+
type: 'unsupported',
|
|
408
|
+
rawType: innerType.toLowerCase(),
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
else {
|
|
414
|
+
malloyType = (_a = this.sqlToMalloyType(trinoType)) !== null && _a !== void 0 ? _a : {
|
|
415
|
+
type: 'unsupported',
|
|
416
|
+
rawType: trinoType.toLowerCase(),
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
return malloyType;
|
|
420
|
+
}
|
|
421
|
+
structDefFromSchema(rows, structDef) {
|
|
422
|
+
for (const row of rows) {
|
|
423
|
+
const name = row[0];
|
|
424
|
+
const type = row[1] || row[4];
|
|
425
|
+
const malloyType = this.malloyTypeFromTrinoType(name, type);
|
|
426
|
+
// console.log('>', row, '\n<', malloyType);
|
|
427
|
+
structDef.fields.push({ name, ...malloyType });
|
|
428
|
+
}
|
|
429
|
+
}
|
|
318
430
|
async loadSchemaForSqlBlock(sqlBlock, structDef, element) {
|
|
319
|
-
var _a
|
|
431
|
+
var _a;
|
|
320
432
|
try {
|
|
321
433
|
const result = await this.trino.query(sqlBlock);
|
|
322
434
|
const queryResult = await result.next();
|
|
@@ -325,15 +437,7 @@ class TrinoConnection {
|
|
|
325
437
|
throw new Error(`Failed to grab schema for ${element}: ${JSON.stringify(queryResult.value.error)}`);
|
|
326
438
|
}
|
|
327
439
|
const rows = (_a = queryResult.value.data) !== null && _a !== void 0 ? _a : [];
|
|
328
|
-
|
|
329
|
-
const fieldName = row[0];
|
|
330
|
-
const type = row[1];
|
|
331
|
-
const malloyType = (_b = this.sqlToMalloyType(type)) !== null && _b !== void 0 ? _b : {
|
|
332
|
-
type: 'unsupported',
|
|
333
|
-
rawType: type.toLowerCase(),
|
|
334
|
-
};
|
|
335
|
-
structDef.fields.push({ name: fieldName, ...malloyType });
|
|
336
|
-
}
|
|
440
|
+
this.structDefFromSchema(rows, structDef);
|
|
337
441
|
}
|
|
338
442
|
catch (e) {
|
|
339
443
|
throw new Error(`Could not fetch schema for ${element} ${e}`);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright 2024 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
* a copy of this software and associated documentation files
|
|
7
|
+
* (the "Software"), to deal in the Software without restriction,
|
|
8
|
+
* including without limitation the rights to use, copy, modify, merge,
|
|
9
|
+
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
10
|
+
* and to permit persons to whom the Software is furnished to do so,
|
|
11
|
+
* subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be
|
|
14
|
+
* included in all copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
20
|
+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
22
|
+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
+
const _1 = require(".");
|
|
26
|
+
const ARRAY_SCHEMA = 'array(integer)';
|
|
27
|
+
const STRUCT_SCHEMA = 'row(a double, b integer, c varchar(60))';
|
|
28
|
+
const DEEP_SCHEMA = 'array(row(a double, b integer, c varchar(60)))';
|
|
29
|
+
describe('Trino connection', () => {
|
|
30
|
+
describe('schema parser', () => {
|
|
31
|
+
it('parses arrays', () => {
|
|
32
|
+
const connection = new _1.TrinoConnection('trino', {}, _1.TrinoExecutor.getConnectionOptionsFromEnv());
|
|
33
|
+
expect(connection.malloyTypeFromTrinoType('test', ARRAY_SCHEMA)).toEqual({
|
|
34
|
+
'name': 'test',
|
|
35
|
+
'type': 'struct',
|
|
36
|
+
'dialect': 'trino',
|
|
37
|
+
'structRelationship': {
|
|
38
|
+
'fieldName': 'test',
|
|
39
|
+
'isArray': true,
|
|
40
|
+
'type': 'nested',
|
|
41
|
+
},
|
|
42
|
+
'structSource': {
|
|
43
|
+
'type': 'nested',
|
|
44
|
+
},
|
|
45
|
+
'fields': [
|
|
46
|
+
{
|
|
47
|
+
'name': 'value',
|
|
48
|
+
'type': 'number',
|
|
49
|
+
'numberType': 'integer',
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
it('parses structs', () => {
|
|
55
|
+
const connection = new _1.TrinoConnection('trino', {}, _1.TrinoExecutor.getConnectionOptionsFromEnv());
|
|
56
|
+
expect(connection.malloyTypeFromTrinoType('test', STRUCT_SCHEMA)).toEqual({
|
|
57
|
+
'name': 'test',
|
|
58
|
+
'type': 'struct',
|
|
59
|
+
'dialect': 'trino',
|
|
60
|
+
'structRelationship': {
|
|
61
|
+
'fieldName': 'test',
|
|
62
|
+
'isArray': false,
|
|
63
|
+
'type': 'nested',
|
|
64
|
+
},
|
|
65
|
+
'structSource': {
|
|
66
|
+
'type': 'nested',
|
|
67
|
+
},
|
|
68
|
+
'fields': [
|
|
69
|
+
{
|
|
70
|
+
'name': 'a',
|
|
71
|
+
'type': 'number',
|
|
72
|
+
'numberType': 'float',
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
'name': 'b',
|
|
76
|
+
'type': 'number',
|
|
77
|
+
'numberType': 'integer',
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
'name': 'c',
|
|
81
|
+
'type': 'string',
|
|
82
|
+
},
|
|
83
|
+
],
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
it('parses arrays of structs', () => {
|
|
87
|
+
const connection = new _1.TrinoConnection('trino', {}, _1.TrinoExecutor.getConnectionOptionsFromEnv());
|
|
88
|
+
expect(connection.malloyTypeFromTrinoType('test', DEEP_SCHEMA)).toEqual({
|
|
89
|
+
'name': 'test',
|
|
90
|
+
'type': 'struct',
|
|
91
|
+
'dialect': 'trino',
|
|
92
|
+
'structRelationship': {
|
|
93
|
+
'fieldName': 'test',
|
|
94
|
+
'isArray': true,
|
|
95
|
+
'type': 'nested',
|
|
96
|
+
},
|
|
97
|
+
'structSource': { 'type': 'nested' },
|
|
98
|
+
'fields': [
|
|
99
|
+
{ 'name': 'a', 'numberType': 'float', 'type': 'number' },
|
|
100
|
+
{ 'name': 'b', 'numberType': 'integer', 'type': 'number' },
|
|
101
|
+
{ 'name': 'c', 'type': 'string' },
|
|
102
|
+
],
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
//# sourceMappingURL=trino_connection.spec.js.map
|