@milaboratories/pl-middle-layer 2.10.0 → 2.11.0

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 (181) hide show
  1. package/dist/block_registry/registry-v2-provider.cjs +8 -1
  2. package/dist/block_registry/registry-v2-provider.cjs.map +1 -1
  3. package/dist/block_registry/registry-v2-provider.d.ts.map +1 -1
  4. package/dist/block_registry/registry-v2-provider.js +8 -1
  5. package/dist/block_registry/registry-v2-provider.js.map +1 -1
  6. package/dist/index.cjs +25 -1
  7. package/dist/index.d.ts +12 -5
  8. package/dist/index.js +5 -3
  9. package/dist/js_render/bundle_bytecode_cache.cjs +6 -1
  10. package/dist/js_render/bundle_bytecode_cache.cjs.map +1 -1
  11. package/dist/js_render/bundle_bytecode_cache.js +6 -1
  12. package/dist/js_render/bundle_bytecode_cache.js.map +1 -1
  13. package/dist/js_render/column_registry.cjs +17 -0
  14. package/dist/js_render/column_registry.cjs.map +1 -1
  15. package/dist/js_render/column_registry.js +16 -1
  16. package/dist/js_render/column_registry.js.map +1 -1
  17. package/dist/js_render/computable_context.cjs +1 -1
  18. package/dist/js_render/computable_context.js +1 -1
  19. package/dist/js_render/context.cjs +3 -2
  20. package/dist/js_render/context.cjs.map +1 -1
  21. package/dist/js_render/context.js +3 -2
  22. package/dist/js_render/context.js.map +1 -1
  23. package/dist/js_render/index.cjs +1 -1
  24. package/dist/js_render/index.cjs.map +1 -1
  25. package/dist/js_render/index.js +1 -1
  26. package/dist/js_render/index.js.map +1 -1
  27. package/dist/js_render/service_injectors.cjs +2 -9
  28. package/dist/js_render/service_injectors.cjs.map +1 -1
  29. package/dist/js_render/service_injectors.js +2 -9
  30. package/dist/js_render/service_injectors.js.map +1 -1
  31. package/dist/middle_layer/block.cjs +48 -0
  32. package/dist/middle_layer/block.cjs.map +1 -1
  33. package/dist/middle_layer/block.js +51 -4
  34. package/dist/middle_layer/block.js.map +1 -1
  35. package/dist/middle_layer/block_pack_description.cjs +80 -0
  36. package/dist/middle_layer/block_pack_description.cjs.map +1 -0
  37. package/dist/middle_layer/block_pack_description.d.ts +36 -0
  38. package/dist/middle_layer/block_pack_description.d.ts.map +1 -0
  39. package/dist/middle_layer/block_pack_description.js +78 -0
  40. package/dist/middle_layer/block_pack_description.js.map +1 -0
  41. package/dist/middle_layer/build_stamp.cjs +1 -1
  42. package/dist/middle_layer/build_stamp.js +1 -1
  43. package/dist/middle_layer/index.cjs +11 -0
  44. package/dist/middle_layer/index.d.ts +8 -2
  45. package/dist/middle_layer/index.js +3 -1
  46. package/dist/middle_layer/legacy_project_holds.cjs +61 -0
  47. package/dist/middle_layer/legacy_project_holds.cjs.map +1 -0
  48. package/dist/middle_layer/legacy_project_holds.js +61 -0
  49. package/dist/middle_layer/legacy_project_holds.js.map +1 -0
  50. package/dist/middle_layer/middle_layer.cjs +22 -24
  51. package/dist/middle_layer/middle_layer.cjs.map +1 -1
  52. package/dist/middle_layer/middle_layer.d.ts +13 -3
  53. package/dist/middle_layer/middle_layer.d.ts.map +1 -1
  54. package/dist/middle_layer/middle_layer.js +21 -23
  55. package/dist/middle_layer/middle_layer.js.map +1 -1
  56. package/dist/middle_layer/pool_columns.cjs +160 -0
  57. package/dist/middle_layer/pool_columns.cjs.map +1 -0
  58. package/dist/middle_layer/pool_columns.d.ts +59 -0
  59. package/dist/middle_layer/pool_columns.d.ts.map +1 -0
  60. package/dist/middle_layer/pool_columns.js +157 -0
  61. package/dist/middle_layer/pool_columns.js.map +1 -0
  62. package/dist/middle_layer/pool_frame.cjs +82 -0
  63. package/dist/middle_layer/pool_frame.cjs.map +1 -0
  64. package/dist/middle_layer/pool_frame.d.ts +37 -0
  65. package/dist/middle_layer/pool_frame.d.ts.map +1 -0
  66. package/dist/middle_layer/pool_frame.js +81 -0
  67. package/dist/middle_layer/pool_frame.js.map +1 -0
  68. package/dist/middle_layer/pool_table.cjs +66 -0
  69. package/dist/middle_layer/pool_table.cjs.map +1 -0
  70. package/dist/middle_layer/pool_table.d.ts +27 -0
  71. package/dist/middle_layer/pool_table.d.ts.map +1 -0
  72. package/dist/middle_layer/pool_table.js +66 -0
  73. package/dist/middle_layer/pool_table.js.map +1 -0
  74. package/dist/middle_layer/project.cjs +89 -23
  75. package/dist/middle_layer/project.cjs.map +1 -1
  76. package/dist/middle_layer/project.d.ts +70 -4
  77. package/dist/middle_layer/project.d.ts.map +1 -1
  78. package/dist/middle_layer/project.js +89 -22
  79. package/dist/middle_layer/project.js.map +1 -1
  80. package/dist/middle_layer/rich_column_query.cjs +180 -0
  81. package/dist/middle_layer/rich_column_query.cjs.map +1 -0
  82. package/dist/middle_layer/rich_column_query.d.ts +34 -0
  83. package/dist/middle_layer/rich_column_query.d.ts.map +1 -0
  84. package/dist/middle_layer/rich_column_query.js +176 -0
  85. package/dist/middle_layer/rich_column_query.js.map +1 -0
  86. package/dist/model/args.cjs +23 -0
  87. package/dist/model/args.cjs.map +1 -1
  88. package/dist/model/args.js +24 -2
  89. package/dist/model/args.js.map +1 -1
  90. package/dist/model/args_derivation.cjs +60 -2
  91. package/dist/model/args_derivation.cjs.map +1 -1
  92. package/dist/model/args_derivation.d.ts +73 -3
  93. package/dist/model/args_derivation.d.ts.map +1 -1
  94. package/dist/model/args_derivation.js +60 -3
  95. package/dist/model/args_derivation.js.map +1 -1
  96. package/dist/model/block_pack_spec.cjs.map +1 -1
  97. package/dist/model/block_pack_spec.d.ts +3 -3
  98. package/dist/model/block_pack_spec.d.ts.map +1 -1
  99. package/dist/model/block_pack_spec.js.map +1 -1
  100. package/dist/model/block_params_model.cjs +42 -0
  101. package/dist/model/block_params_model.cjs.map +1 -0
  102. package/dist/model/block_params_model.d.ts +45 -0
  103. package/dist/model/block_params_model.d.ts.map +1 -0
  104. package/dist/model/block_params_model.js +42 -0
  105. package/dist/model/block_params_model.js.map +1 -0
  106. package/dist/model/index.d.ts +3 -1
  107. package/dist/model/project_helper.cjs +50 -6
  108. package/dist/model/project_helper.cjs.map +1 -1
  109. package/dist/model/project_helper.d.ts +25 -1
  110. package/dist/model/project_helper.d.ts.map +1 -1
  111. package/dist/model/project_helper.js +51 -7
  112. package/dist/model/project_helper.js.map +1 -1
  113. package/dist/model/template_spec.d.ts +18 -0
  114. package/dist/model/template_spec.d.ts.map +1 -1
  115. package/dist/mutator/block-pack/block_pack.cjs +90 -3
  116. package/dist/mutator/block-pack/block_pack.cjs.map +1 -1
  117. package/dist/mutator/block-pack/block_pack.d.ts +16 -0
  118. package/dist/mutator/block-pack/block_pack.d.ts.map +1 -1
  119. package/dist/mutator/block-pack/block_pack.js +91 -5
  120. package/dist/mutator/block-pack/block_pack.js.map +1 -1
  121. package/dist/mutator/block-pack/required_capabilities.cjs +16 -0
  122. package/dist/mutator/block-pack/required_capabilities.cjs.map +1 -1
  123. package/dist/mutator/block-pack/required_capabilities.js +16 -1
  124. package/dist/mutator/block-pack/required_capabilities.js.map +1 -1
  125. package/dist/mutator/project.cjs +4 -0
  126. package/dist/mutator/project.cjs.map +1 -1
  127. package/dist/mutator/project.d.ts +1 -1
  128. package/dist/mutator/project.d.ts.map +1 -1
  129. package/dist/mutator/project.js +4 -0
  130. package/dist/mutator/project.js.map +1 -1
  131. package/dist/mutator/template/template_cache.cjs +8 -1
  132. package/dist/mutator/template/template_cache.cjs.map +1 -1
  133. package/dist/mutator/template/template_cache.d.ts +7 -3
  134. package/dist/mutator/template/template_cache.d.ts.map +1 -1
  135. package/dist/mutator/template/template_cache.js +8 -1
  136. package/dist/mutator/template/template_cache.js.map +1 -1
  137. package/package.json +9 -9
  138. package/src/block_registry/registry-v2-provider.test.ts +23 -0
  139. package/src/block_registry/registry-v2-provider.ts +11 -1
  140. package/src/index.ts +2 -0
  141. package/src/js_render/bundle_bytecode_cache.ts +15 -4
  142. package/src/js_render/column_registry.ts +24 -3
  143. package/src/js_render/context.ts +4 -3
  144. package/src/js_render/index.ts +3 -2
  145. package/src/js_render/service_injectors.ts +2 -11
  146. package/src/middle_layer/block.ts +61 -2
  147. package/src/middle_layer/block_pack_description.test.ts +235 -0
  148. package/src/middle_layer/block_pack_description.ts +143 -0
  149. package/src/middle_layer/index.ts +9 -1
  150. package/src/middle_layer/legacy_project_holds.test.ts +133 -0
  151. package/src/middle_layer/legacy_project_holds.ts +73 -0
  152. package/src/middle_layer/middle_layer.ts +27 -28
  153. package/src/middle_layer/pool_columns.test.ts +217 -0
  154. package/src/middle_layer/pool_columns.ts +250 -0
  155. package/src/middle_layer/pool_frame.test.ts +144 -0
  156. package/src/middle_layer/pool_frame.ts +127 -0
  157. package/src/middle_layer/pool_table.test.ts +182 -0
  158. package/src/middle_layer/pool_table.ts +98 -0
  159. package/src/middle_layer/project.ts +135 -32
  160. package/src/middle_layer/rich_column_query.test.ts +311 -0
  161. package/src/middle_layer/rich_column_query.ts +214 -0
  162. package/src/model/args.test.ts +23 -1
  163. package/src/model/args.ts +29 -1
  164. package/src/model/args_derivation.test.ts +129 -12
  165. package/src/model/args_derivation.ts +112 -8
  166. package/src/model/block_pack_spec.ts +3 -2
  167. package/src/model/block_params_model.test.ts +194 -0
  168. package/src/model/block_params_model.ts +117 -0
  169. package/src/model/index.ts +6 -0
  170. package/src/model/project_helper.ts +101 -20
  171. package/src/model/template_spec.ts +21 -0
  172. package/src/mutator/block-pack/block_pack.test.ts +10 -1
  173. package/src/mutator/block-pack/block_pack.ts +102 -2
  174. package/src/mutator/block-pack/block_pack_load.test.ts +52 -0
  175. package/src/mutator/block-pack/required_capabilities.ts +25 -0
  176. package/src/mutator/project-v3.test.ts +27 -16
  177. package/src/mutator/project.ts +5 -0
  178. package/src/mutator/template/template_cache.ts +26 -3
  179. package/src/pool/result_pool_markers.test.ts +47 -0
  180. package/src/test/block_packs.ts +12 -0
  181. package/src/test/fake_project.ts +33 -6
