@malloydata/malloy 0.0.191-dev240927163326 → 0.0.191-dev240927225523
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/malloy.d.ts +44 -25
- package/dist/malloy.js +104 -32
- package/dist/model/malloy_query.d.ts +8 -5
- package/dist/model/malloy_query.js +54 -23
- package/dist/model/malloy_types.d.ts +10 -0
- package/dist/run_sql_options.d.ts +1 -0
- package/package.json +1 -1
package/dist/malloy.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { RunSQLOptions } from './run_sql_options';
|
|
3
3
|
import { DocumentCompletion as DocumentCompletionDefinition, DocumentSymbol as DocumentSymbolDefinition, LogMessage, MalloyTranslator } from './lang';
|
|
4
4
|
import { DocumentHelpContext } from './lang/parse-tree-walkers/document-help-context-walker';
|
|
5
|
-
import { CompiledQuery, DocumentLocation, DocumentReference, FieldBooleanDef, FieldDateDef, FieldJSONDef, FieldNumberDef, FieldStringDef, FieldTimestampDef, FieldTypeDef, FilterCondition, Query as InternalQuery, ModelDef, DocumentPosition as ModelDocumentPosition, NamedQuery, QueryData, QueryDataRow, QueryResult, SQLBlock, SQLBlockSource, SQLBlockStructDef, SearchIndexResult, SearchValueMapResult, StructDef, TurtleDef, FeldNativeUnsupportedDef, QueryRunStats, ImportLocation, Annotation } from './model';
|
|
5
|
+
import { CompiledQuery, DocumentLocation, DocumentReference, FieldBooleanDef, FieldDateDef, FieldJSONDef, FieldNumberDef, FieldStringDef, FieldTimestampDef, FieldTypeDef, FilterCondition, Query as InternalQuery, ModelDef, DocumentPosition as ModelDocumentPosition, NamedQuery, QueryData, QueryDataRow, QueryResult, SQLBlock, SQLBlockSource, SQLBlockStructDef, SearchIndexResult, SearchValueMapResult, StructDef, TurtleDef, FeldNativeUnsupportedDef, QueryRunStats, ImportLocation, Annotation, QueryToMaterialize, PrepareResultOptions } from './model';
|
|
6
6
|
import { ModelString, ModelURL, QueryString, QueryURL, URLReader } from './runtime_types';
|
|
7
7
|
import { Connection, InfoConnection, LookupConnection } from './connection/types';
|
|
8
8
|
import { Tag, TagParse, TagParseSpec, Taggable } from './tags';
|
|
@@ -61,13 +61,15 @@ export declare class Malloy {
|
|
|
61
61
|
* @param model A compiled model to build upon (optional).
|
|
62
62
|
* @return A (promise of a) compiled `Model`.
|
|
63
63
|
*/
|
|
64
|
-
static compile({ urlReader, connections, parse, model, refreshSchemaCache, noThrowOnError, }: {
|
|
64
|
+
static compile({ urlReader, connections, parse, model, refreshSchemaCache, noThrowOnError, replaceMaterializedReferences, }: {
|
|
65
65
|
urlReader: URLReader;
|
|
66
66
|
connections: LookupConnection<InfoConnection>;
|
|
67
67
|
parse: Parse;
|
|
68
68
|
model?: Model;
|
|
69
|
-
} & CompileOptions): Promise<Model>;
|
|
70
|
-
static compileSQLBlock(partialModel: ModelDef | undefined, toCompile: SQLBlockSource
|
|
69
|
+
} & CompileOptions & PrepareResultOptions): Promise<Model>;
|
|
70
|
+
static compileSQLBlock(partialModel: ModelDef | undefined, toCompile: SQLBlockSource, options?: {
|
|
71
|
+
replaceMaterializedReferences?: boolean;
|
|
72
|
+
}): SQLBlock;
|
|
71
73
|
/**
|
|
72
74
|
* Run a fully-prepared query.
|
|
73
75
|
*
|
|
@@ -218,6 +220,12 @@ export declare class Model implements Taggable {
|
|
|
218
220
|
* @return A prepared query.
|
|
219
221
|
*/
|
|
220
222
|
get preparedQuery(): PreparedQuery;
|
|
223
|
+
/**
|
|
224
|
+
* Retrieve a prepared query for the final unnamed query at the top level of a model.
|
|
225
|
+
*
|
|
226
|
+
* @return A prepared query.
|
|
227
|
+
*/
|
|
228
|
+
getPreparedQuery(): PreparedQuery;
|
|
221
229
|
/**
|
|
222
230
|
* Retrieve an `Explore` from the model by name.
|
|
223
231
|
*
|
|
@@ -257,6 +265,7 @@ export declare class PreparedQuery implements Taggable {
|
|
|
257
265
|
* @return A fully-prepared query (which contains the generated SQL).
|
|
258
266
|
*/
|
|
259
267
|
get preparedResult(): PreparedResult;
|
|
268
|
+
getPreparedResult(options?: PrepareResultOptions): PreparedResult;
|
|
260
269
|
get dialect(): string;
|
|
261
270
|
/**
|
|
262
271
|
* Get the flattened version of a query -- one that does not have a `pipeHead`.
|
|
@@ -453,6 +462,7 @@ export declare class PreparedResult implements Taggable {
|
|
|
453
462
|
* with the connection name `this.getConnectionName()`.
|
|
454
463
|
*/
|
|
455
464
|
get sql(): string;
|
|
465
|
+
get dependenciesToMaterialize(): Record<string, QueryToMaterialize> | undefined;
|
|
456
466
|
/**
|
|
457
467
|
* @return The `Explore` representing the data that will be returned by running this query.
|
|
458
468
|
*/
|
|
@@ -742,8 +752,8 @@ export declare class Runtime {
|
|
|
742
752
|
* @return A `ModelMaterializer` capable of materializing the requested model,
|
|
743
753
|
* or loading further related objects.
|
|
744
754
|
*/
|
|
745
|
-
loadModel(source: ModelURL | ModelString, options?: ParseOptions & CompileOptions): ModelMaterializer;
|
|
746
|
-
_loadModelFromModelDef(modelDef: ModelDef): ModelMaterializer;
|
|
755
|
+
loadModel(source: ModelURL | ModelString, options?: ParseOptions & CompileOptions & PrepareResultOptions): ModelMaterializer;
|
|
756
|
+
_loadModelFromModelDef(modelDef: ModelDef, options?: PrepareResultOptions): ModelMaterializer;
|
|
747
757
|
/**
|
|
748
758
|
* Load a Malloy query by URL or contents.
|
|
749
759
|
*
|
|
@@ -751,7 +761,7 @@ export declare class Runtime {
|
|
|
751
761
|
* @return A `QueryMaterializer` capable of materializing the requested query, running it,
|
|
752
762
|
* or loading further related objects.
|
|
753
763
|
*/
|
|
754
|
-
loadQuery(query: QueryURL | QueryString, options?: ParseOptions & CompileOptions): QueryMaterializer;
|
|
764
|
+
loadQuery(query: QueryURL | QueryString, options?: ParseOptions & CompileOptions & PrepareResultOptions): QueryMaterializer;
|
|
755
765
|
/**
|
|
756
766
|
* Load a Malloy query by the URL or contents of a Malloy model document
|
|
757
767
|
* and the index of an unnamed query contained in the model.
|
|
@@ -761,7 +771,7 @@ export declare class Runtime {
|
|
|
761
771
|
* @return A `QueryMaterializer` capable of materializing the requested query, running it,
|
|
762
772
|
* or loading further related objects.
|
|
763
773
|
*/
|
|
764
|
-
loadQueryByIndex(model: ModelURL | ModelString, index: number, options?: ParseOptions & CompileOptions): QueryMaterializer;
|
|
774
|
+
loadQueryByIndex(model: ModelURL | ModelString, index: number, options?: ParseOptions & CompileOptions & PrepareResultOptions): QueryMaterializer;
|
|
765
775
|
/**
|
|
766
776
|
* Load a Malloy query by the URL or contents of a Malloy model document
|
|
767
777
|
* and the name of a query contained in the model.
|
|
@@ -771,7 +781,7 @@ export declare class Runtime {
|
|
|
771
781
|
* @return A `QueryMaterializer` capable of materializing the requested query, running it,
|
|
772
782
|
* or loading further related objects.
|
|
773
783
|
*/
|
|
774
|
-
loadQueryByName(model: ModelURL | ModelString, name: string, options?: ParseOptions & CompileOptions): QueryMaterializer;
|
|
784
|
+
loadQueryByName(model: ModelURL | ModelString, name: string, options?: ParseOptions & CompileOptions & PrepareResultOptions): QueryMaterializer;
|
|
775
785
|
/**
|
|
776
786
|
* Load a SQL block by the URL or contents of a Malloy model document
|
|
777
787
|
* and the name of a query contained in the model.
|
|
@@ -781,7 +791,7 @@ export declare class Runtime {
|
|
|
781
791
|
* @return A `SQLBlockMaterializer` capable of materializing the requested query, running it,
|
|
782
792
|
* or loading further related objects.
|
|
783
793
|
*/
|
|
784
|
-
loadSQLBlockByName(model: ModelURL | ModelString, name: string, options?: ParseOptions & CompileOptions): SQLBlockMaterializer;
|
|
794
|
+
loadSQLBlockByName(model: ModelURL | ModelString, name: string, options?: ParseOptions & CompileOptions & PrepareResultOptions): SQLBlockMaterializer;
|
|
785
795
|
/**
|
|
786
796
|
* Load a SQL block by the URL or contents of a Malloy model document
|
|
787
797
|
* and the name of a query contained in the model.
|
|
@@ -864,8 +874,8 @@ declare class FluentState<T> {
|
|
|
864
874
|
constructor(runtime: Runtime, materialize: () => Promise<T>);
|
|
865
875
|
protected materialize(): Promise<T>;
|
|
866
876
|
protected rematerialize(): Promise<T>;
|
|
867
|
-
protected makeQueryMaterializer(materialize: () => Promise<PreparedQuery
|
|
868
|
-
protected makeExploreMaterializer(materialize: () => Promise<Explore
|
|
877
|
+
protected makeQueryMaterializer(materialize: () => Promise<PreparedQuery>, options?: PrepareResultOptions): QueryMaterializer;
|
|
878
|
+
protected makeExploreMaterializer(materialize: () => Promise<Explore>, options?: PrepareResultOptions): ExploreMaterializer;
|
|
869
879
|
protected makePreparedResultMaterializer(materialize: () => Promise<PreparedResult>): PreparedResultMaterializer;
|
|
870
880
|
protected makeSQLBlockMaterializer(materialize: () => Promise<SQLBlockStructDef>): SQLBlockMaterializer;
|
|
871
881
|
}
|
|
@@ -875,13 +885,16 @@ declare class FluentState<T> {
|
|
|
875
885
|
* queries or explores (via e.g. `loadFinalQuery()`, `loadQuery`, `loadExploreByName`, etc.).
|
|
876
886
|
*/
|
|
877
887
|
export declare class ModelMaterializer extends FluentState<Model> {
|
|
888
|
+
protected runtime: Runtime;
|
|
889
|
+
private readonly replaceMaterializedReferences;
|
|
890
|
+
constructor(runtime: Runtime, materialize: () => Promise<Model>, options?: PrepareResultOptions);
|
|
878
891
|
/**
|
|
879
892
|
* Load the final (unnamed) Malloy query contained within this loaded `Model`.
|
|
880
893
|
*
|
|
881
894
|
* @return A `QueryMaterializer` capable of materializing the requested query, running it,
|
|
882
895
|
* or loading further related objects.
|
|
883
896
|
*/
|
|
884
|
-
loadFinalQuery(): QueryMaterializer;
|
|
897
|
+
loadFinalQuery(options?: PrepareResultOptions): QueryMaterializer;
|
|
885
898
|
/**
|
|
886
899
|
* Load an unnamed query contained within this loaded `Model` by index.
|
|
887
900
|
*
|
|
@@ -889,7 +902,7 @@ export declare class ModelMaterializer extends FluentState<Model> {
|
|
|
889
902
|
* @return A `QueryMaterializer` capable of materializing the requested query, running it,
|
|
890
903
|
* or loading further related objects.
|
|
891
904
|
*/
|
|
892
|
-
loadQueryByIndex(index: number): QueryMaterializer;
|
|
905
|
+
loadQueryByIndex(index: number, options?: PrepareResultOptions): QueryMaterializer;
|
|
893
906
|
/**
|
|
894
907
|
* Load a query contained within this loaded `Model` by its name.
|
|
895
908
|
*
|
|
@@ -897,7 +910,7 @@ export declare class ModelMaterializer extends FluentState<Model> {
|
|
|
897
910
|
* @return A `QueryMaterializer` capable of materializing the requested query, running it,
|
|
898
911
|
* or loading further related objects.
|
|
899
912
|
*/
|
|
900
|
-
loadQueryByName(name: string): QueryMaterializer;
|
|
913
|
+
loadQueryByName(name: string, options?: PrepareResultOptions): QueryMaterializer;
|
|
901
914
|
/**
|
|
902
915
|
* Load a query against this loaded `Model` by its URL or contents.
|
|
903
916
|
*
|
|
@@ -905,7 +918,7 @@ export declare class ModelMaterializer extends FluentState<Model> {
|
|
|
905
918
|
* @return A `QueryMaterializer` capable of materializing the requested query, running it,
|
|
906
919
|
* or loading further related objects.
|
|
907
920
|
*/
|
|
908
|
-
loadQuery(query: QueryString | QueryURL, options?: ParseOptions & CompileOptions): QueryMaterializer;
|
|
921
|
+
loadQuery(query: QueryString | QueryURL, options?: ParseOptions & CompileOptions & PrepareResultOptions): QueryMaterializer;
|
|
909
922
|
/**
|
|
910
923
|
* Extend a Malloy model by URL or contents.
|
|
911
924
|
*
|
|
@@ -913,7 +926,7 @@ export declare class ModelMaterializer extends FluentState<Model> {
|
|
|
913
926
|
* @return A `ModelMaterializer` capable of materializing the requested model,
|
|
914
927
|
* or loading further related objects.
|
|
915
928
|
*/
|
|
916
|
-
extendModel(query: QueryString | QueryURL, options?: ParseOptions & CompileOptions): ModelMaterializer;
|
|
929
|
+
extendModel(query: QueryString | QueryURL, options?: ParseOptions & CompileOptions & PrepareResultOptions): ModelMaterializer;
|
|
917
930
|
search(sourceName: string, searchTerm: string, limit?: number, searchField?: string | undefined): Promise<SearchIndexResult[] | undefined>;
|
|
918
931
|
searchValueMap(sourceName: string, limit?: number, options?: ParseOptions): Promise<SearchValueMapResult[] | undefined>;
|
|
919
932
|
/**
|
|
@@ -977,7 +990,7 @@ export declare class ModelMaterializer extends FluentState<Model> {
|
|
|
977
990
|
* TODO feature-sql-block Should named SQL blocks be indexable? This is not the way unnamed queries work.
|
|
978
991
|
*/
|
|
979
992
|
getSQLBlockByIndex(index: number): Promise<SQLBlockStructDef>;
|
|
980
|
-
_loadQueryFromQueryDef(query: InternalQuery): QueryMaterializer;
|
|
993
|
+
_loadQueryFromQueryDef(query: InternalQuery, options?: PrepareResultOptions): QueryMaterializer;
|
|
981
994
|
/**
|
|
982
995
|
* Load an explore contained within this loaded `Model` by name.
|
|
983
996
|
*
|
|
@@ -1006,32 +1019,35 @@ export declare class ModelMaterializer extends FluentState<Model> {
|
|
|
1006
1019
|
* prepared results or run the query (via e.g. `loadPreparedResult()` or `run()`).
|
|
1007
1020
|
*/
|
|
1008
1021
|
export declare class QueryMaterializer extends FluentState<PreparedQuery> {
|
|
1022
|
+
protected runtime: Runtime;
|
|
1023
|
+
private readonly replaceMaterializedReferences;
|
|
1024
|
+
constructor(runtime: Runtime, materialize: () => Promise<PreparedQuery>, options?: PrepareResultOptions);
|
|
1009
1025
|
/**
|
|
1010
1026
|
* Run this loaded `Query`.
|
|
1011
1027
|
*
|
|
1012
1028
|
* @return The query results from running this loaded query.
|
|
1013
1029
|
*/
|
|
1014
|
-
run(options?: RunSQLOptions): Promise<Result>;
|
|
1015
|
-
runStream(options?: RunSQLOptions): AsyncIterableIterator<DataRecord>;
|
|
1030
|
+
run(options?: RunSQLOptions & PrepareResultOptions): Promise<Result>;
|
|
1031
|
+
runStream(options?: RunSQLOptions & PrepareResultOptions): AsyncIterableIterator<DataRecord>;
|
|
1016
1032
|
/**
|
|
1017
1033
|
* Load the prepared result of this loaded query.
|
|
1018
1034
|
*
|
|
1019
1035
|
* @return A `PreparedResultMaterializer` capable of materializing the requested
|
|
1020
1036
|
* prepared query or running it.
|
|
1021
1037
|
*/
|
|
1022
|
-
loadPreparedResult(): PreparedResultMaterializer;
|
|
1038
|
+
loadPreparedResult(options?: PrepareResultOptions): PreparedResultMaterializer;
|
|
1023
1039
|
/**
|
|
1024
1040
|
* Materialize the prepared result of this loaded query.
|
|
1025
1041
|
*
|
|
1026
1042
|
* @return A promise of the prepared result of this loaded query.
|
|
1027
1043
|
*/
|
|
1028
|
-
getPreparedResult(): Promise<PreparedResult>;
|
|
1044
|
+
getPreparedResult(options?: PrepareResultOptions): Promise<PreparedResult>;
|
|
1029
1045
|
/**
|
|
1030
1046
|
* Materialize the SQL of this loaded query.
|
|
1031
1047
|
*
|
|
1032
1048
|
* @return A promise of the SQL string.
|
|
1033
1049
|
*/
|
|
1034
|
-
getSQL(): Promise<string>;
|
|
1050
|
+
getSQL(options?: PrepareResultOptions): Promise<string>;
|
|
1035
1051
|
/**
|
|
1036
1052
|
* Materialize this loaded query.
|
|
1037
1053
|
*
|
|
@@ -1043,7 +1059,7 @@ export declare class QueryMaterializer extends FluentState<PreparedQuery> {
|
|
|
1043
1059
|
*
|
|
1044
1060
|
* @return The estimated cost of running this loaded query.
|
|
1045
1061
|
*/
|
|
1046
|
-
estimateQueryCost(): Promise<QueryRunStats>;
|
|
1062
|
+
estimateQueryCost(options?: PrepareResultOptions): Promise<QueryRunStats>;
|
|
1047
1063
|
}
|
|
1048
1064
|
/**
|
|
1049
1065
|
* An object representing the task of loading a `PreparedResult`, capable of
|
|
@@ -1108,6 +1124,9 @@ export declare class SQLBlockMaterializer extends FluentState<SQLBlockStructDef>
|
|
|
1108
1124
|
* related queries.
|
|
1109
1125
|
*/
|
|
1110
1126
|
export declare class ExploreMaterializer extends FluentState<Explore> {
|
|
1127
|
+
protected runtime: Runtime;
|
|
1128
|
+
private readonly replaceMaterializedReferences;
|
|
1129
|
+
constructor(runtime: Runtime, materialize: () => Promise<Explore>, options?: PrepareResultOptions);
|
|
1111
1130
|
/**
|
|
1112
1131
|
* Load a query contained within this loaded explore.
|
|
1113
1132
|
*
|
|
@@ -1115,7 +1134,7 @@ export declare class ExploreMaterializer extends FluentState<Explore> {
|
|
|
1115
1134
|
* @return A `QueryMaterializer` capable of materializing the requested query, running it,
|
|
1116
1135
|
* or loading further related objects.
|
|
1117
1136
|
*/
|
|
1118
|
-
loadQueryByName(name: string): QueryMaterializer;
|
|
1137
|
+
loadQueryByName(name: string, options?: PrepareResultOptions): QueryMaterializer;
|
|
1119
1138
|
/**
|
|
1120
1139
|
* Materialize a query contained within this loaded explore.
|
|
1121
1140
|
*
|
package/dist/malloy.js
CHANGED
|
@@ -80,7 +80,7 @@ class Malloy {
|
|
|
80
80
|
* @param model A compiled model to build upon (optional).
|
|
81
81
|
* @return A (promise of a) compiled `Model`.
|
|
82
82
|
*/
|
|
83
|
-
static async compile({ urlReader, connections, parse, model, refreshSchemaCache, noThrowOnError, }) {
|
|
83
|
+
static async compile({ urlReader, connections, parse, model, refreshSchemaCache, noThrowOnError, replaceMaterializedReferences, }) {
|
|
84
84
|
var _a, _b, _c, _d;
|
|
85
85
|
let refreshTimestamp;
|
|
86
86
|
if (refreshSchemaCache) {
|
|
@@ -177,7 +177,7 @@ class Malloy {
|
|
|
177
177
|
const connectionName = toCompile.connection;
|
|
178
178
|
try {
|
|
179
179
|
const conn = await connections.lookupConnection(connectionName);
|
|
180
|
-
const expanded = Malloy.compileSQLBlock(result.partialModel, toCompile);
|
|
180
|
+
const expanded = Malloy.compileSQLBlock(result.partialModel, toCompile, { replaceMaterializedReferences });
|
|
181
181
|
const resolved = await conn.fetchSchemaForSQLBlock(expanded, {
|
|
182
182
|
refreshTimestamp,
|
|
183
183
|
modelAnnotation,
|
|
@@ -204,7 +204,7 @@ class Malloy {
|
|
|
204
204
|
}
|
|
205
205
|
}
|
|
206
206
|
}
|
|
207
|
-
static compileSQLBlock(partialModel, toCompile) {
|
|
207
|
+
static compileSQLBlock(partialModel, toCompile, options) {
|
|
208
208
|
let queryModel = undefined;
|
|
209
209
|
let selectStr = '';
|
|
210
210
|
let parenAlready = false;
|
|
@@ -221,7 +221,7 @@ class Malloy {
|
|
|
221
221
|
}
|
|
222
222
|
queryModel = new model_1.QueryModel(partialModel);
|
|
223
223
|
}
|
|
224
|
-
const compiledSql = queryModel.compileQuery(segment, false).sql;
|
|
224
|
+
const compiledSql = queryModel.compileQuery(segment, options, false).sql;
|
|
225
225
|
selectStr += parenAlready ? compiledSql : `(${compiledSql})`;
|
|
226
226
|
parenAlready = false;
|
|
227
227
|
}
|
|
@@ -446,6 +446,14 @@ class Model {
|
|
|
446
446
|
* @return A prepared query.
|
|
447
447
|
*/
|
|
448
448
|
get preparedQuery() {
|
|
449
|
+
return this.getPreparedQuery();
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Retrieve a prepared query for the final unnamed query at the top level of a model.
|
|
453
|
+
*
|
|
454
|
+
* @return A prepared query.
|
|
455
|
+
*/
|
|
456
|
+
getPreparedQuery() {
|
|
449
457
|
if (this.queryList.length === 0) {
|
|
450
458
|
throw new Error('Model has no queries.');
|
|
451
459
|
}
|
|
@@ -516,8 +524,11 @@ class PreparedQuery {
|
|
|
516
524
|
* @return A fully-prepared query (which contains the generated SQL).
|
|
517
525
|
*/
|
|
518
526
|
get preparedResult() {
|
|
527
|
+
return this.getPreparedResult();
|
|
528
|
+
}
|
|
529
|
+
getPreparedResult(options) {
|
|
519
530
|
const queryModel = new model_1.QueryModel(this._modelDef);
|
|
520
|
-
const translatedQuery = queryModel.compileQuery(this._query);
|
|
531
|
+
const translatedQuery = queryModel.compileQuery(this._query, options);
|
|
521
532
|
return new PreparedResult({
|
|
522
533
|
...translatedQuery,
|
|
523
534
|
queryName: this.name || translatedQuery.queryName,
|
|
@@ -787,6 +798,9 @@ class PreparedResult {
|
|
|
787
798
|
get sql() {
|
|
788
799
|
return this.inner.sql;
|
|
789
800
|
}
|
|
801
|
+
get dependenciesToMaterialize() {
|
|
802
|
+
return this.inner.dependenciesToMaterialize;
|
|
803
|
+
}
|
|
790
804
|
/**
|
|
791
805
|
* @return The `Explore` representing the data that will be returned by running this query.
|
|
792
806
|
*/
|
|
@@ -1603,17 +1617,18 @@ class Runtime {
|
|
|
1603
1617
|
parse,
|
|
1604
1618
|
refreshSchemaCache,
|
|
1605
1619
|
noThrowOnError,
|
|
1620
|
+
replaceMaterializedReferences: options === null || options === void 0 ? void 0 : options.replaceMaterializedReferences,
|
|
1606
1621
|
});
|
|
1607
|
-
});
|
|
1622
|
+
}, options);
|
|
1608
1623
|
}
|
|
1609
1624
|
// TODO Consider formalizing this. Perhaps as a `withModel` method,
|
|
1610
1625
|
// as well as a `Model.fromModelDefinition` if we choose to expose
|
|
1611
1626
|
// `ModelDef` to the world formally. For now, this should only
|
|
1612
1627
|
// be used in tests.
|
|
1613
|
-
_loadModelFromModelDef(modelDef) {
|
|
1628
|
+
_loadModelFromModelDef(modelDef, options) {
|
|
1614
1629
|
return new ModelMaterializer(this, async () => {
|
|
1615
1630
|
return new Model(modelDef, [], [], [], []);
|
|
1616
|
-
});
|
|
1631
|
+
}, options);
|
|
1617
1632
|
}
|
|
1618
1633
|
/**
|
|
1619
1634
|
* Load a Malloy query by URL or contents.
|
|
@@ -1635,7 +1650,7 @@ class Runtime {
|
|
|
1635
1650
|
* or loading further related objects.
|
|
1636
1651
|
*/
|
|
1637
1652
|
loadQueryByIndex(model, index, options) {
|
|
1638
|
-
return this.loadModel(model, options).loadQueryByIndex(index);
|
|
1653
|
+
return this.loadModel(model, options).loadQueryByIndex(index, options);
|
|
1639
1654
|
}
|
|
1640
1655
|
/**
|
|
1641
1656
|
* Load a Malloy query by the URL or contents of a Malloy model document
|
|
@@ -1647,7 +1662,7 @@ class Runtime {
|
|
|
1647
1662
|
* or loading further related objects.
|
|
1648
1663
|
*/
|
|
1649
1664
|
loadQueryByName(model, name, options) {
|
|
1650
|
-
return this.loadModel(model, options).loadQueryByName(name);
|
|
1665
|
+
return this.loadModel(model, options).loadQueryByName(name, options);
|
|
1651
1666
|
}
|
|
1652
1667
|
/**
|
|
1653
1668
|
* Load a SQL block by the URL or contents of a Malloy model document
|
|
@@ -1796,11 +1811,11 @@ class FluentState {
|
|
|
1796
1811
|
this.materialized = this._materialize();
|
|
1797
1812
|
return this.materialized;
|
|
1798
1813
|
}
|
|
1799
|
-
makeQueryMaterializer(materialize) {
|
|
1800
|
-
return new QueryMaterializer(this.runtime, materialize);
|
|
1814
|
+
makeQueryMaterializer(materialize, options) {
|
|
1815
|
+
return new QueryMaterializer(this.runtime, materialize, options);
|
|
1801
1816
|
}
|
|
1802
|
-
makeExploreMaterializer(materialize) {
|
|
1803
|
-
return new ExploreMaterializer(this.runtime, materialize);
|
|
1817
|
+
makeExploreMaterializer(materialize, options) {
|
|
1818
|
+
return new ExploreMaterializer(this.runtime, materialize, options);
|
|
1804
1819
|
}
|
|
1805
1820
|
makePreparedResultMaterializer(materialize) {
|
|
1806
1821
|
return new PreparedResultMaterializer(this.runtime, materialize);
|
|
@@ -1815,15 +1830,25 @@ class FluentState {
|
|
|
1815
1830
|
* queries or explores (via e.g. `loadFinalQuery()`, `loadQuery`, `loadExploreByName`, etc.).
|
|
1816
1831
|
*/
|
|
1817
1832
|
class ModelMaterializer extends FluentState {
|
|
1833
|
+
constructor(runtime, materialize, options) {
|
|
1834
|
+
var _a;
|
|
1835
|
+
super(runtime, materialize);
|
|
1836
|
+
this.runtime = runtime;
|
|
1837
|
+
this.replaceMaterializedReferences =
|
|
1838
|
+
(_a = options === null || options === void 0 ? void 0 : options.replaceMaterializedReferences) !== null && _a !== void 0 ? _a : false;
|
|
1839
|
+
}
|
|
1818
1840
|
/**
|
|
1819
1841
|
* Load the final (unnamed) Malloy query contained within this loaded `Model`.
|
|
1820
1842
|
*
|
|
1821
1843
|
* @return A `QueryMaterializer` capable of materializing the requested query, running it,
|
|
1822
1844
|
* or loading further related objects.
|
|
1823
1845
|
*/
|
|
1824
|
-
loadFinalQuery() {
|
|
1846
|
+
loadFinalQuery(options) {
|
|
1825
1847
|
return this.makeQueryMaterializer(async () => {
|
|
1826
|
-
return (await this.materialize()).
|
|
1848
|
+
return (await this.materialize()).getPreparedQuery();
|
|
1849
|
+
}, {
|
|
1850
|
+
replaceMaterializedReferences: this.replaceMaterializedReferences,
|
|
1851
|
+
...options,
|
|
1827
1852
|
});
|
|
1828
1853
|
}
|
|
1829
1854
|
/**
|
|
@@ -1833,9 +1858,12 @@ class ModelMaterializer extends FluentState {
|
|
|
1833
1858
|
* @return A `QueryMaterializer` capable of materializing the requested query, running it,
|
|
1834
1859
|
* or loading further related objects.
|
|
1835
1860
|
*/
|
|
1836
|
-
loadQueryByIndex(index) {
|
|
1861
|
+
loadQueryByIndex(index, options) {
|
|
1837
1862
|
return this.makeQueryMaterializer(async () => {
|
|
1838
1863
|
return (await this.materialize()).getPreparedQueryByIndex(index);
|
|
1864
|
+
}, {
|
|
1865
|
+
replaceMaterializedReferences: this.replaceMaterializedReferences,
|
|
1866
|
+
...options,
|
|
1839
1867
|
});
|
|
1840
1868
|
}
|
|
1841
1869
|
/**
|
|
@@ -1845,9 +1873,12 @@ class ModelMaterializer extends FluentState {
|
|
|
1845
1873
|
* @return A `QueryMaterializer` capable of materializing the requested query, running it,
|
|
1846
1874
|
* or loading further related objects.
|
|
1847
1875
|
*/
|
|
1848
|
-
loadQueryByName(name) {
|
|
1876
|
+
loadQueryByName(name, options) {
|
|
1849
1877
|
return this.makeQueryMaterializer(async () => {
|
|
1850
1878
|
return (await this.materialize()).getPreparedQueryByName(name);
|
|
1879
|
+
}, {
|
|
1880
|
+
replaceMaterializedReferences: this.replaceMaterializedReferences,
|
|
1881
|
+
...options,
|
|
1851
1882
|
});
|
|
1852
1883
|
}
|
|
1853
1884
|
/**
|
|
@@ -1860,6 +1891,7 @@ class ModelMaterializer extends FluentState {
|
|
|
1860
1891
|
loadQuery(query, options) {
|
|
1861
1892
|
const { refreshSchemaCache, noThrowOnError } = options || {};
|
|
1862
1893
|
return this.makeQueryMaterializer(async () => {
|
|
1894
|
+
var _a;
|
|
1863
1895
|
const urlReader = this.runtime.urlReader;
|
|
1864
1896
|
const connections = this.runtime.connections;
|
|
1865
1897
|
if (this.runtime.isTestRuntime) {
|
|
@@ -1888,6 +1920,7 @@ class ModelMaterializer extends FluentState {
|
|
|
1888
1920
|
model,
|
|
1889
1921
|
refreshSchemaCache,
|
|
1890
1922
|
noThrowOnError,
|
|
1923
|
+
replaceMaterializedReferences: (_a = options === null || options === void 0 ? void 0 : options.replaceMaterializedReferences) !== null && _a !== void 0 ? _a : this.replaceMaterializedReferences,
|
|
1891
1924
|
});
|
|
1892
1925
|
return queryModel.preparedQuery;
|
|
1893
1926
|
});
|
|
@@ -1909,6 +1942,7 @@ class ModelMaterializer extends FluentState {
|
|
|
1909
1942
|
}
|
|
1910
1943
|
}
|
|
1911
1944
|
return new ModelMaterializer(this.runtime, async () => {
|
|
1945
|
+
var _a;
|
|
1912
1946
|
const urlReader = this.runtime.urlReader;
|
|
1913
1947
|
const connections = this.runtime.connections;
|
|
1914
1948
|
const parse = query instanceof URL
|
|
@@ -1929,9 +1963,10 @@ class ModelMaterializer extends FluentState {
|
|
|
1929
1963
|
model,
|
|
1930
1964
|
refreshSchemaCache: options === null || options === void 0 ? void 0 : options.refreshSchemaCache,
|
|
1931
1965
|
noThrowOnError: options === null || options === void 0 ? void 0 : options.noThrowOnError,
|
|
1966
|
+
replaceMaterializedReferences: (_a = options === null || options === void 0 ? void 0 : options.replaceMaterializedReferences) !== null && _a !== void 0 ? _a : this.replaceMaterializedReferences,
|
|
1932
1967
|
});
|
|
1933
1968
|
return queryModel;
|
|
1934
|
-
});
|
|
1969
|
+
}, options);
|
|
1935
1970
|
}
|
|
1936
1971
|
async search(sourceName, searchTerm, limit = 1000, searchField = undefined) {
|
|
1937
1972
|
const model = await this.materialize();
|
|
@@ -2059,10 +2094,13 @@ class ModelMaterializer extends FluentState {
|
|
|
2059
2094
|
// as well as a `PreparedQuery.fromQueryDefinition` if we choose to expose
|
|
2060
2095
|
// `InternalQuery` to the world formally. For now, this should only
|
|
2061
2096
|
// be used in tests.
|
|
2062
|
-
_loadQueryFromQueryDef(query) {
|
|
2097
|
+
_loadQueryFromQueryDef(query, options) {
|
|
2063
2098
|
return this.makeQueryMaterializer(async () => {
|
|
2064
2099
|
const model = await this.materialize();
|
|
2065
2100
|
return new PreparedQuery(query, model._modelDef, model.problems);
|
|
2101
|
+
}, {
|
|
2102
|
+
replaceMaterializedReferences: this.replaceMaterializedReferences,
|
|
2103
|
+
...options,
|
|
2066
2104
|
});
|
|
2067
2105
|
}
|
|
2068
2106
|
/**
|
|
@@ -2075,6 +2113,8 @@ class ModelMaterializer extends FluentState {
|
|
|
2075
2113
|
loadExploreByName(name) {
|
|
2076
2114
|
return this.makeExploreMaterializer(async () => {
|
|
2077
2115
|
return (await this.materialize()).getExploreByName(name);
|
|
2116
|
+
}, {
|
|
2117
|
+
replaceMaterializedReferences: this.replaceMaterializedReferences,
|
|
2078
2118
|
});
|
|
2079
2119
|
}
|
|
2080
2120
|
/**
|
|
@@ -2102,6 +2142,13 @@ exports.ModelMaterializer = ModelMaterializer;
|
|
|
2102
2142
|
* prepared results or run the query (via e.g. `loadPreparedResult()` or `run()`).
|
|
2103
2143
|
*/
|
|
2104
2144
|
class QueryMaterializer extends FluentState {
|
|
2145
|
+
constructor(runtime, materialize, options) {
|
|
2146
|
+
var _a;
|
|
2147
|
+
super(runtime, materialize);
|
|
2148
|
+
this.runtime = runtime;
|
|
2149
|
+
this.replaceMaterializedReferences =
|
|
2150
|
+
(_a = options === null || options === void 0 ? void 0 : options.replaceMaterializedReferences) !== null && _a !== void 0 ? _a : false;
|
|
2151
|
+
}
|
|
2105
2152
|
/**
|
|
2106
2153
|
* Run this loaded `Query`.
|
|
2107
2154
|
*
|
|
@@ -2109,12 +2156,18 @@ class QueryMaterializer extends FluentState {
|
|
|
2109
2156
|
*/
|
|
2110
2157
|
async run(options) {
|
|
2111
2158
|
const connections = this.runtime.connections;
|
|
2112
|
-
const preparedResult = await this.getPreparedResult(
|
|
2159
|
+
const preparedResult = await this.getPreparedResult({
|
|
2160
|
+
replaceMaterializedReferences: this.replaceMaterializedReferences,
|
|
2161
|
+
...options,
|
|
2162
|
+
});
|
|
2113
2163
|
const finalOptions = runSQLOptionsWithAnnotations(preparedResult, options);
|
|
2114
2164
|
return Malloy.run({ connections, preparedResult, options: finalOptions });
|
|
2115
2165
|
}
|
|
2116
2166
|
async *runStream(options) {
|
|
2117
|
-
const preparedResult = await this.getPreparedResult(
|
|
2167
|
+
const preparedResult = await this.getPreparedResult({
|
|
2168
|
+
replaceMaterializedReferences: this.replaceMaterializedReferences,
|
|
2169
|
+
...options,
|
|
2170
|
+
});
|
|
2118
2171
|
const connections = this.runtime.connections;
|
|
2119
2172
|
const finalOptions = runSQLOptionsWithAnnotations(preparedResult, options);
|
|
2120
2173
|
const stream = Malloy.runStream({
|
|
@@ -2132,9 +2185,12 @@ class QueryMaterializer extends FluentState {
|
|
|
2132
2185
|
* @return A `PreparedResultMaterializer` capable of materializing the requested
|
|
2133
2186
|
* prepared query or running it.
|
|
2134
2187
|
*/
|
|
2135
|
-
loadPreparedResult() {
|
|
2188
|
+
loadPreparedResult(options) {
|
|
2136
2189
|
return this.makePreparedResultMaterializer(async () => {
|
|
2137
|
-
return (await this.materialize()).
|
|
2190
|
+
return (await this.materialize()).getPreparedResult({
|
|
2191
|
+
replaceMaterializedReferences: this.replaceMaterializedReferences,
|
|
2192
|
+
...options,
|
|
2193
|
+
});
|
|
2138
2194
|
});
|
|
2139
2195
|
}
|
|
2140
2196
|
/**
|
|
@@ -2142,16 +2198,22 @@ class QueryMaterializer extends FluentState {
|
|
|
2142
2198
|
*
|
|
2143
2199
|
* @return A promise of the prepared result of this loaded query.
|
|
2144
2200
|
*/
|
|
2145
|
-
getPreparedResult() {
|
|
2146
|
-
return this.loadPreparedResult(
|
|
2201
|
+
getPreparedResult(options) {
|
|
2202
|
+
return this.loadPreparedResult({
|
|
2203
|
+
replaceMaterializedReferences: this.replaceMaterializedReferences,
|
|
2204
|
+
...options,
|
|
2205
|
+
}).getPreparedResult();
|
|
2147
2206
|
}
|
|
2148
2207
|
/**
|
|
2149
2208
|
* Materialize the SQL of this loaded query.
|
|
2150
2209
|
*
|
|
2151
2210
|
* @return A promise of the SQL string.
|
|
2152
2211
|
*/
|
|
2153
|
-
async getSQL() {
|
|
2154
|
-
return (await this.getPreparedResult(
|
|
2212
|
+
async getSQL(options) {
|
|
2213
|
+
return (await this.getPreparedResult({
|
|
2214
|
+
replaceMaterializedReferences: this.replaceMaterializedReferences,
|
|
2215
|
+
...options,
|
|
2216
|
+
})).sql;
|
|
2155
2217
|
}
|
|
2156
2218
|
/**
|
|
2157
2219
|
* Materialize this loaded query.
|
|
@@ -2166,9 +2228,12 @@ class QueryMaterializer extends FluentState {
|
|
|
2166
2228
|
*
|
|
2167
2229
|
* @return The estimated cost of running this loaded query.
|
|
2168
2230
|
*/
|
|
2169
|
-
async estimateQueryCost() {
|
|
2231
|
+
async estimateQueryCost(options) {
|
|
2170
2232
|
const connections = this.runtime.connections;
|
|
2171
|
-
const preparedResult = await this.getPreparedResult(
|
|
2233
|
+
const preparedResult = await this.getPreparedResult({
|
|
2234
|
+
replaceMaterializedReferences: this.replaceMaterializedReferences,
|
|
2235
|
+
...options,
|
|
2236
|
+
});
|
|
2172
2237
|
return Malloy.estimateQueryCost({ connections, preparedResult });
|
|
2173
2238
|
}
|
|
2174
2239
|
}
|
|
@@ -2290,6 +2355,13 @@ exports.SQLBlockMaterializer = SQLBlockMaterializer;
|
|
|
2290
2355
|
* related queries.
|
|
2291
2356
|
*/
|
|
2292
2357
|
class ExploreMaterializer extends FluentState {
|
|
2358
|
+
constructor(runtime, materialize, options) {
|
|
2359
|
+
var _a;
|
|
2360
|
+
super(runtime, materialize);
|
|
2361
|
+
this.runtime = runtime;
|
|
2362
|
+
this.replaceMaterializedReferences =
|
|
2363
|
+
(_a = options === null || options === void 0 ? void 0 : options.replaceMaterializedReferences) !== null && _a !== void 0 ? _a : false;
|
|
2364
|
+
}
|
|
2293
2365
|
/**
|
|
2294
2366
|
* Load a query contained within this loaded explore.
|
|
2295
2367
|
*
|
|
@@ -2297,10 +2369,10 @@ class ExploreMaterializer extends FluentState {
|
|
|
2297
2369
|
* @return A `QueryMaterializer` capable of materializing the requested query, running it,
|
|
2298
2370
|
* or loading further related objects.
|
|
2299
2371
|
*/
|
|
2300
|
-
loadQueryByName(name) {
|
|
2372
|
+
loadQueryByName(name, options) {
|
|
2301
2373
|
return this.makeQueryMaterializer(async () => {
|
|
2302
2374
|
return (await this.materialize()).getQueryByName(name);
|
|
2303
|
-
});
|
|
2375
|
+
}, options);
|
|
2304
2376
|
}
|
|
2305
2377
|
/**
|
|
2306
2378
|
* Materialize a query contained within this loaded explore.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Dialect, DialectFieldList } from '../dialect';
|
|
2
|
-
import { AggregateFunctionType, Annotation, CompiledQuery, Expr, FieldDef, Filtered, FunctionOverloadDef, FunctionParameterDef, JoinRelationship, ModelDef, OrderBy, OutputFieldNode, Parameter, ParameterNode, PipeSegment, Query, QueryFieldDef, QuerySegment, ResultMetadataDef, ResultStructMetadataDef, SearchIndexResult, SegmentFieldDef, StructDef, StructRef, TurtleDef, FunctionOrderBy, Argument, AggregateExpr, FilterCondition, GenericSQLExpr, FieldnameNode, FunctionCallNode, UngroupNode, SourceReferenceNode, PickExpr, SpreadExpr, FilteredExpr } from './malloy_types';
|
|
2
|
+
import { AggregateFunctionType, Annotation, CompiledQuery, Expr, FieldDef, Filtered, FunctionOverloadDef, FunctionParameterDef, JoinRelationship, ModelDef, OrderBy, OutputFieldNode, Parameter, ParameterNode, PipeSegment, Query, QueryFieldDef, QuerySegment, ResultMetadataDef, ResultStructMetadataDef, SearchIndexResult, SegmentFieldDef, StructDef, StructRef, TurtleDef, FunctionOrderBy, Argument, AggregateExpr, FilterCondition, GenericSQLExpr, FieldnameNode, FunctionCallNode, UngroupNode, SourceReferenceNode, PickExpr, SpreadExpr, FilteredExpr, QueryToMaterialize, PrepareResultOptions } from './malloy_types';
|
|
3
3
|
import { Connection } from '../connection/types';
|
|
4
4
|
import { AndChain } from './utils';
|
|
5
5
|
import { QueryInfo } from '../dialect/dialect';
|
|
@@ -27,6 +27,7 @@ declare class StageWriter {
|
|
|
27
27
|
withs: string[];
|
|
28
28
|
udfs: string[];
|
|
29
29
|
pdts: string[];
|
|
30
|
+
dependenciesToMaterialize: Record<string, QueryToMaterialize>;
|
|
30
31
|
stagePrefix: string;
|
|
31
32
|
useCTE: boolean;
|
|
32
33
|
constructor(useCTE: boolean | undefined, parent: StageWriter | undefined);
|
|
@@ -34,6 +35,7 @@ declare class StageWriter {
|
|
|
34
35
|
root(): StageWriter;
|
|
35
36
|
addStage(sql: string): string;
|
|
36
37
|
addUDF(stageWriter: StageWriter, dialect: Dialect, structDef: StructDef): string;
|
|
38
|
+
addMaterializedQuery(fieldName: string, query: Query): string;
|
|
37
39
|
addPDT(baseName: string, dialect: Dialect): string;
|
|
38
40
|
combineStages(includeLastStage: boolean): {
|
|
39
41
|
sql: string;
|
|
@@ -290,6 +292,7 @@ declare class QueryQuery extends QueryField {
|
|
|
290
292
|
/** Structure object as it is used to build a query */
|
|
291
293
|
declare class QueryStruct extends QueryNode {
|
|
292
294
|
readonly sourceArguments: Record<string, Argument> | undefined;
|
|
295
|
+
readonly prepareResultOptions?: PrepareResultOptions | undefined;
|
|
293
296
|
fieldDef: StructDef;
|
|
294
297
|
parent: QueryStruct | undefined;
|
|
295
298
|
model: QueryModel;
|
|
@@ -297,7 +300,7 @@ declare class QueryStruct extends QueryNode {
|
|
|
297
300
|
pathAliasMap: Map<string, string>;
|
|
298
301
|
dialect: Dialect;
|
|
299
302
|
connectionName: string;
|
|
300
|
-
constructor(fieldDef: StructDef, sourceArguments: Record<string, Argument> | undefined, parent: ParentQueryStruct | ParentQueryModel);
|
|
303
|
+
constructor(fieldDef: StructDef, sourceArguments: Record<string, Argument> | undefined, parent: ParentQueryStruct | ParentQueryModel, prepareResultOptions?: PrepareResultOptions | undefined);
|
|
301
304
|
resolveParentParameterReferences(param: Parameter): Parameter;
|
|
302
305
|
private _arguments;
|
|
303
306
|
arguments(): Record<string, Argument>;
|
|
@@ -354,9 +357,9 @@ export declare class QueryModel {
|
|
|
354
357
|
constructor(modelDef: ModelDef | undefined);
|
|
355
358
|
loadModelFromDef(modelDef: ModelDef): void;
|
|
356
359
|
getStructByName(name: string): QueryStruct;
|
|
357
|
-
getStructFromRef(structRef: StructRef, sourceArguments: Record<string, Argument> | undefined): QueryStruct;
|
|
358
|
-
loadQuery(query: Query, stageWriter: StageWriter | undefined, emitFinalStage?: boolean, isJoinedSubquery?: boolean): QueryResults;
|
|
359
|
-
compileQuery(query: Query, finalize?: boolean): CompiledQuery;
|
|
360
|
+
getStructFromRef(structRef: StructRef, sourceArguments: Record<string, Argument> | undefined, prepareResultOptions?: PrepareResultOptions): QueryStruct;
|
|
361
|
+
loadQuery(query: Query, stageWriter: StageWriter | undefined, prepareResultOptions?: PrepareResultOptions, emitFinalStage?: boolean, isJoinedSubquery?: boolean): QueryResults;
|
|
362
|
+
compileQuery(query: Query, prepareResultOptions?: PrepareResultOptions, finalize?: boolean): CompiledQuery;
|
|
360
363
|
exploreSearchSQLMap: Map<any, any>;
|
|
361
364
|
searchIndex(connection: Connection, explore: string, searchValue: string, limit?: number, searchField?: string | undefined): Promise<SearchIndexResult[] | undefined>;
|
|
362
365
|
}
|
|
@@ -27,6 +27,7 @@ const dialect_1 = require("../dialect");
|
|
|
27
27
|
const standardsql_1 = require("../dialect/standardsql/standardsql");
|
|
28
28
|
const malloy_types_1 = require("./malloy_types");
|
|
29
29
|
const utils_1 = require("./utils");
|
|
30
|
+
const tags_1 = require("../tags");
|
|
30
31
|
function pathToCol(path) {
|
|
31
32
|
return path.map(el => encodeURIComponent(el)).join('/');
|
|
32
33
|
}
|
|
@@ -68,6 +69,7 @@ class StageWriter {
|
|
|
68
69
|
this.withs = [];
|
|
69
70
|
this.udfs = [];
|
|
70
71
|
this.pdts = [];
|
|
72
|
+
this.dependenciesToMaterialize = {};
|
|
71
73
|
this.stagePrefix = '__stage';
|
|
72
74
|
this.useCTE = useCTE;
|
|
73
75
|
}
|
|
@@ -104,9 +106,34 @@ class StageWriter {
|
|
|
104
106
|
this.root().udfs.push(sql);
|
|
105
107
|
return id;
|
|
106
108
|
}
|
|
109
|
+
addMaterializedQuery(fieldName, query) {
|
|
110
|
+
var _a;
|
|
111
|
+
const name = query.name;
|
|
112
|
+
if (!name) {
|
|
113
|
+
throw new Error(`Source ${fieldName} on a unnamed query that is tagged as materialize, only named queries can be materialized.`);
|
|
114
|
+
}
|
|
115
|
+
const path = (_a = query.location) === null || _a === void 0 ? void 0 : _a.url;
|
|
116
|
+
if (!path) {
|
|
117
|
+
throw new Error(`Trying to materialize query ${name}, but its path is not set.`);
|
|
118
|
+
}
|
|
119
|
+
// Creating an object that should uniquely identify a query within a Malloy model repo.
|
|
120
|
+
const queryRep = {
|
|
121
|
+
path: path,
|
|
122
|
+
source: undefined,
|
|
123
|
+
queryName: name,
|
|
124
|
+
};
|
|
125
|
+
const id = (0, utils_1.generateHash)(JSON.stringify(queryRep));
|
|
126
|
+
const uniqueName = `${name}-${id}`;
|
|
127
|
+
this.root().dependenciesToMaterialize[uniqueName] = {
|
|
128
|
+
...queryRep,
|
|
129
|
+
id,
|
|
130
|
+
};
|
|
131
|
+
return uniqueName;
|
|
132
|
+
}
|
|
107
133
|
addPDT(baseName, dialect) {
|
|
108
134
|
const sql = this.combineStages(false).sql + this.withs[this.withs.length - 1];
|
|
109
|
-
const
|
|
135
|
+
const name = baseName + (0, utils_1.generateHash)(sql);
|
|
136
|
+
const tableName = `scratch.${name}`;
|
|
110
137
|
this.root().pdts.push(dialect.sqlCreateTableAsSelect(tableName, sql));
|
|
111
138
|
return tableName;
|
|
112
139
|
}
|
|
@@ -1562,7 +1589,7 @@ class QueryQuery extends QueryField {
|
|
|
1562
1589
|
parent = new QueryStruct({
|
|
1563
1590
|
...sourceDef,
|
|
1564
1591
|
fields: [...sourceDef.fields, ...firstStage.extendSource],
|
|
1565
|
-
}, parentStruct.sourceArguments, parent.parent ? { struct: parent } : { model: parent.model });
|
|
1592
|
+
}, parentStruct.sourceArguments, parent.parent ? { struct: parent } : { model: parent.model }, parent.prepareResultOptions);
|
|
1566
1593
|
turtleWithFilters = {
|
|
1567
1594
|
...turtleWithFilters,
|
|
1568
1595
|
pipeline: [
|
|
@@ -2631,7 +2658,7 @@ class QueryQuery extends QueryField {
|
|
|
2631
2658
|
structDef.structSource = { type: 'sql', method: 'nested' };
|
|
2632
2659
|
const qs = new QueryStruct(structDef, undefined, {
|
|
2633
2660
|
model: this.parent.getModel(),
|
|
2634
|
-
});
|
|
2661
|
+
}, this.parent.prepareResultOptions);
|
|
2635
2662
|
const q = QueryQuery.makeQuery(newTurtle, qs, stageWriter, this.isJoinedSubquery);
|
|
2636
2663
|
pipeOut = q.generateSQLFromPipeline(stageWriter);
|
|
2637
2664
|
// console.log(stageWriter.generateSQLStages());
|
|
@@ -2689,7 +2716,7 @@ class QueryQuery extends QueryField {
|
|
|
2689
2716
|
for (const transform of pipeline) {
|
|
2690
2717
|
const s = new QueryStruct(structDef, undefined, {
|
|
2691
2718
|
model: this.parent.getModel(),
|
|
2692
|
-
});
|
|
2719
|
+
}, this.parent.prepareResultOptions);
|
|
2693
2720
|
const q = QueryQuery.makeQuery({ type: 'turtle', name: 'ignoreme', pipeline: [transform] }, s, stageWriter, this.isJoinedSubquery);
|
|
2694
2721
|
q.prepare(stageWriter);
|
|
2695
2722
|
lastStageName = q.generateSQL(stageWriter);
|
|
@@ -2954,9 +2981,10 @@ class QueryQueryIndex extends QueryQuery {
|
|
|
2954
2981
|
}
|
|
2955
2982
|
/** Structure object as it is used to build a query */
|
|
2956
2983
|
class QueryStruct extends QueryNode {
|
|
2957
|
-
constructor(fieldDef, sourceArguments, parent) {
|
|
2984
|
+
constructor(fieldDef, sourceArguments, parent, prepareResultOptions) {
|
|
2958
2985
|
super(fieldDef);
|
|
2959
2986
|
this.sourceArguments = sourceArguments;
|
|
2987
|
+
this.prepareResultOptions = prepareResultOptions;
|
|
2960
2988
|
this.nameMap = new Map();
|
|
2961
2989
|
this._arguments = undefined;
|
|
2962
2990
|
this.setParent(parent);
|
|
@@ -3030,7 +3058,7 @@ class QueryStruct extends QueryNode {
|
|
|
3030
3058
|
case 'struct': {
|
|
3031
3059
|
this.addFieldToNameMap(as, new QueryStruct(field, undefined, {
|
|
3032
3060
|
struct: this,
|
|
3033
|
-
}));
|
|
3061
|
+
}, this.prepareResultOptions));
|
|
3034
3062
|
break;
|
|
3035
3063
|
}
|
|
3036
3064
|
// case "reduce" || "project" || "index": {
|
|
@@ -3161,7 +3189,7 @@ class QueryStruct extends QueryNode {
|
|
|
3161
3189
|
resolveQueryFields() {
|
|
3162
3190
|
if (this.fieldDef.structSource.type === 'query') {
|
|
3163
3191
|
const structDef = this.model
|
|
3164
|
-
.loadQuery(this.fieldDef.structSource.query, undefined)
|
|
3192
|
+
.loadQuery(this.fieldDef.structSource.query, undefined, this.prepareResultOptions)
|
|
3165
3193
|
.structs.pop();
|
|
3166
3194
|
// should never happen.
|
|
3167
3195
|
if (!structDef) {
|
|
@@ -3235,6 +3263,7 @@ class QueryStruct extends QueryNode {
|
|
|
3235
3263
|
}
|
|
3236
3264
|
}
|
|
3237
3265
|
structSourceSQL(stageWriter) {
|
|
3266
|
+
var _a;
|
|
3238
3267
|
switch (this.fieldDef.structSource.type) {
|
|
3239
3268
|
case 'table': {
|
|
3240
3269
|
const tablePath = this.fieldDef.structSource.tablePath;
|
|
@@ -3257,17 +3286,18 @@ class QueryStruct extends QueryNode {
|
|
|
3257
3286
|
return '';
|
|
3258
3287
|
case 'query': {
|
|
3259
3288
|
// cache derived table.
|
|
3260
|
-
const
|
|
3261
|
-
|
|
3262
|
-
|
|
3263
|
-
|
|
3264
|
-
|
|
3265
|
-
|
|
3266
|
-
|
|
3289
|
+
const clonedAnnotation = structuredClone(this.fieldDef.structSource.query.annotation);
|
|
3290
|
+
if (clonedAnnotation) {
|
|
3291
|
+
clonedAnnotation.inherits = undefined;
|
|
3292
|
+
}
|
|
3293
|
+
const sourceTag = tags_1.Tag.annotationToTag(clonedAnnotation).tag;
|
|
3294
|
+
if (((_a = this.prepareResultOptions) === null || _a === void 0 ? void 0 : _a.replaceMaterializedReferences) &&
|
|
3295
|
+
sourceTag.has('materialize')) {
|
|
3296
|
+
return stageWriter.addMaterializedQuery((0, malloy_types_1.getIdentifier)(this.fieldDef), this.fieldDef.structSource.query);
|
|
3267
3297
|
}
|
|
3268
3298
|
else {
|
|
3269
3299
|
// returns the stage name.
|
|
3270
|
-
return this.model.loadQuery(this.fieldDef.structSource.query, stageWriter, false, true // this is an intermediate stage.
|
|
3300
|
+
return this.model.loadQuery(this.fieldDef.structSource.query, stageWriter, this.prepareResultOptions, false, true // this is an intermediate stage.
|
|
3271
3301
|
).lastStageName;
|
|
3272
3302
|
}
|
|
3273
3303
|
}
|
|
@@ -3424,13 +3454,13 @@ class QueryModel {
|
|
|
3424
3454
|
throw new Error(`Struct ${name} not found in model.`);
|
|
3425
3455
|
}
|
|
3426
3456
|
}
|
|
3427
|
-
getStructFromRef(structRef, sourceArguments) {
|
|
3457
|
+
getStructFromRef(structRef, sourceArguments, prepareResultOptions) {
|
|
3428
3458
|
var _a;
|
|
3429
3459
|
let structDef;
|
|
3430
3460
|
if (typeof structRef === 'string') {
|
|
3431
3461
|
const ret = this.getStructByName(structRef);
|
|
3432
3462
|
if (sourceArguments !== undefined) {
|
|
3433
|
-
return new QueryStruct(ret.fieldDef, sourceArguments, (_a = ret.parent) !== null && _a !== void 0 ? _a : { model: this });
|
|
3463
|
+
return new QueryStruct(ret.fieldDef, sourceArguments, (_a = ret.parent) !== null && _a !== void 0 ? _a : { model: this }, prepareResultOptions);
|
|
3434
3464
|
}
|
|
3435
3465
|
return ret;
|
|
3436
3466
|
}
|
|
@@ -3440,9 +3470,9 @@ class QueryModel {
|
|
|
3440
3470
|
else {
|
|
3441
3471
|
throw new Error('Broken for now');
|
|
3442
3472
|
}
|
|
3443
|
-
return new QueryStruct(structDef, sourceArguments, { model: this });
|
|
3473
|
+
return new QueryStruct(structDef, sourceArguments, { model: this }, prepareResultOptions);
|
|
3444
3474
|
}
|
|
3445
|
-
loadQuery(query, stageWriter, emitFinalStage = false, isJoinedSubquery = false) {
|
|
3475
|
+
loadQuery(query, stageWriter, prepareResultOptions, emitFinalStage = false, isJoinedSubquery = false) {
|
|
3446
3476
|
const malloy = '';
|
|
3447
3477
|
if (!stageWriter) {
|
|
3448
3478
|
stageWriter = new StageWriter(true, undefined);
|
|
@@ -3453,7 +3483,7 @@ class QueryModel {
|
|
|
3453
3483
|
pipeline: query.pipeline,
|
|
3454
3484
|
filterList: query.filterList,
|
|
3455
3485
|
};
|
|
3456
|
-
const q = QueryQuery.makeQuery(turtleDef, this.getStructFromRef(query.structRef, query.sourceArguments), stageWriter, isJoinedSubquery);
|
|
3486
|
+
const q = QueryQuery.makeQuery(turtleDef, this.getStructFromRef(query.structRef, query.sourceArguments, prepareResultOptions), stageWriter, isJoinedSubquery);
|
|
3457
3487
|
const ret = q.generateSQLFromPipeline(stageWriter);
|
|
3458
3488
|
if (emitFinalStage && q.parent.dialect.hasFinalStage) {
|
|
3459
3489
|
// const fieldNames: string[] = [];
|
|
@@ -3471,10 +3501,10 @@ class QueryModel {
|
|
|
3471
3501
|
connectionName: q.parent.connectionName,
|
|
3472
3502
|
};
|
|
3473
3503
|
}
|
|
3474
|
-
compileQuery(query, finalize = true) {
|
|
3504
|
+
compileQuery(query, prepareResultOptions, finalize = true) {
|
|
3475
3505
|
let newModel;
|
|
3476
3506
|
const m = newModel || this;
|
|
3477
|
-
const ret = m.loadQuery(query, undefined, finalize, false);
|
|
3507
|
+
const ret = m.loadQuery(query, undefined, prepareResultOptions, finalize, false);
|
|
3478
3508
|
const sourceExplore = typeof query.structRef === 'string'
|
|
3479
3509
|
? query.structRef
|
|
3480
3510
|
: // LTNOTE: the parser needs to capture the query before the |. This will work
|
|
@@ -3492,6 +3522,7 @@ class QueryModel {
|
|
|
3492
3522
|
lastStageName: ret.lastStageName,
|
|
3493
3523
|
malloy: ret.malloy,
|
|
3494
3524
|
sql: ret.stageWriter.generateSQLStages(),
|
|
3525
|
+
dependenciesToMaterialize: ret.stageWriter.dependenciesToMaterialize,
|
|
3495
3526
|
structs: ret.structs,
|
|
3496
3527
|
sourceExplore,
|
|
3497
3528
|
sourceFilters: query.filterList,
|
|
@@ -3533,7 +3564,7 @@ class QueryModel {
|
|
|
3533
3564
|
// if we've compiled the SQL before use it otherwise
|
|
3534
3565
|
let sqlPDT = this.exploreSearchSQLMap.get(explore);
|
|
3535
3566
|
if (sqlPDT === undefined) {
|
|
3536
|
-
sqlPDT = this.compileQuery(indexQuery, false).sql;
|
|
3567
|
+
sqlPDT = this.compileQuery(indexQuery, undefined, false).sql;
|
|
3537
3568
|
this.exploreSearchSQLMap.set(explore, sqlPDT);
|
|
3538
3569
|
}
|
|
3539
3570
|
let query = `SELECT
|
|
@@ -719,6 +719,12 @@ export interface DrillSource {
|
|
|
719
719
|
sourceExplore: string;
|
|
720
720
|
sourceFilters?: FilterCondition[];
|
|
721
721
|
}
|
|
722
|
+
export type QueryToMaterialize = {
|
|
723
|
+
id: string;
|
|
724
|
+
path: string;
|
|
725
|
+
source: string | undefined;
|
|
726
|
+
queryName: string;
|
|
727
|
+
};
|
|
722
728
|
export interface CompiledQuery extends DrillSource {
|
|
723
729
|
structs: StructDef[];
|
|
724
730
|
sql: string;
|
|
@@ -728,6 +734,7 @@ export interface CompiledQuery extends DrillSource {
|
|
|
728
734
|
connectionName: string;
|
|
729
735
|
queryTimezone?: string;
|
|
730
736
|
annotation?: Annotation;
|
|
737
|
+
dependenciesToMaterialize?: Record<string, QueryToMaterialize>;
|
|
731
738
|
}
|
|
732
739
|
/** Result type for running a Malloy query. */
|
|
733
740
|
export interface QueryResult extends CompiledQuery {
|
|
@@ -773,4 +780,7 @@ export interface SearchValueMapResult {
|
|
|
773
780
|
weight: number;
|
|
774
781
|
}[];
|
|
775
782
|
}
|
|
783
|
+
export interface PrepareResultOptions {
|
|
784
|
+
replaceMaterializedReferences?: boolean;
|
|
785
|
+
}
|
|
776
786
|
export {};
|