@hypequery/datasets 0.32.0 → 0.35.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/agent-catalog.d.ts +3 -3
- package/dist/agent-catalog.d.ts.map +1 -1
- package/dist/cache/protocol-cache-keys.d.ts.map +1 -1
- package/dist/cache/protocol-cache-keys.js +10 -48
- package/dist/cache/query-signature.d.ts.map +1 -1
- package/dist/cache/query-signature.js +16 -5
- package/dist/catalog.d.ts +5 -1
- package/dist/catalog.d.ts.map +1 -1
- package/dist/catalog.js +9 -0
- package/dist/client-contract/build-from-deployment.d.ts +2 -2
- package/dist/client-contract/build-from-deployment.d.ts.map +1 -1
- package/dist/client-contract/build-from-deployment.js +7 -0
- package/dist/client-contract/dataset-fields.d.ts.map +1 -1
- package/dist/client-contract/dataset-fields.js +2 -1
- package/dist/cloud-publication.d.ts +3 -3
- package/dist/cloud-publication.d.ts.map +1 -1
- package/dist/cloud-publishing.d.ts +2 -2
- package/dist/cloud-publishing.d.ts.map +1 -1
- package/dist/codegen/dataset-source.d.ts.map +1 -1
- package/dist/codegen/dataset-source.js +10 -1
- package/dist/contract-3-authoring.fixture.d.ts +103 -0
- package/dist/contract-3-authoring.fixture.d.ts.map +1 -0
- package/dist/contract-3-authoring.fixture.js +40 -0
- package/dist/contract.d.ts +5 -1
- package/dist/contract.d.ts.map +1 -1
- package/dist/contract.js +9 -0
- package/dist/dataset-compilation.d.ts +3 -0
- package/dist/dataset-compilation.d.ts.map +1 -1
- package/dist/dataset-query.d.ts +5 -0
- package/dist/dataset-query.d.ts.map +1 -1
- package/dist/dataset.d.ts.map +1 -1
- package/dist/dataset.js +12 -2
- package/dist/event-operations.d.ts +43 -0
- package/dist/event-operations.d.ts.map +1 -0
- package/dist/event-operations.js +137 -0
- package/dist/execution-capabilities.d.ts +16 -3
- package/dist/execution-capabilities.d.ts.map +1 -1
- package/dist/execution-capabilities.js +5 -3
- package/dist/execution-capabilities.json +7 -8
- package/dist/executor.d.ts +13 -0
- package/dist/executor.d.ts.map +1 -1
- package/dist/executor.js +42 -8
- package/dist/index.d.ts +7 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/portable-execution-errors.d.ts +11 -0
- package/dist/portable-execution-errors.d.ts.map +1 -1
- package/dist/portable-execution-errors.js +14 -0
- package/dist/portable-executor.d.ts +22 -5
- package/dist/portable-executor.d.ts.map +1 -1
- package/dist/portable-executor.js +64 -5
- package/dist/protocol-adapter.d.ts +12 -0
- package/dist/protocol-adapter.d.ts.map +1 -1
- package/dist/protocol-adapter.js +32 -10
- package/dist/protocol-deployment-adapter.d.ts +10 -3
- package/dist/protocol-deployment-adapter.d.ts.map +1 -1
- package/dist/protocol-deployment-adapter.js +60 -26
- package/dist/protocol-rehydrate.d.ts +3 -16
- package/dist/protocol-rehydrate.d.ts.map +1 -1
- package/dist/protocol-rehydrate.js +72 -14
- package/dist/query-builder-protocol.d.ts +10 -0
- package/dist/query-builder-protocol.d.ts.map +1 -1
- package/dist/query-planner.d.ts.map +1 -1
- package/dist/query-planner.js +3 -1
- package/dist/relationship-check.d.ts.map +1 -1
- package/dist/relationship-check.js +2 -0
- package/dist/semantic-planner.d.ts.map +1 -1
- package/dist/semantic-planner.js +17 -4
- package/dist/semantic-query-schema.d.ts +18 -0
- package/dist/semantic-query-schema.d.ts.map +1 -1
- package/dist/semantic-query-schema.js +79 -55
- package/dist/tools.d.ts +6 -0
- package/dist/tools.d.ts.map +1 -1
- package/dist/types.d.ts +121 -10
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/agent-catalog-projection.d.ts +2 -12
- package/dist/utils/agent-catalog-projection.d.ts.map +1 -1
- package/dist/utils/agent-catalog-projection.js +39 -2
- package/dist/utils/build-dataset-query-builder.d.ts.map +1 -1
- package/dist/utils/build-dataset-query-builder.js +4 -8
- package/dist/utils/calculation-stage-sql.d.ts +31 -0
- package/dist/utils/calculation-stage-sql.d.ts.map +1 -0
- package/dist/utils/calculation-stage-sql.js +98 -0
- package/dist/utils/compile-calculated-dataset-query.d.ts +24 -0
- package/dist/utils/compile-calculated-dataset-query.d.ts.map +1 -0
- package/dist/utils/compile-calculated-dataset-query.js +133 -0
- package/dist/utils/compile-dataset-query.d.ts.map +1 -1
- package/dist/utils/compile-dataset-query.js +11 -3
- package/dist/utils/dataset-definition-validation.d.ts.map +1 -1
- package/dist/utils/dataset-definition-validation.js +94 -7
- package/dist/utils/dataset-events.d.ts +19 -0
- package/dist/utils/dataset-events.d.ts.map +1 -0
- package/dist/utils/dataset-events.js +46 -0
- package/dist/utils/dataset-having.d.ts.map +1 -1
- package/dist/utils/dataset-having.js +6 -1
- package/dist/utils/dataset-query-result.d.ts.map +1 -1
- package/dist/utils/dataset-query-result.js +7 -1
- package/dist/utils/dataset-query-validation.d.ts +1 -1
- package/dist/utils/dataset-query-validation.d.ts.map +1 -1
- package/dist/utils/dataset-query-validation.js +25 -47
- package/dist/utils/dataset-source.d.ts +2 -2
- package/dist/utils/dataset-source.d.ts.map +1 -1
- package/dist/utils/dataset-source.js +8 -1
- package/dist/utils/dataset-validation.d.ts.map +1 -1
- package/dist/utils/dataset-validation.js +2 -1
- package/dist/utils/dimension-selection-overrides.d.ts +19 -0
- package/dist/utils/dimension-selection-overrides.d.ts.map +1 -0
- package/dist/utils/dimension-selection-overrides.js +22 -0
- package/dist/utils/event-breakdown-sql.d.ts +15 -0
- package/dist/utils/event-breakdown-sql.d.ts.map +1 -0
- package/dist/utils/event-breakdown-sql.js +17 -0
- package/dist/utils/event-condition-sql.d.ts +18 -0
- package/dist/utils/event-condition-sql.d.ts.map +1 -0
- package/dist/utils/event-condition-sql.js +63 -0
- package/dist/utils/event-operation-shape.d.ts +8 -0
- package/dist/utils/event-operation-shape.d.ts.map +1 -0
- package/dist/utils/event-operation-shape.js +66 -0
- package/dist/utils/event-operation-spec.d.ts +43 -0
- package/dist/utils/event-operation-spec.d.ts.map +1 -0
- package/dist/utils/event-operation-spec.js +68 -0
- package/dist/utils/event-range-span.d.ts +4 -0
- package/dist/utils/event-range-span.d.ts.map +1 -0
- package/dist/utils/event-range-span.js +22 -0
- package/dist/utils/event-result-values.d.ts +8 -0
- package/dist/utils/event-result-values.d.ts.map +1 -0
- package/dist/utils/event-result-values.js +21 -0
- package/dist/utils/event-source-sql.d.ts +38 -0
- package/dist/utils/event-source-sql.d.ts.map +1 -0
- package/dist/utils/event-source-sql.js +71 -0
- package/dist/utils/filter-predicate-tree.d.ts +28 -0
- package/dist/utils/filter-predicate-tree.d.ts.map +1 -0
- package/dist/utils/filter-predicate-tree.js +80 -0
- package/dist/utils/filter-tree.d.ts +42 -0
- package/dist/utils/filter-tree.d.ts.map +1 -0
- package/dist/utils/filter-tree.js +164 -0
- package/dist/utils/filtered-aggregation-sql.d.ts.map +1 -1
- package/dist/utils/filtered-aggregation-sql.js +22 -1
- package/dist/utils/funnel-rows.d.ts +10 -0
- package/dist/utils/funnel-rows.d.ts.map +1 -0
- package/dist/utils/funnel-rows.js +30 -0
- package/dist/utils/funnel-sql.d.ts +18 -0
- package/dist/utils/funnel-sql.d.ts.map +1 -0
- package/dist/utils/funnel-sql.js +104 -0
- package/dist/utils/metric-derived-query.d.ts.map +1 -1
- package/dist/utils/metric-derived-query.js +4 -5
- package/dist/utils/metric-query-builder.d.ts.map +1 -1
- package/dist/utils/metric-query-builder.js +4 -5
- package/dist/utils/metric-query-validation.d.ts.map +1 -1
- package/dist/utils/metric-query-validation.js +18 -7
- package/dist/utils/other-bucket-sql.d.ts +23 -0
- package/dist/utils/other-bucket-sql.d.ts.map +1 -0
- package/dist/utils/other-bucket-sql.js +51 -0
- package/dist/utils/portable-aggregations.d.ts +10 -6
- package/dist/utils/portable-aggregations.d.ts.map +1 -1
- package/dist/utils/portable-aggregations.js +13 -8
- package/dist/utils/portable-grains.d.ts +17 -8
- package/dist/utils/portable-grains.d.ts.map +1 -1
- package/dist/utils/portable-grains.js +26 -11
- package/dist/utils/portable-query-calculations.d.ts +14 -0
- package/dist/utils/portable-query-calculations.d.ts.map +1 -0
- package/dist/utils/portable-query-calculations.js +31 -0
- package/dist/utils/portable-semantic-query.d.ts.map +1 -1
- package/dist/utils/portable-semantic-query.js +27 -1
- package/dist/utils/protocol-contract-3.d.ts +30 -0
- package/dist/utils/protocol-contract-3.d.ts.map +1 -0
- package/dist/utils/protocol-contract-3.js +93 -0
- package/dist/utils/protocol-dataset-query.d.ts +19 -0
- package/dist/utils/protocol-dataset-query.d.ts.map +1 -0
- package/dist/utils/protocol-dataset-query.js +50 -0
- package/dist/utils/protocol-filter-nodes.d.ts +9 -0
- package/dist/utils/protocol-filter-nodes.d.ts.map +1 -0
- package/dist/utils/protocol-filter-nodes.js +68 -0
- package/dist/utils/protocol-metric-capabilities.d.ts.map +1 -1
- package/dist/utils/protocol-metric-capabilities.js +2 -1
- package/dist/utils/protocol-metric-expressions.d.ts +9 -1
- package/dist/utils/protocol-metric-expressions.d.ts.map +1 -1
- package/dist/utils/protocol-metric-expressions.js +27 -2
- package/dist/utils/protocol-rehydrate-filters.d.ts +8 -1
- package/dist/utils/protocol-rehydrate-filters.d.ts.map +1 -1
- package/dist/utils/protocol-rehydrate-filters.js +20 -1
- package/dist/utils/protocol-rehydrate-time-keys.d.ts +6 -0
- package/dist/utils/protocol-rehydrate-time-keys.d.ts.map +1 -0
- package/dist/utils/protocol-rehydrate-time-keys.js +7 -0
- package/dist/utils/query-calculation-errors.d.ts +6 -0
- package/dist/utils/query-calculation-errors.d.ts.map +1 -0
- package/dist/utils/query-calculation-errors.js +9 -0
- package/dist/utils/query-calculation-formula.d.ts +19 -0
- package/dist/utils/query-calculation-formula.d.ts.map +1 -0
- package/dist/utils/query-calculation-formula.js +77 -0
- package/dist/utils/query-calculation-outputs.d.ts +24 -0
- package/dist/utils/query-calculation-outputs.d.ts.map +1 -0
- package/dist/utils/query-calculation-outputs.js +72 -0
- package/dist/utils/query-calculation-plan.d.ts +67 -0
- package/dist/utils/query-calculation-plan.d.ts.map +1 -0
- package/dist/utils/query-calculation-plan.js +416 -0
- package/dist/utils/query-calculation-types.d.ts +97 -0
- package/dist/utils/query-calculation-types.d.ts.map +1 -0
- package/dist/utils/query-calculation-types.js +8 -0
- package/dist/utils/query-field-references.d.ts.map +1 -1
- package/dist/utils/query-field-references.js +3 -2
- package/dist/utils/query-filter-errors.d.ts +11 -0
- package/dist/utils/query-filter-errors.d.ts.map +1 -0
- package/dist/utils/query-filter-errors.js +65 -0
- package/dist/utils/query-schema-calculations.d.ts +55 -0
- package/dist/utils/query-schema-calculations.d.ts.map +1 -0
- package/dist/utils/query-schema-calculations.js +150 -0
- package/dist/utils/query-schema-filters.d.ts +47 -0
- package/dist/utils/query-schema-filters.d.ts.map +1 -0
- package/dist/utils/query-schema-filters.js +132 -0
- package/dist/utils/query-timezone.d.ts +6 -3
- package/dist/utils/query-timezone.d.ts.map +1 -1
- package/dist/utils/query-timezone.js +5 -1
- package/dist/utils/relationship-builder-plan.d.ts.map +1 -1
- package/dist/utils/relationship-builder-plan.js +2 -1
- package/dist/utils/relationship-fields.d.ts.map +1 -1
- package/dist/utils/relationship-fields.js +5 -0
- package/dist/utils/relationship-measure-sql.d.ts.map +1 -1
- package/dist/utils/relationship-measure-sql.js +4 -1
- package/dist/utils/relationship-measures.d.ts.map +1 -1
- package/dist/utils/relationship-measures.js +2 -1
- package/dist/utils/relationship-validation.d.ts +3 -1
- package/dist/utils/relationship-validation.d.ts.map +1 -1
- package/dist/utils/relationship-validation.js +8 -4
- package/dist/utils/relative-date-filters.d.ts +21 -0
- package/dist/utils/relative-date-filters.d.ts.map +1 -0
- package/dist/utils/relative-date-filters.js +86 -0
- package/dist/utils/relative-date-range.d.ts +20 -0
- package/dist/utils/relative-date-range.d.ts.map +1 -0
- package/dist/utils/relative-date-range.js +75 -0
- package/dist/utils/retention-rows.d.ts +9 -0
- package/dist/utils/retention-rows.d.ts.map +1 -0
- package/dist/utils/retention-rows.js +30 -0
- package/dist/utils/retention-sql.d.ts +16 -0
- package/dist/utils/retention-sql.d.ts.map +1 -0
- package/dist/utils/retention-sql.js +68 -0
- package/dist/utils/scoped-dataset-source.d.ts.map +1 -1
- package/dist/utils/scoped-dataset-source.js +4 -0
- package/dist/utils/segments.d.ts +3 -2
- package/dist/utils/segments.d.ts.map +1 -1
- package/dist/utils/segments.js +5 -4
- package/dist/utils/semantic-query-syntax.d.ts.map +1 -1
- package/dist/utils/semantic-query-syntax.js +8 -2
- package/dist/utils/sql-dataset-source.d.ts +36 -0
- package/dist/utils/sql-dataset-source.d.ts.map +1 -0
- package/dist/utils/sql-dataset-source.js +126 -0
- package/dist/utils/time-axis-bounds.d.ts +11 -4
- package/dist/utils/time-axis-bounds.d.ts.map +1 -1
- package/dist/utils/time-axis-bounds.js +14 -6
- package/dist/utils/time-measure-axis.d.ts +2 -2
- package/dist/utils/time-measure-axis.d.ts.map +1 -1
- package/dist/utils/time-measure-axis.js +13 -3
- package/dist/utils/time-measure-dataset-sql.d.ts +1 -1
- package/dist/utils/time-measure-dataset-sql.d.ts.map +1 -1
- package/dist/utils/time-measure-dataset-sql.js +15 -5
- package/dist/utils/time-measure-source-sql.d.ts +2 -2
- package/dist/utils/time-measure-source-sql.d.ts.map +1 -1
- package/dist/utils/time-measure-source-sql.js +7 -8
- package/dist/utils/time-query-measures.d.ts +5 -0
- package/dist/utils/time-query-measures.d.ts.map +1 -1
- package/dist/utils/time-query-measures.js +7 -0
- package/dist/utils/unsupported-contract-reasons.d.ts +5 -1
- package/dist/utils/unsupported-contract-reasons.d.ts.map +1 -1
- package/dist/utils/unsupported-contract-reasons.js +5 -1
- package/package.json +3 -3
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { filterLeaves } from './filter-tree.js';
|
|
2
|
+
import { assertTableSource } from './sql-dataset-source.js';
|
|
1
3
|
import { currentRowPredicate } from './dataset-source.js';
|
|
2
4
|
import { resolveRelationshipMeasure } from './relationship-measures.js';
|
|
3
5
|
import { resolveDimensionExpression } from '../query-planner.js';
|
|
@@ -23,7 +25,7 @@ export function relationshipJoinSource(target, to, names, dialect) {
|
|
|
23
25
|
const measure = target.measures[name];
|
|
24
26
|
if (measure.__type !== 'measure_definition')
|
|
25
27
|
continue;
|
|
26
|
-
for (const field of [measure.field, measure.argField, ...(measure.filters
|
|
28
|
+
for (const field of [measure.field, measure.argField, ...filterLeaves(measure.filters).map(filter => target.filters[filter.field]?.field ?? filter.field)]) {
|
|
27
29
|
if (field && field !== '*')
|
|
28
30
|
columns.add(target.dimensions[field]?.column ?? field);
|
|
29
31
|
}
|
|
@@ -44,6 +46,7 @@ export function relationshipJoinSource(target, to, names, dialect) {
|
|
|
44
46
|
}
|
|
45
47
|
/** The target's quoted table, read with `FINAL` when the target dataset requires it. */
|
|
46
48
|
function relationshipTargetTable(target, dialect) {
|
|
49
|
+
assertTableSource(target, 'a relationship target');
|
|
47
50
|
const quoted = dialect.quoteQualifiedName(target.source);
|
|
48
51
|
if (!target.rowResolution)
|
|
49
52
|
return quoted;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"relationship-measures.d.ts","sourceRoot":"","sources":["../../src/utils/relationship-measures.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"relationship-measures.d.ts","sourceRoot":"","sources":["../../src/utils/relationship-measures.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AAMjG,MAAM,WAAW,2BAA2B;IAC1C,gBAAgB,EAAE,MAAM,CAAC;IACzB,YAAY,EAAE,sBAAsB,CAAC;IACrC,MAAM,EAAE,kBAAkB,CAAC;IAC3B,UAAU,EAAE,iBAAiB,CAAC;CAC/B;AAED,iFAAiF;AACjF,wBAAgB,0BAA0B,CAAC,EAAE,EAAE,kBAAkB,EAAE,IAAI,EAAE,MAAM,GAAG,2BAA2B,CAqB5G;AAED,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,MAAM,EAAE,YAAY,EAAE,sBAAsB,GAAG,MAAM,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAS9H;AAED,yGAAyG;AACzG,wBAAgB,uBAAuB,CAAC,EAAE,EAAE,kBAAkB,EAAE,QAAQ,EAAE,SAAS,MAAM,EAAE,kEAM1F;AAED,kFAAkF;AAClF,wBAAgB,4BAA4B,CAAC,EAAE,EAAE,kBAAkB,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,GAAE,SAAS,MAAM,EAAO;;;;;;;;SAIlH"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { filterLeaves } from './filter-tree.js';
|
|
1
2
|
import { isBaseMeasure } from './dataset-measures.js';
|
|
2
3
|
import { parseQualifiedField, resolveQualifiedField } from './relationship-fields.js';
|
|
3
4
|
const DUPLICATE_INSENSITIVE = new Set(['countDistinct', 'approxCountDistinct', 'min', 'max', 'argMax', 'argMin']);
|
|
@@ -23,7 +24,7 @@ export function resolveRelationshipMeasure(ds, name) {
|
|
|
23
24
|
if (relationship.kind === 'belongsTo' && !DUPLICATE_INSENSITIVE.has(definition.aggregation)) {
|
|
24
25
|
throw new Error(`Measure "${name}" cannot use ${definition.aggregation} through belongsTo: repeated target rows would inflate the aggregation. Use a duplicate-insensitive measure or query the target dataset.`);
|
|
25
26
|
}
|
|
26
|
-
const referenced = [definition.field, definition.argField, ...(definition.filters
|
|
27
|
+
const referenced = [definition.field, definition.argField, ...filterLeaves(definition.filters).map(filter => target.filters[filter.field]?.field ?? filter.field)];
|
|
27
28
|
if (definition.sql || referenced.some(field => field && target.dimensions[field]?.sql)) {
|
|
28
29
|
throw new Error(`SQL-backed measure "${name}" or its inputs cannot be traversed through relationships.`);
|
|
29
30
|
}
|
|
@@ -18,6 +18,8 @@ export declare function validateRelationshipTenantRuntime(ds: AnyDatasetInstance
|
|
|
18
18
|
* active and the joined target declares a `tenantKey`, an explicit filter on
|
|
19
19
|
* that column is rejected (the planner injects the tenant predicate instead).
|
|
20
20
|
*/
|
|
21
|
-
export declare function validateQualifiedFilter(ds: AnyDatasetInstance, filter: MetricFilter, context?: ExecutionContext
|
|
21
|
+
export declare function validateQualifiedFilter(ds: AnyDatasetInstance, filter: MetricFilter, context?: ExecutionContext,
|
|
22
|
+
/** Operators the allowlist must permit: the negation's under `not` (RFC 0029). */
|
|
23
|
+
operators?: readonly string[]): string | null;
|
|
22
24
|
export {};
|
|
23
25
|
//# sourceMappingURL=relationship-validation.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"relationship-validation.d.ts","sourceRoot":"","sources":["../../src/utils/relationship-validation.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"relationship-validation.d.ts","sourceRoot":"","sources":["../../src/utils/relationship-validation.ts"],"names":[],"mappings":"AAIA;;;GAGG;AAEH,OAAO,KAAK,EACV,kBAAkB,EAClB,YAAY,EACZ,gBAAgB,EAChB,YAAY,EACZ,WAAW,EACZ,MAAM,aAAa,CAAC;AAYrB,KAAK,iBAAiB,GAAG,IAAI,CAC3B,YAAY,GAAG,WAAW,EAC1B,YAAY,GAAG,SAAS,GAAG,SAAS,CACrC,GAAG,IAAI,CAAC,YAAY,EAAE,UAAU,CAAC,CAAC;AAEnC;;;;GAIG;AACH,wBAAgB,iCAAiC,CAC/C,EAAE,EAAE,kBAAkB,EACtB,KAAK,EAAE,iBAAiB,EACxB,OAAO,CAAC,EAAE,gBAAgB,GACzB,MAAM,GAAG,SAAS,CAqBpB;AAED;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CACrC,EAAE,EAAE,kBAAkB,EACtB,MAAM,EAAE,YAAY,EACpB,OAAO,CAAC,EAAE,gBAAgB;AAC1B,kFAAkF;AAClF,SAAS,GAAE,SAAS,MAAM,EAAsB,GAC/C,MAAM,GAAG,IAAI,CA6Bf"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { filterLeaves } from './filter-tree.js';
|
|
1
2
|
import { resolveRelationshipReference } from './relationship-measures.js';
|
|
2
3
|
import { filterAllowsOperator } from './filter-operator-policy.js';
|
|
3
4
|
import { effectiveFilters } from './effective-filters.js';
|
|
@@ -17,7 +18,7 @@ export function validateRelationshipTenantRuntime(ds, query, context) {
|
|
|
17
18
|
const referenced = [
|
|
18
19
|
...(query.dimensions ?? []),
|
|
19
20
|
...(query.measures ?? []),
|
|
20
|
-
...(query.filters
|
|
21
|
+
...filterLeaves(query.filters).map((filter) => filter.field),
|
|
21
22
|
...(query.orderBy ?? []).map((order) => order.field),
|
|
22
23
|
...runtimeConstraints(context).map((constraint) => constraint.field),
|
|
23
24
|
].filter(isQualifiedField);
|
|
@@ -37,7 +38,9 @@ export function validateRelationshipTenantRuntime(ds, query, context) {
|
|
|
37
38
|
* active and the joined target declares a `tenantKey`, an explicit filter on
|
|
38
39
|
* that column is rejected (the planner injects the tenant predicate instead).
|
|
39
40
|
*/
|
|
40
|
-
export function validateQualifiedFilter(ds, filter, context
|
|
41
|
+
export function validateQualifiedFilter(ds, filter, context,
|
|
42
|
+
/** Operators the allowlist must permit: the negation's under `not` (RFC 0029). */
|
|
43
|
+
operators = [filter.operator]) {
|
|
41
44
|
const resolution = resolveQualifiedField(ds, filter.field);
|
|
42
45
|
if (!resolution) {
|
|
43
46
|
return null;
|
|
@@ -57,8 +60,9 @@ export function validateQualifiedFilter(ds, filter, context) {
|
|
|
57
60
|
|| definition.field !== targetDimensionName) {
|
|
58
61
|
return `Filter "${filter.field}" is not exposed by target dataset "${target.name}".`;
|
|
59
62
|
}
|
|
60
|
-
|
|
61
|
-
|
|
63
|
+
const refused = operators.find(operator => !filterAllowsOperator(definition, operator));
|
|
64
|
+
if (refused) {
|
|
65
|
+
return `Filter "${filter.field}" does not allow operator "${refused}".`;
|
|
62
66
|
}
|
|
63
67
|
return validateFilterValue(filter, targetDimension.fieldType);
|
|
64
68
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { AnyDatasetInstance, DatasetFilterNode, MetricFilter } from '../types.js';
|
|
2
|
+
/** Why `filter` can't be resolved, or `undefined` when it can. */
|
|
3
|
+
export declare function relativeDateFilterError(ds: AnyDatasetInstance, filter: MetricFilter): string | undefined;
|
|
4
|
+
/**
|
|
5
|
+
* The query's filters with every resolvable `inDateRange` replaced by its
|
|
6
|
+
* bounds. A comparison at the top level, or directly inside an `and`, becomes
|
|
7
|
+
* two comparisons there, so a top-level range still bounds the time axis; one
|
|
8
|
+
* under `or`/`not` becomes an `and` group. Unresolvable ones, and every
|
|
9
|
+
* filter of a malformed tree, are left for validation to report.
|
|
10
|
+
*/
|
|
11
|
+
export declare function resolveRelativeDateFilters(ds: AnyDatasetInstance, filters: DatasetFilterNode[] | undefined, now: Date, timezone: string | undefined): DatasetFilterNode[] | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* `query` with every relative range resolved against `now` in the query's
|
|
14
|
+
* zone (UTC by default), as the dataset client prepares it. A query with an
|
|
15
|
+
* invalid zone is returned as written, for validation to report.
|
|
16
|
+
*/
|
|
17
|
+
export declare function withResolvedRelativeDates<T extends {
|
|
18
|
+
filters?: DatasetFilterNode[];
|
|
19
|
+
timezone?: string;
|
|
20
|
+
}>(ds: AnyDatasetInstance, query: T, now: Date): T;
|
|
21
|
+
//# sourceMappingURL=relative-date-filters.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"relative-date-filters.d.ts","sourceRoot":"","sources":["../../src/utils/relative-date-filters.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAcvF,kEAAkE;AAClE,wBAAgB,uBAAuB,CAAC,EAAE,EAAE,kBAAkB,EAAE,MAAM,EAAE,YAAY,GAAG,MAAM,GAAG,SAAS,CAaxG;AAED;;;;;;GAMG;AACH,wBAAgB,0BAA0B,CACxC,EAAE,EAAE,kBAAkB,EACtB,OAAO,EAAE,iBAAiB,EAAE,GAAG,SAAS,EACxC,GAAG,EAAE,IAAI,EACT,QAAQ,EAAE,MAAM,GAAG,SAAS,GAC3B,iBAAiB,EAAE,GAAG,SAAS,CAqCjC;AAED;;;;GAIG;AACH,wBAAgB,yBAAyB,CAAC,CAAC,SAAS;IAAE,OAAO,CAAC,EAAE,iBAAiB,EAAE,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAE,EACtG,EAAE,EAAE,kBAAkB,EACtB,KAAK,EAAE,CAAC,EACR,GAAG,EAAE,IAAI,GACR,CAAC,CAIH"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { isProtocolRelativeDateRange } from '@hypequery/protocol';
|
|
2
|
+
import { filterTreeErrors, isFilterGroup } from './filter-tree.js';
|
|
3
|
+
import { datasetTimeKeys } from './dataset-time-keys.js';
|
|
4
|
+
import { isTimeKeyFilter, queryTimezoneErrors } from './query-timezone.js';
|
|
5
|
+
import { resolveRelativeDateRange } from './relative-date-range.js';
|
|
6
|
+
/**
|
|
7
|
+
* `inDateRange` caller filters (RFC 0029). The client resolves each one to a
|
|
8
|
+
* half-open `gte`/`lt` pair once per query, before validation, caching and
|
|
9
|
+
* planning, so every later stage sees ordinary comparisons on the time axis.
|
|
10
|
+
*/
|
|
11
|
+
const GRAMMAR = 'today, yesterday, this|last <unit>, last <N> <unit>s or month|quarter|year to date (unit: day, week, month, quarter, year)';
|
|
12
|
+
/** Why `filter` can't be resolved, or `undefined` when it can. */
|
|
13
|
+
export function relativeDateFilterError(ds, filter) {
|
|
14
|
+
const axes = datasetTimeKeys(ds);
|
|
15
|
+
// Compared by backing column, as the planner reads the axis: a `timeKey`
|
|
16
|
+
// may name a column that a differently named dimension reads.
|
|
17
|
+
if (!isTimeKeyFilter(ds, filter.field)) {
|
|
18
|
+
return axes.length === 0
|
|
19
|
+
? `Filter "${filter.field}" cannot use "inDateRange": dataset "${ds.name}" has no time axis.`
|
|
20
|
+
: `Filter "${filter.field}" cannot use "inDateRange": it applies only to a time axis (${axes.join(', ')}).`;
|
|
21
|
+
}
|
|
22
|
+
if (!isProtocolRelativeDateRange(filter.value)) {
|
|
23
|
+
return `Filter "${filter.field}": ${JSON.stringify(filter.value) ?? String(filter.value)} is not a relative date range. Use ${GRAMMAR}.`;
|
|
24
|
+
}
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The query's filters with every resolvable `inDateRange` replaced by its
|
|
29
|
+
* bounds. A comparison at the top level, or directly inside an `and`, becomes
|
|
30
|
+
* two comparisons there, so a top-level range still bounds the time axis; one
|
|
31
|
+
* under `or`/`not` becomes an `and` group. Unresolvable ones, and every
|
|
32
|
+
* filter of a malformed tree, are left for validation to report.
|
|
33
|
+
*/
|
|
34
|
+
export function resolveRelativeDateFilters(ds, filters, now, timezone) {
|
|
35
|
+
// A malformed tree (a null filter, a cycle, a group nested too deep) is left
|
|
36
|
+
// as written: validation reports it, and walking it here could throw.
|
|
37
|
+
if (!Array.isArray(filters) || filterTreeErrors(filters).length > 0)
|
|
38
|
+
return filters;
|
|
39
|
+
let changed = false;
|
|
40
|
+
const bounds = (filter) => {
|
|
41
|
+
if (filter.operator !== 'inDateRange' || typeof filter.field !== 'string' || relativeDateFilterError(ds, filter)) {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
const range = resolveRelativeDateRange(filter.value, now, timezone ?? 'UTC');
|
|
45
|
+
changed = true;
|
|
46
|
+
return [
|
|
47
|
+
{ field: filter.field, operator: 'gte', value: range.start },
|
|
48
|
+
{ field: filter.field, operator: 'lt', value: range.end },
|
|
49
|
+
];
|
|
50
|
+
};
|
|
51
|
+
// Within a conjunction (the top level, an `and`), bounds splice in place.
|
|
52
|
+
const conjunction = (nodes) => nodes.flatMap(node => {
|
|
53
|
+
if (!isFilterGroup(node))
|
|
54
|
+
return bounds(node) ?? [node];
|
|
55
|
+
return [group(node)];
|
|
56
|
+
});
|
|
57
|
+
const single = (node) => {
|
|
58
|
+
if (!isFilterGroup(node)) {
|
|
59
|
+
const resolved = bounds(node);
|
|
60
|
+
return resolved ? { and: resolved } : node;
|
|
61
|
+
}
|
|
62
|
+
return group(node);
|
|
63
|
+
};
|
|
64
|
+
// The tree is well formed here: each group has exactly one key.
|
|
65
|
+
const group = (node) => {
|
|
66
|
+
const record = node;
|
|
67
|
+
if (record.and)
|
|
68
|
+
return { and: conjunction(record.and) };
|
|
69
|
+
if (record.or)
|
|
70
|
+
return { or: record.or.map(single) };
|
|
71
|
+
return { not: single(record.not) };
|
|
72
|
+
};
|
|
73
|
+
const resolved = conjunction(filters);
|
|
74
|
+
return changed ? resolved : filters;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* `query` with every relative range resolved against `now` in the query's
|
|
78
|
+
* zone (UTC by default), as the dataset client prepares it. A query with an
|
|
79
|
+
* invalid zone is returned as written, for validation to report.
|
|
80
|
+
*/
|
|
81
|
+
export function withResolvedRelativeDates(ds, query, now) {
|
|
82
|
+
if (!Array.isArray(query.filters) || queryTimezoneErrors(query.timezone).length > 0)
|
|
83
|
+
return query;
|
|
84
|
+
const filters = resolveRelativeDateFilters(ds, query.filters, now, query.timezone);
|
|
85
|
+
return filters === query.filters ? query : { ...query, filters };
|
|
86
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Relative date ranges (RFC 0029): `today`, `last 30 days`, `this quarter`
|
|
3
|
+
* and the rest of the grammar, resolved to half-open local-date bounds.
|
|
4
|
+
*/
|
|
5
|
+
/** Half-open `[start, end)` local dates, each `YYYY-MM-DD`. */
|
|
6
|
+
export interface ResolvedDateRange {
|
|
7
|
+
readonly start: string;
|
|
8
|
+
readonly end: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Resolve a relative range at `now` in `timezone`, or `undefined` when the
|
|
12
|
+
* value is not in the grammar.
|
|
13
|
+
*
|
|
14
|
+
* - `this <unit>` includes the whole current period, future days included;
|
|
15
|
+
* - `last <unit>` and `last N <unit>s` are whole periods ending where the
|
|
16
|
+
* current one starts, so they exclude it (`last 7 days` ends yesterday);
|
|
17
|
+
* - `<unit> to date` runs from the current period's start through today.
|
|
18
|
+
*/
|
|
19
|
+
export declare function resolveRelativeDateRange(value: unknown, now: Date, timezone: string): ResolvedDateRange | undefined;
|
|
20
|
+
//# sourceMappingURL=relative-date-range.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"relative-date-range.d.ts","sourceRoot":"","sources":["../../src/utils/relative-date-range.ts"],"names":[],"mappings":"AAEA;;;GAGG;AAEH,+DAA+D;AAC/D,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACtB;AA+CD;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,CACtC,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,IAAI,EACT,QAAQ,EAAE,MAAM,GACf,iBAAiB,GAAG,SAAS,CAqB/B"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { parseProtocolRelativeDateRange } from '@hypequery/protocol';
|
|
2
|
+
/** The earliest bound a range resolves to: ClickHouse stores nothing earlier. */
|
|
3
|
+
const EARLIEST = Date.UTC(1900, 0, 1);
|
|
4
|
+
const DAY_MS = 86_400_000;
|
|
5
|
+
/** Today's calendar date in `timezone`, as a UTC-midnight timestamp. */
|
|
6
|
+
function localToday(now, timezone) {
|
|
7
|
+
const parts = new Intl.DateTimeFormat('en-US', {
|
|
8
|
+
timeZone: timezone, year: 'numeric', month: '2-digit', day: '2-digit',
|
|
9
|
+
}).formatToParts(now);
|
|
10
|
+
const part = (type) => Number(parts.find(entry => entry.type === type).value);
|
|
11
|
+
return Date.UTC(part('year'), part('month') - 1, part('day'));
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The first day of the unit containing `day`. Weeks start on Sunday, the day
|
|
15
|
+
* the `week` grain truncates to, so `this week` is always one `week` bucket.
|
|
16
|
+
*/
|
|
17
|
+
function startOf(unit, day) {
|
|
18
|
+
const date = new Date(day);
|
|
19
|
+
const year = date.getUTCFullYear();
|
|
20
|
+
const month = date.getUTCMonth();
|
|
21
|
+
switch (unit) {
|
|
22
|
+
case 'day': return day;
|
|
23
|
+
case 'week': return day - date.getUTCDay() * DAY_MS;
|
|
24
|
+
case 'month': return Date.UTC(year, month, 1);
|
|
25
|
+
case 'quarter': return Date.UTC(year, Math.floor(month / 3) * 3, 1);
|
|
26
|
+
case 'year': return Date.UTC(year, 0, 1);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** `start` moved by `count` units; `start` is always the first day of its unit. */
|
|
30
|
+
function shift(unit, start, count) {
|
|
31
|
+
if (unit === 'day')
|
|
32
|
+
return start + count * DAY_MS;
|
|
33
|
+
if (unit === 'week')
|
|
34
|
+
return start + count * 7 * DAY_MS;
|
|
35
|
+
const months = count * (unit === 'month' ? 1 : unit === 'quarter' ? 3 : 12);
|
|
36
|
+
const date = new Date(start);
|
|
37
|
+
return Date.UTC(date.getUTCFullYear(), date.getUTCMonth() + months, 1);
|
|
38
|
+
}
|
|
39
|
+
function formatDate(day) {
|
|
40
|
+
const date = new Date(Math.max(day, EARLIEST));
|
|
41
|
+
const pad = (value, width = 2) => String(value).padStart(width, '0');
|
|
42
|
+
return `${pad(date.getUTCFullYear(), 4)}-${pad(date.getUTCMonth() + 1)}-${pad(date.getUTCDate())}`;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Resolve a relative range at `now` in `timezone`, or `undefined` when the
|
|
46
|
+
* value is not in the grammar.
|
|
47
|
+
*
|
|
48
|
+
* - `this <unit>` includes the whole current period, future days included;
|
|
49
|
+
* - `last <unit>` and `last N <unit>s` are whole periods ending where the
|
|
50
|
+
* current one starts, so they exclude it (`last 7 days` ends yesterday);
|
|
51
|
+
* - `<unit> to date` runs from the current period's start through today.
|
|
52
|
+
*/
|
|
53
|
+
export function resolveRelativeDateRange(value, now, timezone) {
|
|
54
|
+
const range = parseProtocolRelativeDateRange(value);
|
|
55
|
+
if (!range)
|
|
56
|
+
return undefined;
|
|
57
|
+
const today = localToday(now, timezone);
|
|
58
|
+
const tomorrow = today + DAY_MS;
|
|
59
|
+
switch (range.kind) {
|
|
60
|
+
case 'today':
|
|
61
|
+
return { start: formatDate(today), end: formatDate(tomorrow) };
|
|
62
|
+
case 'yesterday':
|
|
63
|
+
return { start: formatDate(today - DAY_MS), end: formatDate(today) };
|
|
64
|
+
case 'this': {
|
|
65
|
+
const start = startOf(range.unit, today);
|
|
66
|
+
return { start: formatDate(start), end: formatDate(shift(range.unit, start, 1)) };
|
|
67
|
+
}
|
|
68
|
+
case 'last': {
|
|
69
|
+
const current = startOf(range.unit, today);
|
|
70
|
+
return { start: formatDate(shift(range.unit, current, -range.count)), end: formatDate(current) };
|
|
71
|
+
}
|
|
72
|
+
case 'toDate':
|
|
73
|
+
return { start: formatDate(startOf(range.unit, today)), end: formatDate(tomorrow) };
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { RetentionSpec } from './event-operation-spec.js';
|
|
2
|
+
import { type EventResultRow } from './event-result-values.js';
|
|
3
|
+
/**
|
|
4
|
+
* Expand one row per cohort (`_hq_size`, `_hq_retained[]`, `_hq_observable`)
|
|
5
|
+
* into one row per observable period (RFC 0027): breakdown values, `cohort`,
|
|
6
|
+
* `period`, `entities`, `retained` and `rate`.
|
|
7
|
+
*/
|
|
8
|
+
export declare function retentionResultRows(spec: RetentionSpec, breakdownAliases: readonly string[], rows: readonly Record<string, unknown>[]): EventResultRow[];
|
|
9
|
+
//# sourceMappingURL=retention-rows.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retention-rows.d.ts","sourceRoot":"","sources":["../../src/utils/retention-rows.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAA6B,KAAK,cAAc,EAAyB,MAAM,0BAA0B,CAAC;AAEjH;;;;GAIG;AACH,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,aAAa,EACnB,gBAAgB,EAAE,SAAS,MAAM,EAAE,EACnC,IAAI,EAAE,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,GACvC,cAAc,EAAE,CAyBlB"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { countValue, ratio, scalar } from './event-result-values.js';
|
|
2
|
+
/**
|
|
3
|
+
* Expand one row per cohort (`_hq_size`, `_hq_retained[]`, `_hq_observable`)
|
|
4
|
+
* into one row per observable period (RFC 0027): breakdown values, `cohort`,
|
|
5
|
+
* `period`, `entities`, `retained` and `rate`.
|
|
6
|
+
*/
|
|
7
|
+
export function retentionResultRows(spec, breakdownAliases, rows) {
|
|
8
|
+
const result = [];
|
|
9
|
+
for (const row of rows) {
|
|
10
|
+
const entities = countValue(row._hq_size);
|
|
11
|
+
const retained = row._hq_retained;
|
|
12
|
+
if (!Array.isArray(retained) || retained.length !== spec.periods) {
|
|
13
|
+
throw new Error('The executor returned retention counts of the wrong length.');
|
|
14
|
+
}
|
|
15
|
+
const observable = Math.min(countValue(row._hq_observable), spec.periods);
|
|
16
|
+
const group = Object.fromEntries(spec.breakdown.map((name, index) => [name, scalar(row[breakdownAliases[index]])]));
|
|
17
|
+
for (let period = 1; period <= observable; period += 1) {
|
|
18
|
+
const count = countValue(retained[period - 1]);
|
|
19
|
+
result.push(Object.freeze({
|
|
20
|
+
...group,
|
|
21
|
+
cohort: scalar(row._hq_cohort),
|
|
22
|
+
period,
|
|
23
|
+
entities,
|
|
24
|
+
retained: count,
|
|
25
|
+
rate: ratio(count, entities),
|
|
26
|
+
}));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { AnyDatasetInstance } from '../types.js';
|
|
2
|
+
import type { DatasetQueryExecutionOptions } from '../dataset-query.js';
|
|
3
|
+
import { buildEventSourceSql } from './event-source-sql.js';
|
|
4
|
+
import type { RetentionSpec } from './event-operation-spec.js';
|
|
5
|
+
import type { PlannedEventStatement } from './funnel-sql.js';
|
|
6
|
+
/**
|
|
7
|
+
* Plan retention (RFC 0027) as one statement: scan → per entity, the bucket
|
|
8
|
+
* of its first start event and the set of buckets it returned in → per
|
|
9
|
+
* cohort, its size and how many returned in each of the next N buckets →
|
|
10
|
+
* ranked breakdown groups.
|
|
11
|
+
*
|
|
12
|
+
* Each cohort also reports how many of its periods end by the range end;
|
|
13
|
+
* later ones are not fully observed and get no row.
|
|
14
|
+
*/
|
|
15
|
+
export declare function buildRetentionSql(ds: AnyDatasetInstance, spec: RetentionSpec, options: DatasetQueryExecutionOptions, readsFinal: (joinCtx: ReturnType<typeof buildEventSourceSql>['joinCtx']) => boolean): PlannedEventStatement;
|
|
16
|
+
//# sourceMappingURL=retention-sql.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retention-sql.d.ts","sourceRoot":"","sources":["../../src/utils/retention-sql.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,qBAAqB,CAAC;AAIxE,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAM5D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AAE7D;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAC/B,EAAE,EAAE,kBAAkB,EACtB,IAAI,EAAE,aAAa,EACnB,OAAO,EAAE,4BAA4B,EACrC,UAAU,EAAE,CAAC,OAAO,EAAE,UAAU,CAAC,OAAO,mBAAmB,CAAC,CAAC,SAAS,CAAC,KAAK,OAAO,GAClF,qBAAqB,CAkDvB"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { resolveDatasetSqlDialect } from './dataset-sql-dialect.js';
|
|
2
|
+
import { assertRawSqlForDialect } from './raw-sql-dialect-check.js';
|
|
3
|
+
import { StatementParameters } from './statement-parameters.js';
|
|
4
|
+
import { buildEventSourceSql } from './event-source-sql.js';
|
|
5
|
+
import { filterLeaves } from './filter-tree.js';
|
|
6
|
+
import { eventConditionFlagSql } from './event-condition-sql.js';
|
|
7
|
+
import { rankedBreakdownSql } from './event-breakdown-sql.js';
|
|
8
|
+
import { resolveTimeAxisRange } from './time-axis-bounds.js';
|
|
9
|
+
import { queryTimezoneName } from './query-timezone.js';
|
|
10
|
+
/**
|
|
11
|
+
* Plan retention (RFC 0027) as one statement: scan → per entity, the bucket
|
|
12
|
+
* of its first start event and the set of buckets it returned in → per
|
|
13
|
+
* cohort, its size and how many returned in each of the next N buckets →
|
|
14
|
+
* ranked breakdown groups.
|
|
15
|
+
*
|
|
16
|
+
* Each cohort also reports how many of its periods end by the range end;
|
|
17
|
+
* later ones are not fully observed and get no row.
|
|
18
|
+
*/
|
|
19
|
+
export function buildRetentionSql(ds, spec, options, readsFinal) {
|
|
20
|
+
const dialect = resolveDatasetSqlDialect(options.builderFactory);
|
|
21
|
+
if (dialect.name !== 'clickhouse') {
|
|
22
|
+
throw new Error(`Retention needs ClickHouse; the "${dialect.name}" datasets dialect has no equivalent yet.`);
|
|
23
|
+
}
|
|
24
|
+
assertRawSqlForDialect(ds, dialect);
|
|
25
|
+
const range = resolveTimeAxisRange(ds, [...spec.filters]);
|
|
26
|
+
if (!range)
|
|
27
|
+
throw new Error('Retention requires exactly one bounded time range.');
|
|
28
|
+
const statement = new StatementParameters(dialect);
|
|
29
|
+
const conditionFields = [...new Set(filterLeaves([...spec.start, ...spec.return]).map(condition => condition.field))];
|
|
30
|
+
const source = buildEventSourceSql(ds, {
|
|
31
|
+
entity: spec.entity,
|
|
32
|
+
breakdown: spec.breakdown,
|
|
33
|
+
conditionFields,
|
|
34
|
+
filters: spec.filters,
|
|
35
|
+
...(spec.timezone === undefined ? {} : { timezone: spec.timezone }),
|
|
36
|
+
}, options, statement);
|
|
37
|
+
const breakdown = source.breakdown;
|
|
38
|
+
const partition = ['_hq_entity', ...breakdown].join(', ');
|
|
39
|
+
const start = eventConditionFlagSql(spec.start, source.conditionPredicates, statement);
|
|
40
|
+
const returning = eventConditionFlagSql(spec.return, source.conditionPredicates, statement);
|
|
41
|
+
const bucket = dialect.truncateTime('_hq_time', spec.grain);
|
|
42
|
+
const flagged = `_hq_flagged AS (SELECT _hq_time, ${partition}, ${start} AS _hq_start, ${returning} AS _hq_return `
|
|
43
|
+
+ 'FROM _hq_events WHERE _hq_start = 1 OR _hq_return = 1)';
|
|
44
|
+
const entities = `_hq_entities AS (SELECT ${partition}, minIf(${bucket}, _hq_start = 1) AS _hq_cohort_start, `
|
|
45
|
+
+ `groupUniqArrayIf(${bucket}, _hq_return = 1) AS _hq_returns FROM _hq_flagged `
|
|
46
|
+
+ `GROUP BY ${partition} HAVING countIf(_hq_start = 1) > 0)`;
|
|
47
|
+
const period = (index) => dialect.addTime('_hq_cohort_start', index, spec.grain);
|
|
48
|
+
const zone = queryTimezoneName(spec.timezone);
|
|
49
|
+
const upper = dialect.parseTimestamp(statement.bind(range.end.text, 'string'), dialect.stringLiteral(zone));
|
|
50
|
+
const periods = `range(1, ${spec.periods + 1})`;
|
|
51
|
+
const groupKeys = [...breakdown, '_hq_cohort_start'].join(', ');
|
|
52
|
+
const groups = `_hq_groups AS (SELECT ${[
|
|
53
|
+
...breakdown,
|
|
54
|
+
`formatDateTime(_hq_cohort_start, '%Y-%m-%d') AS _hq_cohort`,
|
|
55
|
+
'count() AS _hq_size',
|
|
56
|
+
`sumForEach(arrayMap(_hq_p -> toUInt64(has(_hq_returns, ${period('_hq_p')})), ${periods})) AS _hq_retained`,
|
|
57
|
+
// A period is reported only when its whole bucket ends by the range end:
|
|
58
|
+
// a partly covered one would understate retention. A bucket boundary is
|
|
59
|
+
// local midnight in the query zone, whatever the bucket's type.
|
|
60
|
+
`arrayCount(_hq_p -> ${dialect.toQueryTimezone(period('_hq_p + 1'), zone)} <= ${upper}, ${periods}) AS _hq_observable`,
|
|
61
|
+
].join(', ')} FROM _hq_entities GROUP BY ${groupKeys})`;
|
|
62
|
+
const body = rankedBreakdownSql({
|
|
63
|
+
relation: '_hq_groups', breakdown, sizeColumn: '_hq_size', limit: spec.breakdownLimit,
|
|
64
|
+
orderBy: [...(breakdown.length ? ['_hq_rank ASC'] : []), '_hq_cohort ASC'],
|
|
65
|
+
});
|
|
66
|
+
const text = `WITH ${[...source.ctes, flagged, entities, groups].join(', ')} ${body}`;
|
|
67
|
+
return { statement: { text, parameters: statement }, breakdown, readsFinal: readsFinal(source.joinCtx) };
|
|
68
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scoped-dataset-source.d.ts","sourceRoot":"","sources":["../../src/utils/scoped-dataset-source.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACxE,OAAO,KAAK,EAAE,uBAAuB,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAC9F,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,2BAA2B,CAAC;AAIxE,OAAO,EAGL,KAAK,0BAA0B,EAChC,MAAM,gCAAgC,CAAC;AAIxC,MAAM,WAAW,0BAA0B;IACzC,cAAc,EAAE,IAAI,CAAC,uBAAuB,EAAE,OAAO,CAAC,CAAC;IACvD,EAAE,EAAE,kBAAkB,CAAC;IACvB,OAAO,CAAC,EAAE,gBAAgB,CAAC;IAC3B,OAAO,EAAE,0BAA0B,GAAG,SAAS,CAAC;IAChD,OAAO,EAAE,sBAAsB,CAAC;IAChC,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;CAC9B;AAED;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,0BAA0B,GAAG,gBAAgB,
|
|
1
|
+
{"version":3,"file":"scoped-dataset-source.d.ts","sourceRoot":"","sources":["../../src/utils/scoped-dataset-source.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACxE,OAAO,KAAK,EAAE,uBAAuB,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAC9F,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,2BAA2B,CAAC;AAIxE,OAAO,EAGL,KAAK,0BAA0B,EAChC,MAAM,gCAAgC,CAAC;AAIxC,MAAM,WAAW,0BAA0B;IACzC,cAAc,EAAE,IAAI,CAAC,uBAAuB,EAAE,OAAO,CAAC,CAAC;IACvD,EAAE,EAAE,kBAAkB,CAAC;IACvB,OAAO,CAAC,EAAE,gBAAgB,CAAC;IAC3B,OAAO,EAAE,0BAA0B,GAAG,SAAS,CAAC;IAChD,OAAO,EAAE,sBAAsB,CAAC;IAChC,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;CAC9B;AAED;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,0BAA0B,GAAG,gBAAgB,CAqBzF"}
|
|
@@ -17,6 +17,10 @@ export function scopedDatasetSource(options) {
|
|
|
17
17
|
const { ds, context, joinCtx, dialect, timezone } = options;
|
|
18
18
|
// Validators report this too; checked here so no path can skip it.
|
|
19
19
|
assertEnforcedConstraints(ds, context);
|
|
20
|
+
// RFC 0028's SQL rules are ClickHouse's.
|
|
21
|
+
if (ds.sqlSource && dialect.name !== 'clickhouse') {
|
|
22
|
+
throw new Error(`Dataset "${ds.name}" reads a ClickHouse SQL source, which the "${dialect.name}" datasets dialect cannot run.`);
|
|
23
|
+
}
|
|
20
24
|
let qb = applyRelationshipJoins(datasetSourceBuilder(options.builderFactory, ds), joinCtx);
|
|
21
25
|
// A resolved join target is read with FINAL even when this dataset isn't.
|
|
22
26
|
if (queryReadsFinal(ds, joinCtx) && qb.settings)
|
package/dist/utils/segments.d.ts
CHANGED
|
@@ -16,8 +16,9 @@ interface SegmentOwner {
|
|
|
16
16
|
/** Throws when a declared segment is malformed; returns a frozen copy otherwise. */
|
|
17
17
|
export declare function normalizeSegments(datasetName: string, owner: SegmentOwner, segments: Record<string, SegmentDefinition> | undefined): Record<string, SegmentDefinition>;
|
|
18
18
|
/**
|
|
19
|
-
*
|
|
20
|
-
* (RFC 0015), so a dataset that declares them is refused by name.
|
|
19
|
+
* The standalone dataset contract is contract 2 shaped and cannot carry
|
|
20
|
+
* segments (RFC 0015), so a dataset that declares them is refused by name.
|
|
21
|
+
* Deployments carry them as contract 3.
|
|
21
22
|
*/
|
|
22
23
|
export declare function assertNoPublishedSegments(ds: Pick<AnyDatasetInstance, 'name' | 'segments'>): void;
|
|
23
24
|
/** Validation errors for a query's `segments` selection. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"segments.d.ts","sourceRoot":"","sources":["../../src/utils/segments.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,KAAK,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAK5G,UAAU,YAAY;IACpB,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,UAAU,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,mBAAmB,CAAC,CAAC,CAAC;CACpE;AAED,oFAAoF;AACpF,wBAAgB,iBAAiB,CAC/B,WAAW,EAAE,MAAM,EACnB,KAAK,EAAE,YAAY,EACnB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,iBAAiB,CAAC,GAAG,SAAS,GACtD,MAAM,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAqCnC;AAED
|
|
1
|
+
{"version":3,"file":"segments.d.ts","sourceRoot":"","sources":["../../src/utils/segments.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,KAAK,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAK5G,UAAU,YAAY;IACpB,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,UAAU,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,mBAAmB,CAAC,CAAC,CAAC;CACpE;AAED,oFAAoF;AACpF,wBAAgB,iBAAiB,CAC/B,WAAW,EAAE,MAAM,EACnB,KAAK,EAAE,YAAY,EACnB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,iBAAiB,CAAC,GAAG,SAAS,GACtD,MAAM,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAqCnC;AAED;;;;GAIG;AACH,wBAAgB,yBAAyB,CAAC,EAAE,EAAE,IAAI,CAAC,kBAAkB,EAAE,MAAM,GAAG,UAAU,CAAC,GAAG,IAAI,CAQjG;AAED,4DAA4D;AAC5D,wBAAgB,sBAAsB,CAAC,EAAE,EAAE,kBAAkB,EAAE,KAAK,EAAE,OAAO,GAAG,MAAM,EAAE,CAiBvF;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,EAAE,EAAE,kBAAkB,EAAE,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,GAAG,YAAY,EAAE,CAE3G"}
|
package/dist/utils/segments.js
CHANGED
|
@@ -54,14 +54,15 @@ export function normalizeSegments(datasetName, owner, segments) {
|
|
|
54
54
|
return Object.freeze(Object.fromEntries(result));
|
|
55
55
|
}
|
|
56
56
|
/**
|
|
57
|
-
*
|
|
58
|
-
* (RFC 0015), so a dataset that declares them is refused by name.
|
|
57
|
+
* The standalone dataset contract is contract 2 shaped and cannot carry
|
|
58
|
+
* segments (RFC 0015), so a dataset that declares them is refused by name.
|
|
59
|
+
* Deployments carry them as contract 3.
|
|
59
60
|
*/
|
|
60
61
|
export function assertNoPublishedSegments(ds) {
|
|
61
62
|
const names = Object.keys(ds.segments ?? {});
|
|
62
63
|
if (names.length > 0) {
|
|
63
|
-
throw new Error(`Dataset "${ds.name}" declares segments (${names.join(', ')}), which
|
|
64
|
-
'segments need deployment contract 3 (RFC 0015).
|
|
64
|
+
throw new Error(`Dataset "${ds.name}" declares segments (${names.join(', ')}), which a dataset contract cannot carry: ` +
|
|
65
|
+
'segments need deployment contract 3 (RFC 0015). Publish the dataset in a deployment instead.');
|
|
65
66
|
}
|
|
66
67
|
}
|
|
67
68
|
/** Validation errors for a query's `segments` selection. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"semantic-query-syntax.d.ts","sourceRoot":"","sources":["../../src/utils/semantic-query-syntax.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"semantic-query-syntax.d.ts","sourceRoot":"","sources":["../../src/utils/semantic-query-syntax.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE7D,oFAAoF;AACpF,wBAAgB,yBAAyB,CAAC,KAAK,EAAE,YAAY,GAAG,WAAW,GAAG,MAAM,EAAE,CAgBrF;AAED,kEAAkE;AAClE,wBAAgB,sBAAsB,CAAC,SAAS,EAAE,OAAO,GAAG,KAAK,GAAG,MAAM,CAIzE"}
|
|
@@ -1,8 +1,14 @@
|
|
|
1
|
+
import { filterLeaves, filterTreeErrors } from './filter-tree.js';
|
|
1
2
|
import { isSemanticFilterOperator } from '../constants.js';
|
|
2
3
|
/** Reject SQL structure outside the semantic contract before reaching a builder. */
|
|
3
4
|
export function semanticQuerySyntaxErrors(query) {
|
|
4
|
-
const errors =
|
|
5
|
-
|
|
5
|
+
const errors = filterTreeErrors(query.filters);
|
|
6
|
+
if (errors.length > 0)
|
|
7
|
+
return errors;
|
|
8
|
+
for (const filter of filterLeaves(query.filters)) {
|
|
9
|
+
// RFC 0029: validation reports an unresolved relative range with its reason.
|
|
10
|
+
if (filter.operator === 'inDateRange')
|
|
11
|
+
continue;
|
|
6
12
|
if (!isSemanticFilterOperator(filter.operator)) {
|
|
7
13
|
errors.push(`Unsupported semantic filter operator "${String(filter.operator)}".`);
|
|
8
14
|
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { AnyDatasetInstance, DatasetSqlSource, FieldType, ResolvedDatasetSqlSource } from '../types.js';
|
|
2
|
+
export declare function isDatasetSqlSource(source: unknown): source is DatasetSqlSource;
|
|
3
|
+
/** Check a code-first SQL source and freeze it with what the checker found. */
|
|
4
|
+
export declare function resolveDatasetSqlSource(datasetName: string, source: DatasetSqlSource): ResolvedDatasetSqlSource;
|
|
5
|
+
/**
|
|
6
|
+
* The derived table a SQL-sourced dataset reads: only its declared columns,
|
|
7
|
+
* aliased as the dataset so base columns qualify exactly as for a table.
|
|
8
|
+
*/
|
|
9
|
+
export declare function sqlSourceRelation(ds: Pick<AnyDatasetInstance, 'sqlSource'>): string;
|
|
10
|
+
/** The RFC 0028 contract form. */
|
|
11
|
+
export declare function sqlSourceToContract(source: ResolvedDatasetSqlSource): {
|
|
12
|
+
kind: "sql";
|
|
13
|
+
dialect: "clickhouse";
|
|
14
|
+
sql: string;
|
|
15
|
+
alias: string;
|
|
16
|
+
dependencies: string[];
|
|
17
|
+
columns: {
|
|
18
|
+
name: string;
|
|
19
|
+
type: FieldType;
|
|
20
|
+
}[];
|
|
21
|
+
};
|
|
22
|
+
/** The code-first form of a contract's SQL source. */
|
|
23
|
+
export declare function sqlSourceFromContract(source: {
|
|
24
|
+
readonly sql: string;
|
|
25
|
+
readonly alias: string;
|
|
26
|
+
readonly dependencies: readonly string[];
|
|
27
|
+
readonly columns: readonly {
|
|
28
|
+
readonly name: string;
|
|
29
|
+
readonly type: FieldType;
|
|
30
|
+
}[];
|
|
31
|
+
}): DatasetSqlSource;
|
|
32
|
+
/** Lowercase hex SHA-256 of the contract form, for cache identity. */
|
|
33
|
+
export declare function sqlSourceSha256(source: ResolvedDatasetSqlSource): string;
|
|
34
|
+
/** Refuses a SQL-sourced dataset on paths that read sources by table name. */
|
|
35
|
+
export declare function assertTableSource(ds: Pick<AnyDatasetInstance, 'name' | 'sqlSource'>, context: string): void;
|
|
36
|
+
//# sourceMappingURL=sql-dataset-source.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sql-dataset-source.d.ts","sourceRoot":"","sources":["../../src/utils/sql-dataset-source.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,SAAS,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AA6B7G,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,IAAI,gBAAgB,CAE9E;AAED,+EAA+E;AAC/E,wBAAgB,uBAAuB,CAAC,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,gBAAgB,GAAG,wBAAwB,CA2C/G;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,EAAE,EAAE,IAAI,CAAC,kBAAkB,EAAE,WAAW,CAAC,GAAG,MAAM,CAKnF;AAED,kCAAkC;AAClC,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,wBAAwB;;;;;;;;;;EASnE;AAED,sDAAsD;AACtD,wBAAgB,qBAAqB,CAAC,MAAM,EAAE;IAC5C,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,YAAY,EAAE,SAAS,MAAM,EAAE,CAAC;IACzC,QAAQ,CAAC,OAAO,EAAE,SAAS;QAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAA;KAAE,EAAE,CAAC;CAClF,GAAG,gBAAgB,CAOnB;AAED,sEAAsE;AACtE,wBAAgB,eAAe,CAAC,MAAM,EAAE,wBAAwB,GAAG,MAAM,CAExE;AAED,8EAA8E;AAC9E,wBAAgB,iBAAiB,CAAC,EAAE,EAAE,IAAI,CAAC,kBAAkB,EAAE,MAAM,GAAG,WAAW,CAAC,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAO3G"}
|