@@ -0,0 +1,180 @@
1
+ let _milaboratories_pl_model_common = require("@milaboratories/pl-model-common");
2
+ // src/middle_layer/rich_column_query.ts
3
+ /**
4
+ * The query of the column `id` stands for, built on the host the way the sdk/model recipes'
5
+ * `getQuery` builds it in a block's model, without running them:
6
+ * - a leaf (local or global id) is the column itself;
7
+ * - a filtered id slices the axes of its source (`sliceAxes`, axes named from the source's spec);
8
+ * - an overridden id patches the spec of its source (`specOverride`);
9
+ * - a discovered id is its hit, joined through each linker of its `path` (`linkerJoin`, the
10
+ * first linker outermost).
11
+ *
12
+ * In every layer the leaf that carries the data is named by the id of that layer, so the leaf of
13
+ * the whole query is named by `id`: a filter or sort of a table refers to the column by `id`.
14
+ * Linker columns keep their own ids.
15
+ *
16
+ * @throws when a filtered id's source has no spec in `specOf`, or filters an axis it does not have
17
+ */
18
+ function richColumnQuery(id, specOf) {
19
+ const key = (0, _milaboratories_pl_model_common.parseColumnIdSafely)(id);
20
+ if ((0, _milaboratories_pl_model_common.isColumnFilteredKey)(key)) {
21
+ const input = rebrand(richColumnQuery(key.source, specOf), key.source, id);
22
+ if (key.axisFilters.length === 0) return input;
23
+ const sourceSpec = specOfId(key.source, specOf);
24
+ return {
25
+ type: "sliceAxes",
26
+ input,
27
+ axisFilters: key.axisFilters.map(([index, constant]) => {
28
+ const axis = sourceSpec.axesSpec[index];
29
+ if (axis === void 0) throw new Error(`Column ${id} filters axis ${index}, which its source does not have.`);
30
+ return {
31
+ axisSelector: axisSelector(axis),
32
+ constant
33
+ };
34
+ })
35
+ };
36
+ }
37
+ if ((0, _milaboratories_pl_model_common.isColumnOverriddenKey)(key)) return {
38
+ type: "specOverride",
39
+ input: rebrand(richColumnQuery(key.source, specOf), key.source, id),
40
+ override: key.specOverrides
41
+ };
42
+ if ((0, _milaboratories_pl_model_common.isColumnDiscoveredKey)(key)) {
43
+ let query = rebrand(richColumnQuery(key.column, specOf), key.column, id);
44
+ const path = key.path ?? [];
45
+ for (let i = path.length - 1; i >= 0; i--) query = {
46
+ type: "linkerJoin",
47
+ linker: richColumnQuery(path[i].column, specOf),
48
+ secondary: [{ entry: query }]
49
+ };
50
+ return query;
51
+ }
52
+ return {
53
+ type: "column",
54
+ column: id
55
+ };
56
+ }
57
+ /**
58
+ * The ids whose exported columns the query of `id` reads: `id` itself, standing for its leaf, then
59
+ * the linker columns of every discovered layer, each with the columns its own query reads.
60
+ */
61
+ function richColumnReferences(id) {
62
+ const key = (0, _milaboratories_pl_model_common.parseColumnIdSafely)(id);
63
+ if ((0, _milaboratories_pl_model_common.isColumnFilteredKey)(key) || (0, _milaboratories_pl_model_common.isColumnOverriddenKey)(key)) return [id, ...richColumnReferences(key.source).slice(1)];
64
+ if ((0, _milaboratories_pl_model_common.isColumnDiscoveredKey)(key)) return [
65
+ id,
66
+ ...richColumnReferences(key.column).slice(1),
67
+ ...(key.path ?? []).flatMap((step) => richColumnReferences(step.column))
68
+ ];
69
+ return [id];
70
+ }
71
+ /**
72
+ * The qualifications of the join entry of a discovered column: those of its hit, from the
73
+ * outermost discovered layer of `id` (under overrides and filters); none for other ids.
74
+ */
75
+ function hitQualifications(id) {
76
+ return discoveredKey(id)?.columnQualifications ?? [];
77
+ }
78
+ /**
79
+ * The qualifications a discovered column puts on the primary columns of a table it is joined
80
+ * onto, keyed by the leaf id of each primary column; none for other ids.
81
+ */
82
+ function queriesQualifications(id) {
83
+ return discoveredKey(id)?.queriesQualifications ?? {};
84
+ }
85
+ /**
86
+ * `def` with each column of a join entry, of the root or under a filter or sort, made the query
87
+ * its id stands for ({@link richColumnQuery}), and the qualifications a block's
88
+ * `createPTableDefV3` gives discovered columns: a secondary column of an outer join gets its
89
+ * {@link hitQualifications}, and the primary entry gets each secondary column's
90
+ * {@link queriesQualifications} of the primary columns. Primary columns get none of their own.
91
+ * Other nodes are taken as already built and kept as they are.
92
+ */
93
+ function richTableDef(def, specOf) {
94
+ const expand = (node) => {
95
+ switch (node.type) {
96
+ case "column": return richColumnQuery(node.column, specOf);
97
+ case "innerJoin":
98
+ case "fullJoin": return {
99
+ ...node,
100
+ entries: node.entries.map((e) => ({
101
+ ...e,
102
+ entry: expand(e.entry)
103
+ }))
104
+ };
105
+ case "outerJoin": {
106
+ const primaryIds = topColumns(node.primary.entry);
107
+ const fromSecondary = node.secondary.flatMap((s) => {
108
+ if (s.entry.type !== "column") return [];
109
+ const byPrimary = queriesQualifications(s.entry.column);
110
+ return primaryIds.flatMap((p) => byPrimary[p] ?? []);
111
+ });
112
+ return {
113
+ ...node,
114
+ primary: withQualifications({
115
+ ...node.primary,
116
+ entry: expand(node.primary.entry)
117
+ }, fromSecondary),
118
+ secondary: node.secondary.map((s) => s.entry.type !== "column" ? s : withQualifications({
119
+ ...s,
120
+ entry: expand(s.entry)
121
+ }, hitQualifications(s.entry.column)))
122
+ };
123
+ }
124
+ case "filter":
125
+ case "sort": return {
126
+ ...node,
127
+ input: expand(node.input)
128
+ };
129
+ default: return node;
130
+ }
131
+ };
132
+ return { query: expand(def.query) };
133
+ }
134
+ function withQualifications(entry, added) {
135
+ if (added.length === 0) return entry;
136
+ return {
137
+ ...entry,
138
+ qualifications: [...entry.qualifications ?? [], ...added]
139
+ };
140
+ }
141
+ /** The columns a join entry joins at its top: its column, or those of a symmetric join. */
142
+ function topColumns(node) {
143
+ if (node.type === "column") return [node.column];
144
+ if (node.type === "innerJoin" || node.type === "fullJoin") return node.entries.flatMap((e) => topColumns(e.entry));
145
+ return [];
146
+ }
147
+ /** The outermost discovered layer of `id`, under overrides and filters. */
148
+ function discoveredKey(id) {
149
+ const key = (0, _milaboratories_pl_model_common.parseColumnIdSafely)(id);
150
+ if ((0, _milaboratories_pl_model_common.isColumnDiscoveredKey)(key)) return key;
151
+ if ((0, _milaboratories_pl_model_common.isColumnFilteredKey)(key) || (0, _milaboratories_pl_model_common.isColumnOverriddenKey)(key)) return discoveredKey(key.source);
152
+ }
153
+ /** `query` with its leaf named `from` named `to`; linker columns keep their ids. */
154
+ function rebrand(query, from, to) {
155
+ if (from === to) return query;
156
+ return (0, _milaboratories_pl_model_common.mapSpecQueryColumns)(query, { column: (c) => c === from ? to : c });
157
+ }
158
+ /** The spec the column `id` stands for. */
159
+ function specOfId(id, specOf) {
160
+ const leaf = (0, _milaboratories_pl_model_common.extractPObjectId)(id);
161
+ const spec = specOf(leaf);
162
+ if (spec === void 0) throw new Error(`Column ${leaf} has no spec.`);
163
+ return (0, _milaboratories_pl_model_common.reconstructSpecFromId)(spec, id);
164
+ }
165
+ /** The selector of one axis, with its domains when it has any, as the recipes write it. */
166
+ function axisSelector(axis) {
167
+ return {
168
+ name: axis.name,
169
+ type: axis.type,
170
+ ...axis.domain !== void 0 && Object.keys(axis.domain).length > 0 ? { domain: axis.domain } : {},
171
+ ...axis.contextDomain !== void 0 && Object.keys(axis.contextDomain).length > 0 ? { contextDomain: axis.contextDomain } : {}
172
+ };
173
+ }
174
+ exports.hitQualifications = hitQualifications;
175
+ exports.queriesQualifications = queriesQualifications;
176
+ exports.richColumnQuery = richColumnQuery;
177
+ exports.richColumnReferences = richColumnReferences;
178
+ exports.richTableDef = richTableDef;
179
+
180
+ //# sourceMappingURL=rich_column_query.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rich_column_query.cjs","names":["parseColumnIdSafely","isColumnFilteredKey","isColumnOverriddenKey","isColumnDiscoveredKey","mapSpecQueryColumns","extractPObjectId","reconstructSpecFromId"],"sources":["../../src/middle_layer/rich_column_query.ts"],"sourcesContent":["import {\n extractPObjectId,\n isColumnDiscoveredKey,\n isColumnFilteredKey,\n isColumnOverriddenKey,\n mapSpecQueryColumns,\n parseColumnIdSafely,\n reconstructSpecFromId,\n type AxisQualification,\n type AxisSpec,\n type ColumnDiscoveredKey,\n type ColumnUniversalId,\n type PColumnSpec,\n type PObjectId,\n type PTableDefV2,\n type SingleAxisSelector,\n type SpecQuery,\n type SpecQueryJoinEntry,\n} from \"@milaboratories/pl-model-common\";\n\n/** The spec of an exported column, by its id; `undefined` when it has none (yet). */\nexport type LeafSpecOf = (leaf: PObjectId) => PColumnSpec | undefined;\n\n/**\n * The query of the column `id` stands for, built on the host the way the sdk/model recipes'\n * `getQuery` builds it in a block's model, without running them:\n * - a leaf (local or global id) is the column itself;\n * - a filtered id slices the axes of its source (`sliceAxes`, axes named from the source's spec);\n * - an overridden id patches the spec of its source (`specOverride`);\n * - a discovered id is its hit, joined through each linker of its `path` (`linkerJoin`, the\n * first linker outermost).\n *\n * In every layer the leaf that carries the data is named by the id of that layer, so the leaf of\n * the whole query is named by `id`: a filter or sort of a table refers to the column by `id`.\n * Linker columns keep their own ids.\n *\n * @throws when a filtered id's source has no spec in `specOf`, or filters an axis it does not have\n */\nexport function richColumnQuery(id: ColumnUniversalId, specOf: LeafSpecOf): SpecQuery {\n const key = parseColumnIdSafely(id);\n if (isColumnFilteredKey(key)) {\n const input = rebrand(richColumnQuery(key.source, specOf), key.source, id);\n if (key.axisFilters.length === 0) return input;\n const sourceSpec = specOfId(key.source, specOf);\n return {\n type: \"sliceAxes\",\n input,\n axisFilters: key.axisFilters.map(([index, constant]) => {\n const axis = sourceSpec.axesSpec[index];\n if (axis === undefined)\n throw new Error(`Column ${id} filters axis ${index}, which its source does not have.`);\n return { axisSelector: axisSelector(axis), constant };\n }),\n };\n }\n if (isColumnOverriddenKey(key))\n return {\n type: \"specOverride\",\n input: rebrand(richColumnQuery(key.source, specOf), key.source, id),\n override: key.specOverrides,\n };\n if (isColumnDiscoveredKey(key)) {\n let query = rebrand(richColumnQuery(key.column, specOf), key.column, id);\n const path = key.path ?? [];\n for (let i = path.length - 1; i >= 0; i--)\n query = {\n type: \"linkerJoin\",\n linker: richColumnQuery(path[i].column, specOf),\n secondary: [{ entry: query }],\n };\n return query;\n }\n return { type: \"column\", column: id };\n}\n\n/**\n * The ids whose exported columns the query of `id` reads: `id` itself, standing for its leaf, then\n * the linker columns of every discovered layer, each with the columns its own query reads.\n */\nexport function richColumnReferences(id: ColumnUniversalId): ColumnUniversalId[] {\n const key = parseColumnIdSafely(id);\n if (isColumnFilteredKey(key) || isColumnOverriddenKey(key))\n return [id, ...richColumnReferences(key.source).slice(1)];\n if (isColumnDiscoveredKey(key))\n return [\n id,\n ...richColumnReferences(key.column).slice(1),\n ...(key.path ?? []).flatMap((step) => richColumnReferences(step.column)),\n ];\n return [id];\n}\n\n/**\n * The qualifications of the join entry of a discovered column: those of its hit, from the\n * outermost discovered layer of `id` (under overrides and filters); none for other ids.\n */\nexport function hitQualifications(id: ColumnUniversalId): readonly AxisQualification[] {\n return discoveredKey(id)?.columnQualifications ?? [];\n}\n\n/**\n * The qualifications a discovered column puts on the primary columns of a table it is joined\n * onto, keyed by the leaf id of each primary column; none for other ids.\n */\nexport function queriesQualifications(\n id: ColumnUniversalId,\n): Readonly<Record<string, readonly AxisQualification[]>> {\n return discoveredKey(id)?.queriesQualifications ?? {};\n}\n\n/**\n * `def` with each column of a join entry, of the root or under a filter or sort, made the query\n * its id stands for ({@link richColumnQuery}), and the qualifications a block's\n * `createPTableDefV3` gives discovered columns: a secondary column of an outer join gets its\n * {@link hitQualifications}, and the primary entry gets each secondary column's\n * {@link queriesQualifications} of the primary columns. Primary columns get none of their own.\n * Other nodes are taken as already built and kept as they are.\n */\nexport function richTableDef(\n def: PTableDefV2<ColumnUniversalId>,\n specOf: LeafSpecOf,\n): PTableDefV2<ColumnUniversalId> {\n const expand = (node: SpecQuery): SpecQuery => {\n switch (node.type) {\n case \"column\":\n return richColumnQuery(node.column, specOf);\n case \"innerJoin\":\n case \"fullJoin\":\n return { ...node, entries: node.entries.map((e) => ({ ...e, entry: expand(e.entry) })) };\n case \"outerJoin\": {\n const primaryIds = topColumns(node.primary.entry);\n // Per secondary column, then per primary column in primary order, as createPTableDefV3.\n const fromSecondary = node.secondary.flatMap((s) => {\n if (s.entry.type !== \"column\") return [];\n const byPrimary = queriesQualifications(s.entry.column);\n return primaryIds.flatMap((p) => byPrimary[p] ?? []);\n });\n return {\n ...node,\n primary: withQualifications(\n { ...node.primary, entry: expand(node.primary.entry) },\n fromSecondary,\n ),\n secondary: node.secondary.map((s) =>\n s.entry.type !== \"column\"\n ? s\n : withQualifications(\n { ...s, entry: expand(s.entry) },\n hitQualifications(s.entry.column),\n ),\n ),\n };\n }\n case \"filter\":\n case \"sort\":\n return { ...node, input: expand(node.input) };\n default:\n return node;\n }\n };\n return { query: expand(def.query) };\n}\n\nfunction withQualifications(\n entry: SpecQueryJoinEntry,\n added: readonly AxisQualification[],\n): SpecQueryJoinEntry {\n if (added.length === 0) return entry;\n return { ...entry, qualifications: [...(entry.qualifications ?? []), ...added] };\n}\n\n/** The columns a join entry joins at its top: its column, or those of a symmetric join. */\nfunction topColumns(node: SpecQuery): ColumnUniversalId[] {\n if (node.type === \"column\") return [node.column];\n if (node.type === \"innerJoin\" || node.type === \"fullJoin\")\n return node.entries.flatMap((e) => topColumns(e.entry));\n return [];\n}\n\n/** The outermost discovered layer of `id`, under overrides and filters. */\nfunction discoveredKey(id: ColumnUniversalId): ColumnDiscoveredKey | undefined {\n const key = parseColumnIdSafely(id);\n if (isColumnDiscoveredKey(key)) return key;\n if (isColumnFilteredKey(key) || isColumnOverriddenKey(key)) return discoveredKey(key.source);\n return undefined;\n}\n\n/** `query` with its leaf named `from` named `to`; linker columns keep their ids. */\nfunction rebrand(query: SpecQuery, from: ColumnUniversalId, to: ColumnUniversalId): SpecQuery {\n if (from === to) return query;\n return mapSpecQueryColumns(query, { column: (c) => (c === from ? to : c) });\n}\n\n/** The spec the column `id` stands for. */\nfunction specOfId(id: ColumnUniversalId, specOf: LeafSpecOf): PColumnSpec {\n const leaf = extractPObjectId(id);\n const spec = specOf(leaf);\n if (spec === undefined) throw new Error(`Column ${leaf} has no spec.`);\n return reconstructSpecFromId(spec, id);\n}\n\n/** The selector of one axis, with its domains when it has any, as the recipes write it. */\nfunction axisSelector(axis: AxisSpec): SingleAxisSelector {\n return {\n name: axis.name,\n type: axis.type,\n ...(axis.domain !== undefined && Object.keys(axis.domain).length > 0\n ? { domain: axis.domain }\n : {}),\n ...(axis.contextDomain !== undefined && Object.keys(axis.contextDomain).length > 0\n ? { contextDomain: axis.contextDomain }\n : {}),\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAsCA,SAAgB,gBAAgB,IAAu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AAA,iBAAAA,CAAiB,EAAE;CAChC,MAAM,OAAO,OAAO,IAAI;CACxB,IAAI,SAAS,KAAA,GAAW,MAAM,IAAI,MAAM,UAAU,KAAK,cAAc;CACrE,QAAA,GAAOC,gCAAAA,sBAAAA,CAAsB,MAAM,EAAE;AACvC;;AAGA,SAAS,aAAa,MAAoC;CACxD,OAAO;EACL,MAAM,KAAK;EACX,MAAM,KAAK;EACX,GAAI,KAAK,WAAW,KAAA,KAAa,OAAO,KAAK,KAAK,MAAM,CAAC,CAAC,SAAS,IAC/D,EAAE,QAAQ,KAAK,OAAO,IACtB,CAAC;EACL,GAAI,KAAK,kBAAkB,KAAA,KAAa,OAAO,KAAK,KAAK,aAAa,CAAC,CAAC,SAAS,IAC7E,EAAE,eAAe,KAAK,cAAc,IACpC,CAAC;CACP;AACF"}
@@ -0,0 +1,34 @@
1
+ import { ColumnUniversalId, PColumnSpec, PObjectId, PTableDefV2, SpecQuery } from "@milaboratories/pl-model-common";
2
+ /** The spec of an exported column, by its id; `undefined` when it has none (yet). */
3
+ export type LeafSpecOf = (leaf: PObjectId) => PColumnSpec | undefined;
4
+ /**
5
+ * The query of the column `id` stands for, built on the host the way the sdk/model recipes'
6
+ * `getQuery` builds it in a block's model, without running them:
7
+ * - a leaf (local or global id) is the column itself;
8
+ * - a filtered id slices the axes of its source (`sliceAxes`, axes named from the source's spec);
9
+ * - an overridden id patches the spec of its source (`specOverride`);
10
+ * - a discovered id is its hit, joined through each linker of its `path` (`linkerJoin`, the
11
+ * first linker outermost).
12
+ *
13
+ * In every layer the leaf that carries the data is named by the id of that layer, so the leaf of
14
+ * the whole query is named by `id`: a filter or sort of a table refers to the column by `id`.
15
+ * Linker columns keep their own ids.
16
+ *
17
+ * @throws when a filtered id's source has no spec in `specOf`, or filters an axis it does not have
18
+ */
19
+ export declare function richColumnQuery(id: ColumnUniversalId, specOf: LeafSpecOf): SpecQuery;
20
+ /**
21
+ * The ids whose exported columns the query of `id` reads: `id` itself, standing for its leaf, then
22
+ * the linker columns of every discovered layer, each with the columns its own query reads.
23
+ */
24
+ export declare function richColumnReferences(id: ColumnUniversalId): ColumnUniversalId[];
25
+ /**
26
+ * `def` with each column of a join entry, of the root or under a filter or sort, made the query
27
+ * its id stands for ({@link richColumnQuery}), and the qualifications a block's
28
+ * `createPTableDefV3` gives discovered columns: a secondary column of an outer join gets its
29
+ * {@link hitQualifications}, and the primary entry gets each secondary column's
30
+ * {@link queriesQualifications} of the primary columns. Primary columns get none of their own.
31
+ * Other nodes are taken as already built and kept as they are.
32
+ */
33
+ export declare function richTableDef(def: PTableDefV2<ColumnUniversalId>, specOf: LeafSpecOf): PTableDefV2<ColumnUniversalId>;
34
+ //# sourceMappingURL=rich_column_query.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rich_column_query.d.ts","names":[],"sources":["../../src/middle_layer/rich_column_query.ts"],"mappings":";;YAqBY,cAAc,MAAM,cAAc;;;;;;;;;;;;;;;;wBAiB9B,gBAAgB,IAAI,mBAAmB,QAAQ,aAAa;;;;;wBAyC5D,qBAAqB,IAAI,oBAAoB;;;;;;;;;wBAuC7C,aACd,KAAK,YAAY,oBACjB,QAAQ,aACP,YAAY"}
@@ -0,0 +1,176 @@
1
+ import { extractPObjectId, isColumnDiscoveredKey, isColumnFilteredKey, isColumnOverriddenKey, mapSpecQueryColumns, parseColumnIdSafely, reconstructSpecFromId } from "@milaboratories/pl-model-common";
2
+ // src/middle_layer/rich_column_query.ts
3
+ /**
4
+ * The query of the column `id` stands for, built on the host the way the sdk/model recipes'
5
+ * `getQuery` builds it in a block's model, without running them:
6
+ * - a leaf (local or global id) is the column itself;
7
+ * - a filtered id slices the axes of its source (`sliceAxes`, axes named from the source's spec);
8
+ * - an overridden id patches the spec of its source (`specOverride`);
9
+ * - a discovered id is its hit, joined through each linker of its `path` (`linkerJoin`, the
10
+ * first linker outermost).
11
+ *
12
+ * In every layer the leaf that carries the data is named by the id of that layer, so the leaf of
13
+ * the whole query is named by `id`: a filter or sort of a table refers to the column by `id`.
14
+ * Linker columns keep their own ids.
15
+ *
16
+ * @throws when a filtered id's source has no spec in `specOf`, or filters an axis it does not have
17
+ */
18
+ function richColumnQuery(id, specOf) {
19
+ const key = parseColumnIdSafely(id);
20
+ if (isColumnFilteredKey(key)) {
21
+ const input = rebrand(richColumnQuery(key.source, specOf), key.source, id);
22
+ if (key.axisFilters.length === 0) return input;
23
+ const sourceSpec = specOfId(key.source, specOf);
24
+ return {
25
+ type: "sliceAxes",
26
+ input,
27
+ axisFilters: key.axisFilters.map(([index, constant]) => {
28
+ const axis = sourceSpec.axesSpec[index];
29
+ if (axis === void 0) throw new Error(`Column ${id} filters axis ${index}, which its source does not have.`);
30
+ return {
31
+ axisSelector: axisSelector(axis),
32
+ constant
33
+ };
34
+ })
35
+ };
36
+ }
37
+ if (isColumnOverriddenKey(key)) return {
38
+ type: "specOverride",
39
+ input: rebrand(richColumnQuery(key.source, specOf), key.source, id),
40
+ override: key.specOverrides
41
+ };
42
+ if (isColumnDiscoveredKey(key)) {
43
+ let query = rebrand(richColumnQuery(key.column, specOf), key.column, id);
44
+ const path = key.path ?? [];
45
+ for (let i = path.length - 1; i >= 0; i--) query = {
46
+ type: "linkerJoin",
47
+ linker: richColumnQuery(path[i].column, specOf),
48
+ secondary: [{ entry: query }]
49
+ };
50
+ return query;
51
+ }
52
+ return {
53
+ type: "column",
54
+ column: id
55
+ };
56
+ }
57
+ /**
58
+ * The ids whose exported columns the query of `id` reads: `id` itself, standing for its leaf, then
59
+ * the linker columns of every discovered layer, each with the columns its own query reads.
60
+ */
61
+ function richColumnReferences(id) {
62
+ const key = parseColumnIdSafely(id);
63
+ if (isColumnFilteredKey(key) || isColumnOverriddenKey(key)) return [id, ...richColumnReferences(key.source).slice(1)];
64
+ if (isColumnDiscoveredKey(key)) return [
65
+ id,
66
+ ...richColumnReferences(key.column).slice(1),
67
+ ...(key.path ?? []).flatMap((step) => richColumnReferences(step.column))
68
+ ];
69
+ return [id];
70
+ }
71
+ /**
72
+ * The qualifications of the join entry of a discovered column: those of its hit, from the
73
+ * outermost discovered layer of `id` (under overrides and filters); none for other ids.
74
+ */
75
+ function hitQualifications(id) {
76
+ return discoveredKey(id)?.columnQualifications ?? [];
77
+ }
78
+ /**
79
+ * The qualifications a discovered column puts on the primary columns of a table it is joined
80
+ * onto, keyed by the leaf id of each primary column; none for other ids.
81
+ */
82
+ function queriesQualifications(id) {
83
+ return discoveredKey(id)?.queriesQualifications ?? {};
84
+ }
85
+ /**
86
+ * `def` with each column of a join entry, of the root or under a filter or sort, made the query
87
+ * its id stands for ({@link richColumnQuery}), and the qualifications a block's
88
+ * `createPTableDefV3` gives discovered columns: a secondary column of an outer join gets its
89
+ * {@link hitQualifications}, and the primary entry gets each secondary column's
90
+ * {@link queriesQualifications} of the primary columns. Primary columns get none of their own.
91
+ * Other nodes are taken as already built and kept as they are.
92
+ */
93
+ function richTableDef(def, specOf) {
94
+ const expand = (node) => {
95
+ switch (node.type) {
96
+ case "column": return richColumnQuery(node.column, specOf);
97
+ case "innerJoin":
98
+ case "fullJoin": return {
99
+ ...node,
100
+ entries: node.entries.map((e) => ({
101
+ ...e,
102
+ entry: expand(e.entry)
103
+ }))
104
+ };
105
+ case "outerJoin": {
106
+ const primaryIds = topColumns(node.primary.entry);
107
+ const fromSecondary = node.secondary.flatMap((s) => {
108
+ if (s.entry.type !== "column") return [];
109
+ const byPrimary = queriesQualifications(s.entry.column);
110
+ return primaryIds.flatMap((p) => byPrimary[p] ?? []);
111
+ });
112
+ return {
113
+ ...node,
114
+ primary: withQualifications({
115
+ ...node.primary,
116
+ entry: expand(node.primary.entry)
117
+ }, fromSecondary),
118
+ secondary: node.secondary.map((s) => s.entry.type !== "column" ? s : withQualifications({
119
+ ...s,
120
+ entry: expand(s.entry)
121
+ }, hitQualifications(s.entry.column)))
122
+ };
123
+ }
124
+ case "filter":
125
+ case "sort": return {
126
+ ...node,
127
+ input: expand(node.input)
128
+ };
129
+ default: return node;
130
+ }
131
+ };
132
+ return { query: expand(def.query) };
133
+ }
134
+ function withQualifications(entry, added) {
135
+ if (added.length === 0) return entry;
136
+ return {
137
+ ...entry,
138
+ qualifications: [...entry.qualifications ?? [], ...added]
139
+ };
140
+ }
141
+ /** The columns a join entry joins at its top: its column, or those of a symmetric join. */
142
+ function topColumns(node) {
143
+ if (node.type === "column") return [node.column];
144
+ if (node.type === "innerJoin" || node.type === "fullJoin") return node.entries.flatMap((e) => topColumns(e.entry));
145
+ return [];
146
+ }
147
+ /** The outermost discovered layer of `id`, under overrides and filters. */
148
+ function discoveredKey(id) {
149
+ const key = parseColumnIdSafely(id);
150
+ if (isColumnDiscoveredKey(key)) return key;
151
+ if (isColumnFilteredKey(key) || isColumnOverriddenKey(key)) return discoveredKey(key.source);
152
+ }
153
+ /** `query` with its leaf named `from` named `to`; linker columns keep their ids. */
154
+ function rebrand(query, from, to) {
155
+ if (from === to) return query;
156
+ return mapSpecQueryColumns(query, { column: (c) => c === from ? to : c });
157
+ }
158
+ /** The spec the column `id` stands for. */
159
+ function specOfId(id, specOf) {
160
+ const leaf = extractPObjectId(id);
161
+ const spec = specOf(leaf);
162
+ if (spec === void 0) throw new Error(`Column ${leaf} has no spec.`);
163
+ return reconstructSpecFromId(spec, id);
164
+ }
165
+ /** The selector of one axis, with its domains when it has any, as the recipes write it. */
166
+ function axisSelector(axis) {
167
+ return {
168
+ name: axis.name,
169
+ type: axis.type,
170
+ ...axis.domain !== void 0 && Object.keys(axis.domain).length > 0 ? { domain: axis.domain } : {},
171
+ ...axis.contextDomain !== void 0 && Object.keys(axis.contextDomain).length > 0 ? { contextDomain: axis.contextDomain } : {}
172
+ };
173
+ }
174
+ export { hitQualifications, queriesQualifications, richColumnQuery, richColumnReferences, richTableDef };
175
+
176
+ //# sourceMappingURL=rich_column_query.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rich_column_query.js","names":[],"sources":["../../src/middle_layer/rich_column_query.ts"],"sourcesContent":["import {\n extractPObjectId,\n isColumnDiscoveredKey,\n isColumnFilteredKey,\n isColumnOverriddenKey,\n mapSpecQueryColumns,\n parseColumnIdSafely,\n reconstructSpecFromId,\n type AxisQualification,\n type AxisSpec,\n type ColumnDiscoveredKey,\n type ColumnUniversalId,\n type PColumnSpec,\n type PObjectId,\n type PTableDefV2,\n type SingleAxisSelector,\n type SpecQuery,\n type SpecQueryJoinEntry,\n} from \"@milaboratories/pl-model-common\";\n\n/** The spec of an exported column, by its id; `undefined` when it has none (yet). */\nexport type LeafSpecOf = (leaf: PObjectId) => PColumnSpec | undefined;\n\n/**\n * The query of the column `id` stands for, built on the host the way the sdk/model recipes'\n * `getQuery` builds it in a block's model, without running them:\n * - a leaf (local or global id) is the column itself;\n * - a filtered id slices the axes of its source (`sliceAxes`, axes named from the source's spec);\n * - an overridden id patches the spec of its source (`specOverride`);\n * - a discovered id is its hit, joined through each linker of its `path` (`linkerJoin`, the\n * first linker outermost).\n *\n * In every layer the leaf that carries the data is named by the id of that layer, so the leaf of\n * the whole query is named by `id`: a filter or sort of a table refers to the column by `id`.\n * Linker columns keep their own ids.\n *\n * @throws when a filtered id's source has no spec in `specOf`, or filters an axis it does not have\n */\nexport function richColumnQuery(id: ColumnUniversalId, specOf: LeafSpecOf): SpecQuery {\n const key = parseColumnIdSafely(id);\n if (isColumnFilteredKey(key)) {\n const input = rebrand(richColumnQuery(key.source, specOf), key.source, id);\n if (key.axisFilters.length === 0) return input;\n const sourceSpec = specOfId(key.source, specOf);\n return {\n type: \"sliceAxes\",\n input,\n axisFilters: key.axisFilters.map(([index, constant]) => {\n const axis = sourceSpec.axesSpec[index];\n if (axis === undefined)\n throw new Error(`Column ${id} filters axis ${index}, which its source does not have.`);\n return { axisSelector: axisSelector(axis), constant };\n }),\n };\n }\n if (isColumnOverriddenKey(key))\n return {\n type: \"specOverride\",\n input: rebrand(richColumnQuery(key.source, specOf), key.source, id),\n override: key.specOverrides,\n };\n if (isColumnDiscoveredKey(key)) {\n let query = rebrand(richColumnQuery(key.column, specOf), key.column, id);\n const path = key.path ?? [];\n for (let i = path.length - 1; i >= 0; i--)\n query = {\n type: \"linkerJoin\",\n linker: richColumnQuery(path[i].column, specOf),\n secondary: [{ entry: query }],\n };\n return query;\n }\n return { type: \"column\", column: id };\n}\n\n/**\n * The ids whose exported columns the query of `id` reads: `id` itself, standing for its leaf, then\n * the linker columns of every discovered layer, each with the columns its own query reads.\n */\nexport function richColumnReferences(id: ColumnUniversalId): ColumnUniversalId[] {\n const key = parseColumnIdSafely(id);\n if (isColumnFilteredKey(key) || isColumnOverriddenKey(key))\n return [id, ...richColumnReferences(key.source).slice(1)];\n if (isColumnDiscoveredKey(key))\n return [\n id,\n ...richColumnReferences(key.column).slice(1),\n ...(key.path ?? []).flatMap((step) => richColumnReferences(step.column)),\n ];\n return [id];\n}\n\n/**\n * The qualifications of the join entry of a discovered column: those of its hit, from the\n * outermost discovered layer of `id` (under overrides and filters); none for other ids.\n */\nexport function hitQualifications(id: ColumnUniversalId): readonly AxisQualification[] {\n return discoveredKey(id)?.columnQualifications ?? [];\n}\n\n/**\n * The qualifications a discovered column puts on the primary columns of a table it is joined\n * onto, keyed by the leaf id of each primary column; none for other ids.\n */\nexport function queriesQualifications(\n id: ColumnUniversalId,\n): Readonly<Record<string, readonly AxisQualification[]>> {\n return discoveredKey(id)?.queriesQualifications ?? {};\n}\n\n/**\n * `def` with each column of a join entry, of the root or under a filter or sort, made the query\n * its id stands for ({@link richColumnQuery}), and the qualifications a block's\n * `createPTableDefV3` gives discovered columns: a secondary column of an outer join gets its\n * {@link hitQualifications}, and the primary entry gets each secondary column's\n * {@link queriesQualifications} of the primary columns. Primary columns get none of their own.\n * Other nodes are taken as already built and kept as they are.\n */\nexport function richTableDef(\n def: PTableDefV2<ColumnUniversalId>,\n specOf: LeafSpecOf,\n): PTableDefV2<ColumnUniversalId> {\n const expand = (node: SpecQuery): SpecQuery => {\n switch (node.type) {\n case \"column\":\n return richColumnQuery(node.column, specOf);\n case \"innerJoin\":\n case \"fullJoin\":\n return { ...node, entries: node.entries.map((e) => ({ ...e, entry: expand(e.entry) })) };\n case \"outerJoin\": {\n const primaryIds = topColumns(node.primary.entry);\n // Per secondary column, then per primary column in primary order, as createPTableDefV3.\n const fromSecondary = node.secondary.flatMap((s) => {\n if (s.entry.type !== \"column\") return [];\n const byPrimary = queriesQualifications(s.entry.column);\n return primaryIds.flatMap((p) => byPrimary[p] ?? []);\n });\n return {\n ...node,\n primary: withQualifications(\n { ...node.primary, entry: expand(node.primary.entry) },\n fromSecondary,\n ),\n secondary: node.secondary.map((s) =>\n s.entry.type !== \"column\"\n ? s\n : withQualifications(\n { ...s, entry: expand(s.entry) },\n hitQualifications(s.entry.column),\n ),\n ),\n };\n }\n case \"filter\":\n case \"sort\":\n return { ...node, input: expand(node.input) };\n default:\n return node;\n }\n };\n return { query: expand(def.query) };\n}\n\nfunction withQualifications(\n entry: SpecQueryJoinEntry,\n added: readonly AxisQualification[],\n): SpecQueryJoinEntry {\n if (added.length === 0) return entry;\n return { ...entry, qualifications: [...(entry.qualifications ?? []), ...added] };\n}\n\n/** The columns a join entry joins at its top: its column, or those of a symmetric join. */\nfunction topColumns(node: SpecQuery): ColumnUniversalId[] {\n if (node.type === \"column\") return [node.column];\n if (node.type === \"innerJoin\" || node.type === \"fullJoin\")\n return node.entries.flatMap((e) => topColumns(e.entry));\n return [];\n}\n\n/** The outermost discovered layer of `id`, under overrides and filters. */\nfunction discoveredKey(id: ColumnUniversalId): ColumnDiscoveredKey | undefined {\n const key = parseColumnIdSafely(id);\n if (isColumnDiscoveredKey(key)) return key;\n if (isColumnFilteredKey(key) || isColumnOverriddenKey(key)) return discoveredKey(key.source);\n return undefined;\n}\n\n/** `query` with its leaf named `from` named `to`; linker columns keep their ids. */\nfunction rebrand(query: SpecQuery, from: ColumnUniversalId, to: ColumnUniversalId): SpecQuery {\n if (from === to) return query;\n return mapSpecQueryColumns(query, { column: (c) => (c === from ? to : c) });\n}\n\n/** The spec the column `id` stands for. */\nfunction specOfId(id: ColumnUniversalId, specOf: LeafSpecOf): PColumnSpec {\n const leaf = extractPObjectId(id);\n const spec = specOf(leaf);\n if (spec === undefined) throw new Error(`Column ${leaf} has no spec.`);\n return reconstructSpecFromId(spec, id);\n}\n\n/** The selector of one axis, with its domains when it has any, as the recipes write it. */\nfunction axisSelector(axis: AxisSpec): SingleAxisSelector {\n return {\n name: axis.name,\n type: axis.type,\n ...(axis.domain !== undefined && Object.keys(axis.domain).length > 0\n ? { domain: axis.domain }\n : {}),\n ...(axis.contextDomain !== undefined && Object.keys(axis.contextDomain).length > 0\n ? { contextDomain: axis.contextDomain }\n : {}),\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAsCA,SAAgB,gBAAgB,IAAuB,QAA+B;CACpF,MAAM,MAAM,oBAAoB,EAAE;CAClC,IAAI,oBAAoB,GAAG,GAAG;EAC5B,MAAM,QAAQ,QAAQ,gBAAgB,IAAI,QAAQ,MAAM,GAAG,IAAI,QAAQ,EAAE;EACzE,IAAI,IAAI,YAAY,WAAW,GAAG,OAAO;EACzC,MAAM,aAAa,SAAS,IAAI,QAAQ,MAAM;EAC9C,OAAO;GACL,MAAM;GACN;GACA,aAAa,IAAI,YAAY,KAAK,CAAC,OAAO,cAAc;IACtD,MAAM,OAAO,WAAW,SAAS;IACjC,IAAI,SAAS,KAAA,GACX,MAAM,IAAI,MAAM,UAAU,GAAG,gBAAgB,MAAM,kCAAkC;IACvF,OAAO;KAAE,cAAc,aAAa,IAAI;KAAG;IAAS;GACtD,CAAC;EACH;CACF;CACA,IAAI,sBAAsB,GAAG,GAC3B,OAAO;EACL,MAAM;EACN,OAAO,QAAQ,gBAAgB,IAAI,QAAQ,MAAM,GAAG,IAAI,QAAQ,EAAE;EAClE,UAAU,IAAI;CAChB;CACF,IAAI,sBAAsB,GAAG,GAAG;EAC9B,IAAI,QAAQ,QAAQ,gBAAgB,IAAI,QAAQ,MAAM,GAAG,IAAI,QAAQ,EAAE;EACvE,MAAM,OAAO,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SAAS,IAAI,OAAO;CACxB,OAAO,oBAAoB,OAAO,EAAE,SAAS,MAAO,MAAM,OAAO,KAAK,EAAG,CAAC;AAC5E;;AAGA,SAAS,SAAS,IAAuB,QAAiC;CACxE,MAAM,OAAO,iBAAiB,EAAE;CAChC,MAAM,OAAO,OAAO,IAAI;CACxB,IAAI,SAAS,KAAA,GAAW,MAAM,IAAI,MAAM,UAAU,KAAK,cAAc;CACrE,OAAO,sBAAsB,MAAM,EAAE;AACvC;;AAGA,SAAS,aAAa,MAAoC;CACxD,OAAO;EACL,MAAM,KAAK;EACX,MAAM,KAAK;EACX,GAAI,KAAK,WAAW,KAAA,KAAa,OAAO,KAAK,KAAK,MAAM,CAAC,CAAC,SAAS,IAC/D,EAAE,QAAQ,KAAK,OAAO,IACtB,CAAC;EACL,GAAI,KAAK,kBAAkB,KAAA,KAAa,OAAO,KAAK,KAAK,aAAa,CAAC,CAAC,SAAS,IAC7E,EAAE,eAAe,KAAK,cAAc,IACpC,CAAC;CACP;AACF"}
@@ -59,8 +59,31 @@ function recordRef(result, blockId, requireEnrichments, allowed) {
59
59
  if (requireEnrichments) result.upstreamsRequiringEnrichments.add(blockId);
60
60
  } else result.missingReferences = true;
