@malloydata/malloy 0.0.239-dev250226010331 → 0.0.239-dev250227145856

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.
Files changed (41) hide show
  1. package/dist/api/asynchronous.d.ts +13 -0
  2. package/dist/api/asynchronous.js +205 -0
  3. package/dist/api/asynchronous.spec.d.ts +1 -0
  4. package/dist/api/asynchronous.spec.js +238 -0
  5. package/dist/api/connection.d.ts +12 -0
  6. package/dist/api/connection.js +9 -0
  7. package/dist/api/core.d.ts +37 -0
  8. package/dist/api/core.js +448 -0
  9. package/dist/api/index.d.ts +3 -0
  10. package/dist/api/index.js +36 -0
  11. package/dist/api/sessioned.d.ts +10 -0
  12. package/dist/api/sessioned.js +210 -0
  13. package/dist/api/sessioned.spec.d.ts +1 -0
  14. package/dist/api/sessioned.spec.js +417 -0
  15. package/dist/api/stateless.d.ts +4 -0
  16. package/dist/api/stateless.js +46 -0
  17. package/dist/api/stateless.spec.d.ts +1 -0
  18. package/dist/api/stateless.spec.js +394 -0
  19. package/dist/connection/base_connection.d.ts +3 -2
  20. package/dist/connection/base_connection.js +3 -1
  21. package/dist/connection/types.d.ts +2 -1
  22. package/dist/index.d.ts +5 -2
  23. package/dist/index.js +28 -1
  24. package/dist/lang/ast/source-elements/sql-source.d.ts +4 -4
  25. package/dist/lang/ast/source-elements/sql-source.js +20 -12
  26. package/dist/lang/malloy-to-ast.d.ts +1 -1
  27. package/dist/lang/parse-malloy.js +0 -1
  28. package/dist/lang/test/locations.spec.js +1 -1
  29. package/dist/lang/test/parse-expects.js +1 -1
  30. package/dist/lang/test/parse.spec.js +18 -15
  31. package/dist/lang/test/sql-block.spec.js +18 -21
  32. package/dist/lang/test/test-translator.d.ts +5 -3
  33. package/dist/lang/test/test-translator.js +10 -10
  34. package/dist/lang/translate-response.d.ts +6 -3
  35. package/dist/malloy.d.ts +2 -4
  36. package/dist/malloy.js +11 -45
  37. package/dist/model/malloy_types.d.ts +0 -5
  38. package/dist/model/sql_block.d.ts +4 -7
  39. package/dist/model/sql_block.js +31 -21
  40. package/dist/to_stable.js +9 -0
  41. package/package.json +3 -3
