import type { QueryBuilderFactoryInput } from './query-builder-protocol.js'; import type { SemanticCacheMetaInfo, SemanticCacheRuntime } from './cache/semantic-query-cache.js'; import type { SemanticExpression } from './semantic-plan.js'; export type FieldType = 'string' | 'number' | 'boolean' | 'timestamp'; export type DimensionType = FieldType; export interface DimensionOptions { label?: string; description?: string; column?: string; sql?: string; /** Identifiers referenced by `sql`; required when producing a protocol artifact. */ dependencies?: readonly string[]; filterable?: boolean; groupable?: boolean; } export interface DimensionDefinition { __type: 'field_definition'; fieldType: TType; label?: string; description?: string; column?: string; sql?: string; /** Identifiers referenced by `sql`; required when producing a protocol artifact. */ dependencies?: readonly string[]; filterable?: boolean; groupable?: boolean; } export type InferDimensionType = T['fieldType'] extends 'string' ? string : T['fieldType'] extends 'number' ? number : T['fieldType'] extends 'boolean' ? boolean : T['fieldType'] extends 'timestamp' ? string : never; export type RelationshipKind = 'belongsTo' | 'hasMany' | 'hasOne'; export interface RelationshipDefinition { __type: 'relationship'; kind: TKind; target: () => TTarget; from: string; to: string; } export type AggregationType = 'sum' | 'count' | 'countDistinct' | 'avg' | 'min' | 'max' | 'argMax' | 'argMin' | 'percentile' | 'stddev' | 'variance'; export type MeasureAggregation = AggregationType; export interface AggregationSpec { __type: 'aggregation_spec'; aggregation: AggregationType; field: string; /** Second column for argMax/argMin: the field whose extreme selects the row. */ argField?: string; /** Percentile level in [0, 1]. Required when aggregation is 'percentile'. */ level?: number; sql?: string; filters?: MetricFilter[]; } export interface MeasureOptions { sql?: string; /** Identifiers referenced by `sql`; required when producing a protocol artifact. */ dependencies?: readonly string[]; label?: string; description?: string; filters?: MetricFilter[]; } export interface MeasureDefinition { __type: 'measure_definition'; aggregation: MeasureAggregation; field: string; /** Second column for argMax/argMin: the field whose extreme selects the row. */ argField?: string; /** Percentile level in [0, 1]. Required when aggregation is 'percentile'. */ level?: number; sql?: string; /** Identifiers referenced by `sql`; required when producing a protocol artifact. */ dependencies?: readonly string[]; label?: string; description?: string; filters?: MetricFilter[]; } export type FormulaExpr = { __type: 'formula_expr'; expression: SemanticExpression; toSQL: () => string; }; export interface DerivedMetricSpec { __type: 'derived_metric_spec'; uses: Record>; formula: (inputs: Record) => FormulaExpr; } /** * The wide dataset instance used as the default `TDataset` for a metric ref. * Refs created via `DatasetInstance.metric()` carry their dataset's concrete * dimension/measure types instead, enabling field-level typing downstream. */ export type DefaultMetricDataset = DatasetInstance, Record, Record, TDatasetName>; export interface MetricRef = AggregationSpec | DerivedMetricSpec, TDataset extends DatasetInstance = DefaultMetricDataset> { __type: 'metric_ref'; datasetName: TDatasetName; name: TMetricName; spec: TSpec; label?: string; description?: string; dataset: TDataset; by(grain: TimeGrain): GrainedMetricRef; contract(): MetricContract; } export type BaseMetricRef = DefaultMetricDataset> = MetricRef; export type DerivedMetricRef = DefaultMetricDataset> = MetricRef, TDataset>; export interface GrainedMetricRef = AggregationSpec | DerivedMetricSpec, TDataset extends DatasetInstance = DefaultMetricDataset> { __type: 'grained_metric_ref'; metric: MetricRef; grain: TimeGrain; contract(): MetricContract; } export type MetricHandle = AggregationSpec | DerivedMetricSpec, TDataset extends DatasetInstance = DefaultMetricDataset> = MetricRef | GrainedMetricRef; export type TimeGrain = 'day' | 'week' | 'month' | 'quarter' | 'year'; export interface MetricContract { kind: 'metric' | 'derived_metric' | 'grained_metric'; name: string; dataset: string; valueType: 'number'; label?: string; description?: string; dimensions: string[]; measures?: string[]; filters: string[]; grains: TimeGrain[]; grain?: TimeGrain; requires?: string[]; tenantScoped: boolean; } export interface MetricFilter { field: TField; operator: 'eq' | 'neq' | 'gt' | 'gte' | 'lt' | 'lte' | 'in' | 'notIn' | 'between' | 'like'; value: TValue; } export interface MetricOrderBy { field: TField; direction: 'asc' | 'desc'; } export interface MetricQuery { dimensions?: string[]; filters?: MetricFilter[]; orderBy?: MetricOrderBy[]; limit?: number; offset?: number; by?: TimeGrain; } export interface DatasetQuery { dimensions?: string[]; measures?: string[]; filters?: MetricFilter[]; orderBy?: MetricOrderBy[]; limit?: number; offset?: number; by?: TimeGrain; } export interface MetricResultMeta { timingMs?: number; sql?: string; tenant?: string; /** Number of rows returned in `data`. */ rowCount?: number; /** * Offset pagination state. Present when the query specified a `limit`. * `hasMore` is derived by over-fetching one row, so it is exact without a * separate count query. */ pagination?: { limit: number; offset: number; hasMore: boolean; }; /** Result-cache observability. Present when the call went through the cache. */ cache?: SemanticCacheMetaInfo; } export interface MetricResult> { data: T[]; meta?: MetricResultMeta; } export interface DatasetQueryResult> { data: T[]; meta?: MetricResultMeta; } export type SemanticTenantRuntime = string | { id: string; } | { in: string[]; } | { scope: 'all'; }; export interface SemanticExecutionRuntime { builderFactory?: QueryBuilderFactoryInput; tenant?: SemanticTenantRuntime; } export interface ExecutionContext { runtime?: SemanticExecutionRuntime; /** * Per-call result-cache controls. `false` bypasses the cache entirely; * `{ ttlMs }` opts this call into caching (or overrides the client default). */ cache?: SemanticCacheRuntime | false; } export interface SemanticFilterDefinition { __type: 'filter_definition'; field: string; label?: string; description?: string; operators?: MetricFilter['operator'][]; } export type SemanticFiltersDefinition = Record; export interface DatasetLimits { maxDimensions?: number; maxMeasures?: number; maxFilters?: number; maxResultSize?: number; } export interface BaseMetricConfig = Record> { measure: keyof TMeasures & string; label?: string; description?: string; } export interface DerivedMetricConfig { uses: Record>; formula: (inputs: Record) => FormulaExpr; label?: string; description?: string; } export interface DatasetConfig = Record, TMeasures extends Record = Record, TRelationships extends Record = Record> { source: string; tenantKey?: string; timeKey?: string; dimensions: TDimensions; measures?: TMeasures; filters?: SemanticFiltersDefinition; relationships?: TRelationships; limits?: DatasetLimits; } export interface DatasetInstance = Record, TMeasures extends Record = Record, TRelationships extends Record = Record, TDatasetName extends string = string> { __type: 'dataset'; name: TDatasetName; source: string; tenantKey?: string; timeKey?: string; dimensions: TDimensions; measures: TMeasures; filters: SemanticFiltersDefinition; relationships: TRelationships; limits?: DatasetLimits; metric(metricName: TName, metricConfig: BaseMetricConfig): BaseMetricRef>; metric(metricName: TName, metricConfig: DerivedMetricConfig): DerivedMetricRef>; } export interface DatasetRegistryInstance { register(ds: DatasetInstance): void; get(name: string): DatasetInstance | undefined; getAll(): DatasetInstance[]; has(name: string): boolean; } export type AnyDatasetInstance = DatasetInstance, Record, Record, string>; type NonNeverStringKeys> = { [K in keyof T]: [T[K]] extends [never] ? never : K; }[keyof T] & string; export type KnownStringKeys = { [K in keyof T]: string extends K ? never : K extends string ? K : never; }[keyof T]; type KnownStringKeysOrFallback> = [ KnownStringKeys ] extends [never] ? NonNeverStringKeys : KnownStringKeys & NonNeverStringKeys; type NonSqlDimensionNames> = { [TName in KnownStringKeysOrFallback]: TDimensions[TName] extends { sql: string; } ? never : TName; }[KnownStringKeysOrFallback]; type QueryableRelationshipDimensionNames> = { [TRelationshipName in KnownStringKeys]: TRelationships[TRelationshipName] extends RelationshipDefinition ? TKind extends 'hasMany' ? never : TTarget extends DatasetInstance ? `${TRelationshipName}.${NonSqlDimensionNames}` : never : never; }[KnownStringKeys]; type DatasetDimensionDefinitionByName, TName extends string> = TName extends keyof TDataset['dimensions'] ? TDataset['dimensions'][TName] : TName extends `${infer TRelationshipName}.${infer TDimensionName}` ? TRelationshipName extends keyof TDataset['relationships'] ? TDataset['relationships'][TRelationshipName] extends RelationshipDefinition ? TKind extends 'hasMany' ? never : TTarget extends DatasetInstance ? TDimensionName extends keyof TDimensions ? TDimensions[TDimensionName] : never : never : never : never : never; /** Dimension names declared by a dataset. */ export type DatasetDimensionNames> = KnownStringKeysOrFallback | QueryableRelationshipDimensionNames; /** Dimension definitions addressable by a one-hop dataset query. */ export type DatasetQueryableDimensions> = { [TName in DatasetDimensionNames]: DatasetDimensionDefinitionByName; }; /** Backwards-compatible alias for all queryable dataset dimension names. */ export type DatasetFieldNames> = DatasetDimensionNames; /** Measure names declared by a dataset. */ export type DatasetMeasureNames> = KnownStringKeysOrFallback; /** * Fields a result can be ordered by. This is the selection-independent superset * (every dimension + measure, plus the synthetic `period` column for grained * queries); the runtime validator additionally requires the field to be selected. */ export type DatasetOrderableNames> = DatasetDimensionNames | DatasetMeasureNames | 'period'; /** A dataset query whose fields are constrained to the dataset's contract. */ export interface DatasetQueryFor> { dimensions?: readonly DatasetDimensionNames[]; measures?: readonly DatasetMeasureNames[]; filters?: readonly MetricFilter[]; orderBy?: readonly MetricOrderBy>[]; limit?: number; offset?: number; by?: TimeGrain; includeMeta?: boolean; } type SelectedDimensions, TQuery> = TQuery extends { dimensions: readonly (infer TName)[]; } ? Extract> : never; type SelectedDatasetMeasures, TQuery> = TQuery extends { measures: readonly (infer TName)[]; } ? Extract> : DatasetMeasureNames; type PeriodSelection = TQuery extends { by: TimeGrain; } ? { period?: string; } : {}; /** A broad typed result row for a dataset, independent of projection. */ export type DatasetRow> = { [K in DatasetDimensionNames]?: InferDimensionType[K]>; } & { [K in DatasetMeasureNames]?: string; } & { period?: string; }; /** A projection-aware typed result row for a dataset query. */ export type DatasetRowFor, TQuery = DatasetQueryFor> = { [K in SelectedDimensions]?: InferDimensionType[K]>; } & { [K in SelectedDatasetMeasures]?: string; } & PeriodSelection; export interface DatasetQueryResultFor, TQuery = DatasetQueryFor> { data: DatasetRowFor[]; meta?: MetricResultMeta; } /** A metric query whose fields are constrained to the metric's dataset + value column. */ export interface MetricQueryFor, TMetricName extends string> { dimensions?: readonly DatasetDimensionNames[]; filters?: readonly MetricFilter[]; orderBy?: readonly MetricOrderBy | TMetricName | 'period'>[]; limit?: number; offset?: number; by?: TimeGrain; includeMeta?: boolean; } /** A best-effort typed result row for a metric query (dimensions + the metric value). */ export type MetricRow, TMetricName extends string> = { [K in DatasetDimensionNames]?: InferDimensionType[K]>; } & { [K in TMetricName]?: string; } & { period?: string; }; /** A projection-aware typed result row for a metric query. */ export type MetricRowFor, TMetricName extends string, TQuery = MetricQueryFor> = { [K in SelectedDimensions]?: InferDimensionType[K]>; } & { [K in TMetricName]?: string; } & PeriodSelection; export interface MetricResultFor, TMetricName extends string, TQuery = MetricQueryFor> { data: MetricRowFor[]; meta?: MetricResultMeta; } export {}; //# sourceMappingURL=types.d.ts.map