61
61
  }
62
+ /**
63
+ * The references in a block's args to blocks it cannot read from: blocks the project does not
64
+ * have, and blocks that are not above it. Each block is listed once, in the order first found.
65
+ *
66
+ * @param args - The block's args
67
+ * @param blockId - The block the args belong to
68
+ * @param blockIdsInOrder - Every block of the project, top to bottom
69
+ */
70
+ function missingBlockReferences(args, blockId, blockIdsInOrder) {
71
+ const position = blockIdsInOrder.indexOf(blockId);
72
+ const above = new Set(position < 0 ? [] : blockIdsInOrder.slice(0, position));
73
+ const known = new Set(blockIdsInOrder);
74
+ const missing = [];
75
+ for (const referenced of inferAllReferencedBlocks(args).upstreams) {
76
+ if (above.has(referenced)) continue;
77
+ missing.push({
78
+ blockId: (0, _milaboratories_pl_model_common.asBlockId)(referenced),
79
+ reason: known.has(referenced) ? "not-above" : "unknown-block"
80
+ });
81
+ }
82
+ return missing;
83
+ }
62
84
  exports.inferAllReferencedBlocks = inferAllReferencedBlocks;
63
85
  exports.isBlockOutputReference = isBlockOutputReference;
86
+ exports.missingBlockReferences = missingBlockReferences;
64
87
  exports.outputRef = outputRef;
