derive-dashboards 0.5.1 → 0.7.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 (342) hide show
  1. package/README.md +4 -2
  2. package/dist/body.mjs +2 -2
  3. package/dist/charts/data/buckets.d.mts +11 -0
  4. package/dist/charts/data/buckets.mjs +66 -0
  5. package/dist/charts/data/clean.mjs +55 -0
  6. package/dist/charts/data/format.d.mts +19 -0
  7. package/dist/charts/data/format.mjs +174 -0
  8. package/dist/charts/data/input-error.d.mts +5 -0
  9. package/dist/charts/data/input-error.mjs +11 -0
  10. package/dist/charts/data/range.mjs +14 -0
  11. package/dist/charts/data/rows.d.mts +13 -0
  12. package/dist/charts/data/rows.mjs +31 -0
  13. package/dist/charts/data/shares.mjs +55 -0
  14. package/dist/charts/data/types.d.mts +106 -0
  15. package/dist/charts/data/words.d.mts +44 -0
  16. package/dist/charts/data/words.mjs +31 -0
  17. package/dist/charts/describe.mjs +110 -0
  18. package/dist/charts/index.d.mts +34 -0
  19. package/dist/charts/index.mjs +24 -0
  20. package/dist/charts/kinds/area-marks.mjs +120 -0
  21. package/dist/charts/kinds/area.d.mts +23 -0
  22. package/dist/charts/kinds/area.mjs +71 -0
  23. package/dist/charts/kinds/bars-data.d.mts +11 -0
  24. package/dist/charts/kinds/bars-data.mjs +30 -0
  25. package/dist/charts/kinds/bars-layout.mjs +30 -0
  26. package/dist/charts/kinds/bars-marks.mjs +93 -0
  27. package/dist/charts/kinds/bars-words.mjs +56 -0
  28. package/dist/charts/kinds/bars.d.mts +17 -0
  29. package/dist/charts/kinds/bars.mjs +68 -0
  30. package/dist/charts/kinds/columns-data.mjs +116 -0
  31. package/dist/charts/kinds/columns-layout.mjs +29 -0
  32. package/dist/charts/kinds/columns-marks.mjs +200 -0
  33. package/dist/charts/kinds/columns-words.mjs +26 -0
  34. package/dist/charts/kinds/columns.d.mts +13 -0
  35. package/dist/charts/kinds/columns.mjs +80 -0
  36. package/dist/charts/kinds/dumbbell-data.d.mts +22 -0
  37. package/dist/charts/kinds/dumbbell-data.mjs +59 -0
  38. package/dist/charts/kinds/dumbbell-layout.mjs +113 -0
  39. package/dist/charts/kinds/dumbbell-marks.mjs +119 -0
  40. package/dist/charts/kinds/dumbbell-words.d.mts +7 -0
  41. package/dist/charts/kinds/dumbbell-words.mjs +94 -0
  42. package/dist/charts/kinds/dumbbell.d.mts +14 -0
  43. package/dist/charts/kinds/dumbbell.mjs +67 -0
  44. package/dist/charts/kinds/funnel-data.d.mts +11 -0
  45. package/dist/charts/kinds/funnel-data.mjs +70 -0
  46. package/dist/charts/kinds/funnel-layout.mjs +32 -0
  47. package/dist/charts/kinds/funnel-marks.mjs +204 -0
  48. package/dist/charts/kinds/funnel-words.mjs +77 -0
  49. package/dist/charts/kinds/funnel.d.mts +11 -0
  50. package/dist/charts/kinds/funnel.mjs +75 -0
  51. package/dist/charts/kinds/grid-data.d.mts +18 -0
  52. package/dist/charts/kinds/grid-data.mjs +35 -0
  53. package/dist/charts/kinds/grid-layout.mjs +77 -0
  54. package/dist/charts/kinds/grid-marks.mjs +140 -0
  55. package/dist/charts/kinds/grid-time.mjs +41 -0
  56. package/dist/charts/kinds/grid-words.mjs +86 -0
  57. package/dist/charts/kinds/grid.d.mts +22 -0
  58. package/dist/charts/kinds/grid.mjs +55 -0
  59. package/dist/charts/kinds/histogram-data.mjs +142 -0
  60. package/dist/charts/kinds/histogram-layout.mjs +144 -0
  61. package/dist/charts/kinds/histogram-marks.mjs +110 -0
  62. package/dist/charts/kinds/histogram-words.mjs +104 -0
  63. package/dist/charts/kinds/histogram.d.mts +15 -0
  64. package/dist/charts/kinds/histogram.mjs +47 -0
  65. package/dist/charts/kinds/line-marks.mjs +36 -0
  66. package/dist/charts/kinds/line.d.mts +17 -0
  67. package/dist/charts/kinds/line.mjs +52 -0
  68. package/dist/charts/kinds/multiples-grid.mjs +107 -0
  69. package/dist/charts/kinds/multiples-head.mjs +44 -0
  70. package/dist/charts/kinds/multiples-layout.mjs +159 -0
  71. package/dist/charts/kinds/multiples-marks.mjs +95 -0
  72. package/dist/charts/kinds/multiples-words.mjs +110 -0
  73. package/dist/charts/kinds/multiples.d.mts +26 -0
  74. package/dist/charts/kinds/multiples.mjs +42 -0
  75. package/dist/charts/kinds/row-chart.mjs +112 -0
  76. package/dist/charts/kinds/row-marks.mjs +121 -0
  77. package/dist/charts/kinds/scatter-data.d.mts +18 -0
  78. package/dist/charts/kinds/scatter-data.mjs +123 -0
  79. package/dist/charts/kinds/scatter-layout.mjs +189 -0
  80. package/dist/charts/kinds/scatter-marks.mjs +149 -0
  81. package/dist/charts/kinds/scatter-words.mjs +80 -0
  82. package/dist/charts/kinds/scatter.d.mts +21 -0
  83. package/dist/charts/kinds/scatter.mjs +72 -0
  84. package/dist/charts/kinds/stat-data.mjs +91 -0
  85. package/dist/charts/kinds/stat-layout.mjs +108 -0
  86. package/dist/charts/kinds/stat-marks.mjs +85 -0
  87. package/dist/charts/kinds/stat-words.mjs +57 -0
  88. package/dist/charts/kinds/stat.d.mts +28 -0
  89. package/dist/charts/kinds/stat.mjs +35 -0
  90. package/dist/charts/kinds/time-chart.mjs +74 -0
  91. package/dist/charts/kinds/time-frame.mjs +113 -0
  92. package/dist/charts/kinds/time-notes.mjs +36 -0
  93. package/dist/charts/kinds/time-plan.d.mts +8 -0
  94. package/dist/charts/kinds/time-plan.mjs +57 -0
  95. package/dist/charts/kinds/time-plot.mjs +56 -0
  96. package/dist/charts/kinds/time-stops.mjs +56 -0
  97. package/dist/charts/kinds/value-plot.mjs +33 -0
  98. package/dist/charts/kinds/waterfall-data.d.mts +7 -0
  99. package/dist/charts/kinds/waterfall-data.mjs +71 -0
  100. package/dist/charts/kinds/waterfall-layout.mjs +38 -0
  101. package/dist/charts/kinds/waterfall-marks.mjs +82 -0
  102. package/dist/charts/kinds/waterfall-words.mjs +118 -0
  103. package/dist/charts/kinds/waterfall.d.mts +17 -0
  104. package/dist/charts/kinds/waterfall.mjs +66 -0
  105. package/dist/charts/layout/boxes.mjs +28 -0
  106. package/dist/charts/layout/dodge.mjs +75 -0
  107. package/dist/charts/layout/extent.mjs +25 -0
  108. package/dist/charts/layout/frame.mjs +97 -0
  109. package/dist/charts/layout/place-labels.mjs +101 -0
  110. package/dist/charts/layout/stack.mjs +31 -0
  111. package/dist/charts/marks/axes.mjs +137 -0
  112. package/dist/charts/marks/dot.mjs +31 -0
  113. package/dist/charts/marks/end-labels.mjs +124 -0
  114. package/dist/charts/marks/legend.mjs +141 -0
  115. package/dist/charts/marks/lines.d.mts +2 -0
  116. package/dist/charts/marks/lines.mjs +38 -0
  117. package/dist/charts/marks/lit.d.mts +3 -0
  118. package/dist/charts/marks/lit.mjs +63 -0
  119. package/dist/charts/marks/more.mjs +18 -0
  120. package/dist/charts/marks/notes.mjs +253 -0
  121. package/dist/charts/marks/series-line.mjs +110 -0
  122. package/dist/charts/marks/thin.mjs +138 -0
  123. package/dist/charts/marks/x-axis.mjs +32 -0
  124. package/dist/charts/scale/calendar.mjs +174 -0
  125. package/dist/charts/scale/time-ticks.mjs +154 -0
  126. package/dist/charts/scale/time.mjs +33 -0
  127. package/dist/charts/scale/value.mjs +161 -0
  128. package/dist/charts/scene/defs.mjs +33 -0
  129. package/dist/charts/scene/markup.mjs +35 -0
  130. package/dist/charts/scene/nearest.d.mts +11 -0
  131. package/dist/charts/scene/nearest.mjs +49 -0
  132. package/dist/charts/scene/paint.d.mts +12 -0
  133. package/dist/charts/scene/paint.mjs +13 -0
  134. package/dist/charts/scene/svg.d.mts +18 -0
  135. package/dist/charts/scene/svg.mjs +185 -0
  136. package/dist/charts/scene/types.d.mts +177 -0
  137. package/dist/charts/text/measure.d.mts +9 -0
  138. package/dist/charts/text/measure.mjs +123 -0
  139. package/dist/charts/theme.d.mts +47 -0
  140. package/dist/charts/theme.mjs +239 -0
  141. package/dist/charts-react/Chart.d.mts +91 -0
  142. package/dist/charts-react/Chart.mjs +166 -0
  143. package/dist/charts-react/Overlay.mjs +171 -0
  144. package/dist/charts-react/Picture.mjs +22 -0
  145. package/dist/charts-react/Reader.mjs +147 -0
  146. package/dist/charts-react/bucket-names.mjs +29 -0
  147. package/dist/charts-react/chart-unlayered.css +1 -0
  148. package/dist/charts-react/chart-unlayered.css.d.ts +1 -0
  149. package/dist/charts-react/chart.css +1 -0
  150. package/dist/charts-react/chart.css.d.ts +1 -0
  151. package/dist/charts-react/events.mjs +72 -0
  152. package/dist/charts-react/exact-sum.mjs +24 -0
  153. package/dist/charts-react/from-answer.mjs +185 -0
  154. package/dist/charts-react/index.d.mts +3 -0
  155. package/dist/charts-react/index.mjs +4 -0
  156. package/dist/charts-react/keys.mjs +117 -0
  157. package/dist/charts-react/opens.mjs +28 -0
  158. package/dist/charts-react/plan-bars.mjs +95 -0
  159. package/dist/charts-react/plan-funnel.mjs +120 -0
  160. package/dist/charts-react/plan-grid.mjs +121 -0
  161. package/dist/charts-react/plan-parts.d.mts +2 -0
  162. package/dist/charts-react/plan-parts.mjs +30 -0
  163. package/dist/charts-react/plan-time.mjs +84 -0
  164. package/dist/charts-react/plan.d.mts +3 -0
  165. package/dist/charts-react/plan.mjs +37 -0
  166. package/dist/charts-react/point-keys.mjs +22 -0
  167. package/dist/charts-react/pointer.mjs +99 -0
  168. package/dist/charts-react/reader-page.d.mts +8 -0
  169. package/dist/charts-react/reader-page.mjs +116 -0
  170. package/dist/charts-react/tiles.d.mts +24 -0
  171. package/dist/charts-react/tiles.mjs +142 -0
  172. package/dist/charts-react/tip-place.mjs +107 -0
  173. package/dist/charts-react/tip-room.mjs +31 -0
  174. package/dist/charts-react/useClientLayoutEffect.mjs +8 -0
  175. package/dist/charts-react/useScene.mjs +76 -0
  176. package/dist/charts-react/useSize.mjs +44 -0
  177. package/dist/charts-react/useStop.mjs +68 -0
  178. package/dist/core/conformance/memory/aggregate.mjs +2 -1
  179. package/dist/core/conformance/memory/rows.mjs +1 -1
  180. package/dist/core/conformance/memory/source.mjs +3 -2
  181. package/dist/core/document/display-comparisons.mjs +49 -0
  182. package/dist/core/document/display-entries.mjs +2 -1
  183. package/dist/core/document/display-validation.d.mts +12 -0
  184. package/dist/core/document/display-validation.mjs +56 -0
  185. package/dist/core/document/display.d.mts +27 -12
  186. package/dist/core/document/display.mjs +40 -49
  187. package/dist/core/document/view-layout.mjs +16 -0
  188. package/dist/core/engine/access.mjs +4 -2
  189. package/dist/core/engine/context.mjs +5 -7
  190. package/dist/core/engine/custom/cells.mjs +1 -1
  191. package/dist/core/engine/custom/records.mjs +2 -1
  192. package/dist/core/engine/dialect.d.mts +2 -0
  193. package/dist/core/engine/index.mjs +1 -1
  194. package/dist/core/engine/levels.mjs +33 -1
  195. package/dist/core/engine/plan.d.mts +2 -0
  196. package/dist/core/engine/postgres.mjs +1 -0
  197. package/dist/core/engine/rank.mjs +1 -1
  198. package/dist/core/engine/reader.d.mts +4 -0
  199. package/dist/core/engine/sql-partition.mjs +45 -0
  200. package/dist/core/engine/sql-reads.mjs +4 -1
  201. package/dist/core/engine/statement.mjs +4 -4
  202. package/dist/core/engine/version.d.mts +1 -1
  203. package/dist/core/engine/version.mjs +1 -1
  204. package/dist/core/error/codes.d.mts +1 -1
  205. package/dist/core/error/codes.mjs +1 -1
  206. package/dist/core/exact/decimal.d.mts +1 -4
  207. package/dist/core/exact/decimal.mjs +1 -14
  208. package/dist/core/exact/text.d.mts +5 -0
  209. package/dist/core/exact/text.mjs +19 -0
  210. package/dist/core/index.d.mts +9 -6
  211. package/dist/core/index.mjs +11 -8
  212. package/dist/core/model/types.d.mts +2 -0
  213. package/dist/core/model/validate/fields.mjs +1 -1
  214. package/dist/core/model/validate/known-members.mjs +2 -0
  215. package/dist/core/model/validate/shape.mjs +5 -1
  216. package/dist/core/oracle/visibility.mjs +2 -2
  217. package/dist/core/policy/bind.mjs +29 -1
  218. package/dist/core/query/drill/compose.d.mts +2 -0
  219. package/dist/core/query/drill/compose.mjs +1 -1
  220. package/dist/core/query/drill/synthetic.mjs +2 -1
  221. package/dist/core/query/drill/types.d.mts +7 -0
  222. package/dist/core/query/drill/types.mjs +3 -1
  223. package/dist/core/result/number.mjs +2 -1
  224. package/dist/core/result/time-axis.mjs +2 -1
  225. package/dist/core/schema/answers.d.mts +1176 -362
  226. package/dist/core/schema/commands.mjs +2 -1
  227. package/dist/core/schema/document.mjs +27 -2
  228. package/dist/core/schema/drill-answers.mjs +19 -1
  229. package/dist/core/schema/drill.d.mts +21 -6
  230. package/dist/core/schema/drill.mjs +7 -1
  231. package/dist/core/schema/index.d.mts +2 -1
  232. package/dist/core/schema/index.mjs +2 -1
  233. package/dist/core/schema/null-ops.d.mts +6 -0
  234. package/dist/core/schema/null-ops.mjs +18 -0
  235. package/dist/core/schema/query.mjs +30 -4
  236. package/dist/core/schema/tools.mjs +13 -0
  237. package/dist/core/schema/union-error.mjs +38 -0
  238. package/dist/core/time/bucket.mjs +2 -1
  239. package/dist/core/time/index.d.mts +3 -2
  240. package/dist/core/time/instant.d.mts +1 -2
  241. package/dist/core/time/instant.mjs +2 -30
  242. package/dist/core/time/range.mjs +2 -1
  243. package/dist/core/time/stored.mjs +2 -1
  244. package/dist/core/time/text-time.d.mts +10 -0
  245. package/dist/core/time/text-time.mjs +80 -0
  246. package/dist/core/types/api.d.mts +1 -1
  247. package/dist/core/types/api.mjs +1 -1
  248. package/dist/core/types/drill-api.d.mts +22 -2
  249. package/dist/dashboard-documents.d.mts +8 -0
  250. package/dist/dashboard-documents.mjs +52 -0
  251. package/dist/drill-access.mjs +40 -3
  252. package/dist/drill-comparison.mjs +71 -0
  253. package/dist/drill-crumbs.mjs +21 -2
  254. package/dist/drill-group-seal.mjs +13 -0
  255. package/dist/drill-open.mjs +7 -1
  256. package/dist/drill-seal.mjs +4 -6
  257. package/dist/drill-views.mjs +5 -13
  258. package/dist/drill.mjs +28 -58
  259. package/dist/editing.mjs +1 -1
  260. package/dist/endpoints/drill.mjs +2 -3
  261. package/dist/endpoints/index.d.mts +20 -0
  262. package/dist/index.d.mts +2 -1
  263. package/dist/index.mjs +6 -27
  264. package/dist/llms.txt +1 -1
  265. package/dist/react/Cell.mjs +2 -2
  266. package/dist/react/DashboardList.mjs +3 -3
  267. package/dist/react/Editing.mjs +3 -3
  268. package/dist/react/Header.mjs +2 -2
  269. package/dist/react/History.mjs +3 -3
  270. package/dist/react/Parts.mjs +2 -2
  271. package/dist/react/RowBody.d.mts +2 -4
  272. package/dist/react/RowBody.mjs +14 -39
  273. package/dist/react/Tile.mjs +4 -5
  274. package/dist/react/builder/FilterEditor.mjs +2 -2
  275. package/dist/react/builder/Picker.mjs +3 -3
  276. package/dist/react/builder/RangeEditor.mjs +3 -3
  277. package/dist/react/builder/Time.mjs +3 -3
  278. package/dist/react/builder/Values.mjs +2 -2
  279. package/dist/react/builder/WidgetBuilder.mjs +2 -2
  280. package/dist/react/digits.mjs +1 -1
  281. package/dist/react/drill/Bars.mjs +34 -38
  282. package/dist/react/drill/Comparison.mjs +204 -0
  283. package/dist/react/drill/Controls.mjs +8 -12
  284. package/dist/react/drill/DrillDrawer.mjs +42 -0
  285. package/dist/react/drill/DrillPanel.mjs +61 -41
  286. package/dist/react/drill/Header.mjs +65 -35
  287. package/dist/react/drill/ItemLink.mjs +40 -0
  288. package/dist/react/drill/Items.d.mts +2 -1
  289. package/dist/react/drill/Items.mjs +126 -99
  290. package/dist/react/drill/ItemsBody.mjs +38 -0
  291. package/dist/react/drill/ItemsFoot.mjs +24 -19
  292. package/dist/react/drill/Notes.mjs +23 -13
  293. package/dist/react/drill/PanelBody.mjs +7 -25
  294. package/dist/react/drill/RecordRow.mjs +47 -0
  295. package/dist/react/drill/Trend.mjs +186 -0
  296. package/dist/react/drill/board.mjs +21 -11
  297. package/dist/react/drill/drawer-context.mjs +5 -0
  298. package/dist/react/drill/item-parts.mjs +53 -35
  299. package/dist/react/drill/item-type.d.mts +2 -2
  300. package/dist/react/drill/lazy.d.mts +1 -4
  301. package/dist/react/drill/lazy.mjs +29 -11
  302. package/dist/react/drill/panel-focus.mjs +18 -7
  303. package/dist/react/drill/props.d.mts +7 -1
  304. package/dist/react/drill/props.mjs +39 -8
  305. package/dist/react/drill/record-page.mjs +61 -0
  306. package/dist/react/drill/state.mjs +17 -2
  307. package/dist/react/drill/trend-touch.mjs +33 -0
  308. package/dist/react/drill/useDrill.d.mts +3 -3
  309. package/dist/react/drill/useDrill.mjs +35 -15
  310. package/dist/react/drill/words.d.mts +14 -0
  311. package/dist/react/drill/words.mjs +20 -8
  312. package/dist/react/english.d.mts +4 -1
  313. package/dist/react/english.mjs +17 -2
  314. package/dist/react/filters/chip.mjs +2 -2
  315. package/dist/react/filters/menu-search.mjs +5 -5
  316. package/dist/react/filters/menu.mjs +2 -2
  317. package/dist/react/filters/number.mjs +2 -2
  318. package/dist/react/filters/operator.mjs +2 -2
  319. package/dist/react/filters/phone.mjs +2 -2
  320. package/dist/react/filters/values.mjs +4 -4
  321. package/dist/react/format-time.d.mts +2 -1
  322. package/dist/react/format-time.mjs +42 -34
  323. package/dist/react/format.mjs +15 -3
  324. package/dist/react/index.d.mts +4 -3
  325. package/dist/react/index.mjs +4 -3
  326. package/dist/react/messages.d.mts +23 -2
  327. package/dist/react/renderers/entries.d.mts +11 -0
  328. package/dist/react/renderers/list.mjs +8 -10
  329. package/dist/react/renderers/table-cells.mjs +38 -9
  330. package/dist/react/renderers/table-measure.mjs +2 -0
  331. package/dist/react/renderers/table-row.mjs +64 -7
  332. package/dist/react/renderers/table-sort.mjs +2 -8
  333. package/dist/react/renderers/table.mjs +16 -15
  334. package/dist/react/series.mjs +8 -3
  335. package/dist/react/theme-unlayered.css +1 -3144
  336. package/dist/react/theme.css +1 -3146
  337. package/dist/recharts/parts.mjs +1 -1
  338. package/dist/skills/derive-dashboards/SKILL.md +10 -5
  339. package/dist/store/apply.mjs +2 -2
  340. package/dist/store/editor.mjs +2 -2
  341. package/dist/store/writer.mjs +2 -2
  342. package/package.json +27 -2
