@lightdash/common 1.144.3 → 1.146.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.
- package/dist/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/compiler/exploreCompiler.mock.d.ts.map +1 -1
- package/dist/cjs/compiler/exploreCompiler.mock.js +3 -0
- package/dist/cjs/compiler/exploreCompiler.mock.js.map +1 -1
- package/dist/cjs/ee/apps/types.d.ts +3 -0
- package/dist/cjs/ee/apps/types.d.ts.map +1 -1
- package/dist/cjs/ee/apps/types.js.map +1 -1
- package/dist/cjs/index.d.ts +2 -0
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +2 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/api/paginatedQuery.d.ts +7 -1
- package/dist/cjs/types/api/paginatedQuery.d.ts.map +1 -1
- package/dist/cjs/types/api/paginatedQuery.js.map +1 -1
- package/dist/cjs/types/api.d.ts +2 -1
- package/dist/cjs/types/api.d.ts.map +1 -1
- package/dist/cjs/types/api.js.map +1 -1
- package/dist/cjs/types/featureFlags.d.ts +2 -1
- package/dist/cjs/types/featureFlags.d.ts.map +1 -1
- package/dist/cjs/types/featureFlags.js +4 -0
- package/dist/cjs/types/featureFlags.js.map +1 -1
- package/dist/cjs/types/mergeQuery.d.ts +339 -0
- package/dist/cjs/types/mergeQuery.d.ts.map +1 -0
- package/dist/cjs/types/mergeQuery.js +339 -0
- package/dist/cjs/types/mergeQuery.js.map +1 -0
- package/dist/cjs/types/mergeQuery.test.d.ts +2 -0
- package/dist/cjs/types/mergeQuery.test.d.ts.map +1 -0
- package/dist/cjs/types/mergeQuery.test.js +230 -0
- package/dist/cjs/types/mergeQuery.test.js.map +1 -0
- package/dist/cjs/types/results.d.ts +2 -1
- package/dist/cjs/types/results.d.ts.map +1 -1
- package/dist/cjs/types/results.js.map +1 -1
- package/dist/cjs/types/savedCharts.d.ts +7 -1
- package/dist/cjs/types/savedCharts.d.ts.map +1 -1
- package/dist/cjs/types/savedCharts.js.map +1 -1
- package/dist/cjs/types/warehouse.d.ts +11 -0
- package/dist/cjs/types/warehouse.d.ts.map +1 -1
- package/dist/cjs/types/warehouse.js.map +1 -1
- package/dist/cjs/utils/convertCustomDimensionsToYaml.d.ts.map +1 -1
- package/dist/cjs/utils/convertCustomDimensionsToYaml.js +7 -0
- package/dist/cjs/utils/convertCustomDimensionsToYaml.js.map +1 -1
- package/dist/cjs/utils/mergeQueryItems.d.ts +23 -0
- package/dist/cjs/utils/mergeQueryItems.d.ts.map +1 -0
- package/dist/cjs/utils/mergeQueryItems.js +29 -0
- package/dist/cjs/utils/mergeQueryItems.js.map +1 -0
- package/dist/cjs/utils/mergeQueryItems.test.d.ts +2 -0
- package/dist/cjs/utils/mergeQueryItems.test.d.ts.map +1 -0
- package/dist/cjs/utils/mergeQueryItems.test.js +68 -0
- package/dist/cjs/utils/mergeQueryItems.test.js.map +1 -0
- package/dist/cjs/utils/virtualView.d.ts.map +1 -1
- package/dist/cjs/utils/virtualView.js +3 -0
- package/dist/cjs/utils/virtualView.js.map +1 -1
- package/dist/cjs/utils/virtualView.test.js +3 -0
- package/dist/cjs/utils/virtualView.test.js.map +1 -1
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/compiler/exploreCompiler.mock.d.ts.map +1 -1
- package/dist/esm/compiler/exploreCompiler.mock.js +3 -0
- package/dist/esm/compiler/exploreCompiler.mock.js.map +1 -1
- package/dist/esm/ee/apps/types.d.ts +3 -0
- package/dist/esm/ee/apps/types.d.ts.map +1 -1
- package/dist/esm/ee/apps/types.js.map +1 -1
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/api/paginatedQuery.d.ts +7 -1
- package/dist/esm/types/api/paginatedQuery.d.ts.map +1 -1
- package/dist/esm/types/api/paginatedQuery.js.map +1 -1
- package/dist/esm/types/api.d.ts +2 -1
- package/dist/esm/types/api.d.ts.map +1 -1
- package/dist/esm/types/api.js.map +1 -1
- package/dist/esm/types/featureFlags.d.ts +2 -1
- package/dist/esm/types/featureFlags.d.ts.map +1 -1
- package/dist/esm/types/featureFlags.js +4 -0
- package/dist/esm/types/featureFlags.js.map +1 -1
- package/dist/esm/types/mergeQuery.d.ts +339 -0
- package/dist/esm/types/mergeQuery.d.ts.map +1 -0
- package/dist/esm/types/mergeQuery.js +331 -0
- package/dist/esm/types/mergeQuery.js.map +1 -0
- package/dist/esm/types/mergeQuery.test.d.ts +2 -0
- package/dist/esm/types/mergeQuery.test.d.ts.map +1 -0
- package/dist/esm/types/mergeQuery.test.js +228 -0
- package/dist/esm/types/mergeQuery.test.js.map +1 -0
- package/dist/esm/types/results.d.ts +2 -1
- package/dist/esm/types/results.d.ts.map +1 -1
- package/dist/esm/types/results.js.map +1 -1
- package/dist/esm/types/savedCharts.d.ts +7 -1
- package/dist/esm/types/savedCharts.d.ts.map +1 -1
- package/dist/esm/types/savedCharts.js.map +1 -1
- package/dist/esm/types/warehouse.d.ts +11 -0
- package/dist/esm/types/warehouse.d.ts.map +1 -1
- package/dist/esm/types/warehouse.js.map +1 -1
- package/dist/esm/utils/convertCustomDimensionsToYaml.d.ts.map +1 -1
- package/dist/esm/utils/convertCustomDimensionsToYaml.js +7 -0
- package/dist/esm/utils/convertCustomDimensionsToYaml.js.map +1 -1
- package/dist/esm/utils/mergeQueryItems.d.ts +23 -0
- package/dist/esm/utils/mergeQueryItems.d.ts.map +1 -0
- package/dist/esm/utils/mergeQueryItems.js +25 -0
- package/dist/esm/utils/mergeQueryItems.js.map +1 -0
- package/dist/esm/utils/mergeQueryItems.test.d.ts +2 -0
- package/dist/esm/utils/mergeQueryItems.test.d.ts.map +1 -0
- package/dist/esm/utils/mergeQueryItems.test.js +66 -0
- package/dist/esm/utils/mergeQueryItems.test.js.map +1 -0
- package/dist/esm/utils/virtualView.d.ts.map +1 -1
- package/dist/esm/utils/virtualView.js +3 -0
- package/dist/esm/utils/virtualView.js.map +1 -1
- package/dist/esm/utils/virtualView.test.js +3 -0
- package/dist/esm/utils/virtualView.test.js.map +1 -1
- package/dist/types/.tsbuildinfo +1 -1
- package/dist/types/compiler/exploreCompiler.mock.d.ts.map +1 -1
- package/dist/types/compiler/exploreCompiler.mock.js +3 -0
- package/dist/types/compiler/exploreCompiler.mock.js.map +1 -1
- package/dist/types/ee/apps/types.d.ts +3 -0
- package/dist/types/ee/apps/types.d.ts.map +1 -1
- package/dist/types/ee/apps/types.js.map +1 -1
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +2 -0
- package/dist/types/index.js.map +1 -1
- package/dist/types/types/api/paginatedQuery.d.ts +7 -1
- package/dist/types/types/api/paginatedQuery.d.ts.map +1 -1
- package/dist/types/types/api/paginatedQuery.js.map +1 -1
- package/dist/types/types/api.d.ts +2 -1
- package/dist/types/types/api.d.ts.map +1 -1
- package/dist/types/types/api.js.map +1 -1
- package/dist/types/types/featureFlags.d.ts +2 -1
- package/dist/types/types/featureFlags.d.ts.map +1 -1
- package/dist/types/types/featureFlags.js +4 -0
- package/dist/types/types/featureFlags.js.map +1 -1
- package/dist/types/types/mergeQuery.d.ts +339 -0
- package/dist/types/types/mergeQuery.d.ts.map +1 -0
- package/dist/types/types/mergeQuery.js +331 -0
- package/dist/types/types/mergeQuery.js.map +1 -0
- package/dist/types/types/mergeQuery.test.d.ts +2 -0
- package/dist/types/types/mergeQuery.test.d.ts.map +1 -0
- package/dist/types/types/mergeQuery.test.js +228 -0
- package/dist/types/types/mergeQuery.test.js.map +1 -0
- package/dist/types/types/results.d.ts +2 -1
- package/dist/types/types/results.d.ts.map +1 -1
- package/dist/types/types/results.js.map +1 -1
- package/dist/types/types/savedCharts.d.ts +7 -1
- package/dist/types/types/savedCharts.d.ts.map +1 -1
- package/dist/types/types/savedCharts.js.map +1 -1
- package/dist/types/types/warehouse.d.ts +11 -0
- package/dist/types/types/warehouse.d.ts.map +1 -1
- package/dist/types/types/warehouse.js.map +1 -1
- package/dist/types/utils/convertCustomDimensionsToYaml.d.ts.map +1 -1
- package/dist/types/utils/convertCustomDimensionsToYaml.js +7 -0
- package/dist/types/utils/convertCustomDimensionsToYaml.js.map +1 -1
- package/dist/types/utils/mergeQueryItems.d.ts +23 -0
- package/dist/types/utils/mergeQueryItems.d.ts.map +1 -0
- package/dist/types/utils/mergeQueryItems.js +25 -0
- package/dist/types/utils/mergeQueryItems.js.map +1 -0
- package/dist/types/utils/mergeQueryItems.test.d.ts +2 -0
- package/dist/types/utils/mergeQueryItems.test.d.ts.map +1 -0
- package/dist/types/utils/mergeQueryItems.test.js +66 -0
- package/dist/types/utils/mergeQueryItems.test.js.map +1 -0
- package/dist/types/utils/virtualView.d.ts.map +1 -1
- package/dist/types/utils/virtualView.js +3 -0
- package/dist/types/utils/virtualView.js.map +1 -1
- package/dist/types/utils/virtualView.test.js +3 -0
- package/dist/types/utils/virtualView.test.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
import { DimensionType, type FieldId, type ItemsMap, type TimestampDomain } from './field';
|
|
2
|
+
import { type MetricQuery } from './metricQuery';
|
|
3
|
+
import { type ParametersValuesMap } from './parameters';
|
|
4
|
+
import { type PivotConfiguration } from './pivot';
|
|
5
|
+
import { type TimeFrames } from './timeFrames';
|
|
6
|
+
/**
|
|
7
|
+
* How unmatched keys survive the merge. Mirrors the SQL join it compiles to.
|
|
8
|
+
*/
|
|
9
|
+
export declare enum MergeJoinType {
|
|
10
|
+
/** Keep every key from every source. */
|
|
11
|
+
FULL = "full",
|
|
12
|
+
/** Keep every key from the first source only. */
|
|
13
|
+
LEFT = "left",
|
|
14
|
+
/** Keep only keys present in all sources. */
|
|
15
|
+
INNER = "inner"
|
|
16
|
+
}
|
|
17
|
+
/** One side of a merge: a metric query. */
|
|
18
|
+
export type MergeQuerySource = {
|
|
19
|
+
/** Stable id. Names the CTE, and the table its merged fields belong to. */
|
|
20
|
+
id: string;
|
|
21
|
+
metricQuery: MetricQuery;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* One column of the join key. Sources name the same real-world key differently
|
|
25
|
+
* (`orders.order_date` vs `users.created_date`), so the mapping is explicit per
|
|
26
|
+
* source rather than positional.
|
|
27
|
+
*/
|
|
28
|
+
export type MergeJoinKeyPart = {
|
|
29
|
+
/** Column name this key part takes in the merged result. */
|
|
30
|
+
name: string;
|
|
31
|
+
/** The field each source joins on. Every source must have an entry. */
|
|
32
|
+
fieldIdBySourceId: Record<string, FieldId>;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* A calculation over the *merged* result, which is the only place a row-wise
|
|
36
|
+
* calculation across two queries can correctly live. References name a source
|
|
37
|
+
* and one of its fields, `${sourceId.fieldId}`, because the merged statement
|
|
38
|
+
* renames columns to keep two sources from colliding.
|
|
39
|
+
*/
|
|
40
|
+
export type MergeTableCalculation = {
|
|
41
|
+
name: string;
|
|
42
|
+
displayName: string;
|
|
43
|
+
sql: string;
|
|
44
|
+
};
|
|
45
|
+
/** `${sourceId.fieldId}` inside a merge table calculation. */
|
|
46
|
+
export declare const mergeCalculationReferencePattern: RegExp;
|
|
47
|
+
export type MergeQuery = {
|
|
48
|
+
sources: MergeQuerySource[];
|
|
49
|
+
joinKey: MergeJoinKeyPart[];
|
|
50
|
+
joinType: MergeJoinType;
|
|
51
|
+
/** Calculations over the merged result. Applied last, after any pivot. */
|
|
52
|
+
tableCalculations: MergeTableCalculation[];
|
|
53
|
+
limit: number;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Where each column of the merged result came from, so callers map results
|
|
57
|
+
* back to fields instead of re-deriving the naming rule.
|
|
58
|
+
*/
|
|
59
|
+
export type MergeQueryColumns = {
|
|
60
|
+
/** Join key columns, in join key order. Shared by every source. */
|
|
61
|
+
joinKeyColumns: string[];
|
|
62
|
+
/** Merged column name for each source column, keyed by source id. */
|
|
63
|
+
valueColumnBySourceColumn: Record<string, Record<string, string>>;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* What a join field actually is. Supplied by the caller from the compiled
|
|
67
|
+
* explores, because field ids alone cannot say whether two sides of a join are
|
|
68
|
+
* comparable.
|
|
69
|
+
*/
|
|
70
|
+
export type MergeFieldMeta = {
|
|
71
|
+
type: DimensionType;
|
|
72
|
+
/** Time grain for date/timestamp dimensions, null otherwise. */
|
|
73
|
+
timeInterval: TimeFrames | null;
|
|
74
|
+
/** Whether a timestamp field is zone-aware or wall-clock; absent means unknown. */
|
|
75
|
+
timestampDomain?: TimestampDomain;
|
|
76
|
+
};
|
|
77
|
+
/** Field metadata by source and field id for every field named in a join key. */
|
|
78
|
+
export type MergeFieldTypes = Record<MergeQuerySource['id'], Record<FieldId, MergeFieldMeta>>;
|
|
79
|
+
/**
|
|
80
|
+
* One column of the merged result, described well enough to be selected,
|
|
81
|
+
* sorted, formatted and charted like any other field.
|
|
82
|
+
*
|
|
83
|
+
* Merged columns are renamed to keep two sources from colliding, so the
|
|
84
|
+
* mapping back to the field a column came from cannot be recovered from its
|
|
85
|
+
* name — it has to be carried.
|
|
86
|
+
*/
|
|
87
|
+
export type MergeQueryField = {
|
|
88
|
+
/** Column name in the merged result. */
|
|
89
|
+
column: string;
|
|
90
|
+
/** Label to show, derived from the source field. */
|
|
91
|
+
label: string;
|
|
92
|
+
/** Whether it behaves as a dimension or a metric downstream. */
|
|
93
|
+
kind: 'dimension' | 'metric';
|
|
94
|
+
/** Underlying value type, for formatting and sort behaviour. */
|
|
95
|
+
type: string;
|
|
96
|
+
/** The query it came from; null for join key columns, which are shared. */
|
|
97
|
+
sourceId: string | null;
|
|
98
|
+
/** The field it came from; null for join key columns. */
|
|
99
|
+
sourceFieldId: string | null;
|
|
100
|
+
};
|
|
101
|
+
export declare enum MergeQueryErrorKind {
|
|
102
|
+
TOO_FEW_SOURCES = "too_few_sources",
|
|
103
|
+
/**
|
|
104
|
+
* More than two sources. The spec stays N-shaped, but N-way FULL OUTER
|
|
105
|
+
* JOIN chains are where dialect risk compounds, so the engine refuses
|
|
106
|
+
* anything the UI cannot produce rather than compiling it untested.
|
|
107
|
+
*/
|
|
108
|
+
TOO_MANY_SOURCES = "too_many_sources",
|
|
109
|
+
DUPLICATE_SOURCE_ID = "duplicate_source_id",
|
|
110
|
+
EMPTY_JOIN_KEY = "empty_join_key",
|
|
111
|
+
JOIN_KEY_COVERAGE = "join_key_coverage",
|
|
112
|
+
UNKNOWN_SOURCE_IN_JOIN_KEY = "unknown_source_in_join_key",
|
|
113
|
+
/**
|
|
114
|
+
* A source still carries a dimension that is not part of the join key.
|
|
115
|
+
* Joining it would repeat the other sources' rows once per value — the
|
|
116
|
+
* merged table looks reasonable and every aggregate over it is wrong.
|
|
117
|
+
*/
|
|
118
|
+
FAN_OUT = "fan_out",
|
|
119
|
+
/**
|
|
120
|
+
* A join key names a field its query does not group by. The merged
|
|
121
|
+
* statement would reference a column that side never produced.
|
|
122
|
+
*/
|
|
123
|
+
JOIN_KEY_NOT_SELECTED = "join_key_not_selected",
|
|
124
|
+
/**
|
|
125
|
+
* Two sides of a join key hold different kinds of value. The warehouse
|
|
126
|
+
* either refuses the comparison or silently coerces it, and a coerced
|
|
127
|
+
* comparison that never matches looks exactly like "no data".
|
|
128
|
+
*/
|
|
129
|
+
JOIN_KEY_TYPE_MISMATCH = "join_key_type_mismatch",
|
|
130
|
+
/**
|
|
131
|
+
* Two sides of a date join key are truncated to different grains. Joining
|
|
132
|
+
* a month to a day matches only the first of each month, which reads as an
|
|
133
|
+
* almost-empty result rather than as a mistake.
|
|
134
|
+
*/
|
|
135
|
+
JOIN_KEY_GRANULARITY_MISMATCH = "join_key_granularity_mismatch",
|
|
136
|
+
/**
|
|
137
|
+
* A table calculation whose value depends on the query's whole row set
|
|
138
|
+
* (running totals, ranks, percent-of-total, pivot functions). Merging
|
|
139
|
+
* changes that row set, so the number would be carried over frozen or
|
|
140
|
+
* arrive as null.
|
|
141
|
+
*/
|
|
142
|
+
UNSUPPORTED_TABLE_CALCULATION = "unsupported_table_calculation",
|
|
143
|
+
/** Two merge calculations share a name, so one would overwrite the other. */
|
|
144
|
+
DUPLICATE_CALCULATION_NAME = "duplicate_calculation_name",
|
|
145
|
+
/** A merge calculation references something the merged result has no column for. */
|
|
146
|
+
UNRESOLVED_CALCULATION_REFERENCE = "unresolved_calculation_reference",
|
|
147
|
+
/**
|
|
148
|
+
* A merged column's value type cannot be resolved from the field it came
|
|
149
|
+
* from. Guessing "string" here poisons everything built on the merged
|
|
150
|
+
* result — filters, formatting, further aggregation — so it is refused.
|
|
151
|
+
*/
|
|
152
|
+
UNRESOLVED_COLUMN_TYPE = "unresolved_column_type",
|
|
153
|
+
/**
|
|
154
|
+
* A source references parameters that have no value, supplied or default.
|
|
155
|
+
* Embedded anyway, the placeholder would reach the warehouse as literal
|
|
156
|
+
* text — the same refusal the query makes when it runs on its own.
|
|
157
|
+
*/
|
|
158
|
+
MISSING_PARAMETERS = "missing_parameters"
|
|
159
|
+
}
|
|
160
|
+
export type MergeQueryError = {
|
|
161
|
+
kind: MergeQueryErrorKind;
|
|
162
|
+
/** The source at fault, or null for errors about the merge as a whole. */
|
|
163
|
+
sourceId: string | null;
|
|
164
|
+
fieldIds: FieldId[];
|
|
165
|
+
message: string;
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
* Dimensions a source carries that are not part of the join key. A non-empty
|
|
169
|
+
* result is the fan-out trap, and is what the explorer renders as an
|
|
170
|
+
* incomplete merge row.
|
|
171
|
+
*/
|
|
172
|
+
export declare const getUnaccountedDimensions: (source: MergeQuerySource, joinKey: MergeJoinKeyPart[]) => FieldId[];
|
|
173
|
+
/**
|
|
174
|
+
* Every reason this merge would produce a wrong or unbuildable result. Empty
|
|
175
|
+
* means the merge is safe to compile.
|
|
176
|
+
*/
|
|
177
|
+
export declare const validateMergeQuery: (mergeQuery: MergeQuery,
|
|
178
|
+
/**
|
|
179
|
+
* Types of every field named in the join key. Omitting it skips the type
|
|
180
|
+
* and granularity checks — structural validation still runs, but a
|
|
181
|
+
* mismatched join will only show up as a puzzling empty result.
|
|
182
|
+
*/
|
|
183
|
+
fieldTypes?: MergeFieldTypes) => MergeQueryError[];
|
|
184
|
+
/**
|
|
185
|
+
* Body of the run endpoint. The pivot configuration is the same one every
|
|
186
|
+
* other query runs with: the merged rows are wrapped by the standard pivot
|
|
187
|
+
* stage, so the pipeline is join within an explore, then merge between
|
|
188
|
+
* explores, then pivot.
|
|
189
|
+
*/
|
|
190
|
+
export type RunMergeQueryRequest = {
|
|
191
|
+
mergeQuery: MergeQuery;
|
|
192
|
+
pivotConfiguration?: PivotConfiguration;
|
|
193
|
+
/**
|
|
194
|
+
* Parameter values for every source query, one map for the whole merge —
|
|
195
|
+
* two sides of one question should never disagree on a parameter.
|
|
196
|
+
*/
|
|
197
|
+
parameters?: ParametersValuesMap;
|
|
198
|
+
};
|
|
199
|
+
/** Body of the compile endpoint: the spec plus the parameter values. */
|
|
200
|
+
export type CompileMergeQueryRequest = {
|
|
201
|
+
mergeQuery: MergeQuery;
|
|
202
|
+
parameters?: ParametersValuesMap;
|
|
203
|
+
};
|
|
204
|
+
/**
|
|
205
|
+
* One column of the composable core, typed accurately enough to build a
|
|
206
|
+
* virtual view over it: the stable field-id alias, the value's
|
|
207
|
+
* `DimensionType`, and where it came from. Merged metrics arrive downstream as
|
|
208
|
+
* typed *columns* — re-aggregating them is explicit and user-owned, so the
|
|
209
|
+
* origin is carried for a surface to suggest metrics, never to re-sum.
|
|
210
|
+
*/
|
|
211
|
+
export type MergeTypedColumn = {
|
|
212
|
+
/** Column name in the core's output — the merged field id. */
|
|
213
|
+
reference: FieldId;
|
|
214
|
+
type: DimensionType;
|
|
215
|
+
origin: MergeFieldOrigin;
|
|
216
|
+
};
|
|
217
|
+
/**
|
|
218
|
+
* The terminal stage of a merged statement, owned by the run path: sort,
|
|
219
|
+
* limit, and truncation detection. Kept as data rather than SQL text so the
|
|
220
|
+
* run path can attach it above whatever it stacked on the core (for example a
|
|
221
|
+
* date spine), and so the composable core stays clean under `SELECT *`.
|
|
222
|
+
*/
|
|
223
|
+
export type MergeTerminalWrapper = {
|
|
224
|
+
/** ORDER BY terms in output-alias space, already quoted for the dialect. */
|
|
225
|
+
orderBy: string[];
|
|
226
|
+
limit: number | null;
|
|
227
|
+
/** Boolean SQL expression that is true when a source exceeded its cap. */
|
|
228
|
+
sourceLimitExceededSql: string | null;
|
|
229
|
+
};
|
|
230
|
+
/**
|
|
231
|
+
* What the compile endpoint returns. `sql` is null exactly when `errors` is
|
|
232
|
+
* non-empty: a merge that would produce wrong numbers is reported, not run.
|
|
233
|
+
*/
|
|
234
|
+
export type ApiCompiledMergeQueryResults = {
|
|
235
|
+
sql: string | null;
|
|
236
|
+
/**
|
|
237
|
+
* The composable core: a self-contained single-statement SELECT with no
|
|
238
|
+
* ORDER BY, no LIMIT and no guard column — valid under `SELECT *`, so it
|
|
239
|
+
* can back a virtual view. `sql` is this core with the terminal wrapper
|
|
240
|
+
* attached.
|
|
241
|
+
*/
|
|
242
|
+
coreSql: string | null;
|
|
243
|
+
/** The core's columns, in the order the statement returns them. */
|
|
244
|
+
typedColumns: MergeTypedColumn[] | null;
|
|
245
|
+
/** The terminal stage `sql` attaches over the core. */
|
|
246
|
+
terminalWrapper: MergeTerminalWrapper | null;
|
|
247
|
+
columns: MergeQueryColumns | null;
|
|
248
|
+
/** Selectable description of every merged column. Empty when sql is null. */
|
|
249
|
+
fields: MergeQueryField[];
|
|
250
|
+
/**
|
|
251
|
+
* Every merged column as an ordinary field, keyed by field id, so results
|
|
252
|
+
* of a merge are consumed exactly like results of a query.
|
|
253
|
+
*/
|
|
254
|
+
itemsMap: ItemsMap;
|
|
255
|
+
/** Provenance of each field in `itemsMap`. */
|
|
256
|
+
fieldOrigins: MergeFieldOrigins;
|
|
257
|
+
/**
|
|
258
|
+
* Field id for each column the statement returns. Warehouse aliases are
|
|
259
|
+
* short and positional so they cannot breach an identifier length limit;
|
|
260
|
+
* identity lives in `itemsMap`, and rows are rekeyed through this map
|
|
261
|
+
* before anything downstream sees them.
|
|
262
|
+
*/
|
|
263
|
+
fieldIdByColumn: Record<string, FieldId>;
|
|
264
|
+
errors: MergeQueryError[];
|
|
265
|
+
};
|
|
266
|
+
/**
|
|
267
|
+
* Column the merged statement carries to report that a query produced more
|
|
268
|
+
* rows than the merge is willing to join. It is a guard, not data: the caller
|
|
269
|
+
* refuses the result rather than showing a partial join.
|
|
270
|
+
*/
|
|
271
|
+
export declare const MERGE_TRUNCATED_COLUMN = "__merge_truncated";
|
|
272
|
+
/** Internal marker that distinguishes the empty-result guard row from data. */
|
|
273
|
+
export declare const MERGE_ROW_PRESENT_COLUMN = "__merge_row_present";
|
|
274
|
+
/**
|
|
275
|
+
* Table merged columns that belong to no single source are attributed to:
|
|
276
|
+
* join keys, which are shared by every source, and calculations over the
|
|
277
|
+
* merged result.
|
|
278
|
+
*/
|
|
279
|
+
export declare const MERGE_TABLE_NAME = "merge";
|
|
280
|
+
/** Label for the pseudo-table a source's merged fields belong to. */
|
|
281
|
+
export declare const getMergeSourceTableLabel: (sourceIndex: number) => string;
|
|
282
|
+
/**
|
|
283
|
+
* Where a merged field came from. Carried beside the fields rather than on
|
|
284
|
+
* them, so every `Field` consumer downstream sees an ordinary field and only
|
|
285
|
+
* the code that needs provenance — drilling into a cell, filtering a source —
|
|
286
|
+
* has to know a merge happened.
|
|
287
|
+
*/
|
|
288
|
+
export type MergeFieldOrigin = {
|
|
289
|
+
kind: 'source';
|
|
290
|
+
sourceId: string;
|
|
291
|
+
sourceFieldId: FieldId;
|
|
292
|
+
}
|
|
293
|
+
/** Shared by every source, so it descends from no single one. */
|
|
294
|
+
| {
|
|
295
|
+
kind: 'joinKey';
|
|
296
|
+
}
|
|
297
|
+
/** Computed over the merged result, so it descends from no source. */
|
|
298
|
+
| {
|
|
299
|
+
kind: 'tableCalculation';
|
|
300
|
+
};
|
|
301
|
+
/** Provenance of every merged field, by field id. */
|
|
302
|
+
export type MergeFieldOrigins = Record<FieldId, MergeFieldOrigin>;
|
|
303
|
+
/**
|
|
304
|
+
* How a merge is stored against a chart version.
|
|
305
|
+
*
|
|
306
|
+
* Deliberately not the runtime `MergeQuery`. The chart version *is* the first
|
|
307
|
+
* query, so storing it again would create two sources of truth that drift the
|
|
308
|
+
* next time the chart is edited. Only the second query and the relationship
|
|
309
|
+
* are kept, and the two sides of each key part are named rather than addressed
|
|
310
|
+
* by a source id that means nothing outside a single request.
|
|
311
|
+
*/
|
|
312
|
+
export type SavedMergeQuery = {
|
|
313
|
+
secondQuery: {
|
|
314
|
+
metricQuery: MetricQuery;
|
|
315
|
+
};
|
|
316
|
+
joinKey: Array<{
|
|
317
|
+
name: string;
|
|
318
|
+
/** Field in the chart's own query. */
|
|
319
|
+
chartFieldId: FieldId;
|
|
320
|
+
/** Field in the second query. */
|
|
321
|
+
secondFieldId: FieldId;
|
|
322
|
+
}>;
|
|
323
|
+
joinType: MergeJoinType;
|
|
324
|
+
tableCalculations: MergeTableCalculation[];
|
|
325
|
+
};
|
|
326
|
+
/** Source ids the stored shape maps onto when it becomes a runnable merge. */
|
|
327
|
+
export declare const MERGE_CHART_SOURCE_ID = "chart";
|
|
328
|
+
export declare const MERGE_SECOND_SOURCE_ID = "second";
|
|
329
|
+
/**
|
|
330
|
+
* Rebuilds a runnable merge from a chart's own query and its stored merge.
|
|
331
|
+
*/
|
|
332
|
+
export declare const buildMergeQueryFromSaved: (chartMetricQuery: MetricQuery, saved: SavedMergeQuery) => MergeQuery;
|
|
333
|
+
/**
|
|
334
|
+
* Reads a stored merge back, returning null for anything that does not hold
|
|
335
|
+
* together. A payload written by an older shape should leave the chart working
|
|
336
|
+
* without its merge rather than break the chart entirely.
|
|
337
|
+
*/
|
|
338
|
+
export declare const parseSavedMergeQuery: (value: unknown) => SavedMergeQuery | null;
|
|
339
|
+
//# sourceMappingURL=mergeQuery.d.ts.map
|
|
@@ -0,0 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CAAA;CAAE,CAAC;AAEnC,qDAAqD;AACrD,MAAM,MAAM,iBAAiB,GAAG,MAAM,CAAC,OAAO,EAAE,gBAAgB,CAAC,CAAC;AAElE;;;;;;;;GAQG;AACH,MAAM,MAAM,eAAe,GAAG;IAC1B,WAAW,EAAE;QACT,WAAW,EAAE,WAAW,CAAC;KAC5B,CAAC;IACF,OAAO,EAAE,KAAK,CAAC;QACX,IAAI,EAAE,MAAM,CAAC;QACb,sCAAsC;QACtC,YAAY,EAAE,OAAO,CAAC;QACtB,iCAAiC;QACjC,aAAa,EAAE,OAAO,CAAC;KAC1B,CAAC,CAAC;IACH,QAAQ,EAAE,aAAa,CAAC;IACxB,iBAAiB,EAAE,qBAAqB,EAAE,CAAC;CAC9C,CAAC;AAEF,8EAA8E;AAC9E,eAAO,MAAM,qBAAqB,UAAU,CAAC;AAC7C,eAAO,MAAM,sBAAsB,WAAW,CAAC;AAE/C;;GAEG;AACH,eAAO,MAAM,wBAAwB,qBACf,WAAW,SACtB,eAAe,KACvB,UAqBD,CAAC;AAEH;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,UACtB,OAAO,KACf,eAAe,GAAG,IAmCpB,CAAC"}
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import { DimensionType, } from './field';
|
|
2
|
+
/**
|
|
3
|
+
* How unmatched keys survive the merge. Mirrors the SQL join it compiles to.
|
|
4
|
+
*/
|
|
5
|
+
export var MergeJoinType;
|
|
6
|
+
(function (MergeJoinType) {
|
|
7
|
+
/** Keep every key from every source. */
|
|
8
|
+
MergeJoinType["FULL"] = "full";
|
|
9
|
+
/** Keep every key from the first source only. */
|
|
10
|
+
MergeJoinType["LEFT"] = "left";
|
|
11
|
+
/** Keep only keys present in all sources. */
|
|
12
|
+
MergeJoinType["INNER"] = "inner";
|
|
13
|
+
})(MergeJoinType || (MergeJoinType = {}));
|
|
14
|
+
/** `${sourceId.fieldId}` inside a merge table calculation. */
|
|
15
|
+
export const mergeCalculationReferencePattern = /\$\{([a-zA-Z0-9_.-]+)\}/g;
|
|
16
|
+
/**
|
|
17
|
+
* Types that can be compared across a join without the warehouse guessing.
|
|
18
|
+
* DATE and TIMESTAMP share a class because every supported warehouse compares
|
|
19
|
+
* them, but their *grain* still has to match — see the granularity check.
|
|
20
|
+
*/
|
|
21
|
+
const getTypeClass = (type) => {
|
|
22
|
+
switch (type) {
|
|
23
|
+
case DimensionType.DATE:
|
|
24
|
+
case DimensionType.TIMESTAMP:
|
|
25
|
+
return 'temporal';
|
|
26
|
+
case DimensionType.NUMBER:
|
|
27
|
+
return 'number';
|
|
28
|
+
case DimensionType.STRING:
|
|
29
|
+
return 'string';
|
|
30
|
+
case DimensionType.BOOLEAN:
|
|
31
|
+
return 'boolean';
|
|
32
|
+
default:
|
|
33
|
+
return 'unknown';
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
export var MergeQueryErrorKind;
|
|
37
|
+
(function (MergeQueryErrorKind) {
|
|
38
|
+
MergeQueryErrorKind["TOO_FEW_SOURCES"] = "too_few_sources";
|
|
39
|
+
/**
|
|
40
|
+
* More than two sources. The spec stays N-shaped, but N-way FULL OUTER
|
|
41
|
+
* JOIN chains are where dialect risk compounds, so the engine refuses
|
|
42
|
+
* anything the UI cannot produce rather than compiling it untested.
|
|
43
|
+
*/
|
|
44
|
+
MergeQueryErrorKind["TOO_MANY_SOURCES"] = "too_many_sources";
|
|
45
|
+
MergeQueryErrorKind["DUPLICATE_SOURCE_ID"] = "duplicate_source_id";
|
|
46
|
+
MergeQueryErrorKind["EMPTY_JOIN_KEY"] = "empty_join_key";
|
|
47
|
+
MergeQueryErrorKind["JOIN_KEY_COVERAGE"] = "join_key_coverage";
|
|
48
|
+
MergeQueryErrorKind["UNKNOWN_SOURCE_IN_JOIN_KEY"] = "unknown_source_in_join_key";
|
|
49
|
+
/**
|
|
50
|
+
* A source still carries a dimension that is not part of the join key.
|
|
51
|
+
* Joining it would repeat the other sources' rows once per value — the
|
|
52
|
+
* merged table looks reasonable and every aggregate over it is wrong.
|
|
53
|
+
*/
|
|
54
|
+
MergeQueryErrorKind["FAN_OUT"] = "fan_out";
|
|
55
|
+
/**
|
|
56
|
+
* A join key names a field its query does not group by. The merged
|
|
57
|
+
* statement would reference a column that side never produced.
|
|
58
|
+
*/
|
|
59
|
+
MergeQueryErrorKind["JOIN_KEY_NOT_SELECTED"] = "join_key_not_selected";
|
|
60
|
+
/**
|
|
61
|
+
* Two sides of a join key hold different kinds of value. The warehouse
|
|
62
|
+
* either refuses the comparison or silently coerces it, and a coerced
|
|
63
|
+
* comparison that never matches looks exactly like "no data".
|
|
64
|
+
*/
|
|
65
|
+
MergeQueryErrorKind["JOIN_KEY_TYPE_MISMATCH"] = "join_key_type_mismatch";
|
|
66
|
+
/**
|
|
67
|
+
* Two sides of a date join key are truncated to different grains. Joining
|
|
68
|
+
* a month to a day matches only the first of each month, which reads as an
|
|
69
|
+
* almost-empty result rather than as a mistake.
|
|
70
|
+
*/
|
|
71
|
+
MergeQueryErrorKind["JOIN_KEY_GRANULARITY_MISMATCH"] = "join_key_granularity_mismatch";
|
|
72
|
+
/**
|
|
73
|
+
* A table calculation whose value depends on the query's whole row set
|
|
74
|
+
* (running totals, ranks, percent-of-total, pivot functions). Merging
|
|
75
|
+
* changes that row set, so the number would be carried over frozen or
|
|
76
|
+
* arrive as null.
|
|
77
|
+
*/
|
|
78
|
+
MergeQueryErrorKind["UNSUPPORTED_TABLE_CALCULATION"] = "unsupported_table_calculation";
|
|
79
|
+
/** Two merge calculations share a name, so one would overwrite the other. */
|
|
80
|
+
MergeQueryErrorKind["DUPLICATE_CALCULATION_NAME"] = "duplicate_calculation_name";
|
|
81
|
+
/** A merge calculation references something the merged result has no column for. */
|
|
82
|
+
MergeQueryErrorKind["UNRESOLVED_CALCULATION_REFERENCE"] = "unresolved_calculation_reference";
|
|
83
|
+
/**
|
|
84
|
+
* A merged column's value type cannot be resolved from the field it came
|
|
85
|
+
* from. Guessing "string" here poisons everything built on the merged
|
|
86
|
+
* result — filters, formatting, further aggregation — so it is refused.
|
|
87
|
+
*/
|
|
88
|
+
MergeQueryErrorKind["UNRESOLVED_COLUMN_TYPE"] = "unresolved_column_type";
|
|
89
|
+
/**
|
|
90
|
+
* A source references parameters that have no value, supplied or default.
|
|
91
|
+
* Embedded anyway, the placeholder would reach the warehouse as literal
|
|
92
|
+
* text — the same refusal the query makes when it runs on its own.
|
|
93
|
+
*/
|
|
94
|
+
MergeQueryErrorKind["MISSING_PARAMETERS"] = "missing_parameters";
|
|
95
|
+
})(MergeQueryErrorKind || (MergeQueryErrorKind = {}));
|
|
96
|
+
const getJoinKeyFieldIdsForSource = (joinKey, sourceId) => joinKey.flatMap((part) => {
|
|
97
|
+
const fieldId = part.fieldIdBySourceId[sourceId];
|
|
98
|
+
return fieldId === undefined ? [] : [fieldId];
|
|
99
|
+
});
|
|
100
|
+
/**
|
|
101
|
+
* Dimensions a source carries that are not part of the join key. A non-empty
|
|
102
|
+
* result is the fan-out trap, and is what the explorer renders as an
|
|
103
|
+
* incomplete merge row.
|
|
104
|
+
*/
|
|
105
|
+
export const getUnaccountedDimensions = (source, joinKey) => {
|
|
106
|
+
const accounted = new Set([
|
|
107
|
+
...getJoinKeyFieldIdsForSource(joinKey, source.id),
|
|
108
|
+
]);
|
|
109
|
+
return source.metricQuery.dimensions.filter((dimension) => !accounted.has(dimension));
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Every reason this merge would produce a wrong or unbuildable result. Empty
|
|
113
|
+
* means the merge is safe to compile.
|
|
114
|
+
*/
|
|
115
|
+
export const validateMergeQuery = (mergeQuery,
|
|
116
|
+
/**
|
|
117
|
+
* Types of every field named in the join key. Omitting it skips the type
|
|
118
|
+
* and granularity checks — structural validation still runs, but a
|
|
119
|
+
* mismatched join will only show up as a puzzling empty result.
|
|
120
|
+
*/
|
|
121
|
+
fieldTypes) => {
|
|
122
|
+
const { sources, joinKey } = mergeQuery;
|
|
123
|
+
const errors = [];
|
|
124
|
+
if (sources.length < 2) {
|
|
125
|
+
errors.push({
|
|
126
|
+
kind: MergeQueryErrorKind.TOO_FEW_SOURCES,
|
|
127
|
+
sourceId: null,
|
|
128
|
+
fieldIds: [],
|
|
129
|
+
message: 'A merge needs at least two queries.',
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
if (sources.length > 2) {
|
|
133
|
+
errors.push({
|
|
134
|
+
kind: MergeQueryErrorKind.TOO_MANY_SOURCES,
|
|
135
|
+
sourceId: null,
|
|
136
|
+
fieldIds: [],
|
|
137
|
+
message: 'A merge joins exactly two queries. Remove the extra queries, or merge them in pairs.',
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
const sourceIds = sources.map((source) => source.id);
|
|
141
|
+
const duplicateIds = sourceIds.filter((id, index) => sourceIds.indexOf(id) !== index);
|
|
142
|
+
duplicateIds.forEach((id) => {
|
|
143
|
+
errors.push({
|
|
144
|
+
kind: MergeQueryErrorKind.DUPLICATE_SOURCE_ID,
|
|
145
|
+
sourceId: id,
|
|
146
|
+
fieldIds: [],
|
|
147
|
+
message: `More than one query uses the id "${id}".`,
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
if (joinKey.length === 0) {
|
|
151
|
+
errors.push({
|
|
152
|
+
kind: MergeQueryErrorKind.EMPTY_JOIN_KEY,
|
|
153
|
+
sourceId: null,
|
|
154
|
+
fieldIds: [],
|
|
155
|
+
message: 'A merge needs at least one field to join on.',
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
const knownSourceIds = new Set(sourceIds);
|
|
159
|
+
joinKey.forEach((part) => {
|
|
160
|
+
sources.forEach((source) => {
|
|
161
|
+
const fieldId = part.fieldIdBySourceId[source.id];
|
|
162
|
+
if (fieldId === undefined) {
|
|
163
|
+
errors.push({
|
|
164
|
+
kind: MergeQueryErrorKind.JOIN_KEY_COVERAGE,
|
|
165
|
+
sourceId: source.id,
|
|
166
|
+
fieldIds: [],
|
|
167
|
+
message: `Query "${source.id}" has no field to join on for "${part.name}".`,
|
|
168
|
+
});
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
// The join compiles against the source's own output columns, so a
|
|
172
|
+
// key naming a field the source does not select produces SQL that
|
|
173
|
+
// references a column the warehouse has never heard of.
|
|
174
|
+
if (!source.metricQuery.dimensions.includes(fieldId)) {
|
|
175
|
+
errors.push({
|
|
176
|
+
kind: MergeQueryErrorKind.JOIN_KEY_NOT_SELECTED,
|
|
177
|
+
sourceId: source.id,
|
|
178
|
+
fieldIds: [fieldId],
|
|
179
|
+
message: `Query "${source.id}" joins on ${fieldId}, which it does not group by. Add it to that query, or join on a field it already has.`,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
Object.keys(part.fieldIdBySourceId)
|
|
184
|
+
.filter((id) => !knownSourceIds.has(id))
|
|
185
|
+
.forEach((id) => {
|
|
186
|
+
errors.push({
|
|
187
|
+
kind: MergeQueryErrorKind.UNKNOWN_SOURCE_IN_JOIN_KEY,
|
|
188
|
+
sourceId: id,
|
|
189
|
+
fieldIds: [],
|
|
190
|
+
message: `Join key "${part.name}" references unknown query "${id}".`,
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
if (fieldTypes === undefined) {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
const joined = Object.entries(part.fieldIdBySourceId).flatMap(([sourceId, fieldId]) => {
|
|
197
|
+
const meta = fieldTypes[sourceId]?.[fieldId];
|
|
198
|
+
return meta === undefined ? [] : [{ fieldId, meta }];
|
|
199
|
+
});
|
|
200
|
+
const typeClasses = new Set(joined.map(({ meta }) => getTypeClass(meta.type)));
|
|
201
|
+
if (typeClasses.size > 1) {
|
|
202
|
+
errors.push({
|
|
203
|
+
kind: MergeQueryErrorKind.JOIN_KEY_TYPE_MISMATCH,
|
|
204
|
+
sourceId: null,
|
|
205
|
+
fieldIds: joined.map(({ fieldId }) => fieldId),
|
|
206
|
+
message: `Join key "${part.name}" compares ${joined
|
|
207
|
+
.map(({ fieldId, meta }) => `${fieldId} (${meta.type})`)
|
|
208
|
+
.join(' to ')}. Those hold different kinds of value, so the join would either be refused or silently never match.`,
|
|
209
|
+
});
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
const grains = new Set(joined
|
|
213
|
+
.filter(({ meta }) => getTypeClass(meta.type) === 'temporal')
|
|
214
|
+
.map(({ meta }) => meta.timeInterval ?? 'RAW'));
|
|
215
|
+
if (grains.size > 1) {
|
|
216
|
+
errors.push({
|
|
217
|
+
kind: MergeQueryErrorKind.JOIN_KEY_GRANULARITY_MISMATCH,
|
|
218
|
+
sourceId: null,
|
|
219
|
+
fieldIds: joined.map(({ fieldId }) => fieldId),
|
|
220
|
+
message: `Join key "${part.name}" joins ${[...grains]
|
|
221
|
+
.map((grain) => String(grain).toLowerCase())
|
|
222
|
+
.join(' to ')}. Dates truncated to different grains only match where the finer one lands on the coarser one, which looks like missing data rather than a mistake.`,
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
sources.forEach((source) => {
|
|
227
|
+
const unaccounted = getUnaccountedDimensions(source, joinKey);
|
|
228
|
+
if (unaccounted.length > 0) {
|
|
229
|
+
errors.push({
|
|
230
|
+
kind: MergeQueryErrorKind.FAN_OUT,
|
|
231
|
+
sourceId: source.id,
|
|
232
|
+
fieldIds: unaccounted,
|
|
233
|
+
message: `Query "${source.id}" carries ${unaccounted.join(', ')}, which is not joined on. Merging would repeat the other queries' rows once per value.`,
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
const calculationNames = mergeQuery.tableCalculations.map((calculation) => calculation.name);
|
|
238
|
+
calculationNames
|
|
239
|
+
.filter((name, index) => calculationNames.indexOf(name) !== index)
|
|
240
|
+
.forEach((name) => {
|
|
241
|
+
errors.push({
|
|
242
|
+
kind: MergeQueryErrorKind.DUPLICATE_CALCULATION_NAME,
|
|
243
|
+
sourceId: null,
|
|
244
|
+
fieldIds: [name],
|
|
245
|
+
message: `More than one calculation is called "${name}".`,
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
// Reference resolution is deliberately not checked here: only the
|
|
249
|
+
// compiler knows the real column names. See ProjectService.compileMergeQuery.
|
|
250
|
+
return errors;
|
|
251
|
+
};
|
|
252
|
+
/**
|
|
253
|
+
* Column the merged statement carries to report that a query produced more
|
|
254
|
+
* rows than the merge is willing to join. It is a guard, not data: the caller
|
|
255
|
+
* refuses the result rather than showing a partial join.
|
|
256
|
+
*/
|
|
257
|
+
export const MERGE_TRUNCATED_COLUMN = '__merge_truncated';
|
|
258
|
+
/** Internal marker that distinguishes the empty-result guard row from data. */
|
|
259
|
+
export const MERGE_ROW_PRESENT_COLUMN = '__merge_row_present';
|
|
260
|
+
/**
|
|
261
|
+
* Table merged columns that belong to no single source are attributed to:
|
|
262
|
+
* join keys, which are shared by every source, and calculations over the
|
|
263
|
+
* merged result.
|
|
264
|
+
*/
|
|
265
|
+
export const MERGE_TABLE_NAME = 'merge';
|
|
266
|
+
/** Label for the pseudo-table a source's merged fields belong to. */
|
|
267
|
+
export const getMergeSourceTableLabel = (sourceIndex) => `Query ${String.fromCharCode(65 + sourceIndex)}`;
|
|
268
|
+
/** Source ids the stored shape maps onto when it becomes a runnable merge. */
|
|
269
|
+
export const MERGE_CHART_SOURCE_ID = 'chart';
|
|
270
|
+
export const MERGE_SECOND_SOURCE_ID = 'second';
|
|
271
|
+
/**
|
|
272
|
+
* Rebuilds a runnable merge from a chart's own query and its stored merge.
|
|
273
|
+
*/
|
|
274
|
+
export const buildMergeQueryFromSaved = (chartMetricQuery, saved) => ({
|
|
275
|
+
sources: [
|
|
276
|
+
{
|
|
277
|
+
id: MERGE_CHART_SOURCE_ID,
|
|
278
|
+
metricQuery: chartMetricQuery,
|
|
279
|
+
},
|
|
280
|
+
{
|
|
281
|
+
id: MERGE_SECOND_SOURCE_ID,
|
|
282
|
+
metricQuery: saved.secondQuery.metricQuery,
|
|
283
|
+
},
|
|
284
|
+
],
|
|
285
|
+
joinKey: saved.joinKey.map((part) => ({
|
|
286
|
+
name: part.name,
|
|
287
|
+
fieldIdBySourceId: {
|
|
288
|
+
[MERGE_CHART_SOURCE_ID]: part.chartFieldId,
|
|
289
|
+
[MERGE_SECOND_SOURCE_ID]: part.secondFieldId,
|
|
290
|
+
},
|
|
291
|
+
})),
|
|
292
|
+
joinType: saved.joinType,
|
|
293
|
+
tableCalculations: saved.tableCalculations,
|
|
294
|
+
limit: chartMetricQuery.limit,
|
|
295
|
+
});
|
|
296
|
+
/**
|
|
297
|
+
* Reads a stored merge back, returning null for anything that does not hold
|
|
298
|
+
* together. A payload written by an older shape should leave the chart working
|
|
299
|
+
* without its merge rather than break the chart entirely.
|
|
300
|
+
*/
|
|
301
|
+
export const parseSavedMergeQuery = (value) => {
|
|
302
|
+
if (value === null || typeof value !== 'object')
|
|
303
|
+
return null;
|
|
304
|
+
const candidate = value;
|
|
305
|
+
const metricQuery = candidate.secondQuery?.metricQuery;
|
|
306
|
+
if (!metricQuery || typeof metricQuery.exploreName !== 'string') {
|
|
307
|
+
return null;
|
|
308
|
+
}
|
|
309
|
+
if (!Array.isArray(candidate.joinKey) || candidate.joinKey.length === 0) {
|
|
310
|
+
return null;
|
|
311
|
+
}
|
|
312
|
+
const joinKey = candidate.joinKey.filter((part) => typeof part?.name === 'string' &&
|
|
313
|
+
typeof part?.chartFieldId === 'string' &&
|
|
314
|
+
typeof part?.secondFieldId === 'string');
|
|
315
|
+
if (joinKey.length !== candidate.joinKey.length)
|
|
316
|
+
return null;
|
|
317
|
+
const joinType = Object.values(MergeJoinType).includes(String(candidate.joinType))
|
|
318
|
+
? candidate.joinType
|
|
319
|
+
: MergeJoinType.FULL;
|
|
320
|
+
return {
|
|
321
|
+
secondQuery: {
|
|
322
|
+
metricQuery,
|
|
323
|
+
},
|
|
324
|
+
joinKey,
|
|
325
|
+
joinType,
|
|
326
|
+
tableCalculations: Array.isArray(candidate.tableCalculations)
|
|
327
|
+
? candidate.tableCalculations
|
|
328
|
+
: [],
|
|
329
|
+
};
|
|
330
|
+
};
|
|
331
|
+
//# sourceMappingURL=mergeQuery.js.map
|