Skip to content

Commit

Permalink
remove dialect: from record and array structdefs
Browse files Browse the repository at this point in the history
  • Loading branch information
mtoy-googly-moogly committed Dec 11, 2024
1 parent eee3baa commit 4f587e2
Show file tree
Hide file tree
Showing 25 changed files with 107 additions and 105 deletions.
2 changes: 1 addition & 1 deletion packages/malloy-db-bigquery/src/bigquery_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ export class BigQueryConnection
// Malloy treats repeated values as an array of scalars.
const malloyType = this.dialect.sqlTypeToMalloyType(type);
if (malloyType) {
structDef.fields.push(mkArrayDef(malloyType, name, this.dialectName));
structDef.fields.push(mkArrayDef(malloyType, name));
}
} else if (isRecord) {
const ifRepeatedRecord: StructDef = {
Expand Down
2 changes: 1 addition & 1 deletion packages/malloy-db-duckdb/src/duckdb.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe('DuckDBConnection', () => {
const structDef = makeStructDef();
connection.fillStructDefFromTypeMap(structDef, {test: ARRAY_SCHEMA});
expect(structDef.fields[0]).toEqual(
mkArrayDef({type: 'number', numberType: 'integer'}, 'test', 'duckdb')
mkArrayDef({type: 'number', numberType: 'integer'}, 'test')
);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/malloy-db-duckdb/src/duckdb_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export abstract class DuckDBCommon
} else {
if (arrayMatch) {
malloyType = this.dialect.sqlTypeToMalloyType(duckDBType);
structDef.fields.push(mkArrayDef(malloyType, name, this.dialectName));
structDef.fields.push(mkArrayDef(malloyType, name));
} else {
structDef.fields.push({...malloyType, name});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/malloy-db-postgres/src/postgres_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export class PostgresConnection
const elementType = this.dialect.sqlTypeToMalloyType(
row['element_type'] as string
);
structDef.fields.push(mkArrayDef(elementType, name, this.dialectName));
structDef.fields.push(mkArrayDef(elementType, name));
} else {
const malloyType = this.dialect.sqlTypeToMalloyType(postgresDataType);
structDef.fields.push({...malloyType, name});
Expand Down
13 changes: 3 additions & 10 deletions packages/malloy-db-snowflake/src/snowflake_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ class SnowObject extends SnowField {
name: this.name,
fields: this.fields,
join: 'one',
dialect: this.dialect.name,
};
return rec;
}
Expand Down Expand Up @@ -175,22 +174,16 @@ class SnowArray extends SnowField {
if (this.objectChild) {
const t = mkArrayDef(
{type: 'record', fields: this.objectChild.fields},
this.name,
this.dialect.name
this.name
);
return t;
}
if (this.arrayChild) {
return mkArrayDef(
this.arrayChild.fieldDef(),
this.name,
this.dialect.name
);
return mkArrayDef(this.arrayChild.fieldDef(), this.name);
}
return mkArrayDef(
this.dialect.sqlTypeToMalloyType(this.arrayOf),
this.name,
this.dialect.name
this.name
);
}

Expand Down
1 change: 0 additions & 1 deletion packages/malloy-db-trino/src/trino_connection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ describe('Trino connection', () => {
{
'name': 'b',
'type': 'array',
'dialect': 'trino',
'elementTypeDef': {type: 'record_element'},
'join': 'many',
'fields': [
Expand Down
10 changes: 4 additions & 6 deletions packages/malloy-db-trino/src/trino_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,7 @@ export abstract class TrinoPrestoConnection
innerName,
innerTrinoType
);
recordType.fields.push(
mkFieldDef(innerMalloyType, innerName, this.dialectName)
);
recordType.fields.push(mkFieldDef(innerMalloyType, innerName));
}
}
return recordType;
Expand All @@ -460,7 +458,7 @@ export abstract class TrinoPrestoConnection
const type = row[4] || row[1];
const malloyType = this.malloyTypeFromTrinoType(name, type);
// console.log('>', row, '\n<', malloyType);
structDef.fields.push(mkFieldDef(malloyType, name, this.dialectName));
structDef.fields.push(mkFieldDef(malloyType, name));
}
}

Expand Down Expand Up @@ -667,7 +665,7 @@ class PrestoExplainParser extends TinyParser {
const name = fieldNames[nameIndex];
this.next('id', ':');
const nextType = this.typeDef();
fields.push(mkFieldDef(nextType, name, this.dialect.name));
fields.push(mkFieldDef(nextType, name));
const sep = this.next();
if (sep.text === ',') {
continue;
Expand Down Expand Up @@ -696,7 +694,7 @@ class PrestoExplainParser extends TinyParser {
for (;;) {
const name = this.next('quoted_name');
const getDef = this.typeDef();
fields.push(mkFieldDef(getDef, name.text, this.dialect.name));
fields.push(mkFieldDef(getDef, name.text));
const sep = this.next();
if (sep.text === ')') {
break;
Expand Down
30 changes: 18 additions & 12 deletions packages/malloy/src/dialect/functions/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,18 @@ export function overload(
}

export interface ArrayBlueprint {
array: TypeDescBlueprint;
array: TypeDescElementBlueprint;
}
export interface RecordBlueprint {
record: Record<string, TypeDescBlueprint>;
record: Record<string, TypeDescElementBlueprint>;
}
export type TypeDescBlueprint =
// default for return type is min scalar
// default for param type is any expression type (max input)
export type TypeDescElementBlueprint =
| LeafExpressionType
| ArrayBlueprint
| RecordBlueprint
| RecordBlueprint;

export type TypeDescBlueprint =
| TypeDescElementBlueprint
| {generic: string}
| {literal: LeafExpressionType | {generic: string}}
| {constant: LeafExpressionType | {generic: string}}
Expand Down Expand Up @@ -302,7 +303,7 @@ function expandReturnTypeBlueprint(
if (typeof blueprint === 'string') {
base = minScalar(blueprint);
} else if ('array' in blueprint) {
const innerType = expandReturnTypeBlueprint(blueprint.array, undefined);
const innerType = expandReturnTypeBlueprint(blueprint.array, generic);
const {expressionType, evalSpace} = innerType;
if (TD.isAtomic(innerType)) {
if (innerType.type !== 'record') {
Expand All @@ -322,18 +323,19 @@ function expandReturnTypeBlueprint(
};
}
} else {
throw new Error('CHRIS WHAT DO I DO WITH AN ARRAY OF WHATEVER');
// mtoy todo fix by doing "exapndElementBlueprint" ...
throw new Error(
`TypeDescElementBlueprint should never allow ${blueprint.array}`
);
}
} else if ('record' in blueprint) {
const fields: FieldDef[] = [];
for (const [fieldName, fieldBlueprint] of Object.entries(
blueprint.record
)) {
const fieldDesc = expandReturnTypeBlueprint(fieldBlueprint, undefined);
const fieldDesc = expandReturnTypeBlueprint(fieldBlueprint, generic);
if (TD.isAtomic(fieldDesc)) {
fields.push(
mkFieldDef(fieldDesc, fieldName, 'HELP CHRIS I NEED THE DIALECT')
);
fields.push(mkFieldDef(fieldDesc, fieldName));
}
}
base = {
Expand Down Expand Up @@ -408,6 +410,10 @@ function expandParamTypeBlueprint(
return maxScalar(removeGeneric(blueprint.dimension, generic));
} else if ('measure' in blueprint) {
return maxAggregate(removeGeneric(blueprint.measure, generic));
} else if ('array' in blueprint) {
throw new Error('mtoy todo understand expand param type blueprint');
} else if ('record' in blueprint) {
throw new Error('mtoy todo understand expand param type blueprint');
} else {
return maxAnalytic(removeGeneric(blueprint.calculation, generic));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ export class RecordLiteral extends ExpressionDef {
if (TD.isAtomic(xVal)) {
dependents.push(xVal);
recLit.kids[el.key] = xVal.value;
recLit.typeDef.fields.push(
mkFieldDef(TDU.atomicDef(xVal), el.key, fs.dialectName())
);
recLit.typeDef.fields.push(mkFieldDef(TDU.atomicDef(xVal), el.key));
} else {
this.logError(
'illegal-record-property-type',
Expand Down
2 changes: 1 addition & 1 deletion packages/malloy/src/lang/ast/field-space/dynamic-space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export abstract class DynamicSpace
protected newTimezone?: string;

constructor(extending: SourceDef) {
super(structuredClone(extending));
super(structuredClone(extending), extending.dialect);
this.fromSource = extending;
this.sourceDef = undefined;
}
Expand Down
5 changes: 3 additions & 2 deletions packages/malloy/src/lang/ast/field-space/join-space-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ import {StructSpaceField} from './static-space';
export class JoinSpaceField extends StructSpaceField {
constructor(
readonly parameterSpace: ParameterSpace,
readonly join: Join
readonly join: Join,
forDialect: string
) {
super(join.structDef(parameterSpace));
super(join.structDef(parameterSpace), forDialect);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class ReferenceField extends SpaceField {
const foundType = check.found.typeDesc();
if (TD.isAtomic(foundType)) {
this.queryFieldDef = {
...mkFieldDef(TDU.atomicDef(foundType), path[0], fs.dialectName()),
...mkFieldDef(TDU.atomicDef(foundType), path[0]),
e: {node: 'parameter', path},
};
} else {
Expand Down
31 changes: 20 additions & 11 deletions packages/malloy/src/lang/ast/field-space/static-space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,29 @@ type FieldMap = Record<string, SpaceEntry>;
export class StaticSpace implements FieldSpace {
readonly type = 'fieldSpace';
private memoMap?: FieldMap;
get dialect() {
return this.fromStruct.dialect;
}
protected fromStruct: StructDef;
protected structDialect: string;

constructor(protected fromStruct: StructDef) {}
constructor(struct: StructDef, dialect_name: string) {
this.fromStruct = struct;
this.structDialect = dialect_name;
}

dialectName(): string {
return this.fromStruct.dialect;
return this.structDialect;
}

dialectObj(): Dialect | undefined {
try {
return getDialect(this.fromStruct.dialect);
return getDialect(this.structDialect);
} catch {
return undefined;
}
}

defToSpaceField(from: FieldDef): SpaceField {
if (isJoined(from)) {
return new StructSpaceField(from);
return new StructSpaceField(from, this.structDialect);
} else if (isTurtle(from)) {
return new IRViewField(this, from);
}
Expand Down Expand Up @@ -156,7 +158,7 @@ export class StaticSpace implements FieldSpace {
// because it is someting like "dimension: joinedArray is arrayComputation"
// which wasn't known to be a join when the fieldspace was constructed.
// TODO don't make one of these every time you do a lookup
found = new StructSpaceField(definition);
found = new StructSpaceField(definition, this.structDialect);
}
// cswenson review todo I don't know how to count the reference properly now
// i tried only writing it as a join reference if there was more in the path
Expand Down Expand Up @@ -206,18 +208,25 @@ export class StaticSpace implements FieldSpace {
}

export class StructSpaceField extends StructSpaceFieldBase {
constructor(def: JoinFieldDef) {
constructor(
def: JoinFieldDef,
private forDialect: string
) {
super(def);
}

get fieldSpace(): FieldSpace {
return new StaticSpace(this.structDef);
if (isSourceDef(this.structDef)) {
return new StaticSourceSpace(this.structDef);
} else {
return new StaticSpace(this.structDef, this.forDialect);
}
}
}

export class StaticSourceSpace extends StaticSpace implements SourceFieldSpace {
constructor(protected source: SourceDef) {
super(source);
super(source, source.dialect);
}
structDef(): SourceDef {
return this.source;
Expand Down
6 changes: 3 additions & 3 deletions packages/malloy/src/lang/ast/query-elements/query-arrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import {Query, StructDef, refIsStructDef} from '../../../model/malloy_types';
import {Source} from '../source-elements/source';
import {StaticSpace} from '../field-space/static-space';
import {StaticSourceSpace} from '../field-space/static-space';
import {FieldSpace} from '../types/field-space';
import {QueryComp} from '../types/query-comp';
import {QueryElement} from '../types/query-element';
Expand Down Expand Up @@ -64,13 +64,13 @@ export class QueryArrow extends QueryBase implements QueryElement {
inputStruct = refIsStructDef(invoked.structRef)
? invoked.structRef
: this.source.getSourceDef(undefined);
fieldSpace = new StaticSpace(inputStruct);
fieldSpace = new StaticSourceSpace(inputStruct);
} else {
// We are adding a second stage to the given "source" query; we get the query and add a segment
const lhsQuery = this.source.queryComp(isRefOk);
queryBase = lhsQuery.query;
inputStruct = lhsQuery.outputStruct;
fieldSpace = new StaticSpace(lhsQuery.outputStruct);
fieldSpace = new StaticSourceSpace(lhsQuery.outputStruct);
}
const {pipeline, annotation, outputStruct, name} =
this.view.pipelineComp(fieldSpace);
Expand Down
4 changes: 2 additions & 2 deletions packages/malloy/src/lang/ast/query-elements/query-refine.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 {StaticSpace} from '../field-space/static-space';
import {StaticSourceSpace} from '../field-space/static-space';
import {getFinalStruct} from '../struct-utils';
import {QueryComp} from '../types/query-comp';
import {QueryElement} from '../types/query-element';
Expand All @@ -45,7 +45,7 @@ export class QueryRefine extends QueryBase implements QueryElement {

queryComp(isRefOk: boolean): QueryComp {
const q = this.base.queryComp(isRefOk);
const inputFS = new StaticSpace(q.inputStruct);
const inputFS = new StaticSourceSpace(q.inputStruct);
const resultPipe = this.refinement.refine(
inputFS,
q.query.pipeline,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,7 @@ export abstract class AtomicFieldDeclaration
}
if (isAtomicFieldType(exprValue.type) && exprValue.type !== 'error') {
this.typecheckExprValue(exprValue);
const ret = mkFieldDef(
TDU.atomicDef(exprValue),
exprName,
exprFS.dialectName()
);
const ret = mkFieldDef(TDU.atomicDef(exprValue), exprName);
if (
(ret.type === 'date' || ret.type === 'timestamp') &&
isGranularResult(exprValue)
Expand Down
3 changes: 2 additions & 1 deletion packages/malloy/src/lang/ast/source-elements/named-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ export class NamedSource extends Source {
return {...entry};
}
}
// I think this is now a never
this.logError(
'invalid-source-source',
`Cannot construct a source from a ${entry.type}`
'Cannot construct a source from a never type'
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/malloy/src/lang/ast/source-properties/join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export abstract class Join
fs.newEntry(
this.name.refString,
this,
new JoinSpaceField(fs.parameterSpace(), this)
new JoinSpaceField(fs.parameterSpace(), this, fs.dialectName())
);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/malloy/src/lang/ast/view-elements/view-arrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import {PipeSegment} from '../../../model/malloy_types';
import {QueryOperationSpace} from '../field-space/query-spaces';
import {StaticSpace} from '../field-space/static-space';
import {StaticSourceSpace} from '../field-space/static-space';
import {FieldSpace} from '../types/field-space';
import {PipelineComp} from '../types/pipeline-comp';
import {View} from './view';
Expand All @@ -46,7 +46,7 @@ export class ViewArrow extends View {

pipelineComp(fs: FieldSpace): PipelineComp {
const baseComp = this.base.pipelineComp(fs);
const nextFS = new StaticSpace(baseComp.outputStruct);
const nextFS = new StaticSourceSpace(baseComp.outputStruct);
const finalComp = this.operation.pipelineComp(nextFS);
return {
pipeline: [...baseComp.pipeline, ...finalComp.pipeline],
Expand Down
Loading

0 comments on commit 4f587e2

Please sign in to comment.