@malloydata/malloy 0.0.239 → 0.0.240-dev250305162504

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.
@@ -1,3 +1,4 @@
1
1
  import * as Malloy from '@malloydata/malloy-interfaces';
2
- import { ModelDef } from './model';
2
+ import { FieldDef, ModelDef, SourceDef } from './model';
3
3
  export declare function modelDefToModelInfo(modelDef: ModelDef): Malloy.ModelInfo;
4
+ export declare function convertFieldInfos(source: SourceDef, fields: FieldDef[]): Malloy.FieldInfo[];
package/dist/to_stable.js CHANGED
@@ -1,8 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.modelDefToModelInfo = void 0;
3
+ exports.convertFieldInfos = exports.modelDefToModelInfo = void 0;
4
4
  const model_1 = require("./model");
5
5
  const malloy_query_1 = require("./model/malloy_query");
6
+ const annotation_1 = require("./annotation");
7
+ const malloy_tag_1 = require("@malloydata/malloy-tag");
6
8
  function modelDefToModelInfo(modelDef) {
7
9
  const modelInfo = {
8
10
  entries: [],
@@ -23,37 +25,74 @@ function modelDefToModelInfo(modelDef) {
23
25
  }
24
26
  else if (entry.type === 'query') {
25
27
  const outputStruct = (0, malloy_query_1.getResultStructDefForQuery)(modelDef, entry);
28
+ const annotations = getAnnotationsFromField(entry);
29
+ const resultMetadataAnnotation = outputStruct.resultMetadata
30
+ ? getResultStructMetadataAnnotation(outputStruct, outputStruct.resultMetadata)
31
+ : undefined;
32
+ const fieldAnnotations = [
33
+ ...(annotations !== null && annotations !== void 0 ? annotations : []),
34
+ ...(resultMetadataAnnotation ? [resultMetadataAnnotation] : []),
35
+ ];
26
36
  const queryInfo = {
27
37
  kind: 'source',
28
38
  name,
29
39
  schema: {
30
40
  fields: convertFieldInfos(outputStruct, outputStruct.fields),
31
41
  },
42
+ annotations: fieldAnnotations.length > 0 ? fieldAnnotations : undefined,
32
43
  };
33
44
  modelInfo.entries.push(queryInfo);
34
45
  }
35
46
  }
36
47
  for (const query of modelDef.queryList) {
37
48
  const outputStruct = (0, malloy_query_1.getResultStructDefForQuery)(modelDef, query);
49
+ const annotations = getAnnotationsFromField(query);
50
+ const resultMetadataAnnotation = outputStruct.resultMetadata
51
+ ? getResultStructMetadataAnnotation(outputStruct, outputStruct.resultMetadata)
52
+ : undefined;
53
+ const fieldAnnotations = [
54
+ ...(annotations !== null && annotations !== void 0 ? annotations : []),
55
+ ...(resultMetadataAnnotation ? [resultMetadataAnnotation] : []),
56
+ ];
38
57
  const queryInfo = {
39
58
  schema: {
40
59
  fields: convertFieldInfos(outputStruct, outputStruct.fields),
41
60
  },
61
+ annotations: fieldAnnotations.length > 0 ? fieldAnnotations : undefined,
42
62
  };
43
63
  modelInfo.anonymous_queries.push(queryInfo);
44
64
  }
45
65
  return modelInfo;
46
66
  }
47
67
  exports.modelDefToModelInfo = modelDefToModelInfo;
