@lightdash/common 2.280.0 → 2.281.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/ee/AiAgent/canonicalizeAiMerge.d.ts +3 -3
- package/dist/cjs/ee/AiAgent/canonicalizeAiMerge.d.ts.map +1 -1
- package/dist/cjs/ee/AiAgent/canonicalizeAiMerge.js +13 -8
- package/dist/cjs/ee/AiAgent/canonicalizeAiMerge.js.map +1 -1
- package/dist/cjs/ee/AiAgent/chartConfig/web/getWebAiChartConfig.d.ts +12 -12
- package/dist/cjs/ee/AiAgent/utils.d.ts +25 -25
- package/dist/cjs/schemas/json/chart-as-code-1.0.json +139 -4
- package/dist/cjs/types/contentAsCode/charts.d.ts +3 -1
- package/dist/cjs/types/contentAsCode/charts.d.ts.map +1 -1
- package/dist/cjs/types/contentReviewRequests.d.ts +7 -1
- package/dist/cjs/types/contentReviewRequests.d.ts.map +1 -1
- package/dist/cjs/types/contentReviewRequests.js.map +1 -1
- package/dist/cjs/types/mergeQuery.d.ts +114 -10
- package/dist/cjs/types/mergeQuery.d.ts.map +1 -1
- package/dist/cjs/types/mergeQuery.js +409 -42
- package/dist/cjs/types/mergeQuery.js.map +1 -1
- package/dist/cjs/types/savedCharts.d.ts +17 -8
- package/dist/cjs/types/savedCharts.d.ts.map +1 -1
- package/dist/cjs/types/savedCharts.js.map +1 -1
- package/dist/cjs/utils/document.d.ts.map +1 -1
- package/dist/cjs/utils/document.js +1 -1
- package/dist/cjs/utils/document.js.map +1 -1
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/ee/AiAgent/canonicalizeAiMerge.d.ts +3 -3
- package/dist/esm/ee/AiAgent/canonicalizeAiMerge.d.ts.map +1 -1
- package/dist/esm/ee/AiAgent/canonicalizeAiMerge.js +13 -8
- package/dist/esm/ee/AiAgent/canonicalizeAiMerge.js.map +1 -1
- package/dist/esm/ee/AiAgent/chartConfig/web/getWebAiChartConfig.d.ts +25 -25
- package/dist/esm/ee/AiAgent/utils.d.ts +28 -28
- package/dist/esm/schemas/json/chart-as-code-1.0.json +139 -4
- package/dist/esm/types/contentAsCode/charts.d.ts +3 -1
- package/dist/esm/types/contentAsCode/charts.d.ts.map +1 -1
- package/dist/esm/types/contentReviewRequests.d.ts +7 -1
- package/dist/esm/types/contentReviewRequests.d.ts.map +1 -1
- package/dist/esm/types/contentReviewRequests.js.map +1 -1
- package/dist/esm/types/mergeQuery.d.ts +114 -10
- package/dist/esm/types/mergeQuery.d.ts.map +1 -1
- package/dist/esm/types/mergeQuery.js +398 -40
- package/dist/esm/types/mergeQuery.js.map +1 -1
- package/dist/esm/types/savedCharts.d.ts +17 -8
- package/dist/esm/types/savedCharts.d.ts.map +1 -1
- package/dist/esm/types/savedCharts.js.map +1 -1
- package/dist/esm/utils/document.d.ts.map +1 -1
- package/dist/esm/utils/document.js +2 -2
- package/dist/esm/utils/document.js.map +1 -1
- package/dist/types/.tsbuildinfo +1 -1
- package/dist/types/ee/AiAgent/canonicalizeAiMerge.d.ts +3 -3
- package/dist/types/ee/AiAgent/canonicalizeAiMerge.d.ts.map +1 -1
- package/dist/types/ee/AiAgent/canonicalizeAiMerge.js +13 -8
- package/dist/types/ee/AiAgent/canonicalizeAiMerge.js.map +1 -1
- package/dist/types/ee/AiAgent/chartConfig/web/getWebAiChartConfig.d.ts +25 -25
- package/dist/types/ee/AiAgent/utils.d.ts +28 -28
- package/dist/types/schemas/json/chart-as-code-1.0.json +139 -4
- package/dist/types/types/contentAsCode/charts.d.ts +3 -1
- package/dist/types/types/contentAsCode/charts.d.ts.map +1 -1
- package/dist/types/types/contentReviewRequests.d.ts +7 -1
- package/dist/types/types/contentReviewRequests.d.ts.map +1 -1
- package/dist/types/types/contentReviewRequests.js.map +1 -1
- package/dist/types/types/mergeQuery.d.ts +114 -10
- package/dist/types/types/mergeQuery.d.ts.map +1 -1
- package/dist/types/types/mergeQuery.js +398 -40
- package/dist/types/types/mergeQuery.js.map +1 -1
- package/dist/types/types/savedCharts.d.ts +17 -8
- package/dist/types/types/savedCharts.d.ts.map +1 -1
- package/dist/types/types/savedCharts.js.map +1 -1
- package/dist/types/utils/document.d.ts.map +1 -1
- package/dist/types/utils/document.js +2 -2
- package/dist/types/utils/document.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { SupportedDbtAdapter } from './dbt';
|
|
2
|
-
import { DimensionType, type FieldId, type ItemsMap, type TimestampDomain } from './field';
|
|
3
|
-
import { type
|
|
2
|
+
import { DimensionType, type CustomDimension, type FieldId, type ItemsMap, type TableCalculation, type TimestampDomain } from './field';
|
|
3
|
+
import { type Filters } from './filter';
|
|
4
|
+
import { type AdditionalMetric, type DimensionOverrides, type MetricOverrides, type MetricQuery, type SortField } from './metricQuery';
|
|
4
5
|
import { type ParametersValuesMap } from './parameters';
|
|
5
6
|
import { type PivotConfiguration } from './pivot';
|
|
6
7
|
import { type TimeFrames } from './timeFrames';
|
|
@@ -444,8 +445,10 @@ export type MergeFieldOrigin = {
|
|
|
444
445
|
};
|
|
445
446
|
/** Provenance of every merged field, by field id. */
|
|
446
447
|
export type MergeFieldOrigins = Record<FieldId, MergeFieldOrigin>;
|
|
447
|
-
/** Current JSON schema stored in `saved_queries_version_merges.merge
|
|
448
|
-
export declare const SAVED_MERGE_QUERY_SCHEMA_VERSION =
|
|
448
|
+
/** Current JSON schema stored in `saved_queries_version_merges.merge`: a merge. */
|
|
449
|
+
export declare const SAVED_MERGE_QUERY_SCHEMA_VERSION = 3;
|
|
450
|
+
/** The legacy shape with explicit source ids. Still read, never written. */
|
|
451
|
+
export declare const SAVED_MERGE_QUERY_SCHEMA_VERSION_V2 = 2;
|
|
449
452
|
/**
|
|
450
453
|
* A persisted merge source.
|
|
451
454
|
*
|
|
@@ -462,7 +465,12 @@ export type SavedMergeQuerySource = {
|
|
|
462
465
|
kind: 'query';
|
|
463
466
|
metricQuery: MetricQuery;
|
|
464
467
|
};
|
|
465
|
-
/**
|
|
468
|
+
/**
|
|
469
|
+
* Schema v2 of a merge stored on a chart version. Accepted on requests and
|
|
470
|
+
* read from rows written before schema v3; rewritten to schema v3 by
|
|
471
|
+
* upgradeSavedMergeQuery. Never written.
|
|
472
|
+
* @deprecated use SavedMergeDefinition
|
|
473
|
+
*/
|
|
466
474
|
export type SavedMergeQuery = {
|
|
467
475
|
/** Source whose rows a LEFT merge preserves. */
|
|
468
476
|
primarySourceId: string;
|
|
@@ -479,13 +487,109 @@ export type SavedMergeQuery = {
|
|
|
479
487
|
repeatValuesSourceIds?: string[];
|
|
480
488
|
};
|
|
481
489
|
/**
|
|
482
|
-
*
|
|
490
|
+
* Reads a v2 merge back, returning null for anything that does not hold
|
|
491
|
+
* together.
|
|
492
|
+
*/
|
|
493
|
+
export declare const parseSavedMergeQuery: (value: unknown) => SavedMergeQuery | null;
|
|
494
|
+
/** A query the merge owns, run against an explore. */
|
|
495
|
+
export type SavedMergeDefinitionQuery = {
|
|
496
|
+
explore: string;
|
|
497
|
+
dimensions: FieldId[];
|
|
498
|
+
metrics: FieldId[];
|
|
499
|
+
filters?: Filters;
|
|
500
|
+
tableCalculations?: TableCalculation[];
|
|
501
|
+
additionalMetrics?: AdditionalMetric[];
|
|
502
|
+
customDimensions?: CustomDimension[];
|
|
503
|
+
metricOverrides?: MetricOverrides;
|
|
504
|
+
dimensionOverrides?: DimensionOverrides;
|
|
505
|
+
timezone?: string;
|
|
506
|
+
/** See MergeQueryMetricSource.repeatValues. Omitted when off. */
|
|
507
|
+
repeat?: boolean;
|
|
508
|
+
};
|
|
509
|
+
export type SavedMergeDefinitionSortDirection = 'asc' | 'desc';
|
|
510
|
+
/**
|
|
511
|
+
* A sort of the merged result. `by` is a chart field id, a `query.fieldId`
|
|
512
|
+
* reference into another query, or the name of a merge table calculation.
|
|
513
|
+
* A chart field that is a join key sorts the key column.
|
|
514
|
+
*/
|
|
515
|
+
export type SavedMergeDefinitionSort = {
|
|
516
|
+
by: string;
|
|
517
|
+
direction: SavedMergeDefinitionSortDirection;
|
|
518
|
+
};
|
|
519
|
+
export type SavedMergeDefinition = {
|
|
520
|
+
/** The chart query's name in merged column ids, when not its explore's. */
|
|
521
|
+
chartAs?: string;
|
|
522
|
+
/** The chart's own query repeats its values. Omitted when off. */
|
|
523
|
+
chartRepeats?: boolean;
|
|
524
|
+
/** The other queries, by the name they go by. */
|
|
525
|
+
queries: {
|
|
526
|
+
[name: string]: SavedMergeDefinitionQuery;
|
|
527
|
+
};
|
|
528
|
+
join: MergeJoinType;
|
|
529
|
+
/**
|
|
530
|
+
* The join keys: each entry is the chart's field and one `query.fieldId`
|
|
531
|
+
* reference per other query. The key column takes the chart field's id.
|
|
532
|
+
*/
|
|
533
|
+
keys: {
|
|
534
|
+
[chartFieldId: string]: string[];
|
|
535
|
+
};
|
|
536
|
+
/** The key column's name, when not the chart field's id. */
|
|
537
|
+
keyNames?: {
|
|
538
|
+
[chartFieldId: string]: string;
|
|
539
|
+
};
|
|
540
|
+
sort?: SavedMergeDefinitionSort[];
|
|
541
|
+
limit: number;
|
|
542
|
+
tableCalculations?: MergeTableCalculation[];
|
|
543
|
+
};
|
|
544
|
+
/** `query.fieldId` into another query, or a bare chart field id. */
|
|
545
|
+
export declare const parseMergeDefinitionReference: (reference: string) => {
|
|
546
|
+
query: string | null;
|
|
547
|
+
fieldId: FieldId;
|
|
548
|
+
};
|
|
549
|
+
/** The name the chart's query goes by in the merged result. */
|
|
550
|
+
export declare const getMergeDefinitionChartName: (merge: Pick<SavedMergeDefinition, 'chartAs'>, chartExploreName: string) => string;
|
|
551
|
+
/** The explore names of the queries beside the chart's, in order. */
|
|
552
|
+
export declare const getMergeDefinitionQueryExploreNames: (merge: Pick<SavedMergeDefinition, 'queries'>) => string[];
|
|
553
|
+
/**
|
|
554
|
+
* The merge a runnable merge saves as. The chart's source must be one of
|
|
555
|
+
* the sources and every other source must be a metric query, because a
|
|
556
|
+
* saved chart cannot re-run a referenced result.
|
|
557
|
+
*/
|
|
558
|
+
export declare const buildSavedMergeDefinition: ({ mergeQuery, chartSourceId, }: {
|
|
559
|
+
mergeQuery: MergeQuery;
|
|
560
|
+
chartSourceId: string;
|
|
561
|
+
}) => SavedMergeDefinition;
|
|
562
|
+
/** Rebuilds a runnable merge from a chart's own query and its stored merge. */
|
|
563
|
+
export declare const buildMergeQueryFromMergeDefinition: (chartMetricQuery: MetricQuery, merge: SavedMergeDefinition) => MergeQuery;
|
|
564
|
+
/**
|
|
565
|
+
* Rebuilds a runnable merge from a chart's own query and a schema v2 merge,
|
|
566
|
+
* the shape requests and Document chart cells still carry.
|
|
483
567
|
*/
|
|
484
568
|
export declare const buildMergeQueryFromSaved: (chartMetricQuery: MetricQuery, saved: SavedMergeQuery) => MergeQuery;
|
|
485
569
|
/**
|
|
486
|
-
*
|
|
487
|
-
*
|
|
488
|
-
*
|
|
570
|
+
* A pure rewrite of the v2 shape, keeping the ids it had. The chart's sort
|
|
571
|
+
* state was the Explorer's and named primary fields, so it is mapped once
|
|
572
|
+
* here; the chart's limit was the merged result's. A v2 merge whose
|
|
573
|
+
* primary was not the chart has no merge form: the chart is always the
|
|
574
|
+
* first input.
|
|
575
|
+
*/
|
|
576
|
+
export declare const upgradeSavedMergeQuery: (saved: SavedMergeQuery, chartMetricQuery: MetricQuery) => SavedMergeDefinition | null;
|
|
577
|
+
/**
|
|
578
|
+
* Reads a stored merge back, returning null for anything that does not
|
|
579
|
+
* hold together: named queries against explores, one join with a key that
|
|
580
|
+
* reaches every query, a known join type, a numeric limit.
|
|
581
|
+
*/
|
|
582
|
+
export declare const parseSavedMergeDefinition: (value: unknown) => SavedMergeDefinition | null;
|
|
583
|
+
/**
|
|
584
|
+
* Reads whatever a chart version stored. A v2 merge is rewritten to a
|
|
585
|
+
* merge with the chart's own sort and limit; an unknown version leaves
|
|
586
|
+
* the chart working without its merge rather than breaking the chart.
|
|
489
587
|
*/
|
|
490
|
-
export declare const
|
|
588
|
+
export declare const parseStoredMergeDefinition: ({ schemaVersion, value, chartMetricQuery, }: {
|
|
589
|
+
schemaVersion: number;
|
|
590
|
+
value: unknown;
|
|
591
|
+
chartMetricQuery: MetricQuery;
|
|
592
|
+
}) => SavedMergeDefinition | null;
|
|
593
|
+
/** Normalizes either supported request shape to the saved schema v3 definition. */
|
|
594
|
+
export declare const normalizeSavedMergeDefinition: (value: SavedMergeDefinition | SavedMergeQuery, chartMetricQuery: MetricQuery) => SavedMergeDefinition | null;
|
|
491
595
|
//# sourceMappingURL=mergeQuery.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mergeQuery.d.ts","sourceRoot":"","sources":["../../../src/types/mergeQuery.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"mergeQuery.d.ts","sourceRoot":"","sources":["../../../src/types/mergeQuery.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,mBAAmB,EAAE,MAAM,OAAO,CAAC;AAC5C,OAAO,EACH,aAAa,EACb,KAAK,eAAe,EACpB,KAAK,OAAO,EACZ,KAAK,QAAQ,EACb,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACvB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,UAAU,CAAC;AACxC,OAAO,EACH,KAAK,gBAAgB,EACrB,KAAK,kBAAkB,EACvB,KAAK,eAAe,EACpB,KAAK,WAAW,EAChB,KAAK,SAAS,EACjB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,KAAK,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,EAAE,KAAK,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAClD,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C;;GAEG;AACH,oBAAY,aAAa;IACrB,wCAAwC;IACxC,IAAI,SAAS;IACb,iDAAiD;IACjD,IAAI,SAAS;IACb,6CAA6C;IAC7C,KAAK,UAAU;CAClB;AAED;;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,mFAAmF;AACnF,eAAO,MAAM,6BAA6B,UAC/B,oBAAoB,GAAG,eAAe,oBAC3B,WAAW,KAC9B,oBAAoB,GAAG,IAG+B,CAAC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import assertUnreachable from '../utils/assertUnreachable';
|
|
2
|
+
import { getTotalFilterRules } from '../utils/filters';
|
|
2
3
|
import { getItemId } from '../utils/item';
|
|
3
4
|
import { SupportedDbtAdapter } from './dbt';
|
|
4
5
|
import { DimensionType, } from './field';
|
|
@@ -400,51 +401,18 @@ export const getMergeCompiledSqlText = (compiled) => {
|
|
|
400
401
|
`-- Merge: runs on the compose engine over the results above, read as merge_source_0, merge_source_1, ...\n${compiled.sql}`,
|
|
401
402
|
].join('\n\n');
|
|
402
403
|
};
|
|
403
|
-
/** Current JSON schema stored in `saved_queries_version_merges.merge
|
|
404
|
-
export const SAVED_MERGE_QUERY_SCHEMA_VERSION =
|
|
405
|
-
/**
|
|
406
|
-
|
|
407
|
-
*/
|
|
408
|
-
export const buildMergeQueryFromSaved = (chartMetricQuery, saved) => {
|
|
409
|
-
const repeating = new Set(saved.repeatValuesSourceIds ?? []);
|
|
410
|
-
const sources = saved.sources.map((source) => {
|
|
411
|
-
const repeat = repeating.has(source.id) ? { repeatValues: true } : {};
|
|
412
|
-
if (source.kind === 'chart') {
|
|
413
|
-
return { ...repeat, id: source.id, metricQuery: chartMetricQuery };
|
|
414
|
-
}
|
|
415
|
-
return { ...repeat, id: source.id, metricQuery: source.metricQuery };
|
|
416
|
-
});
|
|
417
|
-
const primaryIndex = sources.findIndex((source) => source.id === saved.primarySourceId);
|
|
418
|
-
if (primaryIndex > 0) {
|
|
419
|
-
sources.unshift(...sources.splice(primaryIndex, 1));
|
|
420
|
-
}
|
|
421
|
-
return {
|
|
422
|
-
sources,
|
|
423
|
-
joinKey: saved.joinKey,
|
|
424
|
-
joinType: saved.joinType,
|
|
425
|
-
tableCalculations: saved.tableCalculations,
|
|
426
|
-
// The chart's sort state is the Explorer's, so it may still name
|
|
427
|
-
// primary fields from before the merge was built
|
|
428
|
-
sorts: toMergedSorts({
|
|
429
|
-
sorts: chartMetricQuery.sorts,
|
|
430
|
-
primarySourceId: saved.primarySourceId,
|
|
431
|
-
primaryMetricQuery: chartMetricQuery,
|
|
432
|
-
joinKey: saved.joinKey,
|
|
433
|
-
}),
|
|
434
|
-
limit: chartMetricQuery.limit,
|
|
435
|
-
};
|
|
436
|
-
};
|
|
404
|
+
/** Current JSON schema stored in `saved_queries_version_merges.merge`: a merge. */
|
|
405
|
+
export const SAVED_MERGE_QUERY_SCHEMA_VERSION = 3;
|
|
406
|
+
/** The legacy shape with explicit source ids. Still read, never written. */
|
|
407
|
+
export const SAVED_MERGE_QUERY_SCHEMA_VERSION_V2 = 2;
|
|
437
408
|
const parseJoinType = (value) => Object.values(MergeJoinType).includes(value)
|
|
438
409
|
? value
|
|
439
410
|
: MergeJoinType.FULL;
|
|
440
411
|
/**
|
|
441
|
-
* Reads a
|
|
442
|
-
* together.
|
|
443
|
-
* merge rather than breaking the chart entirely.
|
|
412
|
+
* Reads a v2 merge back, returning null for anything that does not hold
|
|
413
|
+
* together.
|
|
444
414
|
*/
|
|
445
|
-
export const parseSavedMergeQuery = (
|
|
446
|
-
if (schemaVersion !== SAVED_MERGE_QUERY_SCHEMA_VERSION)
|
|
447
|
-
return null;
|
|
415
|
+
export const parseSavedMergeQuery = (value) => {
|
|
448
416
|
if (value === null || typeof value !== 'object')
|
|
449
417
|
return null;
|
|
450
418
|
const candidate = value;
|
|
@@ -502,4 +470,394 @@ export const parseSavedMergeQuery = (schemaVersion, value) => {
|
|
|
502
470
|
...(repeatValuesSourceIds.length > 0 ? { repeatValuesSourceIds } : {}),
|
|
503
471
|
};
|
|
504
472
|
};
|
|
473
|
+
const MERGE_DEFINITION_REFERENCE_SEPARATOR = '.';
|
|
474
|
+
const MERGE_DEFINITION_NAME_PATTERN = /^[A-Za-z][A-Za-z0-9_]*$/;
|
|
475
|
+
/** `query.fieldId` into another query, or a bare chart field id. */
|
|
476
|
+
export const parseMergeDefinitionReference = (reference) => {
|
|
477
|
+
const separator = reference.indexOf(MERGE_DEFINITION_REFERENCE_SEPARATOR);
|
|
478
|
+
return separator === -1
|
|
479
|
+
? { query: null, fieldId: reference }
|
|
480
|
+
: {
|
|
481
|
+
query: reference.slice(0, separator),
|
|
482
|
+
fieldId: reference.slice(separator + 1),
|
|
483
|
+
};
|
|
484
|
+
};
|
|
485
|
+
/** The name the chart's query goes by in the merged result. */
|
|
486
|
+
export const getMergeDefinitionChartName = (merge, chartExploreName) => merge.chartAs ?? chartExploreName;
|
|
487
|
+
/** The explore names of the queries beside the chart's, in order. */
|
|
488
|
+
export const getMergeDefinitionQueryExploreNames = (merge) => Object.values(merge.queries).map(({ explore }) => explore);
|
|
489
|
+
const getMergeDefinitionJoinKey = (merge, chartName) => Object.entries(merge.keys).map(([chartFieldId, references]) => ({
|
|
490
|
+
name: merge.keyNames?.[chartFieldId] ?? chartFieldId,
|
|
491
|
+
fieldIdBySourceId: {
|
|
492
|
+
[chartName]: chartFieldId,
|
|
493
|
+
...Object.fromEntries(references.map((reference) => {
|
|
494
|
+
const { query, fieldId } = parseMergeDefinitionReference(reference);
|
|
495
|
+
return [query ?? chartName, fieldId];
|
|
496
|
+
})),
|
|
497
|
+
},
|
|
498
|
+
}));
|
|
499
|
+
/** The merged field id a sort's `by` names. */
|
|
500
|
+
const resolveMergeDefinitionSortField = (by, merge, chartName) => {
|
|
501
|
+
if (merge.tableCalculations?.some(({ name }) => name === by))
|
|
502
|
+
return by;
|
|
503
|
+
if (by in merge.keys) {
|
|
504
|
+
return getItemId({
|
|
505
|
+
table: MERGE_TABLE_NAME,
|
|
506
|
+
name: merge.keyNames?.[by] ?? by,
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
const { query, fieldId } = parseMergeDefinitionReference(by);
|
|
510
|
+
return getItemId({ table: query ?? chartName, name: fieldId });
|
|
511
|
+
};
|
|
512
|
+
/** The `by` that names a merged field id, or null when nothing does. */
|
|
513
|
+
const toMergeDefinitionSortReference = (mergedFieldId, { chartName, queryNames, joinKey, calculationNames, }) => {
|
|
514
|
+
if (calculationNames.includes(mergedFieldId))
|
|
515
|
+
return mergedFieldId;
|
|
516
|
+
const keyPart = joinKey.find((part) => getItemId({ table: MERGE_TABLE_NAME, name: part.name }) ===
|
|
517
|
+
mergedFieldId);
|
|
518
|
+
if (keyPart)
|
|
519
|
+
return keyPart.fieldIdBySourceId[chartName] ?? null;
|
|
520
|
+
// Longest name first: `orders_2` must win over `orders`
|
|
521
|
+
const names = [chartName, ...queryNames].sort((a, b) => b.length - a.length);
|
|
522
|
+
const owner = names.find((name) => mergedFieldId.startsWith(`${name}_`));
|
|
523
|
+
if (!owner)
|
|
524
|
+
return null;
|
|
525
|
+
const fieldId = mergedFieldId.slice(owner.length + 1);
|
|
526
|
+
return owner === chartName
|
|
527
|
+
? fieldId
|
|
528
|
+
: `${owner}${MERGE_DEFINITION_REFERENCE_SEPARATOR}${fieldId}`;
|
|
529
|
+
};
|
|
530
|
+
const toMergeDefinitionQuery = (metricQuery, repeat) => ({
|
|
531
|
+
explore: metricQuery.exploreName,
|
|
532
|
+
dimensions: metricQuery.dimensions,
|
|
533
|
+
metrics: metricQuery.metrics,
|
|
534
|
+
...(getTotalFilterRules(metricQuery.filters).length > 0
|
|
535
|
+
? { filters: metricQuery.filters }
|
|
536
|
+
: {}),
|
|
537
|
+
...(metricQuery.tableCalculations.length > 0
|
|
538
|
+
? { tableCalculations: metricQuery.tableCalculations }
|
|
539
|
+
: {}),
|
|
540
|
+
...(metricQuery.additionalMetrics?.length
|
|
541
|
+
? { additionalMetrics: metricQuery.additionalMetrics }
|
|
542
|
+
: {}),
|
|
543
|
+
...(metricQuery.customDimensions?.length
|
|
544
|
+
? { customDimensions: metricQuery.customDimensions }
|
|
545
|
+
: {}),
|
|
546
|
+
...(metricQuery.metricOverrides &&
|
|
547
|
+
Object.keys(metricQuery.metricOverrides).length > 0
|
|
548
|
+
? { metricOverrides: metricQuery.metricOverrides }
|
|
549
|
+
: {}),
|
|
550
|
+
...(metricQuery.dimensionOverrides &&
|
|
551
|
+
Object.keys(metricQuery.dimensionOverrides).length > 0
|
|
552
|
+
? { dimensionOverrides: metricQuery.dimensionOverrides }
|
|
553
|
+
: {}),
|
|
554
|
+
...(metricQuery.timezone ? { timezone: metricQuery.timezone } : {}),
|
|
555
|
+
...(repeat ? { repeat: true } : {}),
|
|
556
|
+
});
|
|
557
|
+
/**
|
|
558
|
+
* The merge a runnable merge saves as. The chart's source must be one of
|
|
559
|
+
* the sources and every other source must be a metric query, because a
|
|
560
|
+
* saved chart cannot re-run a referenced result.
|
|
561
|
+
*/
|
|
562
|
+
export const buildSavedMergeDefinition = ({ mergeQuery, chartSourceId, }) => {
|
|
563
|
+
const chartSource = mergeQuery.sources.find((source) => source.id === chartSourceId);
|
|
564
|
+
if (!chartSource || !isMergeMetricSource(chartSource)) {
|
|
565
|
+
throw new Error('A saved merge requires the chart query.');
|
|
566
|
+
}
|
|
567
|
+
const others = mergeQuery.sources.filter((source) => source.id !== chartSourceId);
|
|
568
|
+
const queries = Object.fromEntries(others.map((source) => {
|
|
569
|
+
if (!isMergeMetricSource(source)) {
|
|
570
|
+
throw new Error('A merge over existing query results cannot be saved to a chart.');
|
|
571
|
+
}
|
|
572
|
+
return [
|
|
573
|
+
source.id,
|
|
574
|
+
toMergeDefinitionQuery(source.metricQuery, source.repeatValues === true),
|
|
575
|
+
];
|
|
576
|
+
}));
|
|
577
|
+
const keys = Object.fromEntries(mergeQuery.joinKey.map((part) => {
|
|
578
|
+
const chartFieldId = part.fieldIdBySourceId[chartSourceId];
|
|
579
|
+
if (!chartFieldId) {
|
|
580
|
+
throw new Error('Every join key needs the chart query.');
|
|
581
|
+
}
|
|
582
|
+
return [
|
|
583
|
+
chartFieldId,
|
|
584
|
+
others.map((source) => `${source.id}${MERGE_DEFINITION_REFERENCE_SEPARATOR}${part.fieldIdBySourceId[source.id]}`),
|
|
585
|
+
];
|
|
586
|
+
}));
|
|
587
|
+
const keyNames = Object.fromEntries(mergeQuery.joinKey.flatMap((part) => {
|
|
588
|
+
const chartFieldId = part.fieldIdBySourceId[chartSourceId];
|
|
589
|
+
return part.name === chartFieldId
|
|
590
|
+
? []
|
|
591
|
+
: [[chartFieldId, part.name]];
|
|
592
|
+
}));
|
|
593
|
+
const sortContext = {
|
|
594
|
+
chartName: chartSourceId,
|
|
595
|
+
queryNames: others.map(({ id }) => id),
|
|
596
|
+
joinKey: mergeQuery.joinKey,
|
|
597
|
+
calculationNames: mergeQuery.tableCalculations.map(({ name }) => name),
|
|
598
|
+
};
|
|
599
|
+
const sort = (mergeQuery.sorts ?? []).flatMap((field) => {
|
|
600
|
+
const by = toMergeDefinitionSortReference(field.fieldId, sortContext);
|
|
601
|
+
return by === null
|
|
602
|
+
? []
|
|
603
|
+
: [{ by, direction: field.descending ? 'desc' : 'asc' }];
|
|
604
|
+
});
|
|
605
|
+
const chartRepeats = chartSource.repeatValues === true;
|
|
606
|
+
return {
|
|
607
|
+
...(chartSourceId !== chartSource.metricQuery.exploreName
|
|
608
|
+
? { chartAs: chartSourceId }
|
|
609
|
+
: {}),
|
|
610
|
+
queries,
|
|
611
|
+
join: mergeQuery.joinType,
|
|
612
|
+
keys,
|
|
613
|
+
...(Object.keys(keyNames).length > 0 ? { keyNames } : {}),
|
|
614
|
+
...(sort.length > 0 ? { sort } : {}),
|
|
615
|
+
limit: mergeQuery.limit,
|
|
616
|
+
...(mergeQuery.tableCalculations.length > 0
|
|
617
|
+
? { tableCalculations: mergeQuery.tableCalculations }
|
|
618
|
+
: {}),
|
|
619
|
+
...(chartRepeats ? { chartRepeats: true } : {}),
|
|
620
|
+
};
|
|
621
|
+
};
|
|
622
|
+
/** Rebuilds a runnable merge from a chart's own query and its stored merge. */
|
|
623
|
+
export const buildMergeQueryFromMergeDefinition = (chartMetricQuery, merge) => {
|
|
624
|
+
const chartName = getMergeDefinitionChartName(merge, chartMetricQuery.exploreName);
|
|
625
|
+
const sources = [
|
|
626
|
+
{
|
|
627
|
+
id: chartName,
|
|
628
|
+
metricQuery: chartMetricQuery,
|
|
629
|
+
...(merge.chartRepeats ? { repeatValues: true } : {}),
|
|
630
|
+
},
|
|
631
|
+
...Object.entries(merge.queries).map(([name, query]) => {
|
|
632
|
+
const { explore, repeat, ...intent } = query;
|
|
633
|
+
return {
|
|
634
|
+
id: name,
|
|
635
|
+
metricQuery: {
|
|
636
|
+
exploreName: explore,
|
|
637
|
+
...intent,
|
|
638
|
+
filters: query.filters ?? {},
|
|
639
|
+
tableCalculations: query.tableCalculations ?? [],
|
|
640
|
+
sorts: [],
|
|
641
|
+
limit: merge.limit,
|
|
642
|
+
},
|
|
643
|
+
...(repeat ? { repeatValues: true } : {}),
|
|
644
|
+
};
|
|
645
|
+
}),
|
|
646
|
+
];
|
|
647
|
+
return {
|
|
648
|
+
sources,
|
|
649
|
+
joinKey: getMergeDefinitionJoinKey(merge, chartName),
|
|
650
|
+
joinType: merge.join,
|
|
651
|
+
tableCalculations: merge.tableCalculations ?? [],
|
|
652
|
+
sorts: (merge.sort ?? []).map(({ by, direction }) => ({
|
|
653
|
+
fieldId: resolveMergeDefinitionSortField(by, merge, chartName),
|
|
654
|
+
descending: direction === 'desc',
|
|
655
|
+
})),
|
|
656
|
+
limit: merge.limit,
|
|
657
|
+
};
|
|
658
|
+
};
|
|
659
|
+
/**
|
|
660
|
+
* Rebuilds a runnable merge from a chart's own query and a schema v2 merge,
|
|
661
|
+
* the shape requests and Document chart cells still carry.
|
|
662
|
+
*/
|
|
663
|
+
export const buildMergeQueryFromSaved = (chartMetricQuery, saved) => {
|
|
664
|
+
const repeating = new Set(saved.repeatValuesSourceIds ?? []);
|
|
665
|
+
const sources = saved.sources.map((source) => {
|
|
666
|
+
const repeat = repeating.has(source.id) ? { repeatValues: true } : {};
|
|
667
|
+
if (source.kind === 'chart') {
|
|
668
|
+
return { ...repeat, id: source.id, metricQuery: chartMetricQuery };
|
|
669
|
+
}
|
|
670
|
+
return { ...repeat, id: source.id, metricQuery: source.metricQuery };
|
|
671
|
+
});
|
|
672
|
+
const primaryIndex = sources.findIndex((source) => source.id === saved.primarySourceId);
|
|
673
|
+
if (primaryIndex > 0) {
|
|
674
|
+
sources.unshift(...sources.splice(primaryIndex, 1));
|
|
675
|
+
}
|
|
676
|
+
return {
|
|
677
|
+
sources,
|
|
678
|
+
joinKey: saved.joinKey,
|
|
679
|
+
joinType: saved.joinType,
|
|
680
|
+
tableCalculations: saved.tableCalculations,
|
|
681
|
+
// The chart's sort state is the Explorer's, so it may still name
|
|
682
|
+
// primary fields from before the merge was built
|
|
683
|
+
sorts: toMergedSorts({
|
|
684
|
+
sorts: chartMetricQuery.sorts,
|
|
685
|
+
primarySourceId: saved.primarySourceId,
|
|
686
|
+
primaryMetricQuery: chartMetricQuery,
|
|
687
|
+
joinKey: saved.joinKey,
|
|
688
|
+
}),
|
|
689
|
+
limit: chartMetricQuery.limit,
|
|
690
|
+
};
|
|
691
|
+
};
|
|
692
|
+
/**
|
|
693
|
+
* A pure rewrite of the v2 shape, keeping the ids it had. The chart's sort
|
|
694
|
+
* state was the Explorer's and named primary fields, so it is mapped once
|
|
695
|
+
* here; the chart's limit was the merged result's. A v2 merge whose
|
|
696
|
+
* primary was not the chart has no merge form: the chart is always the
|
|
697
|
+
* first input.
|
|
698
|
+
*/
|
|
699
|
+
export const upgradeSavedMergeQuery = (saved, chartMetricQuery) => {
|
|
700
|
+
const chartSource = saved.sources.find((source) => source.kind === 'chart');
|
|
701
|
+
if (!chartSource || chartSource.id !== saved.primarySourceId)
|
|
702
|
+
return null;
|
|
703
|
+
return buildSavedMergeDefinition({
|
|
704
|
+
mergeQuery: buildMergeQueryFromSaved(chartMetricQuery, saved),
|
|
705
|
+
chartSourceId: chartSource.id,
|
|
706
|
+
});
|
|
707
|
+
};
|
|
708
|
+
const isRecord = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
709
|
+
const isStringArray = (value) => Array.isArray(value) && value.every((item) => typeof item === 'string');
|
|
710
|
+
const isMergeDefinitionName = (value) => typeof value === 'string' &&
|
|
711
|
+
MERGE_DEFINITION_NAME_PATTERN.test(value) &&
|
|
712
|
+
value !== MERGE_TABLE_NAME;
|
|
713
|
+
const parseMergeDefinitionQuery = (candidate) => {
|
|
714
|
+
if (!isRecord(candidate) ||
|
|
715
|
+
typeof candidate.explore !== 'string' ||
|
|
716
|
+
!isStringArray(candidate.dimensions) ||
|
|
717
|
+
!isStringArray(candidate.metrics) ||
|
|
718
|
+
(candidate.filters !== undefined && !isRecord(candidate.filters)) ||
|
|
719
|
+
(candidate.tableCalculations !== undefined &&
|
|
720
|
+
!Array.isArray(candidate.tableCalculations)) ||
|
|
721
|
+
(candidate.repeat !== undefined &&
|
|
722
|
+
typeof candidate.repeat !== 'boolean')) {
|
|
723
|
+
return null;
|
|
724
|
+
}
|
|
725
|
+
return toMergeDefinitionQuery({
|
|
726
|
+
exploreName: candidate.explore,
|
|
727
|
+
dimensions: candidate.dimensions,
|
|
728
|
+
metrics: candidate.metrics,
|
|
729
|
+
filters: candidate.filters ?? {},
|
|
730
|
+
tableCalculations: candidate.tableCalculations ?? [],
|
|
731
|
+
sorts: [],
|
|
732
|
+
limit: 0,
|
|
733
|
+
additionalMetrics: Array.isArray(candidate.additionalMetrics)
|
|
734
|
+
? candidate.additionalMetrics
|
|
735
|
+
: undefined,
|
|
736
|
+
customDimensions: Array.isArray(candidate.customDimensions)
|
|
737
|
+
? candidate.customDimensions
|
|
738
|
+
: undefined,
|
|
739
|
+
metricOverrides: isRecord(candidate.metricOverrides)
|
|
740
|
+
? candidate.metricOverrides
|
|
741
|
+
: undefined,
|
|
742
|
+
dimensionOverrides: isRecord(candidate.dimensionOverrides)
|
|
743
|
+
? candidate.dimensionOverrides
|
|
744
|
+
: undefined,
|
|
745
|
+
timezone: typeof candidate.timezone === 'string'
|
|
746
|
+
? candidate.timezone
|
|
747
|
+
: undefined,
|
|
748
|
+
}, candidate.repeat === true);
|
|
749
|
+
};
|
|
750
|
+
const parseMergeDefinitionKeys = (candidate, queryNames) => {
|
|
751
|
+
if (!isRecord(candidate) || Object.keys(candidate).length === 0)
|
|
752
|
+
return null;
|
|
753
|
+
const entries = Object.entries(candidate).map(([chartFieldId, references]) => {
|
|
754
|
+
if (chartFieldId.length === 0 || !isStringArray(references)) {
|
|
755
|
+
return null;
|
|
756
|
+
}
|
|
757
|
+
// One reference per other query, each into a query that exists
|
|
758
|
+
const referenced = references.map((reference) => parseMergeDefinitionReference(reference).query);
|
|
759
|
+
const complete = referenced.length === queryNames.length &&
|
|
760
|
+
queryNames.every((name) => referenced.includes(name)) &&
|
|
761
|
+
references.every((reference) => parseMergeDefinitionReference(reference).fieldId
|
|
762
|
+
.length > 0);
|
|
763
|
+
return complete ? [chartFieldId, references] : null;
|
|
764
|
+
});
|
|
765
|
+
return entries.every((entry) => entry !== null)
|
|
766
|
+
? Object.fromEntries(entries)
|
|
767
|
+
: null;
|
|
768
|
+
};
|
|
769
|
+
/**
|
|
770
|
+
* Reads a stored merge back, returning null for anything that does not
|
|
771
|
+
* hold together: named queries against explores, one join with a key that
|
|
772
|
+
* reaches every query, a known join type, a numeric limit.
|
|
773
|
+
*/
|
|
774
|
+
export const parseSavedMergeDefinition = (value) => {
|
|
775
|
+
if (!isRecord(value))
|
|
776
|
+
return null;
|
|
777
|
+
const rawQueries = value.queries;
|
|
778
|
+
if (!isRecord(rawQueries))
|
|
779
|
+
return null;
|
|
780
|
+
const queryNames = Object.keys(rawQueries);
|
|
781
|
+
if (queryNames.length === 0 ||
|
|
782
|
+
!queryNames.every(isMergeDefinitionName) ||
|
|
783
|
+
(value.chartAs !== undefined &&
|
|
784
|
+
(!isMergeDefinitionName(value.chartAs) ||
|
|
785
|
+
queryNames.includes(value.chartAs)))) {
|
|
786
|
+
return null;
|
|
787
|
+
}
|
|
788
|
+
const parsedQueries = queryNames.map((name) => [
|
|
789
|
+
name,
|
|
790
|
+
parseMergeDefinitionQuery(rawQueries[name]),
|
|
791
|
+
]);
|
|
792
|
+
if (parsedQueries.some(([, query]) => query === null))
|
|
793
|
+
return null;
|
|
794
|
+
if (!Object.values(MergeJoinType).includes(value.join)) {
|
|
795
|
+
return null;
|
|
796
|
+
}
|
|
797
|
+
const keys = parseMergeDefinitionKeys(value.keys, queryNames);
|
|
798
|
+
if (keys === null)
|
|
799
|
+
return null;
|
|
800
|
+
if (value.keyNames !== undefined &&
|
|
801
|
+
(!isRecord(value.keyNames) ||
|
|
802
|
+
!Object.entries(value.keyNames).every(([chartFieldId, name]) => chartFieldId in keys &&
|
|
803
|
+
typeof name === 'string' &&
|
|
804
|
+
name.length > 0))) {
|
|
805
|
+
return null;
|
|
806
|
+
}
|
|
807
|
+
if (value.sort !== undefined &&
|
|
808
|
+
(!Array.isArray(value.sort) ||
|
|
809
|
+
!value.sort.every((sort) => isRecord(sort) &&
|
|
810
|
+
typeof sort.by === 'string' &&
|
|
811
|
+
(sort.direction === 'asc' || sort.direction === 'desc')))) {
|
|
812
|
+
return null;
|
|
813
|
+
}
|
|
814
|
+
if (typeof value.limit !== 'number' ||
|
|
815
|
+
(value.tableCalculations !== undefined &&
|
|
816
|
+
!Array.isArray(value.tableCalculations)) ||
|
|
817
|
+
(value.chartRepeats !== undefined &&
|
|
818
|
+
typeof value.chartRepeats !== 'boolean')) {
|
|
819
|
+
return null;
|
|
820
|
+
}
|
|
821
|
+
const keyNames = value.keyNames;
|
|
822
|
+
const sort = value.sort;
|
|
823
|
+
const tableCalculations = value.tableCalculations;
|
|
824
|
+
return {
|
|
825
|
+
...(value.chartAs !== undefined
|
|
826
|
+
? { chartAs: value.chartAs }
|
|
827
|
+
: {}),
|
|
828
|
+
queries: Object.fromEntries(parsedQueries),
|
|
829
|
+
join: value.join,
|
|
830
|
+
keys,
|
|
831
|
+
...(keyNames && Object.keys(keyNames).length > 0 ? { keyNames } : {}),
|
|
832
|
+
...(sort && sort.length > 0 ? { sort } : {}),
|
|
833
|
+
limit: value.limit,
|
|
834
|
+
...(tableCalculations && tableCalculations.length > 0
|
|
835
|
+
? { tableCalculations }
|
|
836
|
+
: {}),
|
|
837
|
+
...(value.chartRepeats === true ? { chartRepeats: true } : {}),
|
|
838
|
+
};
|
|
839
|
+
};
|
|
840
|
+
/**
|
|
841
|
+
* Reads whatever a chart version stored. A v2 merge is rewritten to a
|
|
842
|
+
* merge with the chart's own sort and limit; an unknown version leaves
|
|
843
|
+
* the chart working without its merge rather than breaking the chart.
|
|
844
|
+
*/
|
|
845
|
+
export const parseStoredMergeDefinition = ({ schemaVersion, value, chartMetricQuery, }) => {
|
|
846
|
+
switch (schemaVersion) {
|
|
847
|
+
case SAVED_MERGE_QUERY_SCHEMA_VERSION:
|
|
848
|
+
return parseSavedMergeDefinition(value);
|
|
849
|
+
case SAVED_MERGE_QUERY_SCHEMA_VERSION_V2: {
|
|
850
|
+
const saved = parseSavedMergeQuery(value);
|
|
851
|
+
return saved
|
|
852
|
+
? upgradeSavedMergeQuery(saved, chartMetricQuery)
|
|
853
|
+
: null;
|
|
854
|
+
}
|
|
855
|
+
default:
|
|
856
|
+
return null;
|
|
857
|
+
}
|
|
858
|
+
};
|
|
859
|
+
/** Normalizes either supported request shape to the saved schema v3 definition. */
|
|
860
|
+
export const normalizeSavedMergeDefinition = (value, chartMetricQuery) => 'queries' in value
|
|
861
|
+
? parseSavedMergeDefinition(value)
|
|
862
|
+
: upgradeSavedMergeQuery(value, chartMetricQuery);
|
|
505
863
|
//# sourceMappingURL=mergeQuery.js.map
|