@malloydata/db-trino 0.0.138-dev240401194552 → 0.0.138-dev240402021905

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.
@@ -57,6 +57,8 @@ export declare class TrinoConnection implements Connection, PersistSQLResults {
57
57
  }>;
58
58
  private structDefFromSqlBlock;
59
59
  private executeAndWait;
60
+ malloyTypeFromTrinoType(name: string, trinoType: string): FieldAtomicTypeDef | StructDef;
61
+ structDefFromSchema(rows: string[][], structDef: StructDef): void;
60
62
  private loadSchemaForSqlBlock;
61
63
  estimateQueryCost(_sqlCommand: string): Promise<QueryRunStats>;
62
64
  executeSQLRaw(_sqlCommand: string): Promise<QueryData>;
@@ -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'},
@@ -315,8 +318,85 @@ class TrinoConnection {
315
318
  while (!(await result.next()).done)
316
319
  ;
317
320
  }
321
+ malloyTypeFromTrinoType(name, trinoType) {
322
+ var _a;
323
+ let malloyType;
324
+ // Arrays look like `array(type)`
325
+ const arrayMatch = trinoType.match(/^array\((.*)\)$/);
326
+ // Structs look like `row(name type, name type)`
327
+ const structMatch = trinoType.match(/^row\((.*)\)$/);
328
+ if (arrayMatch) {
329
+ const arrayType = arrayMatch[1];
330
+ const innerType = this.malloyTypeFromTrinoType(name, arrayType);
331
+ malloyType = {
332
+ type: 'struct',
333
+ name,
334
+ dialect: this.dialectName,
335
+ structSource: { type: 'nested' },
336
+ structRelationship: {
337
+ type: 'nested',
338
+ fieldName: name,
339
+ isArray: true,
340
+ },
341
+ fields: [{ ...innerType, name: 'value' }],
342
+ };
343
+ }
344
+ else if (structMatch) {
345
+ // TODO: Trino doesn't quote or escape commas in field names,
346
+ // so some magic is going to need to be applied before we get here
347
+ // to avoid confusion if a field name contains a comma
348
+ const innerTypes = structMatch[1].split(/,\s+/);
349
+ malloyType = {
350
+ type: 'struct',
351
+ name,
352
+ dialect: this.dialectName,
353
+ structSource: { type: 'nested' },
354
+ structRelationship: {
355
+ type: 'nested',
356
+ fieldName: name,
357
+ isArray: false,
358
+ },
359
+ fields: [],
360
+ };
361
+ for (let innerType of innerTypes) {
362
+ // TODO: Handle time zone type annotation, which is an
363
+ // exception to the types not containing spaces assumption
364
+ innerType = innerType.replace(/ with time zone$/, '');
365
+ const parts = innerType.match(/^(.*)\s(\S+)$/);
366
+ if (parts) {
367
+ const innerName = parts[1];
368
+ const innerTrinoType = parts[2];
369
+ const innerMalloyType = this.malloyTypeFromTrinoType(innerName, innerTrinoType);
370
+ malloyType.fields.push({ ...innerMalloyType, name: innerName });
371
+ }
372
+ else {
373
+ malloyType.fields.push({
374
+ name: 'unknown',
375
+ type: 'unsupported',
376
+ rawType: innerType.toLowerCase(),
377
+ });
378
+ }
379
+ }
380
+ }
381
+ else {
382
+ malloyType = (_a = this.sqlToMalloyType(trinoType)) !== null && _a !== void 0 ? _a : {
383
+ type: 'unsupported',
384
+ rawType: trinoType.toLowerCase(),
385
+ };
386
+ }
387
+ return malloyType;
388
+ }
389
+ structDefFromSchema(rows, structDef) {
390
+ for (const row of rows) {
391
+ const name = row[0];
392
+ const type = row[1] || row[4];
393
+ const malloyType = this.malloyTypeFromTrinoType(name, type);
394
+ // console.log('>', row, '\n<', malloyType);
395
+ structDef.fields.push({ name, ...malloyType });
396
+ }
397
+ }
318
398
  async loadSchemaForSqlBlock(sqlBlock, structDef, element) {
319
- var _a, _b;
399
+ var _a;
320
400
  try {
321
401
  const result = await this.trino.query(sqlBlock);
322
402
  const queryResult = await result.next();
@@ -325,15 +405,7 @@ class TrinoConnection {
325
405
  throw new Error(`Failed to grab schema for ${element}: ${JSON.stringify(queryResult.value.error)}`);
326
406
  }
327
407
  const rows = (_a = queryResult.value.data) !== null && _a !== void 0 ? _a : [];
328
- for (const row of rows) {
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
- }
408
+ this.structDefFromSchema(rows, structDef);
337
409
  }
338
410
  catch (e) {
339
411
  throw new Error(`Could not fetch schema for ${element} ${e}`);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,120 @@
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
+ {
100
+ 'name': 'value',
101
+ 'type': 'struct',
102
+ 'dialect': 'trino',
103
+ 'structRelationship': {
104
+ 'fieldName': 'test',
105
+ 'isArray': false,
106
+ 'type': 'nested',
107
+ },
108
+ 'structSource': { 'type': 'nested' },
109
+ 'fields': [
110
+ { 'name': 'a', 'numberType': 'float', 'type': 'number' },
111
+ { 'name': 'b', 'numberType': 'integer', 'type': 'number' },
112
+ { 'name': 'c', 'type': 'string' },
113
+ ],
114
+ },
115
+ ],
116
+ });
117
+ });
118
+ });
119
+ });
120
+ //# sourceMappingURL=trino_connection.spec.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/db-trino",
3
- "version": "0.0.138-dev240401194552",
3
+ "version": "0.0.138-dev240402021905",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",