@@ -0,0 +1,49 @@
1
+ import { resolveDimension } from "../model/member.mjs";
2
+ import { nearest } from "../model/validate/fields.mjs";
3
+ import { drillMembers, drillableMembers } from "../query/drill/fields.mjs";
4
+ // src/document/display-comparisons.ts
5
+ function fieldReason(model, source, field, choices) {
6
+ const dimension = resolveDimension(model, source, field)?.dimension;
7
+ if (dimension) {
8
+ if (dimension.sensitive === true || dimension.hidden === true || dimension.drill === false) return "This dimension is excluded from drill-downs.";
9
+ if (dimension.kind === "time") return "Time fields belong in the Over time view.";
10
+ if (drillableMembers(model, source, () => true).includes(field)) return "Add the field to the source’s drill.prefer list to enable it.";
11
+ }
12
+ const near = choices.find((id) => id === `${source}.${field}`) ?? nearest(field, choices);
13
+ return near === void 0 ? "" : `Did you mean "${near}"?`;
14
+ }
15
+ /** Only the host registering its own model receives suggested model members. */
16
+ function authorHint(model, source, field, groups) {
17
+ const { fixed, choices } = groups;
18
+ return [fixed.has(field) ? "The table already groups by it." : fieldReason(model, source, field, choices), choices.length > 0 ? `Available fields: ${choices.join(", ")}.` : "The model offers no other breakdown fields."].filter(Boolean).join(" ");
19
+ }
20
+ /** A row's sealed selection fixes its grouping fields. Its comparison needs another breakdown field. */
21
+ function comparisonIssues(query, comparisons, model, options) {
22
+ const { path, author } = options;
23
+ const groups = query?.dimensions ?? [];
24
+ if (!query || groups.length === 0) return [{
25
+ code: "DISPLAY_INVALID",
26
+ path,
27
+ message: "A table with comparisons needs a grouping dimension in its main query."
28
+ }];
29
+ const offered = drillMembers(model, query.source, () => true);
30
+ const fixed = new Set(groups.map((group) => group.member));
31
+ const choices = offered.filter((field) => !fixed.has(field));
32
+ return Object.entries(comparisons).flatMap(([key, fields]) => fields.flatMap((field, index) => {
33
+ if (choices.includes(field)) return [];
34
+ const hint = author ? authorHint(model, query.source, field, {
35
+ fixed,
36
+ choices
37
+ }) : "Open Breakdown on this source to see the fields available to you.";
38
+ return {
39
+ code: "DISPLAY_INVALID",
40
+ path: [
41
+ ...path,
42
+ key,
43
+ index
44
+ ],
45
+ message: `The comparison for "${key}" cannot group by "${field}". ${hint}`
46
+ };
47
+ }));
48
+ }
49
+ export { comparisonIssues };
@@ -3,7 +3,8 @@ import { entryAliases } from "../query/to-wire.mjs";
3
3
  /** The options of each kind that name values of the widget's query. */
