@malloydata/malloy 0.0.104-dev231117205713 → 0.0.104-dev231120193457

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.
@@ -3,7 +3,7 @@ import { DocumentLocation, DocumentPosition, DocumentRange, DocumentReference, I
3
3
  import { LogMessage, MessageLog, MessageLogger } from './parse-log';
4
4
  import { Zone, ZoneData } from './zone';
5
5
  import { ReferenceList } from './reference-list';
6
- import { ASTResponse, CompletionsResponse, DataRequestResponse, ProblemResponse, FatalResponse, FinalResponse, HelpContextResponse, MetadataResponse, ModelDataRequest, NeedURLData, TranslateResponse } from './translate-response';
6
+ import { ASTResponse, CompletionsResponse, DataRequestResponse, ProblemResponse, FatalResponse, FinalResponse, HelpContextResponse, MetadataResponse, ModelDataRequest, NeedURLData, TranslateResponse, ModelAnnotationResponse } from './translate-response';
7
7
  import { Tag } from '../tags';
8
8
  import { MalloyParseInfo } from './malloy-parse-info';
9
9
  export type StepResponses = DataRequestResponse | ASTResponse | TranslateResponse | ParseResponse | MetadataResponse;
@@ -83,6 +83,12 @@ declare class HelpContextStep implements TranslationStep {
83
83
  character: number;
84
84
  }): HelpContextResponse;
85
85
  }