65
88
 
66
89
  //# sourceMappingURL=args.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"args.cjs","names":["peelJsonLayers"],"sources":["../../src/model/args.ts"],"sourcesContent":["import { assertNever } from \"@milaboratories/ts-helpers\";\nimport type { PlRef } from \"@platforma-sdk/model\";\nimport { peelJsonLayers } from \"@milaboratories/pl-model-common\";\n\nexport function outputRef(blockId: string, name: string, requireEnrichments?: boolean): PlRef {\n if (requireEnrichments) return { __isRef: true, blockId, name, requireEnrichments };\n else return { __isRef: true, blockId, name };\n}\n\nexport function isBlockOutputReference(obj: unknown): obj is PlRef {\n return (\n typeof obj === \"object\" &&\n obj !== null &&\n \"__isRef\" in obj &&\n obj.__isRef === true &&\n \"blockId\" in obj &&\n \"name\" in obj\n );\n}\n\nexport interface BlockUpstreams {\n /** All direct block dependencies */\n upstreams: Set<string>;\n /** Direct block dependencies which enrichments are also required by current block */\n upstreamsRequiringEnrichments: Set<string>;\n /** True if not-allowed references was encountered */\n missingReferences: boolean;\n}\n\n/** Extracts all resource ids referenced by args object. */\nexport function inferAllReferencedBlocks(args: unknown, allowed?: Set<string>): BlockUpstreams {\n const result = {\n upstreams: new Set<string>(),\n upstreamsRequiringEnrichments: new Set<string>(),\n missingReferences: false,\n };\n addAllReferencedBlocks(result, args, allowed);\n return result;\n}\n\nfunction addAllReferencedBlocks(result: BlockUpstreams, node: unknown, allowed?: Set<string>) {\n const type = typeof node;\n switch (type) {\n case \"function\":\n case \"bigint\":\n case \"number\":\n case \"boolean\":\n case \"symbol\":\n case \"undefined\":\n return;\n case \"string\": {\n // A reference can be hiding inside a string under any number of `JSON.stringify`\n // passes, and this walk would otherwise never reach it. Peeling is shared with the\n // column-id remapper so that \"how a value hides inside a string\" has one\n // definition; what happens at the bottom is not shared, and here it is deliberately\n // broad — anything the peel produced is walked, including a foreign-schema document\n // that merely contains a reference. That breadth is what makes this detector usable\n // as a guard over carriers the template codec declines to touch.\n const peeled = peelJsonLayers(node as string);\n if (peeled !== undefined) addAllReferencedBlocks(result, peeled.value, allowed);\n return;\n }\n case \"object\": {\n if (node === null) return;\n if (isBlockOutputReference(node)) {\n recordRef(result, node.blockId, node.requireEnrichments === true, allowed);\n } else if (Array.isArray(node)) {\n for (const child of node) addAllReferencedBlocks(result, child, allowed);\n } else {\n // Keys as well as values: a discovered column's `queriesQualifications` is keyed BY\n // column id, so a map key can carry a block id — and dropping the key loses that\n // upstream edge entirely.\n for (const [key, child] of Object.entries(node as object)) {\n addAllReferencedBlocks(result, key, allowed);\n addAllReferencedBlocks(result, child, allowed);\n }\n }\n\n return;\n }\n default:\n assertNever(type);\n }\n}\n\nfunction recordRef(\n result: BlockUpstreams,\n blockId: string,\n requireEnrichments: boolean,\n allowed?: Set<string>,\n) {\n if (allowed === undefined || allowed.has(blockId)) {\n result.upstreams.add(blockId);\n if (requireEnrichments) result.upstreamsRequiringEnrichments.add(blockId);\n } else result.missingReferences = true;\n}\n"],"mappings":";;;AAIA,SAAgB,UAAU,SAAiB,MAAc,oBAAqC;CAC5F,IAAI,oBAAoB,OAAO;EAAE,SAAS;EAAM;EAAS;EAAM;CAAmB;MAC7E,OAAO;EAAE,SAAS;EAAM;EAAS;CAAK;AAC7C;AAEA,SAAgB,uBAAuB,KAA4B;CACjE,OACE,OAAO,QAAQ,YACf,QAAQ,QACR,aAAa,OACb,IAAI,YAAY,QAChB,aAAa,OACb,UAAU;AAEd;;AAYA,SAAgB,yBAAyB,MAAe,SAAuC;CAC7F,MAAM,SAAS;EACb,2BAAW,IAAI,IAAY;EAC3B,+CAA+B,IAAI,IAAY;EAC/C,mBAAmB;CACrB;CACA,uBAAuB,QAAQ,MAAM,OAAO;CAC5C,OAAO;AACT;AAEA,SAAS,uBAAuB,QAAwB,MAAe,SAAuB;CAC5F,MAAM,OAAO,OAAO;CACpB,QAAQ,MAAR;EACE,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,aACH;EACF,KAAK,UAAU;GAQb,MAAM,UAAA,GAASA,gCAAAA,eAAAA,CAAe,IAAc;GAC5C,IAAI,WAAW,KAAA,GAAW,uBAAuB,QAAQ,OAAO,OAAO,OAAO;GAC9E;EACF;EACA,KAAK;GACH,IAAI,SAAS,MAAM;GACnB,IAAI,uBAAuB,IAAI,GAC7B,UAAU,QAAQ,KAAK,SAAS,KAAK,uBAAuB,MAAM,OAAO;QACpE,IAAI,MAAM,QAAQ,IAAI,GAC3B,KAAK,MAAM,SAAS,MAAM,uBAAuB,QAAQ,OAAO,OAAO;QAKvE,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,IAAc,GAAG;IACzD,uBAAuB,QAAQ,KAAK,OAAO;IAC3C,uBAAuB,QAAQ,OAAO,OAAO;GAC/C;GAGF;EAEF,SACE,CAAA,GAAA,2BAAA,YAAA,CAAY,IAAI;CACpB;AACF;AAEA,SAAS,UACP,QACA,SACA,oBACA,SACA;CACA,IAAI,YAAY,KAAA,KAAa,QAAQ,IAAI,OAAO,GAAG;EACjD,OAAO,UAAU,IAAI,OAAO;EAC5B,IAAI,oBAAoB,OAAO,8BAA8B,IAAI,OAAO;CAC1E,OAAO,OAAO,oBAAoB;AACpC"}
