@malloy-publisher/sdk 0.12.0 → 0.12.2
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.
- package/README.md +79 -6
- package/dist/{RenderedResult-SgDlxzs9.cjs.js → RenderedResult-Cgw4iqev.cjs.js} +1 -1
- package/dist/{RenderedResult-D8bCgThO.es.js → RenderedResult-DEjFM1DL.es.js} +2 -2
- package/dist/ServerProvider-BRQl_cBo.cjs.js +1 -0
- package/dist/{ServerProvider-DBw-thY0.es.js → ServerProvider-RnliA2Ao.es.js} +1084 -723
- package/dist/builder/index.cjs.js +1 -1
- package/dist/builder/index.es.js +3 -3
- package/dist/client/api.d.ts +754 -41
- package/dist/client/index.cjs.js +1 -1
- package/dist/client/index.es.js +120 -110
- package/dist/components/Materializations/MaterializationDetailDialog.d.ts +16 -1
- package/dist/components/Model/ExplorerPage.d.ts +9 -0
- package/dist/components/Model/Model.d.ts +27 -11
- package/dist/components/ModelViewer/AccessChips.d.ts +34 -0
- package/dist/components/ModelViewer/EmptyRow.d.ts +23 -0
- package/dist/components/ModelViewer/Facts.d.ts +15 -0
- package/dist/components/ModelViewer/FieldGroup.d.ts +31 -0
- package/dist/components/ModelViewer/FieldRow.d.ts +69 -0
- package/dist/components/ModelViewer/GivenFacts.d.ts +21 -0
- package/dist/components/ModelViewer/HeaderBar.d.ts +75 -0
- package/dist/components/ModelViewer/InlineDoc.d.ts +13 -0
- package/dist/components/ModelViewer/ModelGivensSection.d.ts +32 -0
- package/dist/components/ModelViewer/ModelSection.d.ts +21 -0
- package/dist/components/ModelViewer/ModelViewer.d.ts +91 -0
- package/dist/components/ModelViewer/QueriesGroup.d.ts +15 -0
- package/dist/components/ModelViewer/RowGlyph.d.ts +27 -0
- package/dist/components/ModelViewer/RunResult.d.ts +46 -0
- package/dist/components/ModelViewer/SampleValues.d.ts +20 -0
- package/dist/components/ModelViewer/SchemaJoin.d.ts +51 -0
- package/dist/components/ModelViewer/SchemaView.d.ts +52 -0
- package/dist/components/ModelViewer/SegmentIcons.d.ts +70 -0
- package/dist/components/ModelViewer/SourcesDropdown.d.ts +53 -0
- package/dist/components/ModelViewer/SourcesNav.d.ts +25 -0
- package/dist/components/ModelViewer/StatusPill.d.ts +56 -0
- package/dist/components/ModelViewer/ValueChip.d.ts +17 -0
- package/dist/components/ModelViewer/ViewRow.d.ts +36 -0
- package/dist/components/ModelViewer/clickableBox.d.ts +29 -0
- package/dist/components/ModelViewer/codeDisplay.d.ts +44 -0
- package/dist/components/ModelViewer/docAnnotations.d.ts +90 -0
- package/dist/components/ModelViewer/explorerModel.d.ts +12 -0
- package/dist/components/ModelViewer/extensions.d.ts +94 -0
- package/dist/components/ModelViewer/fieldCode.d.ts +74 -0
- package/dist/components/ModelViewer/fieldEntities.d.ts +45 -0
- package/dist/components/ModelViewer/fieldExpansion.d.ts +41 -0
- package/dist/components/ModelViewer/givenUsage.d.ts +71 -0
- package/dist/components/ModelViewer/headerCard.d.ts +31 -0
- package/dist/components/ModelViewer/httpStatus.d.ts +4 -0
- package/dist/components/ModelViewer/icons.d.ts +31 -0
- package/dist/components/ModelViewer/index.d.ts +9 -0
- package/dist/components/ModelViewer/joinAccess.d.ts +52 -0
- package/dist/components/ModelViewer/malloyRunText.d.ts +22 -0
- package/dist/components/ModelViewer/malloyText.d.ts +21 -0
- package/dist/components/ModelViewer/modelGivens.d.ts +61 -0
- package/dist/components/ModelViewer/modelGivensContext.d.ts +9 -0
- package/dist/components/ModelViewer/modelHeaderPresentation.d.ts +28 -0
- package/dist/components/ModelViewer/modelTextDeclarations.d.ts +32 -0
- package/dist/components/ModelViewer/modelTextParse.d.ts +27 -0
- package/dist/components/ModelViewer/namedQueryCode.d.ts +12 -0
- package/dist/components/ModelViewer/offSurface.d.ts +54 -0
- package/dist/components/ModelViewer/rowLayers.d.ts +30 -0
- package/dist/components/ModelViewer/runGate.d.ts +49 -0
- package/dist/components/ModelViewer/schemaModel.d.ts +109 -0
- package/dist/components/ModelViewer/sectionOpenCommand.d.ts +45 -0
- package/dist/components/ModelViewer/sourceAccess.d.ts +67 -0
- package/dist/components/ModelViewer/sourceLineage.d.ts +14 -0
- package/dist/components/ModelViewer/sourcePicker.d.ts +66 -0
- package/dist/components/ModelViewer/surface.d.ts +47 -0
- package/dist/components/ModelViewer/text.d.ts +6 -0
- package/dist/components/ModelViewer/theme.d.ts +63 -0
- package/dist/components/ModelViewer/tokens.d.ts +52 -0
- package/dist/components/ModelViewer/ui.d.ts +48 -0
- package/dist/components/ModelViewer/useDimensionSamples.d.ts +76 -0
- package/dist/components/ModelViewer/useModelSources.d.ts +92 -0
- package/dist/components/ModelViewer/useRunQuery.d.ts +115 -0
- package/dist/components/ReportBuilder/loadMalloy.d.ts +4 -0
- package/dist/components/ServerProvider.d.ts +17 -2
- package/dist/components/filter/utils.d.ts +1 -1
- package/dist/components/given/GivenInput.d.ts +7 -1
- package/dist/components/given/GivensPanel.d.ts +3 -1
- package/dist/components/given/filterSummary.d.ts +6 -0
- package/dist/components/given/useGivenUrlParams.d.ts +11 -0
- package/dist/components/index.d.ts +2 -1
- package/dist/components/modelPage.d.ts +36 -0
- package/dist/hooks/useDimensionalFilterRangeData.d.ts +9 -0
- package/dist/hooks/useSuggestOptions.d.ts +26 -2
- package/dist/index.cjs.js +47 -19
- package/dist/index.es.js +14137 -9697
- package/dist/useCompiledDocument-D88_2cGS.cjs.js +143 -0
- package/dist/{useCompiledDocument-CXMPtJkJ.es.js → useCompiledDocument-Dmm6xkom.es.js} +6975 -6893
- package/dist/utils/filterCondition.d.ts +14 -0
- package/dist/utils/malloyLiteral.d.ts +20 -1
- package/package.json +2 -2
- package/src/components/Materializations/MaterializationDetailDialog.tsx +33 -7
- package/src/components/Materializations/Materializations.tsx +47 -4
- package/src/components/Materializations/Materializations.versions.spec.tsx +140 -1
- package/src/components/Model/ExplorerPage.tsx +269 -0
- package/src/components/Model/Model.tsx +66 -250
- package/src/components/Model/ModelExplorer.spec.tsx +64 -1
- package/src/components/ModelViewer/AccessChips.tsx +149 -0
- package/src/components/ModelViewer/EmptyRow.tsx +71 -0
- package/src/components/ModelViewer/Facts.tsx +46 -0
- package/src/components/ModelViewer/FieldGroup.tsx +137 -0
- package/src/components/ModelViewer/FieldRow.tsx +566 -0
- package/src/components/ModelViewer/GivenFacts.tsx +89 -0
- package/src/components/ModelViewer/HeaderBar.tsx +362 -0
- package/src/components/ModelViewer/InlineDoc.tsx +50 -0
- package/src/components/ModelViewer/ModelGivensSection.tsx +142 -0
- package/src/components/ModelViewer/ModelSection.tsx +98 -0
- package/src/components/ModelViewer/ModelViewer.spec.tsx +937 -0
- package/src/components/ModelViewer/ModelViewer.tsx +777 -0
- package/src/components/ModelViewer/QueriesGroup.tsx +107 -0
- package/src/components/ModelViewer/RowGlyph.tsx +138 -0
- package/src/components/ModelViewer/RunResult.tsx +298 -0
- package/src/components/ModelViewer/SampleValues.tsx +97 -0
- package/src/components/ModelViewer/SchemaJoin.tsx +258 -0
- package/src/components/ModelViewer/SchemaView.tsx +222 -0
- package/src/components/ModelViewer/SegmentIcons.tsx +129 -0
- package/src/components/ModelViewer/SourcesDropdown.tsx +265 -0
- package/src/components/ModelViewer/SourcesNav.tsx +75 -0
- package/src/components/ModelViewer/StatusPill.tsx +145 -0
- package/src/components/ModelViewer/ValueChip.tsx +39 -0
- package/src/components/ModelViewer/ViewRow.tsx +274 -0
- package/src/components/ModelViewer/clickableBox.ts +59 -0
- package/src/components/ModelViewer/codeDisplay.spec.ts +176 -0
- package/src/components/ModelViewer/codeDisplay.ts +118 -0
- package/src/components/ModelViewer/docAnnotations.spec.ts +181 -0
- package/src/components/ModelViewer/docAnnotations.ts +244 -0
- package/src/components/ModelViewer/docAnnotations.typeDetail.spec.ts +114 -0
- package/src/components/ModelViewer/explorerModel.spec.ts +43 -0
- package/src/components/ModelViewer/explorerModel.ts +31 -0
- package/src/components/ModelViewer/extensions.tsx +112 -0
- package/src/components/ModelViewer/fieldCode.spec.ts +168 -0
- package/src/components/ModelViewer/fieldCode.ts +118 -0
- package/src/components/ModelViewer/fieldEntities.ts +63 -0
- package/src/components/ModelViewer/fieldExpansion.spec.ts +116 -0
- package/src/components/ModelViewer/fieldExpansion.ts +95 -0
- package/src/components/ModelViewer/givenUsage.spec.ts +304 -0
- package/src/components/ModelViewer/givenUsage.ts +325 -0
- package/src/components/ModelViewer/headerCard.tsx +102 -0
- package/src/components/ModelViewer/httpStatus.ts +15 -0
- package/src/components/ModelViewer/icons.tsx +67 -0
- package/src/components/ModelViewer/index.ts +26 -0
- package/src/components/ModelViewer/joinAccess.spec.ts +95 -0
- package/src/components/ModelViewer/joinAccess.ts +54 -0
- package/src/components/ModelViewer/malloyRunText.spec.ts +48 -0
- package/src/components/ModelViewer/malloyRunText.ts +51 -0
- package/src/components/ModelViewer/malloyText.spec.ts +47 -0
- package/src/components/ModelViewer/malloyText.ts +77 -0
- package/src/components/ModelViewer/modelGivens.ts +228 -0
- package/src/components/ModelViewer/modelGivensContext.ts +13 -0
- package/src/components/ModelViewer/modelHeaderPresentation.spec.ts +31 -0
- package/src/components/ModelViewer/modelHeaderPresentation.ts +41 -0
- package/src/components/ModelViewer/modelTextDeclarations.spec.ts +336 -0
- package/src/components/ModelViewer/modelTextDeclarations.ts +228 -0
- package/src/components/ModelViewer/modelTextParse.ts +112 -0
- package/src/components/ModelViewer/namedQueryCode.spec.ts +58 -0
- package/src/components/ModelViewer/namedQueryCode.ts +26 -0
- package/src/components/ModelViewer/offSurface.tsx +124 -0
- package/src/components/ModelViewer/rowLayers.tsx +104 -0
- package/src/components/ModelViewer/runGate.spec.ts +219 -0
- package/src/components/ModelViewer/runGate.ts +145 -0
- package/src/components/ModelViewer/schemaModel.defaultSource.spec.ts +126 -0
- package/src/components/ModelViewer/schemaModel.queries.spec.ts +37 -0
- package/src/components/ModelViewer/schemaModel.ts +300 -0
- package/src/components/ModelViewer/sectionOpenCommand.tsx +125 -0
- package/src/components/ModelViewer/sourceAccess.spec.ts +130 -0
- package/src/components/ModelViewer/sourceAccess.ts +194 -0
- package/src/components/ModelViewer/sourceLineage.spec.ts +73 -0
- package/src/components/ModelViewer/sourceLineage.ts +38 -0
- package/src/components/ModelViewer/sourcePicker.spec.ts +294 -0
- package/src/components/ModelViewer/sourcePicker.ts +162 -0
- package/src/components/ModelViewer/surface.ts +51 -0
- package/src/components/ModelViewer/text.tsx +33 -0
- package/src/components/ModelViewer/theme.spec.ts +38 -0
- package/src/components/ModelViewer/theme.ts +58 -0
- package/src/components/ModelViewer/tokens.ts +87 -0
- package/src/components/ModelViewer/ui.tsx +201 -0
- package/src/components/ModelViewer/useDimensionSamples.spec.tsx +139 -0
- package/src/components/ModelViewer/useDimensionSamples.ts +220 -0
- package/src/components/ModelViewer/useModelSources.spec.ts +110 -0
- package/src/components/ModelViewer/useModelSources.ts +238 -0
- package/src/components/ModelViewer/useRunQuery.spec.ts +50 -0
- package/src/components/ModelViewer/useRunQuery.ts +223 -0
- package/src/components/Package/Package.agents.spec.tsx +5 -4
- package/src/components/Package/Package.tsx +8 -1
- package/src/components/ReportBuilder/emptyStart.spec.ts +8 -0
- package/src/components/ReportBuilder/loadMalloy.ts +8 -1
- package/src/components/ServerProvider.tsx +29 -3
- package/src/components/filter/utils.ts +4 -60
- package/src/components/given/FilterButton.tsx +4 -1
- package/src/components/given/GivenInput.tsx +8 -0
- package/src/components/given/GivensPanel.tsx +6 -1
- package/src/components/given/filterSummary.spec.ts +31 -1
- package/src/components/given/filterSummary.ts +41 -19
- package/src/components/given/useGivenUrlParams.spec.tsx +32 -0
- package/src/components/given/useGivenUrlParams.ts +49 -14
- package/src/components/index.ts +6 -1
- package/src/components/legacy/malloynb/Notebook.spec.tsx +38 -4
- package/src/components/legacy/malloynb/NotebookCell.tsx +8 -2
- package/src/components/modelPage.spec.ts +79 -0
- package/src/components/modelPage.ts +48 -0
- package/src/hooks/useDimensionFilters.ts +3 -58
- package/src/hooks/useDimensionFiltersQuery.ts +3 -58
- package/src/hooks/useDimensionalFilterRangeData.spec.ts +57 -0
- package/src/hooks/useDimensionalFilterRangeData.ts +8 -63
- package/src/hooks/useSuggestOptions.dom.spec.ts +54 -1
- package/src/hooks/useSuggestOptions.spec.ts +71 -0
- package/src/hooks/useSuggestOptions.ts +102 -12
- package/src/utils/filterCondition.spec.ts +87 -0
- package/src/utils/filterCondition.ts +66 -0
- package/src/utils/malloyLiteral.spec.ts +53 -1
- package/src/utils/malloyLiteral.ts +47 -12
- package/dist/ServerProvider-BHfg1Ed6.cjs.js +0 -1
- package/dist/useCompiledDocument-D7X_K6YU.cjs.js +0 -142
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import type * as Malloy from "@malloydata/malloy-interfaces";
|
|
5
|
+
|
|
6
|
+
/** The rows at a result's top level, from the result's JSON; undefined for a
|
|
7
|
+
* string that isn't a result. */
|
|
8
|
+
function rowsInResultJson(result: string): number | undefined {
|
|
9
|
+
try {
|
|
10
|
+
const { data } = JSON.parse(result) as Pick<Malloy.Result, "data">;
|
|
11
|
+
if (data?.kind === "array_cell") return data.array_value.length;
|
|
12
|
+
return data?.kind === "record_cell" ? 1 : 0;
|
|
13
|
+
} catch {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* What a source's access declarations say, read from the compiled model's
|
|
20
|
+
* `sources[]` (the publisher's `Source.authorize` and `Source.accessFilter`).
|
|
21
|
+
*
|
|
22
|
+
* Both lists are the RULE, never a verdict about the reader: the publisher is
|
|
23
|
+
* the only authority on whether a caller passes, and its docs forbid
|
|
24
|
+
* re-deriving that from these strings. So everything here answers "is this
|
|
25
|
+
* source restricted / row filtered, and what does the rule say", and nothing
|
|
26
|
+
* answers "can you query it".
|
|
27
|
+
*/
|
|
28
|
+
export interface SourceAccess {
|
|
29
|
+
/** Effective `#(authorize)` expressions (own and inherited), AND-ed. */
|
|
30
|
+
authorize: string[];
|
|
31
|
+
/** Effective `#(access_filter)` expressions, AND-ed. */
|
|
32
|
+
accessFilter: string[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const EMPTY: SourceAccess = { authorize: [], accessFilter: [] };
|
|
36
|
+
|
|
37
|
+
function stringList(value: unknown): string[] {
|
|
38
|
+
return Array.isArray(value)
|
|
39
|
+
? value.filter(
|
|
40
|
+
(v): v is string => typeof v === "string" && v.trim() !== "",
|
|
41
|
+
)
|
|
42
|
+
: [];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Source name → its access declarations, for every source the compiled model
|
|
47
|
+
* lists. Shape-checked: through the router an older publisher sends neither
|
|
48
|
+
* field (or `null`), which reads as no declaration.
|
|
49
|
+
*/
|
|
50
|
+
export function extractSourceAccess(body: unknown): Map<string, SourceAccess> {
|
|
51
|
+
const out = new Map<string, SourceAccess>();
|
|
52
|
+
const sources = (body as { sources?: unknown })?.sources;
|
|
53
|
+
if (!Array.isArray(sources)) return out;
|
|
54
|
+
for (const raw of sources) {
|
|
55
|
+
if (!raw || typeof raw !== "object") continue;
|
|
56
|
+
const s = raw as Record<string, unknown>;
|
|
57
|
+
if (typeof s.name !== "string" || !s.name) continue;
|
|
58
|
+
const authorize = stringList(s.authorize);
|
|
59
|
+
const accessFilter = stringList(s.accessFilter);
|
|
60
|
+
if (authorize.length === 0 && accessFilter.length === 0) continue;
|
|
61
|
+
out.set(s.name, { authorize, accessFilter });
|
|
62
|
+
}
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The declarations of one source, or none. */
|
|
67
|
+
export function accessOf(
|
|
68
|
+
bySource: ReadonlyMap<string, SourceAccess> | undefined,
|
|
69
|
+
sourceName: string | undefined,
|
|
70
|
+
): SourceAccess {
|
|
71
|
+
return (sourceName && bySource?.get(sourceName)) || EMPTY;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Whether an `#(authorize)` rule locks the source. Read element-wise, as the
|
|
76
|
+
* publisher documents: the list is unlocked only when every element is the
|
|
77
|
+
* `"true"` sentinel (gates from different declaring sources AND together, so
|
|
78
|
+
* `["true", "'finance' in $GROUPS"]` is locked).
|
|
79
|
+
*/
|
|
80
|
+
export function isRestricted(access: SourceAccess | undefined): boolean {
|
|
81
|
+
return !!access && access.authorize.some((e) => e.trim() !== "true");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Whether an `#(access_filter)` scopes the source's rows per reader. */
|
|
85
|
+
export function isRowFiltered(access: SourceAccess | undefined): boolean {
|
|
86
|
+
return !!access && access.accessFilter.length > 0;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** `'finance' in $GROUPS` (either quote) → `finance`. */
|
|
90
|
+
const GROUP_ATOM = /^\(?\s*(?:'([^']*)'|"([^"]*)")\s+in\s+\$GROUPS\s*\)?$/;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* One rule in words, or undefined where it isn't the shape this can say
|
|
94
|
+
* faithfully. Only group membership is put into words: any other given a rule
|
|
95
|
+
* reads (a role, a tenant) is the author's vocabulary, and guessing at it
|
|
96
|
+
* would tell a reader something the rule does not say.
|
|
97
|
+
*/
|
|
98
|
+
function ruleWords(expr: string): string | undefined {
|
|
99
|
+
const e = expr.trim();
|
|
100
|
+
if (e === "false") return "no one";
|
|
101
|
+
const single = GROUP_ATOM.exec(e);
|
|
102
|
+
if (single) return `members of ${single[1] ?? single[2]}`;
|
|
103
|
+
for (const joiner of ["or", "and"] as const) {
|
|
104
|
+
const parts = e.split(new RegExp(`\\s+${joiner}\\s+`));
|
|
105
|
+
if (parts.length < 2) continue;
|
|
106
|
+
const groups = parts.map((p) => GROUP_ATOM.exec(p.trim()));
|
|
107
|
+
if (groups.every((g) => g)) {
|
|
108
|
+
const names = groups.map((g) => g![1] ?? g![2]);
|
|
109
|
+
return joiner === "or"
|
|
110
|
+
? `members of ${names.slice(0, -1).join(", ")} or ${names[names.length - 1]}`
|
|
111
|
+
: `members of all of ${names.join(", ")}`;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Who the `#(authorize)` rule admits, in plain words: "members of finance".
|
|
119
|
+
* Several rules all apply, so they are joined with "and"; a rule this cannot
|
|
120
|
+
* say becomes "readers its access rule admits" rather than a guess.
|
|
121
|
+
*/
|
|
122
|
+
export function restrictedToWords(access: SourceAccess): string {
|
|
123
|
+
const words = access.authorize
|
|
124
|
+
.filter((e) => e.trim() !== "true")
|
|
125
|
+
.map((e) => ruleWords(e) ?? "readers its access rule admits");
|
|
126
|
+
const unique = Array.from(new Set(words));
|
|
127
|
+
return unique.join(", and ");
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** What a Restricted chip's tooltip and accessible name say. */
|
|
131
|
+
export const RESTRICTED_MEANING = "Only some readers can query this source";
|
|
132
|
+
/** What a Row filtered chip's tooltip and accessible name say. */
|
|
133
|
+
export const ROW_FILTERED_MEANING =
|
|
134
|
+
"Each reader sees only the rows they're allowed to see";
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* What a refused (403) run says: that the reader can't access the source, a
|
|
138
|
+
* restricted source's rule in words, and the way out — never a status code, and
|
|
139
|
+
* no Retry, since a second attempt meets the same rule. A 403 on a source with no
|
|
140
|
+
* rule this page knows (a package-level refusal, a rule the compiled model did
|
|
141
|
+
* not report) still says so in the same words.
|
|
142
|
+
*/
|
|
143
|
+
export function refusalMessage(
|
|
144
|
+
sourceName: string | undefined,
|
|
145
|
+
access: SourceAccess,
|
|
146
|
+
): string {
|
|
147
|
+
if (!sourceName)
|
|
148
|
+
return "You don't have access to run this. Ask an administrator of this package for access.";
|
|
149
|
+
if (!isRestricted(access))
|
|
150
|
+
return `You can't access ${sourceName}. Ask an administrator of this package for access.`;
|
|
151
|
+
return `You can't access ${sourceName}. It is restricted to ${restrictedToWords(
|
|
152
|
+
access,
|
|
153
|
+
)}; ask an administrator of this package for access.`;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* The caption under an empty result on a row-filtered source: an access filter
|
|
158
|
+
* that admits none of a reader's rows answers 200 with zero rows, which reads as
|
|
159
|
+
* "there is no data" unless the page says why. Only for zero rows: an ungrouped
|
|
160
|
+
* aggregate over nothing returns one row (`count()` = 0), which the caption
|
|
161
|
+
* cannot tell from a real zero, so it says nothing there.
|
|
162
|
+
*/
|
|
163
|
+
export function filteredEmptyCaption(
|
|
164
|
+
access: SourceAccess,
|
|
165
|
+
result: string | undefined,
|
|
166
|
+
): string | undefined {
|
|
167
|
+
if (!isRowFiltered(access) || result === undefined) return undefined;
|
|
168
|
+
return rowsInResultJson(result) === 0
|
|
169
|
+
? "Filtered to your rows: this source shows each reader only the rows they're allowed to see."
|
|
170
|
+
: undefined;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Whether the source declares either rule, so there is something to show. */
|
|
174
|
+
export function hasAccessRules(access: SourceAccess | undefined): boolean {
|
|
175
|
+
return isRestricted(access) || isRowFiltered(access);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* What an editor is told on an open join to a gated source: a gate applies only
|
|
180
|
+
* on the source a query enters through, so through this join the joined
|
|
181
|
+
* source's rows are read under this source's rules, not its own.
|
|
182
|
+
*/
|
|
183
|
+
export function joinGateWarning(
|
|
184
|
+
sourceName: string,
|
|
185
|
+
access: SourceAccess,
|
|
186
|
+
): string {
|
|
187
|
+
const rule =
|
|
188
|
+
isRestricted(access) && isRowFiltered(access)
|
|
189
|
+
? "access rule and row filter"
|
|
190
|
+
: isRestricted(access)
|
|
191
|
+
? "access rule"
|
|
192
|
+
: "row filter";
|
|
193
|
+
return `This join doesn't apply the ${rule} on ${sourceName}: its rows are read under this source's rules.`;
|
|
194
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { describe, expect, it } from "bun:test";
|
|
5
|
+
import { baseSourceName } from "./sourceLineage";
|
|
6
|
+
|
|
7
|
+
const MODEL = `
|
|
8
|
+
source: raw_orders is materialization_pg.table('public.orders')
|
|
9
|
+
|
|
10
|
+
#@ persist
|
|
11
|
+
source: order_rollup is raw_orders -> {
|
|
12
|
+
measure: total_orders is count()
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
source: category_ranking is order_rollup -> {
|
|
16
|
+
measure: revenue is sum(amount)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
source: orders_preagg is materialization_pg.table('public.orders') extend {
|
|
20
|
+
measure: n is count()
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
source: orders_ext is order_rollup extend {
|
|
24
|
+
dimension: big is total_orders > 100
|
|
25
|
+
}
|
|
26
|
+
`;
|
|
27
|
+
|
|
28
|
+
const KNOWN = new Set([
|
|
29
|
+
"raw_orders",
|
|
30
|
+
"order_rollup",
|
|
31
|
+
"category_ranking",
|
|
32
|
+
"orders_preagg",
|
|
33
|
+
"orders_ext",
|
|
34
|
+
]);
|
|
35
|
+
|
|
36
|
+
describe("baseSourceName", () => {
|
|
37
|
+
it("returns the upstream source of a query-derived source", () => {
|
|
38
|
+
expect(baseSourceName(MODEL, "order_rollup", KNOWN)).toBe("raw_orders");
|
|
39
|
+
expect(baseSourceName(MODEL, "category_ranking", KNOWN)).toBe(
|
|
40
|
+
"order_rollup",
|
|
41
|
+
);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("returns the base of a source-extended source", () => {
|
|
45
|
+
expect(baseSourceName(MODEL, "orders_ext", KNOWN)).toBe("order_rollup");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("returns undefined for a table-backed source", () => {
|
|
49
|
+
expect(baseSourceName(MODEL, "raw_orders", KNOWN)).toBeUndefined();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("returns undefined for a table-backed source that extends the table", () => {
|
|
53
|
+
expect(baseSourceName(MODEL, "orders_preagg", KNOWN)).toBeUndefined();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("returns undefined when the parsed base is not a known source", () => {
|
|
57
|
+
expect(
|
|
58
|
+
baseSourceName(MODEL, "order_rollup", new Set(["order_rollup"])),
|
|
59
|
+
).toBeUndefined();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("returns undefined for absent text or name", () => {
|
|
63
|
+
expect(baseSourceName(undefined, "order_rollup", KNOWN)).toBeUndefined();
|
|
64
|
+
expect(baseSourceName(MODEL, undefined, KNOWN)).toBeUndefined();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("handles backtick-quoted identifiers", () => {
|
|
68
|
+
const text = "source: `my source` is `raw_orders` -> { }";
|
|
69
|
+
expect(baseSourceName(text, "my source", new Set(["raw_orders"]))).toBe(
|
|
70
|
+
"raw_orders",
|
|
71
|
+
);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { escapeRegExp } from "./malloyText";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The immediate upstream SOURCE a derived source is built from, parsed from the
|
|
8
|
+
* model's own Malloy text — e.g. `raw_orders` for
|
|
9
|
+
* `source: order_rollup is raw_orders -> { … }`.
|
|
10
|
+
*
|
|
11
|
+
* Only a source-to-source derivation resolves. A table-backed source
|
|
12
|
+
* (`is conn.table('…')`, `is conn.sql('…')`) returns undefined — it has
|
|
13
|
+
* physical table provenance the header shows instead — and the parsed base
|
|
14
|
+
* must be one of the model's own sources, so a connection name or SQL slice is
|
|
15
|
+
* never mistaken for lineage. This is a best-effort read of source text: when
|
|
16
|
+
* the text is absent, or the declaration takes a form this doesn't recognise,
|
|
17
|
+
* it yields undefined and the header simply shows no `from`.
|
|
18
|
+
*/
|
|
19
|
+
export function baseSourceName(
|
|
20
|
+
sourceText: string | undefined,
|
|
21
|
+
sourceName: string | undefined,
|
|
22
|
+
knownSources: ReadonlySet<string>,
|
|
23
|
+
): string | undefined {
|
|
24
|
+
if (!sourceText || !sourceName) return undefined;
|
|
25
|
+
// Anchored at this source's `source: NAME is BASE`, requiring BASE to be a
|
|
26
|
+
// bare identifier followed immediately by `->` (query-derived) or `extend`
|
|
27
|
+
// (extension). A table/SQL base has `.table(`/`.sql(` after the identifier,
|
|
28
|
+
// so the trailing `->|extend` guard fails and no false base is captured.
|
|
29
|
+
const re = new RegExp(
|
|
30
|
+
"source:\\s*`?" +
|
|
31
|
+
escapeRegExp(sourceName) +
|
|
32
|
+
"`?\\s+is\\s+`?([A-Za-z_]\\w*)`?\\s*(?:->|extend\\b)",
|
|
33
|
+
);
|
|
34
|
+
const match = re.exec(sourceText);
|
|
35
|
+
if (!match) return undefined;
|
|
36
|
+
const base = match[1];
|
|
37
|
+
return knownSources.has(base) ? base : undefined;
|
|
38
|
+
}
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { describe, expect, it } from "bun:test";
|
|
5
|
+
import {
|
|
6
|
+
fieldCount,
|
|
7
|
+
pickDefaultSource,
|
|
8
|
+
type ExplorerField,
|
|
9
|
+
type ExplorerSource,
|
|
10
|
+
} from "./schemaModel";
|
|
11
|
+
import {
|
|
12
|
+
exportOrder,
|
|
13
|
+
inExportOrder,
|
|
14
|
+
orderPickerSources,
|
|
15
|
+
pickerDescription,
|
|
16
|
+
sourceMetaLine,
|
|
17
|
+
} from "./sourcePicker";
|
|
18
|
+
|
|
19
|
+
const field = (name: string, kind: ExplorerField["kind"]): ExplorerField => ({
|
|
20
|
+
name,
|
|
21
|
+
kind,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
/** A source with `views` views (named per source, so none looks like another's extension). */
|
|
25
|
+
function src(
|
|
26
|
+
name: string,
|
|
27
|
+
views: number,
|
|
28
|
+
extra: {
|
|
29
|
+
dimensions?: number;
|
|
30
|
+
measures?: number;
|
|
31
|
+
joins?: number;
|
|
32
|
+
doc?: string;
|
|
33
|
+
viewNames?: string[];
|
|
34
|
+
} = {},
|
|
35
|
+
): ExplorerSource {
|
|
36
|
+
const viewNames =
|
|
37
|
+
extra.viewNames ??
|
|
38
|
+
Array.from({ length: views }, (_, i) => `${name}_v${i}`);
|
|
39
|
+
return {
|
|
40
|
+
name,
|
|
41
|
+
doc: extra.doc,
|
|
42
|
+
accent: "steel",
|
|
43
|
+
calculations: [],
|
|
44
|
+
dimensions: Array.from({ length: extra.dimensions ?? 0 }, (_, i) =>
|
|
45
|
+
field(`d${i}`, "dimension"),
|
|
46
|
+
),
|
|
47
|
+
measures: Array.from({ length: extra.measures ?? 0 }, (_, i) =>
|
|
48
|
+
field(`m${i}`, "measure"),
|
|
49
|
+
),
|
|
50
|
+
views: viewNames.map((n) => field(n, "view")),
|
|
51
|
+
joins: Array.from({ length: extra.joins ?? 0 }, (_, i) => ({
|
|
52
|
+
name: `j${i}`,
|
|
53
|
+
relationship: "one" as never,
|
|
54
|
+
dimensions: [],
|
|
55
|
+
measures: [],
|
|
56
|
+
})),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const names = (sources: ExplorerSource[]) => sources.map((s) => s.name);
|
|
61
|
+
|
|
62
|
+
describe("orderPickerSources", () => {
|
|
63
|
+
it("puts the source the model opens on first, then the rest by view count, most first", () => {
|
|
64
|
+
// flights.malloy declares its lookups first and its subject last.
|
|
65
|
+
const sources = [
|
|
66
|
+
src("carriers", 0),
|
|
67
|
+
src("airports", 1),
|
|
68
|
+
src("aircraft", 0),
|
|
69
|
+
src("flights", 6),
|
|
70
|
+
src("routes", 2),
|
|
71
|
+
];
|
|
72
|
+
const order = orderPickerSources(
|
|
73
|
+
sources,
|
|
74
|
+
pickDefaultSource(sources, "flights.malloy"),
|
|
75
|
+
);
|
|
76
|
+
expect(names(order.sources)).toEqual([
|
|
77
|
+
"flights",
|
|
78
|
+
"routes",
|
|
79
|
+
"airports",
|
|
80
|
+
"carriers",
|
|
81
|
+
"aircraft",
|
|
82
|
+
]);
|
|
83
|
+
expect(order.curated).toBe(false);
|
|
84
|
+
expect(order.exportOrdered).toBe(false);
|
|
85
|
+
expect(order.mostViews).toBe("flights");
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("keeps declaration order among equal view counts", () => {
|
|
89
|
+
const sources = [src("b", 1), src("a", 1), src("top", 3)];
|
|
90
|
+
const order = orderPickerSources(
|
|
91
|
+
sources,
|
|
92
|
+
pickDefaultSource(sources, "m.malloy"),
|
|
93
|
+
);
|
|
94
|
+
expect(names(order.sources)).toEqual(["top", "b", "a"]);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("keeps the root index.malloy's compiled order when its text is unknown, and claims no order or Most views", () => {
|
|
98
|
+
const sources = [src("users", 0), src("orders", 9), src("products", 2)];
|
|
99
|
+
const order = orderPickerSources(
|
|
100
|
+
sources,
|
|
101
|
+
pickDefaultSource(sources, "index.malloy"),
|
|
102
|
+
{ exportOrder: [] },
|
|
103
|
+
);
|
|
104
|
+
expect(names(order.sources)).toEqual(["users", "orders", "products"]);
|
|
105
|
+
expect(order.curated).toBe(true);
|
|
106
|
+
expect(order.exportOrdered).toBe(false);
|
|
107
|
+
expect(order.mostViews).toBeUndefined();
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("lists the root index.malloy's sources in the order its export statement names them", () => {
|
|
111
|
+
// The compiled model lists them where ecommerce.malloy declares them; the author's
|
|
112
|
+
// `export { … }` is the order they chose.
|
|
113
|
+
const sources = [
|
|
114
|
+
src("users", 0),
|
|
115
|
+
src("product", 0),
|
|
116
|
+
src("order_items", 16),
|
|
117
|
+
];
|
|
118
|
+
const text =
|
|
119
|
+
'import {order_items, users, product} from "ecommerce.malloy"\n\nexport { order_items, users, product }\n';
|
|
120
|
+
const order = orderPickerSources(
|
|
121
|
+
sources,
|
|
122
|
+
pickDefaultSource(sources, "index.malloy"),
|
|
123
|
+
{
|
|
124
|
+
exportOrder: exportOrder(text),
|
|
125
|
+
},
|
|
126
|
+
);
|
|
127
|
+
expect(names(order.sources)).toEqual(["order_items", "users", "product"]);
|
|
128
|
+
expect(order.exportOrdered).toBe(true);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("claims no export order when the statement names none of the listed sources", () => {
|
|
132
|
+
const sources = [src("users", 0), src("orders", 1)];
|
|
133
|
+
const order = orderPickerSources(sources, null, {
|
|
134
|
+
exportOrder: ["gone"],
|
|
135
|
+
});
|
|
136
|
+
expect(order.exportOrdered).toBe(false);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("orders a model off the surface as an ordinary one: no curated set, no export order", () => {
|
|
140
|
+
// The caller passes no `publicSurface` for an index.malloy in a folder, or for the
|
|
141
|
+
// root one when the server marks it hidden.
|
|
142
|
+
const sources = [src("users", 0), src("orders", 9)];
|
|
143
|
+
const order = orderPickerSources(
|
|
144
|
+
sources,
|
|
145
|
+
pickDefaultSource(sources, "sub/index.malloy"),
|
|
146
|
+
);
|
|
147
|
+
expect(names(order.sources)).toEqual(["orders", "users"]);
|
|
148
|
+
expect(order.curated).toBe(false);
|
|
149
|
+
expect(order.exportOrdered).toBe(false);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("never claims Most views for a default that is set aside for fewer views (a base its extension outranks)", () => {
|
|
153
|
+
// purchase_events extends events, so it carries events' view plus its own; the model
|
|
154
|
+
// opens on the base, which does not have the most views.
|
|
155
|
+
const events = src("events", 1, { viewNames: ["by_day"] });
|
|
156
|
+
const purchases = src("purchase_events", 3, {
|
|
157
|
+
viewNames: ["by_day", "p1", "p2"],
|
|
158
|
+
});
|
|
159
|
+
const sources = [events, purchases];
|
|
160
|
+
const def = pickDefaultSource(sources, "ga4.malloy");
|
|
161
|
+
expect(def?.name).toBe("events");
|
|
162
|
+
const order = orderPickerSources(sources, def);
|
|
163
|
+
expect(names(order.sources)).toEqual(["events", "purchase_events"]);
|
|
164
|
+
expect(order.mostViews).toBeUndefined();
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("claims Most views on a tie (the default has as many as any), never on zero", () => {
|
|
168
|
+
const tied = [src("a", 2), src("b", 2)];
|
|
169
|
+
expect(
|
|
170
|
+
orderPickerSources(tied, pickDefaultSource(tied, "b.malloy"))
|
|
171
|
+
.mostViews,
|
|
172
|
+
).toBe("b");
|
|
173
|
+
const none = [src("a", 0), src("b", 0)];
|
|
174
|
+
expect(
|
|
175
|
+
orderPickerSources(none, pickDefaultSource(none, "m.malloy"))
|
|
176
|
+
.mostViews,
|
|
177
|
+
).toBeUndefined();
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
describe("pickerDescription", () => {
|
|
182
|
+
it("prefers the author's #(doc)", () => {
|
|
183
|
+
expect(
|
|
184
|
+
pickerDescription("Flights, one row each.", "Generated text"),
|
|
185
|
+
).toEqual({
|
|
186
|
+
text: "Flights, one row each.",
|
|
187
|
+
generated: false,
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("falls back to the index's summary, marked generated", () => {
|
|
192
|
+
expect(
|
|
193
|
+
pickerDescription(undefined, "Airline flights with delays"),
|
|
194
|
+
).toEqual({
|
|
195
|
+
text: "Airline flights with delays",
|
|
196
|
+
generated: true,
|
|
197
|
+
});
|
|
198
|
+
expect(pickerDescription(" ", "Summary")).toEqual({
|
|
199
|
+
text: "Summary",
|
|
200
|
+
generated: true,
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("is nothing when there is neither", () => {
|
|
205
|
+
expect(pickerDescription(undefined, undefined)).toBeUndefined();
|
|
206
|
+
expect(pickerDescription("", " ")).toBeUndefined();
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
describe("sourceMetaLine", () => {
|
|
211
|
+
it("counts views, the header's fields and joins", () => {
|
|
212
|
+
expect(
|
|
213
|
+
sourceMetaLine(
|
|
214
|
+
src("flights", 9, { dimensions: 20, measures: 8, joins: 4 }),
|
|
215
|
+
),
|
|
216
|
+
).toBe("9 views · 37 fields · 4 joins");
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("leaves joins out when there are none, and speaks in the singular for one", () => {
|
|
220
|
+
expect(
|
|
221
|
+
sourceMetaLine(src("carriers", 0, { dimensions: 3, measures: 1 })),
|
|
222
|
+
).toBe("0 views · 4 fields");
|
|
223
|
+
expect(sourceMetaLine(src("one", 1, { joins: 1 }))).toBe(
|
|
224
|
+
"1 view · 1 field · 1 join",
|
|
225
|
+
);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("counts a source's calculations, as its header does", () => {
|
|
229
|
+
const source = {
|
|
230
|
+
...src("orders", 1, { dimensions: 2, measures: 1 }),
|
|
231
|
+
calculations: [field("running_total", "calculate")],
|
|
232
|
+
};
|
|
233
|
+
expect(sourceMetaLine(source)).toBe(
|
|
234
|
+
`1 view · ${fieldCount(source)} fields`,
|
|
235
|
+
);
|
|
236
|
+
expect(fieldCount(source)).toBe(5);
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
describe("exportOrder", () => {
|
|
241
|
+
it("reads every export statement in order, once per name, across lines", () => {
|
|
242
|
+
const text = [
|
|
243
|
+
"##! experimental.access_modifiers",
|
|
244
|
+
'import "a.malloy"',
|
|
245
|
+
"// export { commented_out }",
|
|
246
|
+
"export {",
|
|
247
|
+
" orders, -- the fact table",
|
|
248
|
+
" users",
|
|
249
|
+
"}",
|
|
250
|
+
"export { products, orders }",
|
|
251
|
+
].join("\n");
|
|
252
|
+
expect(exportOrder(text)).toEqual(["orders", "users", "products"]);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("skips block comments, as well as line ones", () => {
|
|
256
|
+
expect(
|
|
257
|
+
exportOrder("/* export { old } */ export { a, /* b, */ c }"),
|
|
258
|
+
).toEqual(["a", "c"]);
|
|
259
|
+
expect(exportOrder("/*\n export { gone }\n*/\nexport { kept }")).toEqual([
|
|
260
|
+
"kept",
|
|
261
|
+
]);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it("matches the keyword without case, as Malloy does", () => {
|
|
265
|
+
expect(exportOrder("EXPORT { a, b }\nExport { c }")).toEqual([
|
|
266
|
+
"a",
|
|
267
|
+
"b",
|
|
268
|
+
"c",
|
|
269
|
+
]);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("reads a backquoted name without its backquotes", () => {
|
|
273
|
+
expect(exportOrder("export { `order items`, users }")).toEqual([
|
|
274
|
+
"order items",
|
|
275
|
+
"users",
|
|
276
|
+
]);
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it("is empty with no text or no export statement", () => {
|
|
280
|
+
expect(exportOrder(undefined)).toEqual([]);
|
|
281
|
+
expect(exportOrder("source: a is duckdb.table('a.parquet')")).toEqual([]);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it("leaves anything the statement does not name after the rest, in its own order", () => {
|
|
285
|
+
expect(
|
|
286
|
+
inExportOrder(
|
|
287
|
+
["a", "b", "c", "d"],
|
|
288
|
+
(n) => n,
|
|
289
|
+
exportOrder("export { c, a }"),
|
|
290
|
+
),
|
|
291
|
+
).toEqual(["c", "a", "b", "d"]);
|
|
292
|
+
expect(inExportOrder(["b", "a"], (n) => n, [])).toEqual(["b", "a"]);
|
|
293
|
+
});
|
|
294
|
+
});
|