Skip to content

Commit

Permalink
Return actual results. (#1697)
Browse files Browse the repository at this point in the history
* start of convert

* Convert rows to structs.

* nested array test.
  • Loading branch information
lloydtabb authored Apr 2, 2024
1 parent 3939785 commit 865a05a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 28 deletions.
19 changes: 3 additions & 16 deletions packages/malloy-db-trino/src/trino_connection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,9 @@ describe('Trino connection', () => {
},
'structSource': {'type': 'nested'},
'fields': [
{
'name': 'value',
'type': 'struct',
'dialect': 'trino',
'structRelationship': {
'fieldName': 'test',
'isArray': false,
'type': 'nested',
},
'structSource': {'type': 'nested'},
'fields': [
{'name': 'a', 'numberType': 'float', 'type': 'number'},
{'name': 'b', 'numberType': 'integer', 'type': 'number'},
{'name': 'c', 'type': 'string'},
],
},
{'name': 'a', 'numberType': 'float', 'type': 'number'},
{'name': 'b', 'numberType': 'integer', 'type': 'number'},
{'name': 'c', 'type': 'string'},
],
});
});
Expand Down
66 changes: 54 additions & 12 deletions packages/malloy-db-trino/src/trino_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,29 @@ export class TrinoConnection implements Connection, PersistSQLResults {
}
}*/

convertRow(structDef: StructDef, row: unknown[]) {
const col = {};
for (let i = 0; i < structDef.fields.length; i++) {
col[structDef.fields[i].name] = row[i];
}
return col;
}

convertNest(structDef: StructDef, dataRows: unknown[][]) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const ret: any[] = [];
if (
structDef.structRelationship.type === 'nested' &&
!structDef.structRelationship.isArray
) {
return this.convertRow(structDef, dataRows);
}
for (const row of dataRows) {
ret.push(this.convertRow(structDef, row));
}
return ret;
}

public async runSQL(
sqlCommand: string,
options: RunSQLOptions = {},
Expand All @@ -270,6 +293,14 @@ export class TrinoConnection implements Connection, PersistSQLResults {
);
}

const malloyColumns = queryResult.value.columns.map(c =>
this.malloyTypeFromTrinoType(c.name, c.type)
);

// Debugging types
// const _x = queryResult.value.columns.map(c => console.log(c.type));
// console.log(JSON.stringify(malloyColumns, null, 2));

let maxRows = options.rowLimit ?? 50;
const malloyRows: QueryDataRow[] = [];
while (queryResult !== null && maxRows--) {
Expand All @@ -278,9 +309,11 @@ export class TrinoConnection implements Connection, PersistSQLResults {
const malloyRow: QueryDataRow = {};
for (let i = 0; i < queryResult.value.columns.length; i++) {
const column = queryResult.value.columns[i];
// TODO: handle arrays etc.
if (column.type === 'json') {
malloyRow[column.name] = JSON.parse(row[i]) as QueryValue;
if (malloyColumns[i].type === 'struct') {
malloyRow[column.name] = this.convertNest(
malloyColumns[i] as StructDef,
row[i]
) as QueryValue;
} else {
malloyRow[column.name] = row[i] as QueryValue;
}
Expand Down Expand Up @@ -468,18 +501,27 @@ export class TrinoConnection implements Connection, PersistSQLResults {
if (arrayMatch) {
const arrayType = arrayMatch[1];
const innerType = this.malloyTypeFromTrinoType(name, arrayType);
malloyType = {
type: 'struct',
name,
dialect: this.dialectName,
structSource: {type: 'nested'},
structRelationship: {
if (innerType.type === 'struct') {
malloyType = innerType;
malloyType.structRelationship = {
type: 'nested',
fieldName: name,
isArray: true,
},
fields: [{...innerType, name: 'value'} as FieldTypeDef],
};
};
} else {
malloyType = {
type: 'struct',
name,
dialect: this.dialectName,
structSource: {type: 'nested'},
structRelationship: {
type: 'nested',
fieldName: name,
isArray: true,
},
fields: [{...innerType, name: 'value'} as FieldTypeDef],
};
}
} else if (structMatch) {
// TODO: Trino doesn't quote or escape commas in field names,
// so some magic is going to need to be applied before we get here
Expand Down

0 comments on commit 865a05a

Please sign in to comment.