Skip to content

Commit

Permalink
Fixes to nested data, read more rows until done.
Browse files Browse the repository at this point in the history
  • Loading branch information
narreola committed Mar 18, 2024
1 parent 5e2fe65 commit c5d75ba
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 185 deletions.
167 changes: 14 additions & 153 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/malloy-db-trino/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@malloydata/db-trino",
"version": "0.0.131",
"version": "0.0.132",
"license": "MIT",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
54 changes: 37 additions & 17 deletions packages/malloy-db-trino/src/trino_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,16 @@ export class TrinoConnection implements Connection, PersistSQLResults {
'string': {type: 'string'},
'date': {type: 'date'},

'STRING': {type: 'string'},
'INTEGER': {type: 'number', numberType: 'integer'},
'INT64': {type: 'number', numberType: 'integer'},
// TODO: cleanup.
/* 'INT64': {type: 'number', numberType: 'integer'},
'FLOAT': {type: 'number', numberType: 'float'},
'FLOAT64': {type: 'number', numberType: 'float'},
'NUMERIC': {type: 'number', numberType: 'float'},
'BIGNUMERIC': {type: 'number', numberType: 'float'},
'TIMESTAMP': {type: 'timestamp'},
'BOOLEAN': {type: 'boolean'},
'BOOL': {type: 'boolean'},
'JSON': {type: 'json'},
'JSON': {type: 'json'},*/
// TODO (https://cloud.google.com/bigquery/docs/reference/rest/v2/tables#tablefieldschema):
// BYTES
// DATETIME
Expand Down Expand Up @@ -265,7 +264,9 @@ export class TrinoConnection implements Connection, PersistSQLResults {
// TODO: fill in with options.
const result = await this.trino.query(sqlCommand);

const queryResult = await result.next();
console.log(`THE SQL ===> ${sqlCommand}`);

let queryResult = await result.next();
if (queryResult.value.error) {
// TODO: handle.
throw new Error(
Expand All @@ -275,21 +276,35 @@ export class TrinoConnection implements Connection, PersistSQLResults {
);
}

const rows = queryResult.value.data ?? [];

const malloyRows: QueryDataRow[] = [];
for (const row of rows) {
const malloyRow: QueryDataRow = {};
for (let i = 0; i < queryResult.value.columns.length; i++) {
const column = queryResult.value.columns[i];
// TODO: handle arrays etc.
malloyRow[column.name] = row[i] as QueryValue;
while (queryResult !== null) {
const rows = queryResult.value.data ?? [];
for (const row of rows) {
const malloyRow: QueryDataRow = {};
for (let i = 0; i < queryResult.value.columns.length; i++) {
console.log(
`COLUMNI ${JSON.stringify(queryResult.value.columns[i])}`
);
const column = queryResult.value.columns[i];
// TODO: handle arrays etc.
if (column.type === 'json') {
malloyRow[column.name] = JSON.parse(row[i]) as QueryValue;
} else {
malloyRow[column.name] = row[i] as QueryValue;
}
}

malloyRows.push(malloyRow);
}

malloyRows.push(malloyRow);
if (!queryResult.done) {
queryResult = await result.next();
} else {
break;
}
}

console.log(`ROWS: ${JSON.stringify(malloyRows)}`);
console.log(`ROWS: ${JSON.stringify(malloyRows)} ${malloyRows.length}`);
// TODO: handle totalrows.
return {rows: malloyRows, totalRows: malloyRows.length};
}
Expand Down Expand Up @@ -325,7 +340,10 @@ export class TrinoConnection implements Connection, PersistSQLResults {

for (const tableKey in missing) {
let inCache = this.schemaCache.get(tableKey);
const tablePath = missing[tableKey].replace(/malloytest/g, "malloy_demo.faa");
const tablePath = missing[tableKey].replace(
/malloytest/g,
'malloy_demo.faa'
);
if (
!inCache ||
(refreshTimestamp && refreshTimestamp > inCache.timestamp)
Expand Down Expand Up @@ -384,7 +402,9 @@ export class TrinoConnection implements Connection, PersistSQLResults {
if (queryResult.value.error) {
// TODO: handle.
throw new Error(
`Failed to grab schema for table ${tablePath}: ${JSON.stringify(queryResult.value.error)}`
`Failed to grab schema for table ${tablePath}: ${JSON.stringify(
queryResult.value.error
)}`
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/malloy/src/dialect/trino/trino.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export class TrinoDialect extends Dialect {
const fields = fieldList
.map(f => `\n '${f.sqlOutputName}' VALUE ${f.sqlExpression}`)
.join(', ');
return `COALESCE(SLICE(COALESCE(ARRAY_AGG(CASE WHEN group_set=${groupSet} THEN JSON_OBJECT(${fields}) END \n ${orderBy} -- ${tail}\n), ARRAY[]), 1, ${limit}), ARRAY[])`;
return `cast(COALESCE(SLICE(COALESCE(ARRAY_AGG(CASE WHEN group_set=${groupSet} THEN JSON_PARSE(JSON_OBJECT(${fields})) END \n ${orderBy} -- ${tail}\n), ARRAY[]), 1, ${limit}), ARRAY[]) as json)`;
}

sqlAnyValueTurtle(groupSet: number, fieldList: DialectFieldList): string {
Expand Down
2 changes: 1 addition & 1 deletion test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"@malloydata/db-snowflake": "^0.0.132",
"@malloydata/malloy": "^0.0.132",
"@malloydata/render": "^0.0.132",
"@malloydata/db-trino": "^0.0.1",
"@malloydata/db-trino": "^0.0.132",
"jsdom": "^22.1.0",
"luxon": "^2.4.0",
"madge": "^6.0.0"
Expand Down
15 changes: 3 additions & 12 deletions test/src/databases/trino/trino.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,9 @@ describe('Trino tests', () => {
// Issue: #151
it(`Basic trino - ${databaseName}`, async () => {
await expect(`
run: trino.table('sample.burstbank.customer') -> {
group_by: city
aggregate: avgIncome is avg(estimated_income)
aggregate: counti is count()
nest: foo is {
select: estimated_income
order_by: estimated_income
limit: 5
}
order_by: counti desc
limit: 5
}
run: trino.table('malloy_demo.faa.aircraft') -> {
select: *
}
`).malloyResultMatches(runtime, {custkey: '1000001'});
});
});
Expand Down

0 comments on commit c5d75ba

Please sign in to comment.