86
+ declare class ModelAnnotationStep implements TranslationStep {
87
+ readonly parseStep: ParseStep;
88
+ response?: ModelAnnotationResponse;
89
+ constructor(parseStep: ParseStep);
90
+ step(that: MalloyTranslation, extendingModel?: ModelDef): ModelAnnotationResponse;
91
+ }
86
92
  declare class TranslateStep implements TranslationStep {
87
93
  readonly astStep: ASTStep;
88
94
  response?: TranslateResponse;
@@ -103,6 +109,7 @@ export declare abstract class MalloyTranslation {
103
109
  imports: ImportLocation[];
104
110
  compilerFlags: Tag;
105
111
  readonly parseStep: ParseStep;
112
+ readonly modelAnnotationStep: ModelAnnotationStep;
106
113
  readonly importsAndTablesStep: ImportsAndTablesStep;
107
114
  readonly astStep: ASTStep;
108
115
  readonly metadataStep: MetadataStep;
@@ -136,6 +143,7 @@ export declare abstract class MalloyTranslation {
136
143
  translate(extendingModel?: ModelDef): TranslateResponse;
137
144
  importAt(position: DocumentPosition): ImportLocation | undefined;
138
145
  metadata(): MetadataResponse;
146
+ modelAnnotation(extendingModel?: ModelDef): ModelAnnotationResponse;
139
147
  completions(position: {
140
148
  line: number;
141
149
  character: number;
@@ -62,6 +62,7 @@ const reference_list_1 = require("./reference-list");
62
62
  const translate_response_1 = require("./translate-response");
63
63
  const utils_1 = require("./utils");
64
64
  const tags_1 = require("../tags");
65
+ const model_annotation_walker_1 = require("./parse-tree-walkers/model-annotation-walker");
65
66
  /**
66
67
  * This ignores a -> popMode when the mode stack is empty, which is a hack,
67
68
  * but it let's us parse }%
@@ -451,6 +452,29 @@ class HelpContextStep {
451
452
  }
452
453
  }
453
454
  }
455
+ class ModelAnnotationStep {
456
+ constructor(parseStep) {
457
+ this.parseStep = parseStep;
458
+ }
459
+ step(that, extendingModel) {
460
+ if (!this.response) {
461
+ const tryParse = this.parseStep.step(that);
462
+ if (!tryParse.parse || tryParse.final) {
463
+ return tryParse;
464
+ }
465
+ else {
466
+ const modelAnnotation = (0, model_annotation_walker_1.walkForModelAnnotation)(that, tryParse.parse.tokenStream, tryParse.parse);
467
+ this.response = {
468
+ modelAnnotation: {
469
+ ...modelAnnotation,
470
+ inherits: extendingModel === null || extendingModel === void 0 ? void 0 : extendingModel.annotation,
471
+ },
472
+ };
473
+ }
474
+ }
475
+ return this.response;
476
+ }
477
+ }
454
478
  class TranslateStep {
455
479
  constructor(astStep) {
456
480
  this.astStep = astStep;
@@ -545,6 +569,7 @@ class MalloyTranslation {
545
569
  * things will happen automatically.
546
570
  */
547
571
  this.parseStep = new ParseStep();
572
+ this.modelAnnotationStep = new ModelAnnotationStep(this.parseStep);
548
573
  this.metadataStep = new MetadataStep(this.parseStep);
549
574
  this.completionsStep = new CompletionsStep(this.parseStep);
550
575
  this.helpContextStep = new HelpContextStep(this.parseStep);
@@ -703,6 +728,9 @@ class MalloyTranslation {
703
728
  metadata() {
704
729
  return this.metadataStep.step(this);
705
730
  }
731
+ modelAnnotation(extendingModel) {
732
+ return this.modelAnnotationStep.step(this, extendingModel);
733
+ }
706
734
  completions(position) {
707
735
  return this.completionsStep.step(this, position);
708
736
  }
@@ -0,0 +1,5 @@
1
+ import { CommonTokenStream } from 'antlr4ts';
2
+ import { MalloyTranslation } from '../parse-malloy';
3
+ import { Annotation } from '../../model/malloy_types';
4
+ import { MalloyParseInfo } from '../malloy-parse-info';
5
+ export declare function walkForModelAnnotation(forParse: MalloyTranslation, tokens: CommonTokenStream, parseInfo: MalloyParseInfo): Annotation;
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright 2023 Google LLC
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files
7
+ * (the "Software"), to deal in the Software without restriction,
8
+ * including without limitation the rights to use, copy, modify, merge,
9
+ * publish, distribute, sublicense, and/or sell copies of the Software,
10
+ * and to permit persons to whom the Software is furnished to do so,
11
+ * subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+ Object.defineProperty(exports, "__esModule", { value: true });
25
+ exports.walkForModelAnnotation = void 0;
26
+ const ParseTreeWalker_1 = require("antlr4ts/tree/ParseTreeWalker");
27
+ class ModelAnnotationWalker {
28
+ constructor(translator, tokens, parseInfo) {
29
+ this.translator = translator;
30
+ this.tokens = tokens;
31
+ this.parseInfo = parseInfo;
32
+ this.notes = [];
33
+ }
34
+ getLocation(cx) {
35
+ return {
36
+ url: this.parseInfo.sourceURL,
37
+ range: this.parseInfo.rangeFromContext(cx),
38
+ };
39
+ }
40
+ enterDocAnnotations(pcx) {
41
+ const allNotes = pcx.DOC_ANNOTATION().map(note => {
42
+ return {
43
+ text: note.text,
44
+ at: this.getLocation(pcx),
45
+ };
46
+ });
47
+ this.notes.push(...allNotes);
48
+ }
49
+ get annotation() {
50
+ return { notes: this.notes };
51
+ }
52
+ }
53
+ function walkForModelAnnotation(forParse, tokens, parseInfo) {
54
+ const finder = new ModelAnnotationWalker(forParse, tokens, parseInfo);
55
+ const listener = finder;
56
+ ParseTreeWalker_1.ParseTreeWalker.DEFAULT.walk(listener, parseInfo.root);
57
+ return finder.annotation;
58
+ }
59
+ exports.walkForModelAnnotation = walkForModelAnnotation;
60
+ //# sourceMappingURL=model-annotation-walker.js.map
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright 2023 Google LLC
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files
7
+ * (the "Software"), to deal in the Software without restriction,
8
+ * including without limitation the rights to use, copy, modify, merge,
9
+ * publish, distribute, sublicense, and/or sell copies of the Software,
10
+ * and to permit persons to whom the Software is furnished to do so,
11
+ * subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+ Object.defineProperty(exports, "__esModule", { value: true });
25
+ const test_translator_1 = require("./test-translator");
26
+ test('model annotations can be retrieved', () => {
27
+ var _a, _b;
28
+ const src = (0, test_translator_1.markSource) `${'## foo'}`;
29
+ const doc = new test_translator_1.TestTranslator(src.code);
30
+ const { modelAnnotation } = doc.modelAnnotation();
31
+ expect((_a = modelAnnotation === null || modelAnnotation === void 0 ? void 0 : modelAnnotation.notes) === null || _a === void 0 ? void 0 : _a.length).toBe(1);
32
+ const notes = (_b = modelAnnotation === null || modelAnnotation === void 0 ? void 0 : modelAnnotation.notes) !== null && _b !== void 0 ? _b : [];
33
+ expect(notes[0].text).toBe('## foo');
34
+ expect(notes[0].at).toMatchObject(src.locations[0]);
35
+ });
36
+ test('does not explode if bad parse', () => {
37
+ const src = (0, test_translator_1.markSource) `
38
+ ${'## foo'}
39
+ asd afsjei ; duavby {{}}}
40
+ `;
41
+ const doc = new test_translator_1.TestTranslator(src.code);
42
+ const response = doc.modelAnnotation();
43
+ expect(response.modelAnnotation).toBe(undefined);
44
+ expect(response.final).toBe(true);
45
+ });
46
+ //# sourceMappingURL=model-annotation-walker.spec.js.map
@@ -1,4 +1,4 @@
1
- import { ModelDef, Query, SQLBlockSource, SQLBlockStructDef } from '../model/malloy_types';
1
+ import { Annotation, ModelDef, Query, SQLBlockSource, SQLBlockStructDef } from '../model/malloy_types';
2
2
  import { MalloyElement } from './ast';
3
3
  import { LogMessage } from './parse-log';
4
4
  import { DocumentSymbol } from './parse-tree-walkers/document-symbol-walker';
@@ -43,6 +43,10 @@ interface Metadata extends NeededData, ProblemResponse, FinalResponse {
43
43
  highlights: DocumentHighlight[];
44
44
  }
45
45
  export type MetadataResponse = Partial<Metadata>;
46
+ interface ModelAnnotationData extends NeededData, ProblemResponse, FinalResponse {
47
+ modelAnnotation: Annotation;
48
+ }
49
+ export type ModelAnnotationResponse = Partial<ModelAnnotationData>;
46
50
  interface Completions extends NeededData, ProblemResponse, FinalResponse {
47
51
  completions: DocumentCompletion[];
48
52
  }
package/dist/malloy.d.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  import { RunSQLOptions } from './run_sql_options';
3
3
  import { DocumentCompletion as DocumentCompletionDefinition, DocumentHighlight as DocumentHighlightDefinition, 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, FilterExpression, Query as InternalQuery, ModelDef, DocumentPosition as ModelDocumentPosition, NamedQuery, QueryData, QueryDataRow, QueryResult, SQLBlock, SQLBlockStructDef, SearchIndexResult, SearchValueMapResult, StructDef, TurtleDef, FieldUnsupportedDef, QueryRunStats, ImportLocation } from './model';
5
+ import { CompiledQuery, DocumentLocation, DocumentReference, FieldBooleanDef, FieldDateDef, FieldJSONDef, FieldNumberDef, FieldStringDef, FieldTimestampDef, FieldTypeDef, FilterExpression, Query as InternalQuery, ModelDef, DocumentPosition as ModelDocumentPosition, NamedQuery, QueryData, QueryDataRow, QueryResult, SQLBlock, SQLBlockStructDef, SearchIndexResult, SearchValueMapResult, StructDef, TurtleDef, FieldUnsupportedDef, QueryRunStats, ImportLocation, Annotation } from './model';
6
6
  import { Connection, InfoConnection, LookupConnection, ModelString, ModelURL, QueryString, QueryURL, URLReader } from './runtime_types';
7
7
  import { Tag, TagParse, TagParseSpec, Taggable } from './tags';
8
8
  export interface Loggable {
@@ -425,6 +425,8 @@ export declare class PreparedResult implements Taggable {
425
425
  constructor(query: CompiledQuery, modelDef: ModelDef);
426
426
  tagParse(spec?: TagParseSpec): TagParse;
427
427
  getTaglines(prefix?: RegExp): string[];
428
+ get annotation(): Annotation | undefined;
429
+ get modelAnnotation(): Annotation | undefined;
428
430
  /**
429
431
  * @return The name of the connection this query should be run against.
430
432
  */
package/dist/malloy.js CHANGED
@@ -129,6 +129,7 @@ class Malloy {
129
129
  }
130
130
  }
131
131
  }
132
+ const { modelAnnotation } = translator.modelAnnotation(model === null || model === void 0 ? void 0 : model._modelDef);
132
133
  if (result.tables) {
133
134
  // collect tables by connection name since there may be multiple connections
134
135
  const tablesByConnection = new Map();
@@ -151,6 +152,7 @@ class Malloy {
151
152
  // the translator runs into an infinite loop fetching tables.
152
153
  const { schemas: tables, errors } = await connection.fetchSchemaForTables(tablePathByKey, {
153
154
  refreshTimestamp,
155
+ modelAnnotation,
154
156
  });
155
157
  translator.update({ tables, errors: { tables: errors } });
156
158
  }
@@ -175,6 +177,7 @@ class Malloy {
175
177
  const expanded = Malloy.compileSQLBlock(result.partialModel, toCompile);
176
178
  const resolved = await conn.fetchSchemaForSQLBlock(expanded, {
177
179
  refreshTimestamp,
180
+ modelAnnotation,
178
181
  });
179
182
  if (resolved.error) {
180
183
  translator.update({
@@ -747,6 +750,12 @@ class PreparedResult {
747
750
  getTaglines(prefix) {
748
751
  return tags_1.Tag.annotationToTaglines(this.inner.annotation, prefix);
749
752
  }
753
+ get annotation() {
754
+ return this.inner.annotation;
755
+ }
756
+ get modelAnnotation() {
757
+ return this.modelDef.annotation;
758
+ }
750
759
  /**
751
760
  * @return The name of the connection this query should be run against.
752
761
  */
@@ -1256,10 +1265,8 @@ class AtomicField extends Entity {
1256
1265
  return this.parent;
1257
1266
  }
1258
1267
  get expression() {
1259
- var _a, _b;
1260
- return (((_a = this.fieldTypeDef.resultMetadata) === null || _a === void 0 ? void 0 : _a.sourceExpression) ||
1261
- ((_b = this.fieldTypeDef.resultMetadata) === null || _b === void 0 ? void 0 : _b.sourceField) ||
1262
- this.name);
1268
+ var _a;
1269
+ return ((_a = this.fieldTypeDef.resultMetadata) === null || _a === void 0 ? void 0 : _a.sourceExpression) || this.name;
1263
1270
  }
1264
1271
  get location() {
1265
1272
  return this.fieldTypeDef.location;
@@ -2033,12 +2040,18 @@ class QueryMaterializer extends FluentState {
2033
2040
  async run(options) {
2034
2041
  const connections = this.runtime.connections;
2035
2042
  const preparedResult = await this.getPreparedResult();
2036
- return Malloy.run({ connections, preparedResult, options });
2043
+ const finalOptions = runSQLOptionsWithAnnotations(preparedResult, options);
2044
+ return Malloy.run({ connections, preparedResult, options: finalOptions });
2037
2045
  }
2038
2046
  async *runStream(options) {
2039
2047
  const preparedResult = await this.getPreparedResult();
2040
2048
  const connections = this.runtime.connections;
2041
- const stream = Malloy.runStream({ connections, preparedResult, options });
2049
+ const finalOptions = runSQLOptionsWithAnnotations(preparedResult, options);
2050
+ const stream = Malloy.runStream({
2051
+ connections,
2052
+ preparedResult,
2053
+ options: finalOptions,
2054
+ });
2042
2055
  for await (const row of stream) {
2043
2056
  yield row;
2044
2057
  }
@@ -2090,6 +2103,13 @@ class QueryMaterializer extends FluentState {
2090
2103
  }
2091
2104
  }
2092
2105
  exports.QueryMaterializer = QueryMaterializer;
2106
+ function runSQLOptionsWithAnnotations(preparedResult, givenOptions) {
2107
+ return {
2108
+ queryAnnotation: preparedResult.annotation,
2109
+ modelAnnotation: preparedResult.modelAnnotation,
2110
+ ...givenOptions,
2111
+ };
2112
+ }
2093
2113
  /**
2094
2114
  * An object representing the task of loading a `PreparedResult`, capable of
2095
2115
  * materializing the prepared result (via `getPreparedResult()`) or extending the task run
@@ -2104,12 +2124,22 @@ class PreparedResultMaterializer extends FluentState {
2104
2124
  async run(options) {
2105
2125
  const preparedResult = await this.getPreparedResult();
2106
2126
  const connections = this.runtime.connections;
2107
- return Malloy.run({ connections, preparedResult, options });
2127
+ const finalOptions = runSQLOptionsWithAnnotations(preparedResult, options);
2128
+ return Malloy.run({
2129
+ connections,
2130
+ preparedResult,
2131
+ options: finalOptions,
2132
+ });
2108
2133
  }
2109
2134
  async *runStream(options) {
2110
2135
  const preparedResult = await this.getPreparedResult();
2111
2136
  const connections = this.runtime.connections;
2112
- const stream = Malloy.runStream({ connections, preparedResult, options });
2137
+ const finalOptions = runSQLOptionsWithAnnotations(preparedResult, options);
2138
+ const stream = Malloy.runStream({
2139
+ connections,
2140
+ preparedResult,
2141
+ options: finalOptions,
2142
+ });
2113
2143
  for await (const row of stream) {
2114
2144
  yield row;
2115
2145
  }
@@ -1,4 +1,7 @@
1
+ import { Annotation } from './model/malloy_types';
1
2
  export interface RunSQLOptions {
2
3
  rowLimit?: number;
3
4
  abortSignal?: AbortSignal;
5
+ modelAnnotation?: Annotation;
6
+ queryAnnotation?: Annotation;
4
7
  }
@@ -1,5 +1,5 @@
1
1
  import { RunSQLOptions } from './run_sql_options';
2
- import { MalloyQueryData, QueryDataRow, QueryRunStats, SQLBlock, StructDef } from './model/malloy_types';
2
+ import { Annotation, MalloyQueryData, QueryDataRow, QueryRunStats, SQLBlock, StructDef } from './model/malloy_types';
3
3
  import { Dialect } from './dialect';
4
4
  /**
5
5
  * The contents of a Malloy query document.
@@ -34,6 +34,7 @@ export interface URLReader {
34
34
  */
35
35
  export interface FetchSchemaOptions {
36
36
  refreshTimestamp?: number;
37
+ modelAnnotation?: Annotation;
37
38
  }
38
39
  /**
39
40
  * An object capable of reading schemas for given table names.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/malloy",
3
- "version": "0.0.104-dev231117205713",
3
+ "version": "0.0.104-dev231120193457",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",