@malloydata/db-trino 0.0.138-dev240403210124 → 0.0.138-dev240404145618

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,8 +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
+ convertRow(structDef: StructDef, _row: unknown): {};
42
+ convertNest(structDef: StructDef, data: unknown): {};
43
43
  runSQL(sqlCommand: string, options?: RunSQLOptions, _rowIndex?: number): Promise<MalloyQueryData>;
44
44
  runSQLBlockAndFetchResultSchema(_sqlBlock: SQLBlock, _options?: RunSQLOptions): Promise<{
45
45
  data: MalloyQueryData;
@@ -166,20 +166,36 @@ class TrinoConnection {
166
166
  throw maybeRewriteError(e);
167
167
  }
168
168
  }*/
169
- convertRow(structDef, row) {
170
- const col = {};
169
+ convertRow(structDef, _row) {
170
+ const retRow = {};
171
+ const row = _row;
171
172
  for (let i = 0; i < structDef.fields.length; i++) {
172
- col[structDef.fields[i].name] = row[i] === undefined ? null : row[i];
173
+ const field = structDef.fields[i];
174
+ if (field.type === 'struct') {
175
+ const struct = field;
176
+ if (struct.structSource.type === 'inline') {
177
+ retRow[field.name] = this.convertRow(struct, row[i]);
178
+ }
179
+ else {
180
+ retRow[field.name] = this.convertNest(struct, row[i]);
181
+ }
182
+ }
183
+ else {
184
+ retRow[field.name] = row[i] === undefined ? null : row[i];
185
+ }
173
186
  }
174
- return col;
187
+ //console.log(retRow);
188
+ return retRow;
175
189
  }
176
- convertNest(structDef, dataRows) {
177
- const rows = dataRows === null || dataRows === undefined ? [] : dataRows;
190
+ convertNest(structDef, data) {
178
191
  const ret = [];
179
- if (structDef.structRelationship.type === 'nested' &&
180
- !structDef.structRelationship.isArray) {
181
- return this.convertRow(structDef, rows);
192
+ //console.log(
193
+ // `${JSON.stringify(structDef, null, 2)} ${JSON.stringify(data, null, 2)} `
194
+ // );
195
+ if (structDef.structSource.type === 'inline') {
196
+ return this.convertRow(structDef, data);
182
197
  }
198
+ const rows = (data === null || data === undefined ? [] : data);
183
199
  for (const row of rows) {
184
200
  ret.push(this.convertRow(structDef, row));
185
201
  }
@@ -210,7 +226,19 @@ class TrinoConnection {
210
226
  for (let i = 0; i < queryResult.value.columns.length; i++) {
211
227
  const column = queryResult.value.columns[i];
212
228
  if (malloyColumns[i].type === 'struct') {
213
- malloyRow[column.name] = this.convertNest(malloyColumns[i], row[i]);
229
+ const structDef = malloyColumns[i];
230
+ if (structDef.structSource.type === 'inline') {
231
+ malloyRow[column.name] = this.convertRow(structDef, row[i]);
232
+ }
233
+ else {
234
+ malloyRow[column.name] = this.convertNest(structDef, row[i]);
235
+ }
236
+ // console.log(
237
+ // column.name,
238
+ // JSON.stringify(malloyColumns[i], null, 2),
239
+ // JSON.stringify(row[i]),
240
+ // JSON.stringify(malloyRow[column.name])
241
+ // );
214
242
  }
215
243
  else {
216
244
  malloyRow[column.name] = row[i];
@@ -377,14 +405,16 @@ class TrinoConnection {
377
405
  var _a;
378
406
  let malloyType;
379
407
  // Arrays look like `array(type)`
380
- const arrayMatch = trinoType.match(/^array\((.*)\)$/);
408
+ const arrayMatch = trinoType.match(/^(([^,])+\s)?array\((.*)\)$/);
409
+ // console.log(`${trinoType} arrayMatch: ${arrayMatch}`);
381
410
  // Structs look like `row(name type, name type)`
382
- const structMatch = trinoType.match(/^row\((.*)\)$/);
411
+ const structMatch = trinoType.match(/^(([^,])+\s)?row\((.*)\)$/);
412
+ // console.log(`${trinoType} structMatch: ${structMatch}`);
383
413
  if (arrayMatch) {
384
- const arrayType = arrayMatch[1];
414
+ const arrayType = arrayMatch[3];
385
415
  const innerType = this.malloyTypeFromTrinoType(name, arrayType);
386
416
  if (innerType.type === 'struct') {
387
- malloyType = innerType;
417
+ malloyType = { ...innerType, structSource: { type: 'nested' } };
388
418
  malloyType.structRelationship = {
389
419
  type: 'nested',
390
420
  fieldName: name,
@@ -410,16 +440,15 @@ class TrinoConnection {
410
440
  // TODO: Trino doesn't quote or escape commas in field names,
411
441
  // so some magic is going to need to be applied before we get here
412
442
  // to avoid confusion if a field name contains a comma
413
- const innerTypes = this.splitColumns(structMatch[1]);
443
+ const innerTypes = this.splitColumns(structMatch[3]);
444
+ // console.log(`innerType: ${JSON.stringify(innerTypes)}`);
414
445
  malloyType = {
415
446
  type: 'struct',
416
447
  name,
417
448
  dialect: this.dialectName,
418
- structSource: { type: 'nested' },
449
+ structSource: { type: 'inline' },
419
450
  structRelationship: {
420
- type: 'nested',
421
- fieldName: name,
422
- isArray: false,
451
+ type: 'inline',
423
452
  },
424
453
  fields: [],
425
454
  };
@@ -427,7 +456,10 @@ class TrinoConnection {
427
456
  // TODO: Handle time zone type annotation, which is an
428
457
  // exception to the types not containing spaces assumption
429
458
  innerType = innerType.replace(/ with time zone$/, '');
430
- const parts = innerType.match(/^(.*)\s(\S+)$/);
459
+ let parts = innerType.match(/^(.*)\s((array\(|row\().*)$/);
460
+ if (parts === null) {
461
+ parts = innerType.match(/^(.*)\s(\S+)$/);
462
+ }
431
463
  if (parts) {
432
464
  const innerName = parts[1];
433
465
  const innerTrinoType = parts[2];
@@ -449,6 +481,7 @@ class TrinoConnection {
449
481
  rawType: trinoType.toLowerCase(),
450
482
  };
451
483
  }
484
+ // console.log('>', trinoType, '\n<', malloyType);
452
485
  return malloyType;
453
486
  }
454
487
  structDefFromSchema(rows, structDef) {
@@ -23,9 +23,12 @@
23
23
  */
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
25
  const _1 = require(".");
26
+ // Array(varchar) is array
26
27
  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)))';
28
+ // Row(...) is inline
29
+ const INLINE_SCHEMA = 'row(a double, b integer, c varchar(60))';
30
+ // Array(row(....)) is nested
31
+ const NESTED_SCHEMA = 'array(row(a double, b integer, c varchar(60)))';
29
32
  describe('Trino connection', () => {
30
33
  let connection;
31
34
  beforeAll(() => {
@@ -57,19 +60,16 @@ describe('Trino connection', () => {
57
60
  ],
58
61
  });
59
62
  });
60
- it('parses structs', () => {
61
- const connection = new _1.TrinoConnection('trino', {}, _1.TrinoExecutor.getConnectionOptionsFromEnv());
62
- expect(connection.malloyTypeFromTrinoType('test', STRUCT_SCHEMA)).toEqual({
63
+ it('parses inline', () => {
64
+ expect(connection.malloyTypeFromTrinoType('test', INLINE_SCHEMA)).toEqual({
63
65
  'name': 'test',
64
66
  'type': 'struct',
65
67
  'dialect': 'trino',
66
68
  'structRelationship': {
67
- 'fieldName': 'test',
68
- 'isArray': false,
69
- 'type': 'nested',
69
+ 'type': 'inline',
70
70
  },
71
71
  'structSource': {
72
- 'type': 'nested',
72
+ 'type': 'inline',
73
73
  },
74
74
  'fields': [
75
75
  {
@@ -89,9 +89,8 @@ describe('Trino connection', () => {
89
89
  ],
90
90
  });
91
91
  });
92
- it('parses arrays of structs', () => {
93
- const connection = new _1.TrinoConnection('trino', {}, _1.TrinoExecutor.getConnectionOptionsFromEnv());
94
- expect(connection.malloyTypeFromTrinoType('test', DEEP_SCHEMA)).toEqual({
92
+ it('parses nested', () => {
93
+ expect(connection.malloyTypeFromTrinoType('test', NESTED_SCHEMA)).toEqual({
95
94
  'name': 'test',
96
95
  'type': 'struct',
97
96
  'dialect': 'trino',
@@ -108,6 +107,11 @@ describe('Trino connection', () => {
108
107
  ],
109
108
  });
110
109
  });
110
+ it('parses a simple type', () => {
111
+ expect(connection.malloyTypeFromTrinoType('test', 'varchar(60)')).toEqual({
112
+ 'type': 'string',
113
+ });
114
+ });
111
115
  });
112
116
  describe('splitColumns', () => {
113
117
  it('handles internal rows', () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/db-trino",
3
- "version": "0.0.138-dev240403210124",
3
+ "version": "0.0.138-dev240404145618",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",