@malloy-publisher/server 0.0.181 → 0.0.183-dev

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 (74) hide show
  1. package/build.ts +7 -3
  2. package/dist/app/api-doc.yaml +505 -52
  3. package/dist/app/assets/HomePage-Dn3E4CuB.js +1 -0
  4. package/dist/app/assets/{MainPage-B53xidTF.js → MainPage-BzB3yoqi.js} +2 -2
  5. package/dist/app/assets/{ModelPage-UMuQe8qY.js → ModelPage-C9O_sAXT.js} +1 -1
  6. package/dist/app/assets/PackagePage-DcxKEjBX.js +1 -0
  7. package/dist/app/assets/ProjectPage-BDj307rF.js +1 -0
  8. package/dist/app/assets/{RouteError-Cv58zNpb.js → RouteError-DAShbVCG.js} +1 -1
  9. package/dist/app/assets/{WorkbookPage-DZ1StqsX.js → WorkbookPage-Cs_XYEaB.js} +1 -1
  10. package/dist/app/assets/core-CjeTkq8O.es-BqRc6yhC.js +148 -0
  11. package/dist/app/assets/engine-oniguruma-C4vnmooL.es-jdkXmgTr.js +1 -0
  12. package/dist/app/assets/github-light-JYsPkUQd.es-DAi9KRSo.js +1 -0
  13. package/dist/app/assets/index-15BOvhp0.js +456 -0
  14. package/dist/app/assets/{index-DPThhVfX.js → index-Bb2jqquW.js} +1 -1
  15. package/dist/app/assets/{index-M3Zo817E.js → index-D68X76-7.js} +98 -98
  16. package/dist/app/assets/{index.umd-DnfBsVqO.js → index.umd-DGBekgSu.js} +1 -1
  17. package/dist/app/assets/json-71t8ZF9g.es-BQoSv7ci.js +1 -0
  18. package/dist/app/assets/sql-DCkt643-.es-COK4E0Yg.js +1 -0
  19. package/dist/app/assets/typescript-buWNZFwO.es-Dj6nwHGl.js +1 -0
  20. package/dist/app/index.html +1 -1
  21. package/dist/{instrumentation.js → instrumentation.mjs} +10567 -10584
  22. package/dist/{server.js → server.mjs} +16959 -15357
  23. package/package.json +19 -17
  24. package/src/controller/connection.controller.ts +27 -20
  25. package/src/controller/manifest.controller.ts +29 -0
  26. package/src/controller/materialization.controller.ts +125 -0
  27. package/src/controller/model.controller.ts +4 -3
  28. package/src/controller/package.controller.ts +53 -2
  29. package/src/controller/query.controller.ts +5 -0
  30. package/src/errors.ts +24 -0
  31. package/src/mcp/prompts/handlers.ts +1 -1
  32. package/src/mcp/resources/model_resource.ts +12 -9
  33. package/src/mcp/resources/source_resource.ts +7 -6
  34. package/src/mcp/resources/view_resource.ts +0 -1
  35. package/src/mcp/tools/execute_query_tool.ts +9 -0
  36. package/src/server.ts +223 -15
  37. package/src/service/connection.ts +1 -4
  38. package/src/service/filter.spec.ts +447 -0
  39. package/src/service/filter.ts +337 -0
  40. package/src/service/filter_integration.spec.ts +825 -0
  41. package/src/service/manifest_service.spec.ts +201 -0
  42. package/src/service/manifest_service.ts +106 -0
  43. package/src/service/materialization_service.spec.ts +648 -0
  44. package/src/service/materialization_service.ts +929 -0
  45. package/src/service/materialized_table_gc.spec.ts +383 -0
  46. package/src/service/materialized_table_gc.ts +279 -0
  47. package/src/service/model.ts +227 -49
  48. package/src/service/package.ts +50 -0
  49. package/src/service/project_store.ts +21 -2
  50. package/src/service/quoting.ts +41 -0
  51. package/src/service/resolve_project.ts +13 -0
  52. package/src/storage/DatabaseInterface.ts +103 -1
  53. package/src/storage/{StorageManager.spec.ts → StorageManager.mock.ts} +9 -0
  54. package/src/storage/StorageManager.ts +119 -1
  55. package/src/storage/duckdb/DuckDBConnection.ts +1 -1
  56. package/src/storage/duckdb/DuckDBManifestStore.ts +70 -0
  57. package/src/storage/duckdb/DuckDBRepository.ts +99 -9
  58. package/src/storage/duckdb/ManifestRepository.ts +119 -0
  59. package/src/storage/duckdb/MaterializationRepository.ts +249 -0
  60. package/src/storage/duckdb/manifest_store.spec.ts +133 -0
  61. package/src/storage/duckdb/schema.ts +59 -1
  62. package/src/storage/ducklake/DuckLakeManifestStore.ts +146 -0
  63. package/tests/fixtures/persist-test/data/orders.csv +5 -0
  64. package/tests/fixtures/persist-test/persist_test.malloy +11 -0
  65. package/tests/fixtures/persist-test/publisher.json +5 -0
  66. package/tests/fixtures/publisher.config.json +15 -0
  67. package/tests/harness/rest_e2e.ts +68 -0
  68. package/tests/integration/materialization/materialization_lifecycle.integration.spec.ts +470 -0
  69. package/tests/integration/mcp/mcp_execute_query_tool.integration.spec.ts +2 -2
  70. package/tsconfig.json +1 -1
  71. package/dist/app/assets/HomePage-B0C6gwGj.js +0 -1
  72. package/dist/app/assets/PackagePage-BEDvm_je.js +0 -1
  73. package/dist/app/assets/ProjectPage-DzN4P86H.js +0 -1
  74. package/dist/app/assets/index-D-xPyBUA.js +0 -467
