Skip to content

Commit f7fdbdb

Browse files
Updates to MalloyTranslator to enable model caching (#2091)
* First pass at allowing translator to accept pre-transalted documents * Simplify * Move `queryList` into `ModelDef` * Change `translationResponse.translated` to `.modelDef` * Add dependencies to `ModelDef` * Internally, call sql blocks `sqlSources` * Add util method to get a child translator * Keep `fromSources` correctly in sync * Remove Malloy file that I didn't mean to check in * Fix hand-written models * Don't error if a child translator is not present -- that just means its parent was cached and therefore never created * Add `newlyTranslatedDependencies` util * Remove unused import * Add a test for newlyTranslatedDependencies
1 parent 813b3a6 commit f7fdbdb

File tree

18 files changed

+285
-110
lines changed

18 files changed

+285
-110
lines changed

packages/malloy/src/lang/ast/statements/import-statement.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ export class ImportStatement
9797
'Cannot import without translation context'
9898
);
9999
} else if (this.fullURL) {
100+
const pretranslated = trans.root.pretranslatedModels.get(this.fullURL);
100101
const src = trans.root.importZone.getEntry(this.fullURL);
101-
if (src.status === 'present') {
102+
if (pretranslated || src.status === 'present') {
102103
const importable = trans.getChildExports(this.fullURL);
103104
if (this.notEmpty()) {
104105
// just import the named objects

packages/malloy/src/lang/ast/types/document-compile-result.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@
2121
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222
*/
2323

24-
import {ModelDef, Query, SQLSourceDef} from '../../../model/malloy_types';
24+
import {ModelDef} from '../../../model/malloy_types';
2525

2626
import {ModelDataRequest} from '../../translate-response';
2727

2828
export interface DocumentCompileResult {
2929
modelDef: ModelDef;
30-
queryList: Query[];
31-
sqlBlocks: SQLSourceDef[];
3230
needs: ModelDataRequest;
3331
}

packages/malloy/src/lang/ast/types/malloy-element.ts

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import {
3030
ModelAnnotation,
3131
NamedModelObject,
3232
Query,
33-
SQLSourceDef,
3433
StructDef,
3534
isSourceDef,
3635
} from '../../../model/malloy_types';
@@ -500,7 +499,6 @@ export class Document extends MalloyElement implements NameSpace {
500499
globalNameSpace: NameSpace = new GlobalNameSpace();
501500
documentModel: Record<string, ModelEntry> = {};
502501
queryList: Query[] = [];
503-
sqlBlocks: SQLSourceDef[] = [];
504502
statements: DocStatementList;
505503
didInitModel = false;
506504
annotation: Annotation = {};
@@ -518,7 +516,6 @@ export class Document extends MalloyElement implements NameSpace {
518516
}
519517
this.documentModel = {};
520518
this.queryList = [];
521-
this.sqlBlocks = [];
522519
if (extendingModelDef) {
523520
if (extendingModelDef.annotation) {
524521
this.annotation.inherits = extendingModelDef.annotation;
@@ -547,21 +544,17 @@ export class Document extends MalloyElement implements NameSpace {
547544
q.modelAnnotation = modelDef.annotation;
548545
}
549546
}
550-
for (const q of this.sqlBlocks) {
551-
if (q.modelAnnotation === undefined && modelDef.annotation) {
552-
q.modelAnnotation = modelDef.annotation;
553-
}
554-
}
555547
}
556548
if (modelDef.annotation) {
557549
for (const sd of this.modelAnnotationTodoList) {
558550
sd.modelAnnotation ||= modelDef.annotation;
559551
}
560552
}
561553
const ret: DocumentCompileResult = {
562-
modelDef,
563-
queryList: this.queryList,
564-
sqlBlocks: this.sqlBlocks,
554+
modelDef: {
555+
...modelDef,
556+
queryList: this.queryList,
557+
},
565558
needs,
566559
};
567560
return ret;
@@ -588,7 +581,13 @@ export class Document extends MalloyElement implements NameSpace {
588581
}
589582

590583
modelDef(): ModelDef {
591-
const def: ModelDef = {name: '', exports: [], contents: {}};
584+
const def: ModelDef = {
585+
name: '',
586+
exports: [],
587+
contents: {},
588+
queryList: [],
589+
dependencies: {},
590+
};
592591
if (this.hasAnnotation()) {
593592
def.annotation = this.currentModelAnnotation();
594593
}
@@ -608,22 +607,6 @@ export class Document extends MalloyElement implements NameSpace {
608607
return def;
609608
}
610609

611-
defineSQL(sql: SQLSourceDef, name?: string): boolean {
612-
const ret = {
613-
...sql,
614-
as: `$${this.sqlBlocks.length}`,
615-
};
616-
if (name) {
617-
if (this.getEntry(name)) {
618-
return false;
619-
}
620-
ret.as = name;
621-
this.setEntry(name, {entry: ret});
622-
}
623-
this.sqlBlocks.push(ret);
624-
return true;
625-
}
626-
627610
getEntry(str: string): ModelEntry {
628611
return this.globalNameSpace.getEntry(str) ?? this.documentModel[str];
629612
}

packages/malloy/src/lang/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222
*/
2323

24-
export {MalloyTranslator} from './parse-malloy';
24+
export {MalloyTranslator, MalloyTranslation} from './parse-malloy';
2525
export type {
2626
UpdateData,
2727
SchemaData,

0 commit comments

Comments
 (0)