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

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;
@@ -166,6 +166,25 @@ class TrinoConnection {
166
166
  throw maybeRewriteError(e);
167
167
  }
168
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
+ }
169
188
  async runSQL(sqlCommand, options = {},
170
189
  // TODO(figutierrez): Use.
171
190
  _rowIndex = 0) {
@@ -176,6 +195,10 @@ class TrinoConnection {
176
195
  // TODO: handle.
177
196
  throw new Error(`Failed to execute sql: ${sqlCommand}. \n Error: ${JSON.stringify(queryResult.value.error)}`);
178
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));
179
202
  let maxRows = (_a = options.rowLimit) !== null && _a !== void 0 ? _a : 50;
180
203
  const malloyRows = [];
181
204
  while (queryResult !== null && maxRows--) {
@@ -184,9 +207,8 @@ class TrinoConnection {
184
207
  const malloyRow = {};
185
208
  for (let i = 0; i < queryResult.value.columns.length; i++) {
186
209
  const column = queryResult.value.columns[i];
187
- // TODO: handle arrays etc.
188
- if (column.type === 'json') {
189
- malloyRow[column.name] = JSON.parse(row[i]);
210
+ if (malloyColumns[i].type === 'struct') {
211
+ malloyRow[column.name] = this.convertNest(malloyColumns[i], row[i]);
190
212
  }
191
213
  else {
192
214
  malloyRow[column.name] = row[i];
@@ -328,18 +350,28 @@ class TrinoConnection {
328
350
  if (arrayMatch) {
329
351
  const arrayType = arrayMatch[1];
330
352
  const innerType = this.malloyTypeFromTrinoType(name, arrayType);
331
- malloyType = {
332
- type: 'struct',
333
- name,
334
- dialect: this.dialectName,
335
- structSource: { type: 'nested' },
336
- structRelationship: {
353
+ if (innerType.type === 'struct') {
354
+ malloyType = innerType;
355
+ malloyType.structRelationship = {
337
356
  type: 'nested',
338
357
  fieldName: name,
339
358
  isArray: true,
340
- },
341
- fields: [{ ...innerType, name: 'value' }],
342
- };
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
+ }
343
375
  }
344
376
  else if (structMatch) {
345
377
  // TODO: Trino doesn't quote or escape commas in field names,
@@ -96,22 +96,9 @@ describe('Trino connection', () => {
96
96
  },
97
97
  'structSource': { 'type': 'nested' },
98
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
- },
99
+ { 'name': 'a', 'numberType': 'float', 'type': 'number' },
100
+ { 'name': 'b', 'numberType': 'integer', 'type': 'number' },
101
+ { 'name': 'c', 'type': 'string' },
115
102
  ],
116
103
  });
117
104
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/db-trino",
3
- "version": "0.0.138-dev240402021905",
3
+ "version": "0.0.138-dev240402171233",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",