Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor TypeDef for arrays and records #2032

Merged
merged 7 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions packages/malloy-db-bigquery/src/bigquery_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {ResourceStream} from '@google-cloud/paginator';
import * as googleCommon from '@google-cloud/common';
import {GaxiosError} from 'gaxios';
import {
arrayEachFields,
mkArrayDef,
Connection,
ConnectionConfig,
Malloy,
Expand Down Expand Up @@ -520,14 +520,7 @@ export class BigQueryConnection
// Malloy treats repeated values as an array of scalars.
const malloyType = this.dialect.sqlTypeToMalloyType(type);
if (malloyType) {
const arrayField: StructDef = {
...structShared,
type: 'array',
elementTypeDef: malloyType,
join: 'many',
fields: arrayEachFields(malloyType),
};
structDef.fields.push(arrayField);
structDef.fields.push(mkArrayDef(malloyType, name, this.dialectName));
}
} else if (isRecord) {
const ifRepeatedRecord: StructDef = {
Expand Down
13 changes: 4 additions & 9 deletions packages/malloy-db-duckdb/src/duckdb.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import {DuckDBCommon} from './duckdb_common';
import {DuckDBConnection} from './duckdb_connection';
import {arrayEachFields, SQLSourceDef, StructDef} from '@malloydata/malloy';
import {SQLSourceDef, StructDef, mkArrayDef} from '@malloydata/malloy';
import {describeIfDatabaseAvailable} from '@malloydata/malloy/test';

const [describe] = describeIfDatabaseAvailable(['duckdb']);
Expand Down Expand Up @@ -132,14 +132,9 @@ describe('DuckDBConnection', () => {
it('parses arrays', () => {
const structDef = makeStructDef();
connection.fillStructDefFromTypeMap(structDef, {test: ARRAY_SCHEMA});
expect(structDef.fields[0]).toEqual({
name: 'test',
type: 'array',
elementTypeDef: intTyp,
join: 'many',
dialect: 'duckdb',
fields: arrayEachFields({type: 'number', numberType: 'integer'}),
});
expect(structDef.fields[0]).toEqual(
mkArrayDef({type: 'number', numberType: 'integer'}, 'test', 'duckdb')
);
});

it('parses inline', () => {
Expand Down
12 changes: 2 additions & 10 deletions packages/malloy-db-duckdb/src/duckdb_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
DuckDBDialect,
SQLSourceDef,
TableSourceDef,
arrayEachFields,
mkArrayDef,
} from '@malloydata/malloy';
import {BaseConnection} from '@malloydata/malloy/connection';

Expand Down Expand Up @@ -240,15 +240,7 @@ export abstract class DuckDBCommon
} else {
if (arrayMatch) {
malloyType = this.dialect.sqlTypeToMalloyType(duckDBType);
const innerStructDef: StructDef = {
type: 'array',
elementTypeDef: malloyType,
name,
dialect: this.dialectName,
join: 'many',
fields: arrayEachFields(malloyType),
};
structDef.fields.push(innerStructDef);
structDef.fields.push(mkArrayDef(malloyType, name, this.dialectName));
} else {
structDef.fields.push({...malloyType, name});
}
Expand Down
11 changes: 2 additions & 9 deletions packages/malloy-db-postgres/src/postgres_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ import {
RunSQLOptions,
SQLSourceDef,
TableSourceDef,
arrayEachFields,
StreamingConnection,
StructDef,
mkArrayDef,
} from '@malloydata/malloy';
import {BaseConnection} from '@malloydata/malloy/connection';

Expand Down Expand Up @@ -237,14 +237,7 @@ export class PostgresConnection
const elementType = this.dialect.sqlTypeToMalloyType(
row['element_type'] as string
);
structDef.fields.push({
type: 'array',
elementTypeDef: elementType,
name,
dialect: this.dialectName,
join: 'many',
fields: arrayEachFields(elementType),
});
structDef.fields.push(mkArrayDef(elementType, name, this.dialectName));
} else {
const malloyType = this.dialect.sqlTypeToMalloyType(postgresDataType);
structDef.fields.push({...malloyType, name});
Expand Down
51 changes: 26 additions & 25 deletions packages/malloy-db-snowflake/src/snowflake_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ import {
QueryDataRow,
SnowflakeDialect,
TestableConnection,
arrayEachFields,
TinyParser,
Dialect,
FieldDef,
RecordDef,
ArrayTypeDef,
mkArrayDef,
AtomicFieldDef,
ArrayDef,
} from '@malloydata/malloy';
import {BaseConnection} from '@malloydata/malloy/connection';

Expand Down Expand Up @@ -75,7 +75,7 @@ class SnowField {
readonly type: string,
readonly dialect: Dialect
) {}
fieldDef(): FieldDef {
fieldDef(): AtomicFieldDef {
return {
...this.dialect.sqlTypeToMalloyType(this.type),
name: this.name,
Expand All @@ -102,15 +102,15 @@ class SnowObject extends SnowField {
super(name, 'object', d);
}

get fields(): FieldDef[] {
const fields: FieldDef[] = [];
get fields(): AtomicFieldDef[] {
const fields: AtomicFieldDef[] = [];
for (const [_, fieldObj] of this.fieldMap) {
fields.push(fieldObj.fieldDef());
}
return fields;
}

fieldDef() {
fieldDef(): RecordDef {
const rec: RecordDef = {
type: 'record',
name: this.name,
Expand Down Expand Up @@ -171,26 +171,27 @@ class SnowArray extends SnowField {
}
}

fieldDef(): ArrayTypeDef {
const arr: ArrayTypeDef = {
type: 'array',
name: this.name,
join: 'many',
dialect: this.dialect.name,
elementTypeDef: {type: 'string'},
fields: [],
};
fieldDef(): ArrayDef {
if (this.objectChild) {
arr.fields = this.objectChild.fieldDef().fields;
arr.elementTypeDef = {type: 'record_element'};
} else if (this.arrayChild) {
arr.elementTypeDef = this.arrayChild.fieldDef();
arr.fields = arrayEachFields(arr.elementTypeDef);
} else {
arr.elementTypeDef = this.dialect.sqlTypeToMalloyType(this.arrayOf);
arr.fields = arrayEachFields(arr.elementTypeDef);
const t = mkArrayDef(
{type: 'record', fields: this.objectChild.fields},
this.name,
this.dialect.name
);
return t;
}
if (this.arrayChild) {
return mkArrayDef(
this.arrayChild.fieldDef(),
this.name,
this.dialect.name
);
}
return arr;
return mkArrayDef(
this.dialect.sqlTypeToMalloyType(this.arrayOf),
this.name,
this.dialect.name
);
}

walk(path: PathChain, fieldType: string) {
Expand Down
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 @@ -21,7 +21,7 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import {arrayEachFields, AtomicTypeDef, FieldDef} from '@malloydata/malloy';
import {AtomicTypeDef, FieldDef} from '@malloydata/malloy';
import {TrinoConnection, TrinoExecutor} from '.';

// array(varchar) is array
Expand Down Expand Up @@ -64,22 +64,15 @@ describe('Trino connection', () => {
describe('schema parser', () => {
it('parses arrays', () => {
expect(connection.malloyTypeFromTrinoType('test', ARRAY_SCHEMA)).toEqual({
'name': 'test',
'type': 'array',
'dialect': 'trino',
'elementTypeDef': intType,
'join': 'many',
'fields': arrayEachFields(intType),
type: 'array',
elementTypeDef: intType,
});
});

it('parses inline', () => {
expect(connection.malloyTypeFromTrinoType('test', INLINE_SCHEMA)).toEqual(
{
'name': 'test',
'type': 'record',
'dialect': 'trino',
'join': 'one',
'fields': recordSchema,
}
);
Expand All @@ -88,11 +81,8 @@ describe('Trino connection', () => {
it('parses nested', () => {
expect(connection.malloyTypeFromTrinoType('test', NESTED_SCHEMA)).toEqual(
{
'name': 'test',
'type': 'array',
'elementTypeDef': {type: 'record_element'},
'dialect': 'trino',
'join': 'many',
'fields': recordSchema,
}
);
Expand All @@ -106,11 +96,8 @@ describe('Trino connection', () => {

it('parses deep nesting', () => {
expect(connection.malloyTypeFromTrinoType('test', DEEP_SCHEMA)).toEqual({
'name': 'test',
'type': 'array',
'dialect': 'trino',
'elementTypeDef': {type: 'record_element'},
'join': 'many',
'fields': [
{'name': 'a', ...doubleType},
{
Expand Down
Loading
Loading