Skip to content

Commit 7f251c3

Browse files
fix trino version of schema parser
1 parent 446f04c commit 7f251c3

File tree

2 files changed

+25
-35
lines changed

2 files changed

+25
-35
lines changed

packages/malloy-db-trino/src/trino_connection.spec.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,39 +63,35 @@ describe('Trino connection', () => {
6363

6464
describe('schema parser', () => {
6565
it('parses arrays', () => {
66-
expect(connection.malloyTypeFromTrinoType('test', ARRAY_SCHEMA)).toEqual({
66+
expect(connection.malloyTypeFromTrinoType(ARRAY_SCHEMA)).toEqual({
6767
type: 'array',
6868
elementTypeDef: intType,
6969
});
7070
});
7171

7272
it('parses inline', () => {
73-
expect(connection.malloyTypeFromTrinoType('test', INLINE_SCHEMA)).toEqual(
74-
{
75-
'type': 'record',
76-
'fields': recordSchema,
77-
}
78-
);
73+
expect(connection.malloyTypeFromTrinoType(INLINE_SCHEMA)).toEqual({
74+
'type': 'record',
75+
'fields': recordSchema,
76+
});
7977
});
8078

8179
it('parses nested', () => {
82-
expect(connection.malloyTypeFromTrinoType('test', NESTED_SCHEMA)).toEqual(
83-
{
84-
'type': 'array',
85-
'elementTypeDef': {type: 'record_element'},
86-
'fields': recordSchema,
87-
}
88-
);
80+
expect(connection.malloyTypeFromTrinoType(NESTED_SCHEMA)).toEqual({
81+
'type': 'array',
82+
'elementTypeDef': {type: 'record_element'},
83+
'fields': recordSchema,
84+
});
8985
});
9086

9187
it('parses a simple type', () => {
92-
expect(connection.malloyTypeFromTrinoType('test', 'varchar(60)')).toEqual(
88+
expect(connection.malloyTypeFromTrinoType('varchar(60)')).toEqual(
9389
stringType
9490
);
9591
});
9692

9793
it('parses deep nesting', () => {
98-
expect(connection.malloyTypeFromTrinoType('test', DEEP_SCHEMA)).toEqual({
94+
expect(connection.malloyTypeFromTrinoType(DEEP_SCHEMA)).toEqual({
9995
'type': 'array',
10096
'elementTypeDef': {type: 'record_element'},
10197
'fields': [

packages/malloy-db-trino/src/trino_connection.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import {
4444
FieldDef,
4545
TinyParser,
4646
isRepeatedRecord,
47-
AtomicFieldDef,
4847
} from '@malloydata/malloy';
4948

5049
import {BaseConnection} from '@malloydata/malloy/connection';
@@ -260,7 +259,7 @@ export abstract class TrinoPrestoConnection
260259
const columns = r.columns;
261260

262261
const malloyColumns = columns.map(c =>
263-
this.malloyTypeFromTrinoType(c.name, c.type)
262+
mkFieldDef(this.malloyTypeFromTrinoType(c.type), c.name)
264263
);
265264

266265
const malloyRows: QueryDataRow[] = [];
@@ -348,24 +347,16 @@ export abstract class TrinoPrestoConnection
348347
//while (!(await result.next()).done);
349348
}
350349

351-
public malloyTypeFromTrinoType(
352-
name: string,
353-
trinoType: string
354-
): AtomicFieldDef {
355-
const atomicType = new TrinoPrestoSchemaParser(
356-
trinoType,
357-
this.dialect
358-
).typeDef();
359-
const def = mkFieldDef(atomicType, name);
360-
return def;
350+
public malloyTypeFromTrinoType(trinoType: string): AtomicTypeDef {
351+
const typeParse = new TrinoPrestoSchemaParser(trinoType, this.dialect);
352+
return typeParse.typeDef();
361353
}
362354

363355
structDefFromSchema(rows: string[][], structDef: StructDef): void {
364356
for (const row of rows) {
365357
const name = row[0];
366358
const type = row[4] || row[1];
367-
const malloyType = this.malloyTypeFromTrinoType(name, type);
368-
// console.log('>', row, '\n<', malloyType);
359+
const malloyType = mkFieldDef(this.malloyTypeFromTrinoType(type), name);
369360
structDef.fields.push(mkFieldDef(malloyType, name));
370361
}
371362
}
@@ -611,7 +602,10 @@ class TrinoPrestoSchemaParser extends TinyParser {
611602
} else if (typToken.text === 'row' && this.next('(')) {
612603
const fields: FieldDef[] = [];
613604
for (;;) {
614-
const name = this.next('quoted_name');
605+
const name = this.next();
606+
if (name.type !== 'id' && name.type !== 'quoted_name') {
607+
throw this.parseError('Expected property name');
608+
}
615609
const getDef = this.typeDef();
616610
fields.push(mkFieldDef(getDef, name.text));
617611
const sep = this.next();
@@ -637,10 +631,10 @@ class TrinoPrestoSchemaParser extends TinyParser {
637631
fields: elType.fields,
638632
}
639633
: {type: 'array', elementTypeDef: elType};
640-
} else if (typToken.type === 'id') {
634+
} else if (typToken.type === 'id' || typToken.type === 'quoted_name') {
641635
const sqlType = typToken.text;
642-
const def = this.dialect.sqlTypeToMalloyType(sqlType);
643-
if (def === undefined) {
636+
const typeDef = this.dialect.sqlTypeToMalloyType(sqlType);
637+
if (typeDef === undefined) {
644638
throw this.parseError(`Can't parse presto type ${sqlType}`);
645639
}
646640
if (sqlType === 'varchar') {
@@ -650,7 +644,7 @@ class TrinoPrestoSchemaParser extends TinyParser {
650644
} else if (sqlType === 'timezone' && this.peek().text === 'with') {
651645
this.nextText('with', 'time', 'zone');
652646
}
653-
return def;
647+
return typeDef;
654648
}
655649
throw this.parseError(
656650
`'${typToken.text}' unexpected while looking for a type`

0 commit comments

Comments
 (0)