4
4
  const NAMES_VALUES = {
5
5
  number: ["value", "secondary"],
6
- table: ["bars", "heat"]
6
+ table: ["bars", "heat"],
7
+ bar: ["against"]
7
8
  };
8
9
  /** The names an option holds: its text, or each text of its list; none for a value of another type, which its type check refuses. */
9
10
  function namesIn(value) {
@@ -0,0 +1,12 @@
1
+ import { Model } from "../model/types.mjs";
2
+ import { Issue } from "../types/primitives.mjs";
3
+ import { Doc } from "./types.mjs";
4
+ /** Check each widget's options. A host with a model also checks the drill fields its rows can open. */
5
+ export declare function documentDisplayIssues(doc: Doc, model?: Model): Issue[];
6
+ /**
7
+ * What an edit did wrong to a display: each option of the wrong type that the edit set or
8
+ * changed, at `widgets.<id>.display.<option>`. Only what the edit touched is checked: an option
9
+ * stored before options were typed stays as it is, so it never blocks an edit of something
10
+ * else, and replaying a draft's own commands never refuses one that was accepted.
11
+ */
12
+ export declare function editedDisplayIssues(before: Doc, after: Doc, model?: Model): Issue[];
@@ -0,0 +1,56 @@
1
+ import { canonical } from "../value/canonical.mjs";
2
+ import { displayIssues, displayOf, isRecord } from "./display.mjs";
3
+ import { comparisonIssues } from "./display-comparisons.mjs";
4
+ import { displayEntryIssues } from "./display-entries.mjs";
5
+ // src/document/display-validation.ts
6
+ /**
7
+ * What is wrong with one widget's display: each option of the wrong type for its kind, and
8
+ * each option that names a value its query does not have (`displayEntryIssues`).
9
+ */
10
+ function widgetDisplayIssues(widget, path, model, author = false) {
11
+ const issues = [...displayIssues(widget.kind, widget.display, path), ...displayEntryIssues(widget, path)];
12
+ if (!model || widget.kind !== "table") return issues;
13
+ const { comparisons } = displayOf("table", widget.display);
14
+ if (!comparisons || Object.keys(comparisons).length === 0) return issues;
15
+ return [...issues, ...comparisonIssues(widget.queries["main"], comparisons, model, {
16
+ path: [...path, "comparisons"],
17
+ author
18
+ })];
19
+ }
20
+ /** Check each widget's options. A host with a model also checks the drill fields its rows can open. */
21
+ function documentDisplayIssues(doc, model) {
22
+ return Object.entries(doc.widgets).flatMap(([id, widget]) => widgetDisplayIssues(widget, [
23
+ "widgets",
24
+ id,
25
+ "display"
26
+ ], model, true));
27
+ }
28
+ /** An option as an object holds it: none when it has no such option, which is not the same as holding null. */
29
+ const held = (display, option) => Object.hasOwn(display, option) ? { is: display[option] ?? null } : null;
30
+ /**
31
+ * What an edit did wrong to a display: each option of the wrong type that the edit set or
32
+ * changed, at `widgets.<id>.display.<option>`. Only what the edit touched is checked: an option
33
+ * stored before options were typed stays as it is, so it never blocks an edit of something
34
+ * else, and replaying a draft's own commands never refuses one that was accepted.
35
+ */
36
+ function editedDisplayIssues(before, after, model) {
37
+ return Object.entries(after.widgets).flatMap(([id, widget]) => {
38
+ const prior = before.widgets[id];
39
+ if (prior === widget) return [];
40
+ const path = [
41
+ "widgets",
42
+ id,
43
+ "display"
44
+ ];
45
+ const was = prior?.display;
46
+ const old = isRecord(was) ? was : {};
47
+ const now = widget.display;
48
+ const previous = prior ? widgetDisplayIssues(prior, path, model) : [];
49
+ return widgetDisplayIssues(widget, path, model).filter((found) => {
50
+ const option = String(found.path[path.length] ?? "display");
51
+ const [set, had] = isRecord(now) ? [held(now, option), held(old, option)] : [now, was];
52
+ return canonical(set ?? null) !== canonical(had ?? null) || option === "comparisons" && !previous.some((issue) => canonical(issue.path) === canonical(found.path));
53
+ });
54
+ });
55
+ }
56
+ export { documentDisplayIssues, editedDisplayIssues };
@@ -1,6 +1,8 @@
1
1
  import { Issue } from "../types/primitives.mjs";
2
- import { Doc } from "./types.mjs";
3
- type ValueOf<T> = T extends "boolean" ? boolean : T extends "text" ? string : T extends "texts" ? readonly string[] : Readonly<Record<string, string>>;
2
+ /** The first field groups the selected row; the second optionally groups its children. */
3
+ type DistinctPair<First extends string, All extends string = First> = First extends unknown ? readonly [First, Exclude<All, First>] : never;
4
+ export type ComparisonFields<Field extends string = string> = readonly [Field] | (string extends Field ? readonly [Field, Field] : DistinctPair<Field>);
5
+ type ValueOf<T, Field extends string = string> = T extends "comparisons" ? Readonly<Record<string, ComparisonFields<Field>>> : T extends "boolean" ? boolean : T extends "text" ? string : T extends "texts" ? readonly string[] : Readonly<Record<string, string>>;
4
6
  /** Options every widget takes. */
5
7
  declare const SHARED: {
6
8
  readonly keepDateRange: "boolean";
@@ -18,6 +20,7 @@ export declare const DISPLAY_OPTIONS: {
18
20
  readonly bars: "texts";
19
21
  readonly heat: "texts";
20
22
  readonly compact: "boolean";
23
+ readonly comparisons: "comparisons";
21
24
  readonly keepDateRange: "boolean";
22
25
  readonly empty: "text";
23
26
  };
@@ -36,24 +39,43 @@ export declare const DISPLAY_OPTIONS: {
36
39
  readonly empty: "text";
37
40
  };
38
41
  readonly bar: {
42
+ readonly against: "text";
39
43
  readonly colors: "colors";
40
44
  readonly keepDateRange: "boolean";
41
45
  readonly empty: "text";
42
46
  };
47
+ readonly area: {
48
+ readonly share: "boolean";
49
+ readonly colors: "colors";
50
+ readonly keepDateRange: "boolean";
51
+ readonly empty: "text";
52
+ };
53
+ readonly multiples: {
54
+ readonly ownScales: "boolean";
55
+ readonly colors: "colors";
56
+ readonly keepDateRange: "boolean";
57
+ readonly empty: "text";
58
+ };
59
+ readonly heatmap: {
60
+ readonly keepDateRange: "boolean";
61
+ readonly empty: "text";
62
+ };
63
+ readonly funnel: {
64
+ readonly keepDateRange: "boolean";
65
+ readonly empty: "text";
66
+ };
43
67
  };
44
68
  /** What each option does, for the builder and for an agent reading a tool definition. */
45
69
  export declare const DISPLAY_HELP: Readonly<Record<string, string>>;
46
70
  /** A kind of widget the library draws. A host's own kind is not here: its options are its own. */
47
71
  export type DisplayKind = keyof typeof DISPLAY_OPTIONS;
48
72
  /** The options a kind of widget reads, each of its type. */
49
- export type DisplayOf<K extends DisplayKind> = { readonly [P in keyof (typeof DISPLAY_OPTIONS)[K]]?: ValueOf<(typeof DISPLAY_OPTIONS)[K][P]>; };
73
+ export type DisplayOf<K extends DisplayKind, Field extends string = string> = { readonly [P in keyof (typeof DISPLAY_OPTIONS)[K]]?: ValueOf<(typeof DISPLAY_OPTIONS)[K][P], Field>; };
50
74
  /**
51
75
  * What is wrong with a widget's display for its kind: each option of the wrong type, at its
52
76
  * path. None for a kind the library does not draw, and none for an option it does not know.
53
77
  */
54
78
  export declare function displayIssues(kind: string, display: unknown, path?: Issue["path"]): Issue[];
55
- /** Every widget of a document whose display does not fit its kind or its query, at `widgets.<id>.display`. */
56
- export declare function documentDisplayIssues(doc: Doc): Issue[];
57
79
  /**
58
80
  * A widget's options as its kind reads them: each option it knows, when it is of its type. A
59
81
  * stored option of the wrong type (written before options were checked, or by hand) is left
@@ -61,13 +83,6 @@ export declare function documentDisplayIssues(doc: Doc): Issue[];
61
83
  * colours that are text are kept.
62
84
  */
63
85
  export declare function displayOf<K extends DisplayKind>(kind: K, display: unknown): DisplayOf<K>;
64
- /**
65
- * What an edit did wrong to a display: each option of the wrong type that the edit set or
66
- * changed, at `widgets.<id>.display.<option>`. Only what the edit touched is checked: an option
67
- * stored before options were typed stays as it is, so it never blocks an edit of something
68
- * else, and replaying a draft's own commands never refuses one that was accepted.
69
- */
70
- export declare function editedDisplayIssues(before: Doc, after: Doc): Issue[];
71
86
  /** The options every widget takes, whatever draws it. */
72
87
  export type SharedDisplay = { readonly [P in keyof typeof SHARED]?: ValueOf<(typeof SHARED)[P]>; };
73
88
  /** The options every widget takes, whatever draws it: `keepDateRange` and `empty`, and no option of any one kind. */
@@ -1,5 +1,3 @@
1
- import { canonical } from "../value/canonical.mjs";
2
- import { displayEntryIssues } from "./display-entries.mjs";
3
1
  // src/document/display.ts
4
2
  /** Options every widget takes. */
5
3
  const SHARED = {
@@ -21,7 +19,8 @@ const DISPLAY_OPTIONS = {
21
19
  ...SHARED,
22
20
  bars: "texts",
23
21
  heat: "texts",
24
- compact: "boolean"
22
+ compact: "boolean",
23
+ comparisons: "comparisons"
25
24
  },
26
25
  list: {
27
26
  ...SHARED,
@@ -32,7 +31,20 @@ const DISPLAY_OPTIONS = {
32
31
  href: "text"
33
32
  },
34
33
  line: CHART,
35
- bar: CHART
34
+ bar: {
35
+ ...CHART,
36
+ against: "text"
37
+ },
38
+ area: {
39
+ ...CHART,
40
+ share: "boolean"
41
+ },
42
+ multiples: {
43
+ ...CHART,
44
+ ownScales: "boolean"
45
+ },
46
+ heatmap: SHARED,
47
+ funnel: SHARED
36
48
  };
37
49
  /** What each option does, for the builder and for an agent reading a tool definition. */
38
50
  const DISPLAY_HELP = {
@@ -42,8 +54,12 @@ const DISPLAY_HELP = {
42
54
  secondary: "A query entry a number tile shows as a second, smaller figure under the number, beside its label.",
43
55
  bars: "Query entries drawn with a bar in each cell, as long as the value is beside the column's largest.",
44
56
  heat: "Query entries whose cells are shaded by their value, darker for more.",
57
+ comparisons: "For a value of the first grouping column, name the drill field for its comparison table and optionally the field to expand each group by.",
45
58
  compact: "Draw a table's rows closer together.",
46
59
  colors: "A colour per series, by the series' label: any CSS colour.",
60
+ against: "The query entry each bar is read against (a goal, the average of all): drawn as a tick on the bar, not as a bar of its own.",
61
+ share: "Draw each band of an area chart as its share of the whole, the stack filling the plot.",
62
+ ownScales: "Give each small chart its own value axis; without it they share one, so their heights compare.",
47
63
  title: "The query entry shown as the row's title.",
48
64
  subtitle: "Query entries shown under the title, joined with a dot.",
49
65
  avatar: "The query entry whose initials make the row's avatar.",
@@ -52,6 +68,7 @@ const DISPLAY_HELP = {
52
68
  };
53
69
  const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
54
70
  const isText = (value) => typeof value === "string";
71
+ const isComparisonFields = (fields) => Array.isArray(fields) && fields.length > 0 && fields.length <= 2 && Array.from(fields).every((field) => typeof field === "string" && field.trim().length > 0) && new Set(fields).size === fields.length;
55
72
  const TYPES = {
56
73
  boolean: {
57
74
  fits: (v) => typeof v === "boolean",
@@ -65,6 +82,10 @@ const TYPES = {
65
82
  fits: (v) => Array.isArray(v) && v.every((entry) => isText(entry)),
66
83
  takes: "a list of texts"
67
84
  },
85
+ comparisons: {
86
+ fits: (v) => isRecord(v) && Object.values(v).every((fields) => isComparisonFields(fields)),
87
+ takes: "one or two distinct drill fields for each group value"
88
+ },
68
89
  colors: {
69
90
  fits: (v) => isRecord(v) && Object.values(v).every((color) => isText(color)),
70
91
  takes: "a colour for each series label, as text",
@@ -96,26 +117,20 @@ function displayIssues(kind, display, path = []) {
96
117
  message: "display is an object of options.",
97
118
  path
98
119
  }];
99
- return misfits(options, display).map(([name, type]) => ({
100
- code: "DISPLAY_INVALID",
101
- message: `A ${kind}'s ${name} is ${TYPES[type].takes}.`,
102
- path: [...path, name]
103
- }));
104
- }
105
- /**
106
- * What is wrong with one widget's display: each option of the wrong type for its kind, and
107
- * each option that names a value its query does not have (`displayEntryIssues`).
108
- */
109
- function widgetDisplayIssues(widget, path) {
110
- return [...displayIssues(widget.kind, widget.display, path), ...displayEntryIssues(widget, path)];
111
- }
112
- /** Every widget of a document whose display does not fit its kind or its query, at `widgets.<id>.display`. */
113
- function documentDisplayIssues(doc) {
114
- return Object.entries(doc.widgets).flatMap(([id, widget]) => widgetDisplayIssues(widget, [
115
- "widgets",
116
- id,
117
- "display"
118
- ]));
120
+ return misfits(options, display).flatMap(([name, type]) => {
121
+ const value = display[name];
122
+ const pathToOption = [...path, name];
123
+ if (type === "comparisons" && isRecord(value)) return Object.keys(value).filter((key) => !isComparisonFields(value[key])).map((key) => ({
124
+ code: "DISPLAY_INVALID",
125
+ message: `The comparison for "${key}" needs one or two distinct drill fields.`,
126
+ path: [...pathToOption, key]
127
+ }));
128
+ return [{
129
+ code: "DISPLAY_INVALID",
130
+ message: `A ${kind}'s ${name} is ${TYPES[type].takes}.`,
131
+ path: pathToOption
132
+ }];
133
+ });
119
134
  }
120
135
  /**
121
136
  * A widget's options as its kind reads them: each option it knows, when it is of its type. A
@@ -134,30 +149,6 @@ function read(options, display) {
134
149
  return value === void 0 ? [] : [[name, value]];
135
150
  }));
136
151
  }
137
- /** An option as an object holds it: none when it has no such option, which is not the same as holding null. */
138
- const held = (display, option) => Object.hasOwn(display, option) ? { is: display[option] ?? null } : null;
139
- /**
140
- * What an edit did wrong to a display: each option of the wrong type that the edit set or
141
- * changed, at `widgets.<id>.display.<option>`. Only what the edit touched is checked: an option
142
- * stored before options were typed stays as it is, so it never blocks an edit of something
143
- * else, and replaying a draft's own commands never refuses one that was accepted.
144
- */
145
- function editedDisplayIssues(before, after) {
146
- return Object.entries(after.widgets).flatMap(([id, widget]) => {
147
- const was = before.widgets[id]?.display;
148
- const old = isRecord(was) ? was : {};
149
- const now = widget.display;
150
- return widgetDisplayIssues(widget, [
151
- "widgets",
152
- id,
153
- "display"
154
- ]).filter((found) => {
155
- const option = String(found.path.at(-1));
156
- const [set, had] = isRecord(now) ? [held(now, option), held(old, option)] : [now, was];
157
- return canonical(set ?? null) !== canonical(had ?? null);
158
- });
159
- });
160
- }
161
152
  /** The options every widget takes, whatever draws it: `keepDateRange` and `empty`, and no option of any one kind. */
162
153
  const sharedDisplay = (display) => read(SHARED, display);
163
- export { DISPLAY_HELP, DISPLAY_OPTIONS, displayIssues, displayOf, documentDisplayIssues, editedDisplayIssues, sharedDisplay };
154
+ export { DISPLAY_HELP, DISPLAY_OPTIONS, displayIssues, displayOf, isRecord, sharedDisplay };
@@ -14,6 +14,22 @@ const SMALLEST = {
14
14
  w: 4,
15
15
  h: 3
16
16
  },
17
+ area: {
18
+ w: 4,
19
+ h: 3
20
+ },
21
+ multiples: {
22
+ w: 4,
23
+ h: 3
24
+ },
25
+ heatmap: {
26
+ w: 4,
27
+ h: 3
28
+ },
29
+ funnel: {
30
+ w: 4,
31
+ h: 3
32
+ },
17
33
  table: {
18
34
  w: 4,
19
35
  h: 3
@@ -1,6 +1,6 @@
1
1
  import { EngineError } from "../error/refusal.mjs";
2
2
  import { policyBinding, tenantBinding, tenantKeys } from "../model/defaults.mjs";
3
- import { bindPolicy, namedPolicy } from "../policy/bind.mjs";
3
+ import { bindPolicy, grantsNothingYet, namedPolicy } from "../policy/bind.mjs";
4
4
  import { isBound } from "../query/filter.mjs";
5
5
  import { principalTenant } from "../types/viewer.mjs";
6
6
  // src/engine/access.ts
@@ -49,9 +49,11 @@ const policyTerms = (input, name) => {
49
49
  via: via(input, name, join, policyTerms)
50
50
  }));
51
51
  const rules = typeof policy === "string" ? namedPolicy(input.model, policy) : policy;
52
+ const rule = bindPolicy(rules, input.principal);
52
53
  return [{
53
54
  kind: "policy",
54
- rule: bindPolicy(rules, input.principal)
55
+ rule,
56
+ ...rule === "none" && grantsNothingYet(rules, input.principal) ? { nothingYet: true } : {}
55
57
  }];
56
58
  };
57
59
  function whereTerms(model, name) {
@@ -33,14 +33,12 @@ function passFor(base, query) {
33
33
  };
34
34
  }
35
35
  /**
36
- * Whether the principal can read any row of a source: its bound policy, or every via parent's,
37
- * is something other than "none". A query on a source it cannot read reads nothing.
36
+ * Whether the principal may read a source: its bound policy, or every via parent's, is something
37
+ * other than "none", or is "none" only because the viewer's lists are empty (that query runs and
38
+ * reads no rows). A query on a source it cannot read reads nothing and says so.
38
39
  */
39
40
  function readable(plan, source) {
40
- return (plan.refs[source]?.terms ?? []).every((t) => {
41
- if (t.kind === "policy") return t.rule !== "none";
42
- if (t.kind === "policy_via") return t.via.parent.terms.every((p) => p.kind !== "policy" || p.rule !== "none");
43
- return true;
44
- });
41
+ const open = (t) => t.kind !== "policy" || t.rule !== "none" || t.nothingYet === true;
42
+ return (plan.refs[source]?.terms ?? []).every((t) => t.kind === "policy_via" ? t.via.parent.terms.every(open) : open(t));
45
43
  }
46
44
  export { passFor, readable, startBase };
@@ -1,4 +1,4 @@
1
- import { parseExact } from "../../exact/decimal.mjs";
1
+ import { parseExact } from "../../exact/text.mjs";
2
2
  import { cellOf, exactNum } from "../../result/number.mjs";
3
3
  import { nanRefusal } from "../nan.mjs";
4
4
  // src/engine/custom/cells.ts
@@ -1,5 +1,6 @@
1
1
  import { EngineError } from "../../error/refusal.mjs";
2
- import { formatTextTime, parseTextTime } from "../../time/instant.mjs";
2
+ import { parseTextTime } from "../../time/text-time.mjs";
3
+ import { formatTextTime } from "../../time/instant.mjs";
3
4
  import { checked, tenantOf } from "./ask.mjs";
4
5
  import { decodedFrom, plainOf } from "./cells.mjs";
5
6
  import { fieldOf, sourceOf } from "./fields.mjs";
@@ -12,6 +12,8 @@ export type SqlType = "text" | "float" | "boolean" | "numeric" | "bigint" | "dat
12
12
  */
13
13
  export interface Dialect {
14
14
  readonly id: "postgres" | "sqlite";
15
+ /** Supports GROUPING SETS and GROUPING() for one-statement top groups, Other and the total. */
16
+ readonly groupingSets?: true;
15
17
  compile(fragment: Fragment): Sql;
16
18
  cast(value: Fragment, type: SqlType): Fragment;
17
19
  /** Unicode simple case folding, from core's table. */
@@ -1,6 +1,6 @@
1
+ import { offeredMembers, trendDimension } from "../query/drill/fields.mjs";
1
2
  import { EngineError } from "../error/refusal.mjs";
2
3
  import { limitedDimensions, rankQuery, segmentCuts } from "../query/segments.mjs";
3
- import { offeredMembers, trendDimension } from "../query/drill/fields.mjs";
4
4
  import { formulaParts, lineScopes, measuresOnly, scopeSteps, scopedLines } from "../query/drill/scopes.mjs";
5
5
  import { keptPath, pathFields, stepMembers } from "../query/drill/steps.mjs";
6
6
  import { trendQuery } from "../query/drill/time.mjs";
@@ -1,7 +1,7 @@
1
1
  import { readsTotal } from "../result/calc.mjs";
2
2
  import { canonical } from "../value/canonical.mjs";
3
3
  import { otherOf, restriction } from "./other.mjs";
4
- import { ranked, readGroups, readSeries } from "./rank.mjs";
4
+ import { rankOrder, ranked, readGroups, readSeries } from "./rank.mjs";
5
5
  import { both, levelRows } from "./reader.mjs";
6
6
  import { bucketRefs, refsOf, totalRefs } from "./refs.mjs";
7
7
  import { fill, perBucketRows, sortSplit, withCalcs } from "./shape.mjs";
@@ -99,6 +99,10 @@ const NOT_RANKED = {
99
99
  async function answered(pass) {
100
100
  const { query } = pass;
101
101
  const grouped = (query.dimensions ?? []).length > 0;
102
+ if (grouped) {
103
+ const combined = await partitioned(pass);
104
+ if (combined !== void 0) return combined;
105
+ }
102
106
  if (grouped && totalsInGroups(pass)) {
103
107
  const read = await readGroups(pass, true);
104
108
  const { refs, own, ...totals } = await totalsFrom(pass, read.total === void 0 ? [] : [read.total]);
@@ -125,6 +129,34 @@ async function answered(pass) {
125
129
  ...grouped ? { groups } : {}
126
130
  };
127
131
  }
132
+ /** A limited, uncut breakdown whose ranking, Other and total cover the same population. */
133
+ function partitionLimit({ query, whole, within, input }) {
134
+ if (whole !== void 0 || within !== void 0 || query.time?.grain !== void 0) return;
135
+ const capped = query.limit !== void 0 && query.limit <= (input.maxRows ?? Infinity);
136
+ return query.total === true && query.other === true && capped ? query.limit : void 0;
137
+ }
138
+ async function partitioned(pass) {
139
+ const { query, reads } = pass;
140
+ const limit = partitionLimit(pass);
141
+ if (reads.partition === void 0 || limit === void 0) return;
142
+ const order = rankOrder(pass);
143
+ if (order === void 0) return;
144
+ const parts = await reads.partition(order, limit);
145
+ const { refs, totals } = await totalsFrom(pass, parts.total === void 0 ? [] : [parts.total]);
146
+ const r = await ranked(pass, refs, {
147
+ groups: parts.rows,
148
+ order
149
+ });
150
+ const other = parts.other === void 0 ? {} : { other: perBucketRows(query, [parts.other], (b) => bucketRefs(refs, b, false)) };
151
+ return {
152
+ answer: {
153
+ rows: keptRows(r),
154
+ ...totals === void 0 ? {} : { totals },
155
+ ...other
156
+ },
157
+ groups: r.kept.map((g) => g.key)
158
+ };
159
+ }
128
160
  /**
129
161
  * The read besides the totals: the groups (of a time series, with its rows by bucket, when
130
162
  * every measure merges), the one row of a query with neither, or nothing yet.
@@ -25,6 +25,8 @@ export type AccessTerm = {
25
25
  } | {
26
26
  kind: "policy";
27
27
  rule: "all" | "none" | BoundPolicyFilter;
28
+ /** "none" only because the viewer's lists are empty: readable, with no rows. */
29
+ nothingYet?: true;
28
30
  } | {
29
31
  kind: "policy_via";
30
32
  via: ViaLink;
@@ -97,6 +97,7 @@ const DATE_PARTS = {
97
97
  const recheck = (test) => sql`(nullif(${test}, FALSE) IS NOT NULL)`;
98
98
  const postgres = {
99
99
  id: "postgres",
100
+ groupingSets: true,
100
101
  compile: compilePostgres,
101
102
  cast: (value, type) => sql`CAST(${value} AS ${keyword(TYPES[type])})`,
102
103
  fold: (text) => sql`${keyword(FOLD_FUNCTION)}(${text})`,
@@ -146,4 +146,4 @@ async function ranked(pass, refs, read) {
146
146
  relabel
147
147
  };
148
148
  }
149
- export { ranked, readGroups, readSeries };
149
+ export { rankOrder, ranked, readGroups, readSeries };
@@ -61,6 +61,10 @@ export type Reads = {
61
61
  * and a time series its groups (`Level.series`).
62
62
  */
63
63
  windowTotals: boolean;
64
+ /** Named groups, Other and the whole population aggregated directly from the contributing rows. */
65
+ partition?: (order: readonly OrderBy[], limit: number) => Promise<LevelRead & {
66
+ other?: Decoded;
67
+ }>;
64
68
  /** Other as the total less the kept groups, column by column, when every measure adds up. */
65
69
  lessKept?: (total: Decoded, kept: readonly (readonly unknown[])[]) => Decoded;
66
70
  /**
@@ -0,0 +1,45 @@
1
+ import { refuseNaN } from "./nan.mjs";
2
+ import { layoutOf, read } from "./read.mjs";
3
+ import { decodeRow } from "./shape.mjs";
4
+ import { ident, join, sql } from "./sql.mjs";
5
+ import { keyFill, orderSql } from "./statement.mjs";
6
+ import { positions } from "./two-step.mjs";
7
+ // src/engine/sql-partition.ts
8
+ /** Rank just the keys, then aggregate the named groups, the remainder and the total in one pass. */
9
+ function partitionSql({ b, p }, order, limit) {
10
+ const keys = p.keys.map((_, i) => ident("kept", `k${i}`));
11
+ const names = p.keys.map((k, i) => sql`${k.sql} AS ${ident(`k${i}`)}`);
12
+ const ranked = sql`SELECT ${join(names)}, TRUE AS named FROM ${p.keyed} WHERE ${p.where}
13
+ ${positions(keys.length)} ORDER BY ${join(order.map((o) => orderSql(p, o)))} LIMIT ${limit}`;
14
+ const matches = p.keys.map((k, i) => {
15
+ const key = ident("kept", `k${i}`);
16
+ const fill = keyFill(b, k.type);
17
+ return sql`((${key} IS NULL) = (${k.sql} IS NULL) AND COALESCE(${key}, ${fill}) = COALESCE(${k.sql}, ${fill}))`;
18
+ });
19
+ const group = [ident("kept", "named"), ...keys];
20
+ const cells = [
21
+ ...group,
22
+ ...p.keys.flatMap((k) => k.label === void 0 ? [] : [k.label]),
23
+ ...p.measures.flatMap((m) => m.sql)
24
+ ];
25
+ if (p.nan !== void 0) cells.push(p.nan.agg);
26
+ return sql`SELECT GROUPING(kept.named), ${join(cells)} FROM ${p.keyed}
27
+ LEFT JOIN (${ranked}) AS kept ON ${join(matches, sql` AND `)}
28
+ WHERE ${p.where} GROUP BY GROUPING SETS ((${join(group)}), ()) HAVING ${p.count} > 0`;
29
+ }
30
+ async function readPartition(pass, order, limit) {
31
+ const result = { rows: [] };
32
+ const layout = layoutOf(pass, pass.p, {
33
+ keys: true,
34
+ bucket: false
35
+ });
36
+ for (const raw of await read(pass, partitionSql(pass, order, limit))) {
37
+ if (pass.p.nan !== void 0) refuseNaN(raw.at(-1));
38
+ const group = decodeRow(layout, raw.slice(2, pass.p.nan === void 0 ? void 0 : -1));
39
+ if (Number(raw[0]) === 1) result.total = group;
40
+ else if (raw[1] === null) result.other = group;
41
+ else result.rows.push(group);
42
+ }
43
+ return result;
44
+ }
45
+ export { readPartition };
@@ -1,6 +1,7 @@
1
1
  import { columnsLess } from "./numbers.mjs";
2
2
  import { measureLayout, readLevel } from "./read.mjs";
3
3
  import { decodeCells } from "./shape.mjs";
4
+ import { readPartition } from "./sql-partition.mjs";
4
5
  // src/engine/sql-reads.ts
5
6
  /**
6
7
  * The SQL reads of one query: every level a statement, with the shapes SQL gives besides:
@@ -9,6 +10,7 @@ import { decodeCells } from "./shape.mjs";
9
10
  */
10
11
  function sqlReads(pass) {
11
12
  const { p } = pass;
13
+ const additive = p.measures.every((m) => m.additive === true);
12
14
  const lessKept = (total, kept) => {
13
15
  const raw = columnsLess(total.raw, kept);
14
16
  return {
@@ -21,8 +23,9 @@ function sqlReads(pass) {
21
23
  level: (level) => readLevel(pass, level),
22
24
  ranks: (index) => p.measures[index]?.order !== void 0,
23
25
  windowTotals: p.measures.every((m) => m.parts !== void 0),
26
+ ...pass.b.dialect.groupingSets === true && p.staged === void 0 && !additive ? { partition: (order, limit) => readPartition(pass, order, limit) } : {},
24
27
  narrows: p.nan === void 0,
25
- ...p.measures.every((m) => m.additive === true) ? { lessKept } : {}
28
+ ...additive ? { lessKept } : {}
26
29
  };
27
30
  }
28
31
  export { sqlReads };
@@ -38,17 +38,17 @@ function plainMatch(b, p, keys) {
38
38
  const match = p.keys.map((k, i) => sql`${ident("kept", `k${i}`)} IS NOT DISTINCT FROM ${k.sql}`);
39
39
  return sql`EXISTS (SELECT 1 FROM (VALUES ${join(rows)}) AS kept(${join(names)}) WHERE ${and(match)})`;
40
40
  }
41
+ const keyFill = (b, type) => b.dialect.cast(sql`${FILL[type]}`, TYPES[type]);
41
42
  function hashedMatch(b, p, keys) {
42
- const fill = (type) => b.dialect.cast(sql`${FILL[type]}`, TYPES[type]);
43
43
  const rows = keys.map((key) => {
44
44
  const cells = p.keys.flatMap((k, i) => {
45
45
  const value = key[i] ?? null;
46
- return [value === null ? sql`TRUE` : sql`FALSE`, value === null ? fill(k.type) : b.dialect.cast(sql`${value}`, TYPES[k.type])];
46
+ return [value === null ? sql`TRUE` : sql`FALSE`, value === null ? keyFill(b, k.type) : b.dialect.cast(sql`${value}`, TYPES[k.type])];
47
47
  });
48
48
  return sql`(${join(cells)})`;
49
49
  });
50
50
  const names = p.keys.flatMap((_k, i) => [ident(`n${i}`), ident(`k${i}`)]);
51
- const match = p.keys.flatMap((k, i) => [sql`${ident("kept", `n${i}`)} = (${k.sql} IS NULL)`, sql`${ident("kept", `k${i}`)} = COALESCE(${k.sql}, ${fill(k.type)})`]);
51
+ const match = p.keys.flatMap((k, i) => [sql`${ident("kept", `n${i}`)} = (${k.sql} IS NULL)`, sql`${ident("kept", `k${i}`)} = COALESCE(${k.sql}, ${keyFill(b, k.type)})`]);
52
52
  return sql`EXISTS (SELECT 1 FROM (VALUES ${join(rows)}) AS kept(${join(names)}) WHERE ${and(match)})`;
53
53
  }
54
54
  const DIR = {
@@ -109,4 +109,4 @@ function groupedSql(b, p, level) {
109
109
  const from = fromOf(p, level, level.keys || level.restrict !== void 0 ? p.keyed : p.from);
110
110
  return sql`SELECT ${join(select)} FROM ${from} WHERE ${and([p.where, ...restrict])}${rest}`;
111
111
  }
112
- export { levelSql };
112
+ export { keyFill, levelSql, orderSql };