@@ -0,0 +1,337 @@
1
+ /**
2
+ * Filter annotation parsing and query filter injection.
3
+ *
4
+ * Annotation format on a Malloy source:
5
+ * #(filter) [name=NAME] dimension=DIMENSION_NAME type=[equal|in|like|greater_than|less_than] [implicit] [required]
6
+ *
7
+ * At query time, Publisher injects `+ {where: <clause>}` into the Malloy query
8
+ * based on the provided filter values and the declared filter definitions.
9
+ */
10
+
11
+ // ---------------------------------------------------------------------------
12
+ // Types
13
+ // ---------------------------------------------------------------------------
14
+
15
+ export type FilterType = "equal" | "in" | "like" | "greater_than" | "less_than";
16
+
17
+ const VALID_FILTER_TYPES = new Set<FilterType>([
18
+ "equal",
19
+ "in",
20
+ "like",
21
+ "greater_than",
22
+ "less_than",
23
+ ]);
24
+
25
+ export interface FilterDefinition {
26
+ /** Display name for the filter. Defaults to the dimension name. */
27
+ name: string;
28
+ /** The source dimension this filter targets. */
29
+ dimension: string;
30
+ /** Comparator type. */
31
+ type: FilterType;
32
+ /** Hidden from user/agent summaries; set by infrastructure (e.g. row-level security). */
33
+ implicit: boolean;
34
+ /** Must be provided for every query (or error). */
35
+ required: boolean;
36
+ }
37
+
38
+ /**
39
+ * Filter values provided at query time.
40
+ * Keys are filter names, values are the filter input(s).
41
+ */
42
+ export type FilterParams = Record<string, string | string[]>;
43
+
44
+ // ---------------------------------------------------------------------------
45
+ // Annotation Parsing
46
+ // ---------------------------------------------------------------------------
47
+
48
+ const ANNOTATION_PREFIX = "#(filter)";
49
+
50
+ /**
51
+ * Parse a single `#(filter)` annotation string into a definition.
52
+ * Returns `null` if the string is not a filter annotation.
53
+ * Throws on malformed annotations (missing required fields, bad type).
54
+ */
55
+ export function parseFilterAnnotation(
56
+ annotation: string,
57
+ ): FilterDefinition | null {
58
+ const trimmed = annotation.trim();
59
+ if (!trimmed.startsWith(ANNOTATION_PREFIX)) {
60
+ return null;
61
+ }
62
+
63
+ const body = trimmed.slice(ANNOTATION_PREFIX.length).trim();
64
+ const tokens = tokenize(body);
65
+
66
+ let name: string | undefined;
67
+ let dimension: string | undefined;
68
+ let type: FilterType | undefined;
69
+ let implicit = false;
70
+ let required = false;
71
+
72
+ for (const token of tokens) {
73
+ if (token.includes("=")) {
74
+ const eqIndex = token.indexOf("=");
75
+ const key = token.slice(0, eqIndex).toLowerCase();
76
+ const value = token.slice(eqIndex + 1);
77
+ switch (key) {
78
+ case "name":
79
+ name = value;
80
+ break;
81
+ case "dimension":
82
+ dimension = value;
83
+ break;
84
+ case "type":
85
+ if (!VALID_FILTER_TYPES.has(value as FilterType)) {
86
+ throw new Error(
87
+ `Invalid filter type "${value}". Must be one of: ${[...VALID_FILTER_TYPES].join(", ")}`,
88
+ );
89
+ }
90
+ type = value as FilterType;
91
+ break;
92
+ default:
93
+ throw new Error(`Unknown filter parameter "${key}"`);
94
+ }
95
+ } else {
96
+ const flag = token.toLowerCase();
97
+ if (flag === "implicit") {
98
+ implicit = true;
99
+ } else if (flag === "required") {
100
+ required = true;
101
+ } else {
102
+ throw new Error(`Unknown filter flag "${token}"`);
103
+ }
104
+ }
105
+ }
106
+
107
+ if (!dimension) {
108
+ throw new Error(
109
+ "filter annotation missing required 'dimension' parameter",
110
+ );
111
+ }
112
+ if (!type) {
113
+ throw new Error("filter annotation missing required 'type' parameter");
114
+ }
115
+
116
+ return {
117
+ name: name ?? dimension,
118
+ dimension,
119
+ type,
120
+ implicit,
121
+ required,
122
+ };
123
+ }
124
+
125
+ /**
126
+ * Extract all `#(filter)` definitions from a list of annotation strings
127
+ * (as found on a Malloy source's `blockNotes`).
128
+ */
129
+ export function parseFilters(annotations: string[]): FilterDefinition[] {
130
+ // Use a Map keyed by filter name so that later annotations (from an
131
+ // extending source) override earlier ones (from the base source).
132
+ // This is important when `source: child is parent extend {}` inherits
133
+ // blockNotes from the parent — the child's annotations come last and
134
+ // should win.
135
+ const byName = new Map<string, FilterDefinition>();
136
+ for (const annotation of annotations) {
137
+ const parsed = parseFilterAnnotation(annotation);
138
+ if (parsed) {
139
+ byName.set(parsed.name, parsed);
140
+ }
141
+ }
142
+ return [...byName.values()];
143
+ }
144
+
145
+ // ---------------------------------------------------------------------------
146
+ // Filter Clause Generation
147
+ // ---------------------------------------------------------------------------
148
+
149
+ /**
150
+ * Escape a string value for embedding in a Malloy query literal.
151
+ */
152
+ function escapeMalloyString(value: string): string {
153
+ return value.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
154
+ }
155
+
156
+ /**
157
+ * Returns true if the string is a bare boolean literal.
158
+ */
159
+ function isBooleanLiteral(v: string): boolean {
160
+ const lower = v.toLowerCase();
161
+ return lower === "true" || lower === "false";
162
+ }
163
+
164
+ const ISO_DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
165
+ const ISO_TIMESTAMP_RE = /^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}/;
166
+
167
+ /**
168
+ * Returns true if the string looks like an ISO date or timestamp.
169
+ */
170
+ function isDateLiteral(v: string): boolean {
171
+ return ISO_DATE_RE.test(v) || ISO_TIMESTAMP_RE.test(v);
172
+ }
173
+
174
+ /**
175
+ * Format a scalar value for Malloy.
176
+ * - Boolean literals → unquoted true/false
177
+ * - Date/timestamp strings → Malloy temporal literal @YYYY-MM-DD
178
+ * - Everything else → single-quoted string
179
+ */
180
+ function malloyLiteral(v: string): string {
181
+ if (isBooleanLiteral(v)) {
182
+ return v.toLowerCase();
183
+ }
184
+ if (isDateLiteral(v)) {
185
+ return `@${v.slice(0, 10)}`;
186
+ }
187
+ return `'${escapeMalloyString(v)}'`;
188
+ }
189
+
190
+ /**
191
+ * Build a single Malloy predicate expression for one filter.
192
+ */
193
+ function buildPredicate(
194
+ filter: FilterDefinition,
195
+ value: string | string[],
196
+ ): string {
197
+ const dim = `\`${filter.dimension}\``;
198
+
199
+ switch (filter.type) {
200
+ case "equal": {
201
+ const v = Array.isArray(value) ? value[0] : value;
202
+ return `${dim} = ${malloyLiteral(v)}`;
203
+ }
204
+ case "in": {
205
+ const values = Array.isArray(value) ? value : [value];
206
+ if (values.length === 1) {
207
+ return `${dim} = ${malloyLiteral(values[0])}`;
208
+ }
209
+ const conditions = values.map((v) => `${dim} = ${malloyLiteral(v)}`);
210
+ return `(${conditions.join(" or ")})`;
211
+ }
212
+ case "like": {
213
+ const v = Array.isArray(value) ? value[0] : value;
214
+ const escaped = escapeMalloyString(v.toLowerCase());
215
+ const pattern =
216
+ escaped.startsWith("%") || escaped.endsWith("%")
217
+ ? escaped
218
+ : `%${escaped}%`;
219
+ return `lower(${dim}) ~ '${pattern}'`;
220
+ }
221
+ case "greater_than": {
222
+ const v = Array.isArray(value) ? value[0] : value;
223
+ return `${dim} > ${malloyLiteral(v)}`;
224
+ }
225
+ case "less_than": {
226
+ const v = Array.isArray(value) ? value[0] : value;
227
+ return `${dim} < ${malloyLiteral(v)}`;
228
+ }
229
+ }
230
+ }
231
+
232
+ /**
233
+ * Build a complete Malloy `where:` clause fragment from filter definitions
234
+ * and provided parameter values.
235
+ *
236
+ * Returns an empty string when no filters apply.
237
+ * Throws if a required filter has no value.
238
+ */
239
+ export function buildFilterClause(
240
+ filters: FilterDefinition[],
241
+ params: FilterParams,
242
+ ): string {
243
+ const predicates: string[] = [];
244
+
245
+ for (const filter of filters) {
246
+ const value = params[filter.name];
247
+ const hasValue =
248
+ value !== undefined &&
249
+ value !== null &&
250
+ (Array.isArray(value) ? value.length > 0 : value !== "");
251
+
252
+ if (!hasValue) {
253
+ if (filter.required) {
254
+ throw new FilterValidationError(
255
+ `Required filter "${filter.name}" (dimension: ${filter.dimension}) was not provided`,
256
+ );
257
+ }
258
+ continue;
259
+ }
260
+
261
+ predicates.push(buildPredicate(filter, value));
262
+ }
263
+
264
+ if (predicates.length === 0) {
265
+ return "";
266
+ }
267
+
268
+ return predicates.join(" and ");
269
+ }
270
+
271
+ /**
272
+ * Append a filter refinement to a Malloy query string.
273
+ * Uses Malloy's `+ {where: ...}` refinement syntax.
274
+ *
275
+ * If `filterClause` is empty, returns the original query unchanged.
276
+ */
277
+ export function injectFilterRefinement(
278
+ query: string,
279
+ filterClause: string,
280
+ ): string {
281
+ if (!filterClause) {
282
+ return query;
283
+ }
284
+ return `${query.trimEnd()} + {where: ${filterClause}}`;
285
+ }
286
+
287
+ // ---------------------------------------------------------------------------
288
+ // Validation Error
289
+ // ---------------------------------------------------------------------------
290
+
291
+ export class FilterValidationError extends Error {
292
+ constructor(message: string) {
293
+ super(message);
294
+ this.name = "FilterValidationError";
295
+ }
296
+ }
297
+
298
+ // ---------------------------------------------------------------------------
299
+ // Helpers
300
+ // ---------------------------------------------------------------------------
301
+
302
+ /**
303
+ * Simple tokenizer that splits on whitespace but respects quoted values.
304
+ * Handles: `name="Foo Bar" dimension=status type=equal implicit`
305
+ */
306
+ function tokenize(input: string): string[] {
307
+ const tokens: string[] = [];
308
+ let current = "";
309
+ let inQuote = false;
310
+ let quoteChar = "";
311
+
312
+ for (const ch of input) {
313
+ if (inQuote) {
314
+ if (ch === quoteChar) {
315
+ inQuote = false;
316
+ } else {
317
+ current += ch;
318
+ }
319
+ } else if (ch === '"' || ch === "'") {
320
+ inQuote = true;
321
+ quoteChar = ch;
322
+ } else if (ch === " " || ch === "\t") {
323
+ if (current) {
324
+ tokens.push(current);
325
+ current = "";
326
+ }
327
+ } else {
328
+ current += ch;
329
+ }
330
+ }
331
+
332
+ if (current) {
333
+ tokens.push(current);
334
+ }
335
+
336
+ return tokens;
337
+ }