@@ -0,0 +1,448 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
9
+ if (k2 === undefined) k2 = k;
10
+ var desc = Object.getOwnPropertyDescriptor(m, k);
11
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
12
+ desc = { enumerable: true, get: function() { return m[k]; } };
13
+ }
14
+ Object.defineProperty(o, k2, desc);
15
+ }) : (function(o, m, k, k2) {
16
+ if (k2 === undefined) k2 = k;
17
+ o[k2] = m[k];
18
+ }));
19
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
20
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
21
+ }) : function(o, v) {
22
+ o["default"] = v;
23
+ });
24
+ var __importStar = (this && this.__importStar) || function (mod) {
25
+ if (mod && mod.__esModule) return mod;
26
+ var result = {};
27
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
28
+ __setModuleDefault(result, mod);
29
+ return result;
30
+ };
31
+ Object.defineProperty(exports, "__esModule", { value: true });
32
+ exports.statedCompileQuery = exports.newCompileQueryState = exports.compileSource = exports.compileModel = exports.DEFAULT_LOG_RANGE = exports._statedCompileModel = exports.statedCompileSource = exports.statedCompileModel = exports.newCompileSourceState = exports.newCompileModelState = exports.updateCompileModelState = exports.compileQuery = void 0;
33
+ const Malloy = __importStar(require("@malloydata/malloy-interfaces"));
34
+ const lang_1 = require("../lang");
35
+ const model_1 = require("../model");
36
+ const to_stable_1 = require("../to_stable");
37
+ const sql_block_1 = require("../model/sql_block");
38
+ // TODO find where this should go...
39
+ function tableKey(connectionName, tablePath) {
40
+ return `${connectionName}:${tablePath}`;
41
+ }
42
+ function makeSQLSourceDef(sql, dialect) {
43
+ return {
44
+ type: 'sql_select',
45
+ selectStr: sql.sql,
46
+ connection: sql.connection_name,
47
+ dialect: dialect,
48
+ fields: sql.schema ? getSchemaFields(sql.schema) : [],
49
+ name: (0, sql_block_1.sqlKey)(sql.connection_name, sql.sql),
50
+ };
51
+ }
52
+ function makeTableSourceDef(table, dialect) {
53
+ return {
54
+ type: 'table',
55
+ tablePath: table.name,
56
+ connection: table.connection_name,
57
+ dialect: dialect,
58
+ fields: table.schema ? getSchemaFields(table.schema) : [],
59
+ name: tableKey(table.connection_name, table.name),
60
+ };
61
+ }
62
+ function convertNumberSubtype(subtype) {
63
+ if (subtype === undefined)
64
+ return undefined;
65
+ if (subtype === 'decimal')
66
+ return 'float';
67
+ return 'integer';
68
+ }
69
+ function typeDefFromField(type) {
70
+ switch (type.kind) {
71
+ case 'string_type':
72
+ return { type: 'string' };
73
+ case 'number_type':
74
+ return { type: 'number', numberType: convertNumberSubtype(type.subtype) };
75
+ case 'boolean_type':
76
+ return { type: 'boolean' };
77
+ case 'timestamp_type':
78
+ return { type: 'timestamp', timeframe: type.timeframe };
79
+ case 'date_type':
80
+ return { type: 'date', timeframe: type.timeframe };
81
+ case 'sql_native_type':
82
+ return { type: 'sql native', rawType: type.sql_type };
83
+ case 'json_type':
84
+ return { type: 'json' };
85
+ case 'array_type': {
86
+ if (type.element_type.kind === 'record_type') {
87
+ return {
88
+ type: 'array',
89
+ elementTypeDef: { type: 'record_element' },
90
+ fields: type.element_type.fields.map(convertDimension),
91
+ };
92
+ }
93
+ else {
94
+ const elementTypeDef = typeDefFromField(type.element_type);
95
+ if (elementTypeDef.type === 'record') {
96
+ throw new Error('Arrays of records should be a repeated record type');
97
+ }
98
+ return {
99
+ type: 'array',
100
+ elementTypeDef,
101
+ };
102
+ }
103
+ }
104
+ case 'record_type':
105
+ return { type: 'record', fields: type.fields.map(convertDimension) };
106
+ }
107
+ }
108
+ function convertDimension(field) {
109
+ const typeDef = typeDefFromField(field.type);
110
+ return (0, model_1.mkFieldDef)(typeDef, field.name);
111
+ }
112
+ function convertTableField(field) {
113
+ if (field.kind !== 'dimension') {
114
+ throw new Error('Table schemas must only have dimension fields');
115
+ }
116
+ return convertDimension(field);
117
+ }
118
+ function getSchemaFields(schema) {
119
+ const fields = [];
120
+ for (const field of schema.fields) {
121
+ fields.push(convertTableField(field));
122
+ }
123
+ return fields;
124
+ }
125
+ function compilerNeedsToUpdate(compilerNeeds) {
126
+ var _a, _b, _c, _d, _e, _f;
127
+ const update = {
128
+ urls: {},
129
+ tables: {},
130
+ compileSQL: {},
131
+ translations: {},
132
+ };
133
+ if (compilerNeeds) {
134
+ for (const file of (_a = compilerNeeds.files) !== null && _a !== void 0 ? _a : []) {
135
+ if (file.contents !== undefined) {
136
+ update.urls[file.url] = file.contents;
137
+ }
138
+ }
139
+ for (const table of (_b = compilerNeeds.table_schemas) !== null && _b !== void 0 ? _b : []) {
140
+ const connection = (_c = compilerNeeds.connections) === null || _c === void 0 ? void 0 : _c.find(c => c.name === table.connection_name);
141
+ if (connection && table.schema && connection.dialect) {
142
+ update.tables[tableKey(table.connection_name, table.name)] =
143
+ makeTableSourceDef(table, connection.dialect);
144
+ }
145
+ }
146
+ for (const sql of (_d = compilerNeeds.sql_schemas) !== null && _d !== void 0 ? _d : []) {
147
+ const connection = (_e = compilerNeeds.connections) === null || _e === void 0 ? void 0 : _e.find(c => c.name === sql.connection_name);
148
+ if (connection && connection.dialect) {
149
+ update.compileSQL[(0, sql_block_1.sqlKey)(sql.connection_name, sql.sql)] =
150
+ makeSQLSourceDef(sql, connection.dialect);
151
+ }
152
+ }
153
+ for (const translation of (_f = compilerNeeds.translations) !== null && _f !== void 0 ? _f : []) {
154
+ if (translation.compiled_model_json) {
155
+ const modelDef = JSON.parse(translation.compiled_model_json);
156
+ update.translations[translation.url] = modelDef;
157
+ }
158
+ }
159
+ }
160
+ return update;
161
+ }
162
+ function convertCompilerNeeds(compileSQL, urls, tables) {
163
+ var _a, _b, _c;
164
+ const compilerNeeds = {};
165
+ const neededConnections = new Set();
166
+ if (compileSQL !== undefined) {
167
+ compilerNeeds.sql_schemas = [
168
+ {
169
+ sql: compileSQL.selectStr,
170
+ connection_name: compileSQL.connection,
171
+ },
172
+ ];
173
+ neededConnections.add(compileSQL.connection);
174
+ }
175
+ if (urls !== undefined) {
176
+ for (const url of urls) {
177
+ (_a = compilerNeeds.files) !== null && _a !== void 0 ? _a : (compilerNeeds.files = []);
178
+ compilerNeeds.files.push({ url });
179
+ }
180
+ }
181
+ if (tables !== undefined) {
182
+ for (const key in tables) {
183
+ const table = tables[key];
184
+ // TODO do we even support default connections any more?
185
+ const connectionName = (_b = table.connectionName) !== null && _b !== void 0 ? _b : '__default__';
186
+ (_c = compilerNeeds.table_schemas) !== null && _c !== void 0 ? _c : (compilerNeeds.table_schemas = []);
187
+ compilerNeeds.table_schemas.push({
188
+ name: table.tablePath,
189
+ connection_name: connectionName,
190
+ });
191
+ neededConnections.add(connectionName);
192
+ }
193
+ }
194
+ if (neededConnections.size > 0) {
195
+ compilerNeeds.connections = Array.from(neededConnections).map(c => ({
196
+ name: c,
197
+ }));
198
+ }
199
+ return compilerNeeds;
200
+ }
201
+ function compileQuery(request, state) {
202
+ state !== null && state !== void 0 ? state : (state = newCompileQueryState(request));
203
+ return statedCompileQuery(state);
204
+ }
205
+ exports.compileQuery = compileQuery;
206
+ function updateCompileModelState(state, needs) {
207
+ function performUpdate(state, update) {
208
+ var _a, _b;
209
+ state.translator.update(update);
210
+ if (state.extending) {
211
+ performUpdate(state.extending, update);
212
+ }
213
+ if (!state.hasSource) {
214
+ state.hasSource =
215
+ (_b = (_a = needs === null || needs === void 0 ? void 0 : needs.files) === null || _a === void 0 ? void 0 : _a.some(f => f.url === state.translator.sourceURL)) !== null && _b !== void 0 ? _b : false;
216
+ }
217
+ }
218
+ const update = compilerNeedsToUpdate(needs);
219
+ performUpdate(state, update);
220
+ }
221
+ exports.updateCompileModelState = updateCompileModelState;
222
+ function _newCompileModelState(modelURL, compilerNeeds, extendURL) {
223
+ var _a, _b;
224
+ const translator = new lang_1.MalloyTranslator(modelURL, null, compilerNeedsToUpdate(compilerNeeds));
225
+ const hasSource = (_b = (_a = compilerNeeds === null || compilerNeeds === void 0 ? void 0 : compilerNeeds.files) === null || _a === void 0 ? void 0 : _a.some(f => f.url === modelURL)) !== null && _b !== void 0 ? _b : false;
226
+ if (extendURL) {
227
+ return {
228
+ extending: _newCompileModelState(extendURL, compilerNeeds),
229
+ translator,
230
+ done: false,
231
+ hasSource,
232
+ };
233
+ }
234
+ else {
235
+ return {
236
+ translator,
237
+ done: false,
238
+ hasSource,
239
+ };
240
+ }
241
+ }
242
+ function newCompileModelState(request) {
243
+ return _newCompileModelState(request.model_url, request.compiler_needs, request.extend_model_url);
244
+ }
245
+ exports.newCompileModelState = newCompileModelState;
246
+ function newCompileSourceState(request) {
247
+ return _newCompileModelState(request.model_url, request.compiler_needs, request.extend_model_url);
248
+ }
249
+ exports.newCompileSourceState = newCompileSourceState;
250
+ // function hasNeeds(needs: Malloy.CompilerNeeds | undefined): boolean {
251
+ // if (needs === undefined) return false;
252
+ // if (needs.files && needs.files.length > 0) return true;
253
+ // if (needs.table_schemas && needs.table_schemas.length > 0) return true;
254
+ // if (needs.sql_schemas && needs.sql_schemas.length > 0) return true;
255
+ // if (needs.connections && needs.connections.length > 0) return true;
256
+ // return false;
257
+ // }
258
+ function statedCompileModel(state) {
259
+ return wrapResponse(_statedCompileModel(state), state.translator.sourceURL);
260
+ }
261
+ exports.statedCompileModel = statedCompileModel;
262
+ function statedCompileSource(state, name) {
263
+ return extractSource(_statedCompileModel(state), name, state.translator.sourceURL);
264
+ }
265
+ exports.statedCompileSource = statedCompileSource;
266
+ function _statedCompileModel(state) {
267
+ let extendingModel = undefined;
268
+ if (state.extending) {
269
+ if (!state.extending.done) {
270
+ const extendingResult = _statedCompileModel(state.extending);
271
+ if (!state.extending.done) {
272
+ return extendingResult;
273
+ }
274
+ }
275
+ extendingModel = state.extending.translator.modelDef;
276
+ }
277
+ if (!state.hasSource) {
278
+ return {
279
+ compilerNeeds: convertCompilerNeeds(undefined, [state.translator.sourceURL], undefined),
280
+ };
281
+ }
282
+ const result = state.translator.translate(extendingModel);
283
+ if (result.final) {
284
+ state.done = true;
285
+ if (result.modelDef) {
286
+ return {
287
+ model: (0, to_stable_1.modelDefToModelInfo)(result.modelDef),
288
+ modelDef: result.modelDef,
289
+ };
290
+ }
291
+ else {
292
+ if (result.problems === undefined || result.problems.length === 0) {
293
+ throw new Error('No problems found, but no model either');
294
+ }
295
+ return {
296
+ logs: result.problems,
297
+ };
298
+ }
299
+ }
300
+ else {
301
+ const compilerNeeds = convertCompilerNeeds(result.compileSQL, result.urls, result.tables);
302
+ return { compilerNeeds, logs: result.problems };
303
+ }
304
+ }
305
+ exports._statedCompileModel = _statedCompileModel;
306
+ exports.DEFAULT_LOG_RANGE = {
307
+ start: {
308
+ line: 0,
309
+ character: 0,
310
+ },
311
+ end: {
312
+ line: 0,
313
+ character: 0,
314
+ },
315
+ };
316
+ function mapLogs(logs, defaultURL) {
317
+ return logs.map(log => {
318
+ var _a, _b, _c, _d;
319
+ return ({
320
+ severity: log.severity,
321
+ message: log.message,
322
+ range: (_b = (_a = log.at) === null || _a === void 0 ? void 0 : _a.range) !== null && _b !== void 0 ? _b : exports.DEFAULT_LOG_RANGE,
323
+ url: (_d = (_c = log.at) === null || _c === void 0 ? void 0 : _c.url) !== null && _d !== void 0 ? _d : defaultURL,
324
+ });
325
+ });
326
+ }
327
+ function wrapResponse(response, defaultURL) {
328
+ const logs = response.logs ? mapLogs(response.logs, defaultURL) : undefined;
329
+ if (response.compilerNeeds) {
330
+ return { compiler_needs: response.compilerNeeds, logs };
331
+ }
332
+ else {
333
+ return { model: response.model, logs };
334
+ }
335
+ }
336
+ function _compileModel(modelURL, compilerNeeds, extendURL, state) {
337
+ state !== null && state !== void 0 ? state : (state = _newCompileModelState(modelURL, compilerNeeds, extendURL));
338
+ return _statedCompileModel(state);
339
+ }
340
+ function compileModel(request, state) {
341
+ state !== null && state !== void 0 ? state : (state = newCompileModelState(request));
342
+ return statedCompileModel(state);
343
+ }
344
+ exports.compileModel = compileModel;
345
+ function compileSource(request) {
346
+ const state = newCompileSourceState(request);
347
+ return statedCompileSource(state, request.name);
348
+ }
349
+ exports.compileSource = compileSource;
350
+ // Given the URL to a model and a name of a queryable thing, get a StableSourceDef
351
+ function extractSource(result, name, defaultURL) {
352
+ const logs = result.logs ? mapLogs(result.logs, defaultURL) : undefined;
353
+ if (result.model) {
354
+ const source = result.model.entries.find(e => e.name === name);
355
+ if (source === undefined) {
356
+ return {
357
+ logs: [
358
+ ...(logs !== null && logs !== void 0 ? logs : []),
359
+ {
360
+ url: defaultURL,
361
+ severity: 'error',
362
+ message: `Model does not contain a source named ${name}`,
363
+ range: exports.DEFAULT_LOG_RANGE,
364
+ },
365
+ ],
366
+ };
367
+ }
368
+ return { source, logs };
369
+ }
370
+ else {
371
+ return { compiler_needs: result.compilerNeeds, logs };
372
+ }
373
+ }
374
+ // Given a StableQueryDef and the URL to a model, run it and return a StableResult
375
+ // Given a StableQueryDef and the URL to a model, compile it and return a StableResultDef
376
+ // Given a StableQueryDef and the URL to a model, validate it and return a list of StableErrors
377
+ // Given a URL to a model and the name of a source, run the indexing query
378
+ function newCompileQueryState(request) {
379
+ var _a, _b;
380
+ const queryMalloy = Malloy.queryToMalloy(request.query);
381
+ const needs = {
382
+ ...((_a = request.compiler_needs) !== null && _a !== void 0 ? _a : {}),
383
+ };
384
+ const queryURL = 'internal://query.malloy';
385
+ needs.files = [
386
+ {
387
+ url: queryURL,
388
+ contents: queryMalloy,
389
+ },
390
+ ...((_b = needs.files) !== null && _b !== void 0 ? _b : []),
391
+ ];
392
+ return _newCompileModelState(queryURL, needs, request.model_url);
393
+ }
394
+ exports.newCompileQueryState = newCompileQueryState;
395
+ function statedCompileQuery(state) {
396
+ const result = _statedCompileModel(state);
397
+ // TODO this can expose the internal URL... is there a better way to handle URL-less errors from the compiler?
398
+ const defaultURL = state.translator.sourceURL;
399
+ const logs = result.logs ? mapLogs(result.logs, defaultURL) : undefined;
400
+ if (result.model) {
401
+ const queries = result.modelDef.queryList;
402
+ if (queries.length === 0) {
403
+ return {
404
+ logs: [
405
+ ...(logs !== null && logs !== void 0 ? logs : []),
406
+ {
407
+ url: defaultURL,
408
+ severity: 'error',
409
+ message: 'Internal error: No queries found',
410
+ range: exports.DEFAULT_LOG_RANGE,
411
+ },
412
+ ],
413
+ };
414
+ }
415
+ const index = queries.length - 1;
416
+ const query = result.modelDef.queryList[index];
417
+ const schema = result.model.anonymous_queries[index].schema;
418
+ try {
419
+ const queryModel = new model_1.QueryModel(result.modelDef);
420
+ const translatedQuery = queryModel.compileQuery(query);
421
+ return {
422
+ result: {
423
+ sql: translatedQuery.sql,
424
+ schema,
425
+ connection_name: translatedQuery.connectionName,
426
+ },
427
+ };
428
+ }
429
+ catch (error) {
430
+ return {
431
+ logs: [
432
+ ...(logs !== null && logs !== void 0 ? logs : []),
433
+ {
434
+ url: defaultURL,
435
+ severity: 'error',
436
+ message: `Internal compiler error: ${error.message}`,
437
+ range: exports.DEFAULT_LOG_RANGE,
438
+ },
439
+ ],
440
+ };
441
+ }
442
+ }
443
+ else {
444
+ return { compiler_needs: result.compilerNeeds, logs };
445
+ }
446
+ }
447
+ exports.statedCompileQuery = statedCompileQuery;
448
+ //# sourceMappingURL=core.js.map
@@ -0,0 +1,3 @@
1
+ export * as sessioned from './sessioned';
2
+ export * as stateless from './stateless';
3
+ export * as asynchronous from './asynchronous';
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.asynchronous = exports.stateless = exports.sessioned = void 0;
27
+ /*
28
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
29
+ *
30
+ * This source code is licensed under the MIT license found in the
31
+ * LICENSE file in the root directory of this source tree.
32
+ */
33
+ exports.sessioned = __importStar(require("./sessioned"));
34
+ exports.stateless = __importStar(require("./stateless"));
35
+ exports.asynchronous = __importStar(require("./asynchronous"));
36
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,10 @@
1
+ import * as Malloy from '@malloydata/malloy-interfaces';
2
+ export type TTL = {
3
+ 'seconds': number;
4
+ } | Date;
5
+ export interface OptionsBase {
6
+ ttl?: TTL;
7
+ }
8
+ export declare function compileModel(request: Malloy.CompileModelRequest, options?: OptionsBase): Malloy.CompileModelResponse;
9
+ export declare function compileSource(request: Malloy.CompileSourceRequest, options?: OptionsBase): Malloy.CompileSourceResponse;
10
+ export declare function compileQuery(request: Malloy.CompileQueryRequest, options?: OptionsBase): Malloy.CompileQueryResponse;