68
+ function getAnnotationsFromField(field) {
69
+ const taglines = (0, annotation_1.annotationToTaglines)(field.annotation);
70
+ return taglines.map(tagline => ({
71
+ value: tagline,
72
+ }));
73
+ }
48
74
  function convertFieldInfos(source, fields) {
49
75
  var _a, _b, _c;
50
76
  const result = [];
51
77
  for (const field of fields) {
78
+ const taglines = (0, annotation_1.annotationToTaglines)(field.annotation);
79
+ const rawAnnotations = taglines.map(tagline => ({
80
+ value: tagline,
81
+ }));
82
+ const annotations = rawAnnotations.length > 0 ? rawAnnotations : undefined;
52
83
  if ((0, model_1.isTurtle)(field)) {
53
84
  const outputStruct = (0, malloy_query_1.getResultStructDefForView)(source, field);
85
+ const resultMetadataAnnotation = outputStruct.resultMetadata
86
+ ? getResultStructMetadataAnnotation(outputStruct, outputStruct.resultMetadata)
87
+ : undefined;
88
+ const fieldAnnotations = [
89
+ ...(annotations !== null && annotations !== void 0 ? annotations : []),
90
+ ...(resultMetadataAnnotation ? [resultMetadataAnnotation] : []),
91
+ ];
54
92
  const fieldInfo = {
55
93
  kind: 'view',
56
94
  name: (_a = field.as) !== null && _a !== void 0 ? _a : field.name,
95
+ annotations: fieldAnnotations.length > 0 ? fieldAnnotations : undefined,
57
96
  schema: { fields: convertFieldInfos(outputStruct, outputStruct.fields) },
58
97
  };
59
98
  result.push(fieldInfo);
@@ -65,10 +104,18 @@ function convertFieldInfos(source, fields) {
65
104
  continue;
66
105
  if (field.type === 'error')
67
106
  continue;
107
+ const resultMetadataAnnotation = field.resultMetadata
108
+ ? getResultMetadataAnnotation(field, field.resultMetadata)
109
+ : undefined;
110
+ const fieldAnnotations = [
111
+ ...(annotations !== null && annotations !== void 0 ? annotations : []),
112
+ ...(resultMetadataAnnotation ? [resultMetadataAnnotation] : []),
113
+ ];
68
114
  const fieldInfo = {
69
115
  kind: aggregate ? 'measure' : 'dimension',
70
116
  name: (_b = field.as) !== null && _b !== void 0 ? _b : field.name,
71
117
  type: typeDefToType(field),
118
+ annotations: fieldAnnotations.length > 0 ? fieldAnnotations : undefined,
72
119
  };
73
120
  result.push(fieldInfo);
74
121
  }
@@ -76,6 +123,7 @@ function convertFieldInfos(source, fields) {
76
123
  const fieldInfo = {
77
124
  kind: 'join',
78
125
  name: (_c = field.as) !== null && _c !== void 0 ? _c : field.name,
126
+ annotations,
79
127
  schema: {
80
128
  fields: convertFieldInfos(field, field.fields),
81
129
  },
@@ -86,6 +134,60 @@ function convertFieldInfos(source, fields) {
86
134
  }
87
135
  return result;
88
136
  }
137
+ exports.convertFieldInfos = convertFieldInfos;
138
+ function getResultMetadataAnnotation(field, resultMetadata) {
139
+ const tag = malloy_tag_1.Tag.withPrefix('#(malloy) ');
140
+ let hasAny = false;
141
+ if (resultMetadata.referenceId !== undefined) {
142
+ tag.set(['reference_id'], resultMetadata.referenceId);
143
+ hasAny = true;
144
+ }
145
+ if (resultMetadata.fieldKind === 'measure') {
146
+ tag.set(['calculation']);
147
+ hasAny = true;
148
+ }
149
+ if (resultMetadata.fieldKind === 'dimension') {
150
+ const dot = '.';
151
+ // If field is joined-in from another table i.e. of type `tableName.columnName`,
152
+ // return sourceField, else return name because this could be a renamed field.
153
+ const drillExpression = (resultMetadata === null || resultMetadata === void 0 ? void 0 : resultMetadata.sourceExpression) ||
154
+ ((resultMetadata === null || resultMetadata === void 0 ? void 0 : resultMetadata.sourceField.includes(dot))
155
+ ? resultMetadata === null || resultMetadata === void 0 ? void 0 : resultMetadata.sourceField
156
+ : field.name);
157
+ tag.set(['drill_expression'], drillExpression);
158
+ hasAny = true;
159
+ }
160
+ return hasAny
161
+ ? {
162
+ value: tag.toString(),
163
+ }
164
+ : undefined;
165
+ }
166
+ function getResultStructMetadataAnnotation(field, resultMetadata) {
167
+ var _a, _b;
168
+ const tag = malloy_tag_1.Tag.withPrefix('#(malloy) ');
169
+ let hasAny = false;
170
+ if (resultMetadata.limit !== undefined) {
171
+ tag.set(['limit'], resultMetadata.limit);
172
+ hasAny = true;
173
+ }
174
+ if (resultMetadata.orderBy) {
175
+ for (let i = 0; i < resultMetadata.orderBy.length; i++) {
176
+ const orderBy = resultMetadata.orderBy[i];
177
+ const orderByField = typeof orderBy.field === 'number'
178
+ ? (_a = field.fields[orderBy.field].as) !== null && _a !== void 0 ? _a : field.fields[orderBy.field].name
179
+ : orderBy.field;
180
+ const direction = (_b = orderBy.dir) !== null && _b !== void 0 ? _b : null;
181
+ tag.set(['ordered_by', i, orderByField], direction);
182
+ }
183
+ hasAny = true;
184
+ }
185
+ return hasAny
186
+ ? {
187
+ value: tag.toString(),
188
+ }
189
+ : undefined;
190
+ }
89
191
  function typeDefToType(field) {
90
192
  if ((0, model_1.isLeafAtomic)(field)) {
91
193
  switch (field.type) {
@@ -102,11 +204,21 @@ function typeDefToType(field) {
102
204
  };
103
205
  case 'boolean':
104
206
  return { kind: 'boolean_type' };
105
- case 'date':
207
+ case 'date': {
208
+ // TODO there seems to be a bug where date literals with a timestamp truncation have
209
+ // type: date, but still have a timestamp truncation.
210
+ const timeframe = field.timeframe;
211
+ if (timeframe && !isDateTimeframe(timeframe)) {
212
+ return {
213
+ kind: 'timestamp_type',
214
+ timeframe: convertTimestampTimeframe(field.timeframe),
215
+ };
216
+ }
106
217
  return {
107
218
  kind: 'date_type',
108
219
  timeframe: convertDateTimeframe(field.timeframe),
109
220
  };
221
+ }
110
222
  case 'timestamp':
111
223
  return {
112
224
  kind: 'timestamp_type',
@@ -144,9 +256,25 @@ function convertRecordType(field) {
144
256
  return {
145
257
  kind: 'record_type',
146
258
  fields: field.fields.map(f => {
259
+ const annotations = [];
260
+ if ('resultMetadata' in f) {
261
+ if (f.resultMetadata) {
262
+ const ann = getResultMetadataAnnotation(f, f.resultMetadata);
263
+ if (ann) {
264
+ annotations.push(ann);
265
+ }
266
+ }
267
+ }
268
+ if (f.annotation) {
269
+ const taglines = (0, annotation_1.annotationToTaglines)(f.annotation);
270
+ annotations.push(...taglines.map(tagline => ({
271
+ value: tagline,
272
+ })));
273
+ }
147
274
  if ((0, model_1.isAtomic)(f)) {
148
275
  return {
149
276
  name: f.name,
277
+ annotations: annotations.length > 0 ? annotations : undefined,
150
278
  type: typeDefToType(f),
151
279
  };
152
280
  }
@@ -156,6 +284,18 @@ function convertRecordType(field) {
156
284
  }),
157
285
  };
158
286
  }
287
+ function isDateTimeframe(timeframe) {
288
+ switch (timeframe) {
289
+ case 'day':
290
+ case 'week':
291
+ case 'month':
292
+ case 'year':
293
+ case 'quarter':
294
+ return true;
295
+ default:
296
+ return false;
297
+ }
298
+ }
159
299
  function convertDateTimeframe(timeframe) {
160
300
  switch (timeframe) {
161
301
  case undefined:
@@ -167,7 +307,7 @@ function convertDateTimeframe(timeframe) {
167
307
  case 'quarter':
168
308
  return timeframe;
169
309
  default:
170
- throw new Error('Invalid date timeframe');
310
+ throw new Error(`Invalid date timeframe ${timeframe}`);
171
311
  }
172
312
  }
173
313
  function convertTimestampTimeframe(timeframe) {
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const MALLOY_VERSION = "0.0.239";
1
+ export declare const MALLOY_VERSION = "0.0.240";
package/dist/version.js CHANGED
@@ -2,5 +2,5 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MALLOY_VERSION = void 0;
4
4
  // generated with 'generate-version-file' script; do not edit manually
5
- exports.MALLOY_VERSION = '0.0.239';
5
+ exports.MALLOY_VERSION = '0.0.240';
6
6
  //# sourceMappingURL=version.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/malloy",
3
- "version": "0.0.239",
3
+ "version": "0.0.240-dev250305162504",
4
4
  "license": "MIT",
5
5
  "exports": {
6
6
  ".": "./dist/index.js",
@@ -41,8 +41,8 @@
41
41
  "generate-version-file": "VERSION=$(npm pkg get version --workspaces=false | tr -d \\\")\necho \"// generated with 'generate-version-file' script; do not edit manually\\nexport const MALLOY_VERSION = '$VERSION';\" > src/version.ts"
42
42
  },
43
43
  "dependencies": {
44
- "@malloydata/malloy-interfaces": "^0.0.239",
45
- "@malloydata/malloy-tag": "^0.0.239",
44
+ "@malloydata/malloy-interfaces": "^0.0.240-dev250305162504",
45
+ "@malloydata/malloy-tag": "^0.0.240-dev250305162504",
46
46
  "antlr4ts": "^0.5.0-alpha.4",
47
47
  "assert": "^2.0.0",
48
48
  "jest-diff": "^29.6.2",