1
+ {"version":3,"file":"args.cjs","names":["peelJsonLayers","asBlockId"],"sources":["../../src/model/args.ts"],"sourcesContent":["import { assertNever } from \"@milaboratories/ts-helpers\";\nimport type { PlRef } from \"@platforma-sdk/model\";\nimport { asBlockId, peelJsonLayers } from \"@milaboratories/pl-model-common\";\nimport type { MissingBlockReference } from \"./args_derivation\";\n\nexport function outputRef(blockId: string, name: string, requireEnrichments?: boolean): PlRef {\n if (requireEnrichments) return { __isRef: true, blockId, name, requireEnrichments };\n else return { __isRef: true, blockId, name };\n}\n\nexport function isBlockOutputReference(obj: unknown): obj is PlRef {\n return (\n typeof obj === \"object\" &&\n obj !== null &&\n \"__isRef\" in obj &&\n obj.__isRef === true &&\n \"blockId\" in obj &&\n \"name\" in obj\n );\n}\n\nexport interface BlockUpstreams {\n /** All direct block dependencies */\n upstreams: Set<string>;\n /** Direct block dependencies which enrichments are also required by current block */\n upstreamsRequiringEnrichments: Set<string>;\n /** True if not-allowed references was encountered */\n missingReferences: boolean;\n}\n\n/** Extracts all resource ids referenced by args object. */\nexport function inferAllReferencedBlocks(args: unknown, allowed?: Set<string>): BlockUpstreams {\n const result = {\n upstreams: new Set<string>(),\n upstreamsRequiringEnrichments: new Set<string>(),\n missingReferences: false,\n };\n addAllReferencedBlocks(result, args, allowed);\n return result;\n}\n\nfunction addAllReferencedBlocks(result: BlockUpstreams, node: unknown, allowed?: Set<string>) {\n const type = typeof node;\n switch (type) {\n case \"function\":\n case \"bigint\":\n case \"number\":\n case \"boolean\":\n case \"symbol\":\n case \"undefined\":\n return;\n case \"string\": {\n // A reference can be hiding inside a string under any number of `JSON.stringify`\n // passes, and this walk would otherwise never reach it. Peeling is shared with the\n // column-id remapper so that \"how a value hides inside a string\" has one\n // definition; what happens at the bottom is not shared, and here it is deliberately\n // broad — anything the peel produced is walked, including a foreign-schema document\n // that merely contains a reference. That breadth is what makes this detector usable\n // as a guard over carriers the template codec declines to touch.\n const peeled = peelJsonLayers(node as string);\n if (peeled !== undefined) addAllReferencedBlocks(result, peeled.value, allowed);\n return;\n }\n case \"object\": {\n if (node === null) return;\n if (isBlockOutputReference(node)) {\n recordRef(result, node.blockId, node.requireEnrichments === true, allowed);\n } else if (Array.isArray(node)) {\n for (const child of node) addAllReferencedBlocks(result, child, allowed);\n } else {\n // Keys as well as values: a discovered column's `queriesQualifications` is keyed BY\n // column id, so a map key can carry a block id — and dropping the key loses that\n // upstream edge entirely.\n for (const [key, child] of Object.entries(node as object)) {\n addAllReferencedBlocks(result, key, allowed);\n addAllReferencedBlocks(result, child, allowed);\n }\n }\n\n return;\n }\n default:\n assertNever(type);\n }\n}\n\nfunction recordRef(\n result: BlockUpstreams,\n blockId: string,\n requireEnrichments: boolean,\n allowed?: Set<string>,\n) {\n if (allowed === undefined || allowed.has(blockId)) {\n result.upstreams.add(blockId);\n if (requireEnrichments) result.upstreamsRequiringEnrichments.add(blockId);\n } else result.missingReferences = true;\n}\n\n/**\n * The references in a block's args to blocks it cannot read from: blocks the project does not\n * have, and blocks that are not above it. Each block is listed once, in the order first found.\n *\n * @param args - The block's args\n * @param blockId - The block the args belong to\n * @param blockIdsInOrder - Every block of the project, top to bottom\n */\nexport function missingBlockReferences(\n args: unknown,\n blockId: string,\n blockIdsInOrder: readonly string[],\n): MissingBlockReference[] {\n const position = blockIdsInOrder.indexOf(blockId);\n const above = new Set(position < 0 ? [] : blockIdsInOrder.slice(0, position));\n const known = new Set(blockIdsInOrder);\n const missing: MissingBlockReference[] = [];\n for (const referenced of inferAllReferencedBlocks(args).upstreams) {\n if (above.has(referenced)) continue;\n missing.push({\n blockId: asBlockId(referenced),\n reason: known.has(referenced) ? \"not-above\" : \"unknown-block\",\n });\n }\n return missing;\n}\n"],"mappings":";;;AAKA,SAAgB,UAAU,SAAiB,MAAc,oBAAqC;CAC5F,IAAI,oBAAoB,OAAO;EAAE,SAAS;EAAM;EAAS;EAAM;CAAmB;MAC7E,OAAO;EAAE,SAAS;EAAM;EAAS;CAAK;AAC7C;AAEA,SAAgB,uBAAuB,KAA4B;CACjE,OACE,OAAO,QAAQ,YACf,QAAQ,QACR,aAAa,OACb,IAAI,YAAY,QAChB,aAAa,OACb,UAAU;AAEd;;AAYA,SAAgB,yBAAyB,MAAe,SAAuC;CAC7F,MAAM,SAAS;EACb,2BAAW,IAAI,IAAY;EAC3B,+CAA+B,IAAI,IAAY;EAC/C,mBAAmB;CACrB;CACA,uBAAuB,QAAQ,MAAM,OAAO;CAC5C,OAAO;AACT;AAEA,SAAS,uBAAuB,QAAwB,MAAe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AAK;GACX,UAAA,GAASC,gCAAAA,UAAAA,CAAU,UAAU;GAC7B,QAAQ,MAAM,IAAI,UAAU,IAAI,cAAc;EAChD,CAAC;CACH;CACA,OAAO;AACT"}
@@ -1,5 +1,5 @@
1
1
  import { assertNever } from "@milaboratories/ts-helpers";
