@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,48 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The model page a server asks for, its `PUBLISHER_MODEL_PAGE`. The Explorer
|
|
6
|
+
* writes the queries it builds back to Malloy text with string values unescaped
|
|
7
|
+
* (`escapeString` in `@malloydata/malloy-interfaces` is a TODO), so a drilled
|
|
8
|
+
* value holding a double quote rewrites its query; `viewer` shows a page that
|
|
9
|
+
* builds none.
|
|
10
|
+
*/
|
|
11
|
+
export type ModelPage = "explorer" | "viewer";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The page to show, from what `/status` answered: `viewer` when it says so, and
|
|
15
|
+
* when it could not be read at all, since a deployment that chose `viewer` has
|
|
16
|
+
* chosen not to show the Explorer and the viewer writes no value into Malloy on
|
|
17
|
+
* any server. `explorer` otherwise, a server that predates the setting
|
|
18
|
+
* included. While `/status` is still loading this says `explorer`; a caller
|
|
19
|
+
* that would draw the Explorer waits on loading first.
|
|
20
|
+
*/
|
|
21
|
+
export function modelPageOf(
|
|
22
|
+
status: { modelPage?: unknown } | undefined,
|
|
23
|
+
failed: boolean,
|
|
24
|
+
): ModelPage {
|
|
25
|
+
if (failed) return "viewer";
|
|
26
|
+
return status?.modelPage === "viewer" ? "viewer" : "explorer";
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The model page, and whether to wait for it, from the `/status` query as
|
|
31
|
+
* react-query reports it. It waits while the query is pending, with no answer
|
|
32
|
+
* and no error yet, which includes a retry paused offline or in a hidden tab:
|
|
33
|
+
* there react-query's `isLoading` is false although nothing has answered, and
|
|
34
|
+
* a `viewer` server would draw the Explorer.
|
|
35
|
+
*/
|
|
36
|
+
export function modelPageState(query: {
|
|
37
|
+
data: { modelPage?: unknown } | undefined;
|
|
38
|
+
isPending: boolean;
|
|
39
|
+
isError: boolean;
|
|
40
|
+
}): { modelPage: ModelPage; isLoadingStatus: boolean } {
|
|
41
|
+
return {
|
|
42
|
+
modelPage: modelPageOf(
|
|
43
|
+
query.data,
|
|
44
|
+
query.isError && query.data === undefined,
|
|
45
|
+
),
|
|
46
|
+
isLoadingStatus: query.isPending,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
@@ -6,8 +6,7 @@ import {
|
|
|
6
6
|
DimensionSpec,
|
|
7
7
|
getDimensionKey,
|
|
8
8
|
} from "./useDimensionalFilterRangeData";
|
|
9
|
-
import {
|
|
10
|
-
import { escapeMalloyString } from "../utils/malloyString";
|
|
9
|
+
import { filterCondition } from "../utils/filterCondition";
|
|
11
10
|
|
|
12
11
|
/**
|
|
13
12
|
* Match types for filtering dimensions
|
|
@@ -80,63 +79,9 @@ export interface UseDimensionFiltersResult {
|
|
|
80
79
|
generateWhereClause: () => string;
|
|
81
80
|
}
|
|
82
81
|
|
|
83
|
-
/**
|
|
84
|
-
* Generates a Malloy filter condition for a single filter selection
|
|
85
|
-
*/
|
|
82
|
+
/** A selection as a Malloy condition on its dimension. */
|
|
86
83
|
function generateFilterCondition(selection: FilterSelection): string {
|
|
87
|
-
|
|
88
|
-
const fieldName = `\`${dimensionName}\``;
|
|
89
|
-
|
|
90
|
-
// Determine if this is a date field based on the value
|
|
91
|
-
const isDate = value instanceof Date;
|
|
92
|
-
|
|
93
|
-
switch (matchType) {
|
|
94
|
-
case "Equals":
|
|
95
|
-
case "Semantic Search":
|
|
96
|
-
if (Array.isArray(value) && value.length > 0) {
|
|
97
|
-
// Handle multi-select: (field = val1 or field = val2)
|
|
98
|
-
const conditions = value.map(
|
|
99
|
-
(v) => `${fieldName} = ${formatMalloyValue(v, isDate)}`,
|
|
100
|
-
);
|
|
101
|
-
return `(${conditions.join(" or ")})`;
|
|
102
|
-
}
|
|
103
|
-
// Empty array or single value
|
|
104
|
-
if (Array.isArray(value)) return "";
|
|
105
|
-
return `${fieldName} = ${formatMalloyValue(value, isDate)}`;
|
|
106
|
-
|
|
107
|
-
case "Contains":
|
|
108
|
-
if (typeof value === "string") {
|
|
109
|
-
return `${fieldName} ~ f'%${escapeMalloyString(value)}%'`;
|
|
110
|
-
}
|
|
111
|
-
if (Array.isArray(value)) return "";
|
|
112
|
-
return `${fieldName} = ${formatMalloyValue(value)}`;
|
|
113
|
-
|
|
114
|
-
case "After":
|
|
115
|
-
if (Array.isArray(value)) return "";
|
|
116
|
-
return `${fieldName} > ${formatMalloyValue(value, isDate)}`;
|
|
117
|
-
|
|
118
|
-
case "Before":
|
|
119
|
-
if (Array.isArray(value)) return "";
|
|
120
|
-
return `${fieldName} < ${formatMalloyValue(value, isDate)}`;
|
|
121
|
-
|
|
122
|
-
case "Greater Than":
|
|
123
|
-
if (Array.isArray(value)) return "";
|
|
124
|
-
return `${fieldName} > ${formatMalloyValue(value)}`;
|
|
125
|
-
|
|
126
|
-
case "Less Than":
|
|
127
|
-
if (Array.isArray(value)) return "";
|
|
128
|
-
return `${fieldName} < ${formatMalloyValue(value)}`;
|
|
129
|
-
|
|
130
|
-
case "Between":
|
|
131
|
-
if (Array.isArray(value)) return "";
|
|
132
|
-
if (value2 !== undefined) {
|
|
133
|
-
return `${fieldName} >= ${formatMalloyValue(value, isDate)} and ${fieldName} <= ${formatMalloyValue(value2, isDate)}`;
|
|
134
|
-
}
|
|
135
|
-
return `${fieldName} >= ${formatMalloyValue(value, isDate)}`;
|
|
136
|
-
|
|
137
|
-
default:
|
|
138
|
-
return "";
|
|
139
|
-
}
|
|
84
|
+
return filterCondition(`\`${selection.dimensionName}\``, selection);
|
|
140
85
|
}
|
|
141
86
|
|
|
142
87
|
/**
|
|
@@ -5,8 +5,7 @@ import { useCallback, useMemo, useState } from "react";
|
|
|
5
5
|
import { createEmbeddedQueryResult } from "../components/QueryResult";
|
|
6
6
|
import { encodeResourceUri } from "../utils/formatting";
|
|
7
7
|
import { FilterSelection } from "./useDimensionFilters";
|
|
8
|
-
import {
|
|
9
|
-
import { escapeMalloyString } from "../utils/malloyString";
|
|
8
|
+
import { filterCondition } from "../utils/filterCondition";
|
|
10
9
|
|
|
11
10
|
/**
|
|
12
11
|
* Parameters for the useDimensionFiltersQuery hook
|
|
@@ -46,63 +45,9 @@ export interface DimensionFiltersQueryResult {
|
|
|
46
45
|
canExecute: boolean;
|
|
47
46
|
}
|
|
48
47
|
|
|
49
|
-
/**
|
|
50
|
-
* Generates a Malloy filter condition for a single filter selection
|
|
51
|
-
*/
|
|
48
|
+
/** A selection as a Malloy condition on its dimension. */
|
|
52
49
|
function generateFilterCondition(selection: FilterSelection): string {
|
|
53
|
-
|
|
54
|
-
const fieldName = `\`${dimensionName}\``;
|
|
55
|
-
|
|
56
|
-
// Determine if this is a date field based on the value
|
|
57
|
-
const isDate = value instanceof Date;
|
|
58
|
-
|
|
59
|
-
switch (matchType) {
|
|
60
|
-
case "Equals":
|
|
61
|
-
case "Semantic Search":
|
|
62
|
-
if (Array.isArray(value) && value.length > 0) {
|
|
63
|
-
// Handle multi-select: (field = val1 or field = val2)
|
|
64
|
-
const conditions = value.map(
|
|
65
|
-
(v) => `${fieldName} = ${formatMalloyValue(v, isDate)}`,
|
|
66
|
-
);
|
|
67
|
-
return `(${conditions.join(" or ")})`;
|
|
68
|
-
}
|
|
69
|
-
// Empty array or single value
|
|
70
|
-
if (Array.isArray(value)) return "";
|
|
71
|
-
return `${fieldName} = ${formatMalloyValue(value, isDate)}`;
|
|
72
|
-
|
|
73
|
-
case "Contains":
|
|
74
|
-
if (typeof value === "string") {
|
|
75
|
-
return `${fieldName} ~ f'%${escapeMalloyString(value)}%'`;
|
|
76
|
-
}
|
|
77
|
-
if (Array.isArray(value)) return "";
|
|
78
|
-
return `${fieldName} = ${formatMalloyValue(value)}`;
|
|
79
|
-
|
|
80
|
-
case "After":
|
|
81
|
-
if (Array.isArray(value)) return "";
|
|
82
|
-
return `${fieldName} > ${formatMalloyValue(value, isDate)}`;
|
|
83
|
-
|
|
84
|
-
case "Before":
|
|
85
|
-
if (Array.isArray(value)) return "";
|
|
86
|
-
return `${fieldName} < ${formatMalloyValue(value, isDate)}`;
|
|
87
|
-
|
|
88
|
-
case "Greater Than":
|
|
89
|
-
if (Array.isArray(value)) return "";
|
|
90
|
-
return `${fieldName} > ${formatMalloyValue(value)}`;
|
|
91
|
-
|
|
92
|
-
case "Less Than":
|
|
93
|
-
if (Array.isArray(value)) return "";
|
|
94
|
-
return `${fieldName} < ${formatMalloyValue(value)}`;
|
|
95
|
-
|
|
96
|
-
case "Between":
|
|
97
|
-
if (Array.isArray(value)) return "";
|
|
98
|
-
if (value2 !== undefined) {
|
|
99
|
-
return `${fieldName} >= ${formatMalloyValue(value, isDate)} and ${fieldName} <= ${formatMalloyValue(value2, isDate)}`;
|
|
100
|
-
}
|
|
101
|
-
return `${fieldName} >= ${formatMalloyValue(value, isDate)}`;
|
|
102
|
-
|
|
103
|
-
default:
|
|
104
|
-
return "";
|
|
105
|
-
}
|
|
50
|
+
return filterCondition(`\`${selection.dimensionName}\``, selection);
|
|
106
51
|
}
|
|
107
52
|
|
|
108
53
|
/**
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { describe, expect, it } from "bun:test";
|
|
5
|
+
import type { DimensionSpec } from "./useDimensionalFilterRangeData";
|
|
6
|
+
import { buildDimensionalIndexQuery } from "./useDimensionalFilterRangeData";
|
|
7
|
+
import type { FilterSelection } from "./useDimensionFilters";
|
|
8
|
+
|
|
9
|
+
const specs = [
|
|
10
|
+
{
|
|
11
|
+
dimensionName: "category",
|
|
12
|
+
filterType: "Star",
|
|
13
|
+
source: "orders",
|
|
14
|
+
model: "orders.malloy",
|
|
15
|
+
},
|
|
16
|
+
] as DimensionSpec[];
|
|
17
|
+
|
|
18
|
+
function whereOf(filters: FilterSelection[]): string | undefined {
|
|
19
|
+
const query = buildDimensionalIndexQuery("orders", specs, 10, filters);
|
|
20
|
+
return /where: (.*)\n/.exec(query)?.[1];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
describe("buildDimensionalIndexQuery", () => {
|
|
24
|
+
it("writes a Contains filter as the text it was given, not as alternatives or wildcards", () => {
|
|
25
|
+
const where = whereOf([
|
|
26
|
+
{
|
|
27
|
+
dimensionName: "name",
|
|
28
|
+
matchType: "Contains",
|
|
29
|
+
value: "a,b",
|
|
30
|
+
source: "orders",
|
|
31
|
+
} as FilterSelection,
|
|
32
|
+
]);
|
|
33
|
+
// The same text `filterCondition` writes for the result rows: the
|
|
34
|
+
// comma escaped, one `contains` clause.
|
|
35
|
+
expect(where).toBe("`name` ~ f'%a\\,b%'");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("spells a date on Greater Than and Less Than as a date", () => {
|
|
39
|
+
const where = whereOf([
|
|
40
|
+
{
|
|
41
|
+
dimensionName: "ordered_at",
|
|
42
|
+
matchType: "Greater Than",
|
|
43
|
+
value: new Date(2024, 0, 5),
|
|
44
|
+
source: "orders",
|
|
45
|
+
} as FilterSelection,
|
|
46
|
+
{
|
|
47
|
+
dimensionName: "ordered_at",
|
|
48
|
+
matchType: "Less Than",
|
|
49
|
+
value: new Date(2024, 1, 5),
|
|
50
|
+
source: "orders",
|
|
51
|
+
} as FilterSelection,
|
|
52
|
+
]);
|
|
53
|
+
expect(where).toBe(
|
|
54
|
+
"`ordered_at` > @2024-01-05 and `ordered_at` < @2024-02-05",
|
|
55
|
+
);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
@@ -5,8 +5,7 @@ import { useMemo } from "react";
|
|
|
5
5
|
import { useServer } from "../components/ServerProvider";
|
|
6
6
|
import { FilterSelection, FilterValuePrimitive } from "./useDimensionFilters";
|
|
7
7
|
import { useQueryWithApiError } from "./useQueryWithApiError";
|
|
8
|
-
import {
|
|
9
|
-
import { escapeMalloyString } from "../utils/malloyString";
|
|
8
|
+
import { filterCondition } from "../utils/filterCondition";
|
|
10
9
|
|
|
11
10
|
/**
|
|
12
11
|
* Filter types for dimensions
|
|
@@ -119,65 +118,6 @@ export interface DimensionalFilterRangeDataResult {
|
|
|
119
118
|
refetch: () => void;
|
|
120
119
|
}
|
|
121
120
|
|
|
122
|
-
/**
|
|
123
|
-
* Generates a Malloy filter condition for a single filter selection
|
|
124
|
-
*/
|
|
125
|
-
function generateFilterCondition(selection: FilterSelection): string {
|
|
126
|
-
const { dimensionName, matchType, value, value2 } = selection;
|
|
127
|
-
const fieldName = `\`${dimensionName}\``;
|
|
128
|
-
|
|
129
|
-
// Determine if this is a date field based on the value
|
|
130
|
-
const isDate = value instanceof Date;
|
|
131
|
-
|
|
132
|
-
switch (matchType) {
|
|
133
|
-
case "Equals":
|
|
134
|
-
case "Semantic Search":
|
|
135
|
-
if (Array.isArray(value) && value.length > 0) {
|
|
136
|
-
// Handle multi-select: (field = val1 or field = val2)
|
|
137
|
-
const conditions = value.map(
|
|
138
|
-
(v) => `${fieldName} = ${formatMalloyValue(v, isDate)}`,
|
|
139
|
-
);
|
|
140
|
-
return `(${conditions.join(" or ")})`;
|
|
141
|
-
}
|
|
142
|
-
// Empty array or single value
|
|
143
|
-
if (Array.isArray(value)) return "";
|
|
144
|
-
return `${fieldName} = ${formatMalloyValue(value, isDate)}`;
|
|
145
|
-
|
|
146
|
-
case "Contains":
|
|
147
|
-
if (typeof value === "string") {
|
|
148
|
-
return `${fieldName} ~ f'%${escapeMalloyString(value)}%'`;
|
|
149
|
-
}
|
|
150
|
-
if (Array.isArray(value)) return "";
|
|
151
|
-
return `${fieldName} = ${formatMalloyValue(value)}`;
|
|
152
|
-
|
|
153
|
-
case "After":
|
|
154
|
-
if (Array.isArray(value)) return "";
|
|
155
|
-
return `${fieldName} > ${formatMalloyValue(value, isDate)}`;
|
|
156
|
-
|
|
157
|
-
case "Before":
|
|
158
|
-
if (Array.isArray(value)) return "";
|
|
159
|
-
return `${fieldName} < ${formatMalloyValue(value, isDate)}`;
|
|
160
|
-
|
|
161
|
-
case "Greater Than":
|
|
162
|
-
if (Array.isArray(value)) return "";
|
|
163
|
-
return `${fieldName} > ${formatMalloyValue(value)}`;
|
|
164
|
-
|
|
165
|
-
case "Less Than":
|
|
166
|
-
if (Array.isArray(value)) return "";
|
|
167
|
-
return `${fieldName} < ${formatMalloyValue(value)}`;
|
|
168
|
-
|
|
169
|
-
case "Between":
|
|
170
|
-
if (Array.isArray(value)) return "";
|
|
171
|
-
if (value2 !== undefined) {
|
|
172
|
-
return `${fieldName} >= ${formatMalloyValue(value, isDate)} and ${fieldName} <= ${formatMalloyValue(value2, isDate)}`;
|
|
173
|
-
}
|
|
174
|
-
return `${fieldName} >= ${formatMalloyValue(value, isDate)}`;
|
|
175
|
-
|
|
176
|
-
default:
|
|
177
|
-
return "";
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
121
|
/**
|
|
182
122
|
* Groups dimension specs by source/model combination
|
|
183
123
|
* Returns a map where key is "source|model" and value is array of specs for that combo
|
|
@@ -201,8 +141,11 @@ function groupSpecsBySourceModel(
|
|
|
201
141
|
* Builds a Malloy index query to fetch dimension values for a single source
|
|
202
142
|
* Uses Malloy's built-in index operator for dimensional search
|
|
203
143
|
* Reference: https://docs.malloydata.dev/documentation/patterns/dim_index
|
|
144
|
+
*
|
|
145
|
+
* Exported for its spec: the active filters are written by `filterCondition`,
|
|
146
|
+
* the same as the result rows' query, so both agree on what a filter matches.
|
|
204
147
|
*/
|
|
205
|
-
function buildDimensionalIndexQuery(
|
|
148
|
+
export function buildDimensionalIndexQuery(
|
|
206
149
|
source: string,
|
|
207
150
|
dimensionSpecs: DimensionSpec[],
|
|
208
151
|
indexLimit: number,
|
|
@@ -236,7 +179,9 @@ function buildDimensionalIndexQuery(
|
|
|
236
179
|
const whereConditions =
|
|
237
180
|
filtersForSource.length > 0
|
|
238
181
|
? filtersForSource
|
|
239
|
-
.map((selection) =>
|
|
182
|
+
.map((selection) =>
|
|
183
|
+
filterCondition(`\`${selection.dimensionName}\``, selection),
|
|
184
|
+
)
|
|
240
185
|
.filter((condition) => condition.length > 0)
|
|
241
186
|
.join(" and ")
|
|
242
187
|
: "";
|
|
@@ -28,7 +28,9 @@ const executeQueryModel = mock(
|
|
|
28
28
|
) => pending(),
|
|
29
29
|
);
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
// What `/status` said: a test sets the model page and clears it again.
|
|
32
|
+
const serverContext: { modelPage?: string } = {};
|
|
33
|
+
mockServerProvider({ models: { executeQueryModel } }, serverContext);
|
|
32
34
|
|
|
33
35
|
const { buildSuggestQuery, SUGGEST_OPTION_LIMIT, useSuggestOptions } =
|
|
34
36
|
await import("./useSuggestOptions");
|
|
@@ -90,6 +92,7 @@ const renderGated = (applied = APPLIED) =>
|
|
|
90
92
|
beforeEach(() => {
|
|
91
93
|
clearCache();
|
|
92
94
|
executeQueryModel.mockClear();
|
|
95
|
+
delete serverContext.modelPage;
|
|
93
96
|
});
|
|
94
97
|
|
|
95
98
|
it("sends the version with the suggest query and keys on it", async () => {
|
|
@@ -187,6 +190,56 @@ it("runs a suggest as the document's definitions plus one run:, by name or by di
|
|
|
187
190
|
]);
|
|
188
191
|
});
|
|
189
192
|
|
|
193
|
+
// A server whose PUBLISHER_MODEL_PAGE is viewer writes every name quoted; the
|
|
194
|
+
// default writes them as the suggest gave them (every other test here).
|
|
195
|
+
it("quotes the suggest's names on a server whose model page is the viewer", async () => {
|
|
196
|
+
serverContext.modelPage = "viewer";
|
|
197
|
+
renderHook(
|
|
198
|
+
() =>
|
|
199
|
+
useSuggestOptions("env", "pkg", "README.malloy", [
|
|
200
|
+
{
|
|
201
|
+
name: "MAKER",
|
|
202
|
+
control: "select",
|
|
203
|
+
suggest: { source: "recalls", dimension: "maker.name" },
|
|
204
|
+
},
|
|
205
|
+
]),
|
|
206
|
+
{ wrapper: serverWrapper },
|
|
207
|
+
);
|
|
208
|
+
await waitFor(() => expect(executeQueryModel).toHaveBeenCalled());
|
|
209
|
+
expect((executeQueryModel.mock.calls[0][3] as { query: string }).query).toBe(
|
|
210
|
+
buildSuggestQuery("recalls", "maker.name", undefined, {
|
|
211
|
+
quoteNames: true,
|
|
212
|
+
}),
|
|
213
|
+
);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
// A list fetched before /status answered went unquoted; once it says viewer
|
|
217
|
+
// the list is a different query, so it is asked again, quoted.
|
|
218
|
+
it("asks again, quoted, when the server's model page turns out to be the viewer", async () => {
|
|
219
|
+
const specs: Given[] = [
|
|
220
|
+
{
|
|
221
|
+
name: "YEAR",
|
|
222
|
+
control: "select",
|
|
223
|
+
suggest: { source: "sales", dimension: "year" },
|
|
224
|
+
},
|
|
225
|
+
];
|
|
226
|
+
const { rerender } = renderHook(
|
|
227
|
+
() => useSuggestOptions("env", "pkg", "README.malloy", specs),
|
|
228
|
+
{ wrapper: serverWrapper },
|
|
229
|
+
);
|
|
230
|
+
await waitFor(() => expect(executeQueryModel).toHaveBeenCalledTimes(1));
|
|
231
|
+
expect((executeQueryModel.mock.calls[0][3] as { query: string }).query).toBe(
|
|
232
|
+
buildSuggestQuery("sales", "year"),
|
|
233
|
+
);
|
|
234
|
+
|
|
235
|
+
serverContext.modelPage = "viewer";
|
|
236
|
+
rerender();
|
|
237
|
+
await waitFor(() => expect(executeQueryModel).toHaveBeenCalledTimes(2));
|
|
238
|
+
expect((executeQueryModel.mock.calls[1][3] as { query: string }).query).toBe(
|
|
239
|
+
buildSuggestQuery("sales", "year", undefined, { quoteNames: true }),
|
|
240
|
+
);
|
|
241
|
+
});
|
|
242
|
+
|
|
190
243
|
describe("a list cut off at the limit", () => {
|
|
191
244
|
/** `n` distinct values for one column, `Value 0001` … in order. */
|
|
192
245
|
const rows = (column: string, n: number) =>
|
|
@@ -115,6 +115,77 @@ describe("the generated suggest query", () => {
|
|
|
115
115
|
});
|
|
116
116
|
});
|
|
117
117
|
|
|
118
|
+
// With quoteNames, as on a server whose PUBLISHER_MODEL_PAGE is viewer and in
|
|
119
|
+
// the model viewer: every name backtick-quoted. Without it (above), the names
|
|
120
|
+
// go as the suggest gave them, as before.
|
|
121
|
+
describe("the generated suggest query, names quoted", () => {
|
|
122
|
+
const quoted = { quoteNames: true };
|
|
123
|
+
|
|
124
|
+
it("quotes the source, each segment of the path, and the output column", () => {
|
|
125
|
+
expect(
|
|
126
|
+
buildSuggestQuery(
|
|
127
|
+
"order_items",
|
|
128
|
+
"products.category",
|
|
129
|
+
undefined,
|
|
130
|
+
quoted,
|
|
131
|
+
),
|
|
132
|
+
).toBe(
|
|
133
|
+
"run: `order_items` -> {\n" +
|
|
134
|
+
" group_by: `products`.`category`\n" +
|
|
135
|
+
" order_by: `category` asc\n" +
|
|
136
|
+
` limit: ${SUGGEST_OPTION_LIMIT + 1}\n` +
|
|
137
|
+
"}",
|
|
138
|
+
);
|
|
139
|
+
expect(
|
|
140
|
+
buildSuggestQuery("products", "category", undefined, quoted),
|
|
141
|
+
).toContain("order_by: `category` asc");
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("quotes every name, so a reserved word is still a name", () => {
|
|
145
|
+
// `year` and `source` are Malloy keywords: bare, the query does not
|
|
146
|
+
// compile. A name the author already quoted is kept as written.
|
|
147
|
+
expect(buildSuggestQuery("source", "dates.year", undefined, quoted)).toBe(
|
|
148
|
+
"run: `source` -> {\n" +
|
|
149
|
+
" group_by: `dates`.`year`\n" +
|
|
150
|
+
" order_by: `year` asc\n" +
|
|
151
|
+
` limit: ${SUGGEST_OPTION_LIMIT + 1}\n` +
|
|
152
|
+
"}",
|
|
153
|
+
);
|
|
154
|
+
expect(
|
|
155
|
+
buildSuggestQuery("`order items`", "`has space`", undefined, quoted),
|
|
156
|
+
).toContain("group_by: `has space`\n");
|
|
157
|
+
// A quoted name holding a dot is one name, not a path.
|
|
158
|
+
expect(buildSuggestQuery("items", "`a.b`", undefined, quoted)).toContain(
|
|
159
|
+
"group_by: `a.b`\n order_by: `a.b` asc\n",
|
|
160
|
+
);
|
|
161
|
+
expect(
|
|
162
|
+
buildSuggestQuery("items", "maker.`a.b`", undefined, quoted),
|
|
163
|
+
).toContain("group_by: `maker`.`a.b`\n");
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("splits a long unclosed quoted name in linear time", () => {
|
|
167
|
+
// The shape a backtracking pattern stalls on: a backtick that never
|
|
168
|
+
// closes, then many escaped backticks.
|
|
169
|
+
const name = "`" + "\\`".repeat(50_000) + "x";
|
|
170
|
+
const started = performance.now();
|
|
171
|
+
buildSuggestQuery("items", name, undefined, quoted);
|
|
172
|
+
expect(performance.now() - started).toBeLessThan(500);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("quotes a search the same way", () => {
|
|
176
|
+
expect(
|
|
177
|
+
buildSuggestQuery("recalls", "maker.name", "General Motors", quoted),
|
|
178
|
+
).toBe(
|
|
179
|
+
"run: `recalls` -> {\n" +
|
|
180
|
+
" group_by: `maker`.`name`\n" +
|
|
181
|
+
" where: strpos(lower(`maker`.`name`::string), 'general motors') > 0\n" +
|
|
182
|
+
" order_by: `name` asc\n" +
|
|
183
|
+
` limit: ${SUGGEST_SEARCH_LIMIT + 1}\n` +
|
|
184
|
+
"}",
|
|
185
|
+
);
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
|
|
118
189
|
describe("a search of a cut-off list", () => {
|
|
119
190
|
it("matches ignoring case, as a plain substring, with a limit and the same order", () => {
|
|
120
191
|
expect(buildSuggestQuery("recalls", "maker.name", "General Motors")).toBe(
|