2
- import { peelJsonLayers } from "@milaboratories/pl-model-common";
2
+ import { asBlockId, peelJsonLayers } from "@milaboratories/pl-model-common";
3
3
  // src/model/args.ts
4
4
  function outputRef(blockId, name, requireEnrichments) {
5
5
  if (requireEnrichments) return {
@@ -59,6 +59,28 @@ function recordRef(result, blockId, requireEnrichments, allowed) {
59
59
  if (requireEnrichments) result.upstreamsRequiringEnrichments.add(blockId);
60
60
  } else result.missingReferences = true;
61
61
  }
62
- export { inferAllReferencedBlocks, isBlockOutputReference, outputRef };
62
+ /**
63
+ * The references in a block's args to blocks it cannot read from: blocks the project does not
64
+ * have, and blocks that are not above it. Each block is listed once, in the order first found.
65
+ *
66
+ * @param args - The block's args
67
+ * @param blockId - The block the args belong to
68
+ * @param blockIdsInOrder - Every block of the project, top to bottom
69
+ */
70
+ function missingBlockReferences(args, blockId, blockIdsInOrder) {
71
+ const position = blockIdsInOrder.indexOf(blockId);
72
+ const above = new Set(position < 0 ? [] : blockIdsInOrder.slice(0, position));
73
+ const known = new Set(blockIdsInOrder);
74
+ const missing = [];
75
+ for (const referenced of inferAllReferencedBlocks(args).upstreams) {
76
+ if (above.has(referenced)) continue;
77
+ missing.push({
78
+ blockId: asBlockId(referenced),
79
+ reason: known.has(referenced) ? "not-above" : "unknown-block"
80
+ });
81
+ }
82
+ return missing;
83
+ }
84
+ export { inferAllReferencedBlocks, isBlockOutputReference, missingBlockReferences, outputRef };
63
85
 
64
86
  //# sourceMappingURL=args.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"args.js","names":[],"sources":["../../src/model/args.ts"],"sourcesContent":["import { assertNever } from \"@milaboratories/ts-helpers\";\nimport type { PlRef } from \"@platforma-sdk/model\";\nimport { peelJsonLayers } from \"@milaboratories/pl-model-common\";\n\nexport function outputRef(blockId: string, name: string, requireEnrichments?: boolean): PlRef {\n if (requireEnrichments) return { __isRef: true, blockId, name, requireEnrichments };\n else return { __isRef: true, blockId, name };\n}\n\nexport function isBlockOutputReference(obj: unknown): obj is PlRef {\n return (\n typeof obj === \"object\" &&\n obj !== null &&\n \"__isRef\" in obj &&\n obj.__isRef === true &&\n \"blockId\" in obj &&\n \"name\" in obj\n );\n}\n\nexport interface BlockUpstreams {\n /** All direct block dependencies */\n upstreams: Set<string>;\n /** Direct block dependencies which enrichments are also required by current block */\n upstreamsRequiringEnrichments: Set<string>;\n /** True if not-allowed references was encountered */\n missingReferences: boolean;\n}\n\n/** Extracts all resource ids referenced by args object. */\nexport function inferAllReferencedBlocks(args: unknown, allowed?: Set<string>): BlockUpstreams {\n const result = {\n upstreams: new Set<string>(),\n upstreamsRequiringEnrichments: new Set<string>(),\n missingReferences: false,\n };\n addAllReferencedBlocks(result, args, allowed);\n return result;\n}\n\nfunction addAllReferencedBlocks(result: BlockUpstreams, node: unknown, allowed?: Set<string>) {\n const type = typeof node;\n switch (type) {\n case \"function\":\n case \"bigint\":\n case \"number\":\n case \"boolean\":\n case \"symbol\":\n case \"undefined\":\n return;\n case \"string\": {\n // A reference can be hiding inside a string under any number of `JSON.stringify`\n // passes, and this walk would otherwise never reach it. Peeling is shared with the\n // column-id remapper so that \"how a value hides inside a string\" has one\n // definition; what happens at the bottom is not shared, and here it is deliberately\n // broad — anything the peel produced is walked, including a foreign-schema document\n // that merely contains a reference. That breadth is what makes this detector usable\n // as a guard over carriers the template codec declines to touch.\n const peeled = peelJsonLayers(node as string);\n if (peeled !== undefined) addAllReferencedBlocks(result, peeled.value, allowed);\n return;\n }\n case \"object\": {\n if (node === null) return;\n if (isBlockOutputReference(node)) {\n recordRef(result, node.blockId, node.requireEnrichments === true, allowed);\n } else if (Array.isArray(node)) {\n for (const child of node) addAllReferencedBlocks(result, child, allowed);\n } else {\n // Keys as well as values: a discovered column's `queriesQualifications` is keyed BY\n // column id, so a map key can carry a block id — and dropping the key loses that\n // upstream edge entirely.\n for (const [key, child] of Object.entries(node as object)) {\n addAllReferencedBlocks(result, key, allowed);\n addAllReferencedBlocks(result, child, allowed);\n }\n }\n\n return;\n }\n default:\n assertNever(type);\n }\n}\n\nfunction recordRef(\n result: BlockUpstreams,\n blockId: string,\n requireEnrichments: boolean,\n allowed?: Set<string>,\n) {\n if (allowed === undefined || allowed.has(blockId)) {\n result.upstreams.add(blockId);\n if (requireEnrichments) result.upstreamsRequiringEnrichments.add(blockId);\n } else result.missingReferences = true;\n}\n"],"mappings":";;;AAIA,SAAgB,UAAU,SAAiB,MAAc,oBAAqC;CAC5F,IAAI,oBAAoB,OAAO;EAAE,SAAS;EAAM;EAAS;EAAM;CAAmB;MAC7E,OAAO;EAAE,SAAS;EAAM;EAAS;CAAK;AAC7C;AAEA,SAAgB,uBAAuB,KAA4B;CACjE,OACE,OAAO,QAAQ,YACf,QAAQ,QACR,aAAa,OACb,IAAI,YAAY,QAChB,aAAa,OACb,UAAU;AAEd;;AAYA,SAAgB,yBAAyB,MAAe,SAAuC;CAC7F,MAAM,SAAS;EACb,2BAAW,IAAI,IAAY;EAC3B,+CAA+B,IAAI,IAAY;EAC/C,mBAAmB;CACrB;CACA,uBAAuB,QAAQ,MAAM,OAAO;CAC5C,OAAO;AACT;AAEA,SAAS,uBAAuB,QAAwB,MAAe,SAAuB;CAC5F,MAAM,OAAO,OAAO;CACpB,QAAQ,MAAR;EACE,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,aACH;EACF,KAAK,UAAU;GAQb,MAAM,SAAS,eAAe,IAAc;GAC5C,IAAI,WAAW,KAAA,GAAW,uBAAuB,QAAQ,OAAO,OAAO,OAAO;GAC9E;EACF;EACA,KAAK;GACH,IAAI,SAAS,MAAM;GACnB,IAAI,uBAAuB,IAAI,GAC7B,UAAU,QAAQ,KAAK,SAAS,KAAK,uBAAuB,MAAM,OAAO;QACpE,IAAI,MAAM,QAAQ,IAAI,GAC3B,KAAK,MAAM,SAAS,MAAM,uBAAuB,QAAQ,OAAO,OAAO;QAKvE,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,IAAc,GAAG;IACzD,uBAAuB,QAAQ,KAAK,OAAO;IAC3C,uBAAuB,QAAQ,OAAO,OAAO;GAC/C;GAGF;EAEF,SACE,YAAY,IAAI;CACpB;AACF;AAEA,SAAS,UACP,QACA,SACA,oBACA,SACA;CACA,IAAI,YAAY,KAAA,KAAa,QAAQ,IAAI,OAAO,GAAG;EACjD,OAAO,UAAU,IAAI,OAAO;EAC5B,IAAI,oBAAoB,OAAO,8BAA8B,IAAI,OAAO;CAC1E,OAAO,OAAO,oBAAoB;AACpC"}
1
+ {"version":3,"file":"args.js","names":[],"sources":["../../src/model/args.ts"],"sourcesContent":["import { assertNever } from \"@milaboratories/ts-helpers\";\nimport type { PlRef } from \"@platforma-sdk/model\";\nimport { asBlockId, peelJsonLayers } from \"@milaboratories/pl-model-common\";\nimport type { MissingBlockReference } from \"./args_derivation\";\n\nexport function outputRef(blockId: string, name: string, requireEnrichments?: boolean): PlRef {\n if (requireEnrichments) return { __isRef: true, blockId, name, requireEnrichments };\n else return { __isRef: true, blockId, name };\n}\n\nexport function isBlockOutputReference(obj: unknown): obj is PlRef {\n return (\n typeof obj === \"object\" &&\n obj !== null &&\n \"__isRef\" in obj &&\n obj.__isRef === true &&\n \"blockId\" in obj &&\n \"name\" in obj\n );\n}\n\nexport interface BlockUpstreams {\n /** All direct block dependencies */\n upstreams: Set<string>;\n /** Direct block dependencies which enrichments are also required by current block */\n upstreamsRequiringEnrichments: Set<string>;\n /** True if not-allowed references was encountered */\n missingReferences: boolean;\n}\n\n/** Extracts all resource ids referenced by args object. */\nexport function inferAllReferencedBlocks(args: unknown, allowed?: Set<string>): BlockUpstreams {\n const result = {\n upstreams: new Set<string>(),\n upstreamsRequiringEnrichments: new Set<string>(),\n missingReferences: false,\n };\n addAllReferencedBlocks(result, args, allowed);\n return result;\n}\n\nfunction addAllReferencedBlocks(result: BlockUpstreams, node: unknown, allowed?: Set<string>) {\n const type = typeof node;\n switch (type) {\n case \"function\":\n case \"bigint\":\n case \"number\":\n case \"boolean\":\n case \"symbol\":\n case \"undefined\":\n return;\n case \"string\": {\n // A reference can be hiding inside a string under any number of `JSON.stringify`\n // passes, and this walk would otherwise never reach it. Peeling is shared with the\n // column-id remapper so that \"how a value hides inside a string\" has one\n // definition; what happens at the bottom is not shared, and here it is deliberately\n // broad — anything the peel produced is walked, including a foreign-schema document\n // that merely contains a reference. That breadth is what makes this detector usable\n // as a guard over carriers the template codec declines to touch.\n const peeled = peelJsonLayers(node as string);\n if (peeled !== undefined) addAllReferencedBlocks(result, peeled.value, allowed);\n return;\n }\n case \"object\": {\n if (node === null) return;\n if (isBlockOutputReference(node)) {\n recordRef(result, node.blockId, node.requireEnrichments === true, allowed);\n } else if (Array.isArray(node)) {\n for (const child of node) addAllReferencedBlocks(result, child, allowed);\n } else {\n // Keys as well as values: a discovered column's `queriesQualifications` is keyed BY\n // column id, so a map key can carry a block id — and dropping the key loses that\n // upstream edge entirely.\n for (const [key, child] of Object.entries(node as object)) {\n addAllReferencedBlocks(result, key, allowed);\n addAllReferencedBlocks(result, child, allowed);\n }\n }\n\n return;\n }\n default:\n assertNever(type);\n }\n}\n\nfunction recordRef(\n result: BlockUpstreams,\n blockId: string,\n requireEnrichments: boolean,\n allowed?: Set<string>,\n) {\n if (allowed === undefined || allowed.has(blockId)) {\n result.upstreams.add(blockId);\n if (requireEnrichments) result.upstreamsRequiringEnrichments.add(blockId);\n } else result.missingReferences = true;\n}\n\n/**\n * The references in a block's args to blocks it cannot read from: blocks the project does not\n * have, and blocks that are not above it. Each block is listed once, in the order first found.\n *\n * @param args - The block's args\n * @param blockId - The block the args belong to\n * @param blockIdsInOrder - Every block of the project, top to bottom\n */\nexport function missingBlockReferences(\n args: unknown,\n blockId: string,\n blockIdsInOrder: readonly string[],\n): MissingBlockReference[] {\n const position = blockIdsInOrder.indexOf(blockId);\n const above = new Set(position < 0 ? [] : blockIdsInOrder.slice(0, position));\n const known = new Set(blockIdsInOrder);\n const missing: MissingBlockReference[] = [];\n for (const referenced of inferAllReferencedBlocks(args).upstreams) {\n if (above.has(referenced)) continue;\n missing.push({\n blockId: asBlockId(referenced),\n reason: known.has(referenced) ? \"not-above\" : \"unknown-block\",\n });\n }\n return missing;\n}\n"],"mappings":";;;AAKA,SAAgB,UAAU,SAAiB,MAAc,oBAAqC;CAC5F,IAAI,oBAAoB,OAAO;EAAE,SAAS;EAAM;EAAS;EAAM;CAAmB;MAC7E,OAAO;EAAE,SAAS;EAAM;EAAS;CAAK;AAC7C;AAEA,SAAgB,uBAAuB,KAA4B;CACjE,OACE,OAAO,QAAQ,YACf,QAAQ,QACR,aAAa,OACb,IAAI,YAAY,QAChB,aAAa,OACb,UAAU;AAEd;;AAYA,SAAgB,yBAAyB,MAAe,SAAuC;CAC7F,MAAM,SAAS;EACb,2BAAW,IAAI,IAAY;EAC3B,+CAA+B,IAAI,IAAY;EAC/C,mBAAmB;CACrB;CACA,uBAAuB,QAAQ,MAAM,OAAO;CAC5C,OAAO;AACT;AAEA,SAAS,uBAAuB,QAAwB,MAAe,SAAuB;CAC5F,MAAM,OAAO,OAAO;CACpB,QAAQ,MAAR;EACE,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,aACH;EACF,KAAK,UAAU;GAQb,MAAM,SAAS,eAAe,IAAc;GAC5C,IAAI,WAAW,KAAA,GAAW,uBAAuB,QAAQ,OAAO,OAAO,OAAO;GAC9E;EACF;EACA,KAAK;GACH,IAAI,SAAS,MAAM;GACnB,IAAI,uBAAuB,IAAI,GAC7B,UAAU,QAAQ,KAAK,SAAS,KAAK,uBAAuB,MAAM,OAAO;QACpE,IAAI,MAAM,QAAQ,IAAI,GAC3B,KAAK,MAAM,SAAS,MAAM,uBAA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