/** * The {@link Aggregation} module facilitates creation of statically typed functional data {@link Expression}'s, as well as providing a set of standard libraries for common {@link Expression} patterns. * * @module Aggregation * */ import { TypeToFields } from '../data'; import { Expression } from './definition'; import { EastFunction, Variable } from './functions'; import { BooleanType, DateTimeType, DictType, EastType, FloatType, IntegerType, NonNullable, Nullable, NullType, SetType, StringType, StructType } from './types'; /** An `CustomAggregation` represents a reduction over rows followed by an optional final transformation. */ /** @internal */ export type CustomAggregation = { type: T; aggregation_type: "Custom"; input_key: string; previous: Variable; initial: EastFunction; reducer: EastFunction; inverse_reducer: EastFunction; finalizer: EastFunction; field: Variable; }; /** * An {@link CustomAggregationDefinition} represents a reduction over rows followed by an optional final transformation. * * @category Aggregation */ export type CustomAggregationDefinition = { type: T; aggregation_type: "Custom"; previous: Variable; initial: EastFunction; reducer: EastFunction; inverse_reducer: EastFunction; finalizer: EastFunction; }; /** @internal */ export type StandardAggregationType = "Count" | "Sum" | "Mean" | "StdDev" | "Any" | "Every" | "DistinctCount" | "Unique" | "CollectSet" | "CollectDict" | "CollectDictCount" | "CollectDictSum" | "CollectDictMean" | "CollectDictMax" | "CollectDictMin" | "Mode" | "Minimum" | "Maximum" | "Span" | "Median" | "FindMinimum" | "FindMaximum"; /** A `StandardAggregation` calculates one of a handful of built-in reductions over rows. */ /** @internal */ export type StandardAggregation = { type: T; aggregation_type: U; input_key: string; value: EastFunction; key?: EastFunction | undefined; field: Variable; }; /** * A {@link StandardAggregationDefinition} calculates one of a handful of built-in reductions over rows. * * @category Aggregation */ export type StandardAggregationDefinition = { type: T; aggregation_type: U; value: EastFunction; key?: EastFunction | undefined; }; /** @internal */ export type AggregateValue = T extends Aggregation ? T : never; /** @internal */ export type Aggregation = CustomAggregation | StandardAggregation; /** * A {@link AggregationDefinition} is either a {@link CustomAggregationDefinition} or {@link StandardAggregationDefinition}. * * @category Aggregation */ export type AggregationDefinition = CustomAggregationDefinition | StandardAggregationDefinition; /** * Create an `Aggregation` to count the number of rows where `value` is not null. * * @param value the value to aggregate * @returns the {@link Count} {@link StandardAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // count the number of Category's that aren't null * ValidCategories: fields => Count(fields.Category), * // ... * } * ``` */ export declare function Count(value?: EastFunction): StandardAggregationDefinition; /** * Create an `Aggregation` to count the number of distinct, non-null values of `value`. * * @param value the value to aggregate * @returns the {@link DistinctCount} {@link StandardAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // count the number of distinct non-null Category's * Categories: fields => DistinctCount(fields.Category), * // ... * } * ``` */ export declare function DistinctCount(value: EastFunction): StandardAggregationDefinition; /** * Create an `Aggregation` to find the unique non-null value of `value`. If multiple non-null values are encountered, it returns null. * * @param value the value to aggregate * @returns the {@link Unique} {@link StandardAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // count the number of unique non-null Category's * Categories: fields => Unique(fields.Category), * // ... * } * ``` */ export declare function Unique(value: EastFunction): T extends NullType ? StandardAggregationDefinition : StandardAggregationDefinition>; /** * Create an {@link AggregationDefinition} to find the sum of the non-null values of `value` (which defaults to `field`). * * @param value the value to aggregate * @returns the {@link Sum} {@link StandardAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // sum all Qty's to find the total * TotalQty: fields => Sum(fields.Qty), * // ... * } * ``` */ export declare function Sum(value: EastFunction): StandardAggregationDefinition; export declare function Sum(value: EastFunction): StandardAggregationDefinition; /** * Create an {@link AggregationDefinition} to find the mean of the non-null values of `value` (which defaults to `field`). * * @param value the value to aggregate * @returns the {@link Mean} {@link StandardAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // find the mean Qty's * MeanQty: fields => Mean(fields.Qty), * // ... * } * ``` */ export declare function Mean(value: EastFunction): StandardAggregationDefinition; /** * Create an {@link AggregationDefinition} to find the standard deviation of the non-null values of `value` (which defaults to `field`). * * @param value the value to aggregate * @returns the {@link StdDev} {@link StandardAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // find the std deviation of Qty's * StdDevQty: fields => StdDev(fields.Qty), * // ... * } * ``` */ export declare function StdDev(value: EastFunction): StandardAggregationDefinition; /** * Create an {@link AggregationDefinition} to find the set of the non-null string values of `value` (which defaults to `field`). * * @param value the value to aggregate * @returns the {@link CollectSet} {@link StandardAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // collect all non-null Categorie's into a set * Categories: fields => CollectSet(fields.Category), * // ... * } * ``` */ export declare function CollectSet(value: EastFunction): StandardAggregationDefinition>; /** * Create an {@link AggregationDefinition} to create a dictionary of distinct key-value pairs. If multiple distinct, non-null values exist for a given key, then the value is `null` for that key. * * @param key the dict key value * @param value the value to aggregate * @returns the {@link CollectDict} {@link StandardAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // collect all non-null HourlyRate's into a dictionary by EmployeeId * HourlyRatePerEmployee: fields => CollectDict( * fields.EmployeeId, * fields.HourlyRate * ), * // ... * } * ``` */ export declare function CollectDict(key: EastFunction, value: EastFunction): StandardAggregationDefinition>; /** * Create an {@link AggregationDefinition} to create a dictionary of the count of non-null keys * * @param value the dict key value * @returns the {@link CollectDictCount} {@link StandardAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // create a dictionary with the count of products by Category * ProductsPerCategory: fields => CollectDictCount( * fields.Category, * ), * // ... * } * ``` */ export declare function CollectDictCount(value: EastFunction): StandardAggregationDefinition>; /** * Create an {@link AggregationDefinition} to create a dictionary of the sum of non-null values for key-value pairs. * * @param key the dict key value * @param value the value to aggregate * @returns the {@link CollectDictSum} {@link StandardAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // create a dictionary with the sum of Qty's by Category * QtyPerCategory: fields => CollectDictSum( * fields.Category, * fields.Qty * ), * // ... * } * ``` */ export declare function CollectDictSum(key: EastFunction, value: EastFunction): T extends NullType ? StandardAggregationDefinition>> : StandardAggregationDefinition>; /** * Create an {@link AggregationDefinition} to create a dictionary of the sum of non-null values for key-value pairs. * * @param key the dict key value * @param value the value to aggregate * @returns the {@link CollectDictMean} {@link StandardAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // create a dictionary with the average of Price per Category * MeanPricePerCategory: fields => CollectDictMean( * fields.Category, * fields.Price * ), * // ... * } * ``` */ export declare function CollectDictMean(key: EastFunction, value: EastFunction): StandardAggregationDefinition>; /** * Create an {@link AggregationDefinition} to create a dictionary of the minimum of non-null values for key-value pairs. * * @param key the dict key value * @param value the value to aggregate * @returns the {@link CollectDictMean} {@link StandardAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // create a dictionary with the min of Price per Category * MinPricePerCategory: fields => CollectDictMin( * fields.Category, * fields.Price * ), * // ... * } * ``` */ export declare function CollectDictMin(key: EastFunction, value: EastFunction): StandardAggregationDefinition>; /** * Create an {@link AggregationDefinition} to create a dictionary of the maximum of non-null values for key-value pairs. * * @param key the dict key value * @param value the value to aggregate * @returns the {@link CollectDictMean} {@link StandardAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // create a dictionary with the min of Price per Category * MaxPricePerCategory: fields => CollectDictMax( * fields.Category, * fields.Price * ), * // ... * } * ``` */ export declare function CollectDictMax(key: EastFunction, value: EastFunction): StandardAggregationDefinition>; /** * Create an {@link AggregationDefinition} that returns true if one or more value is `true`. * * @param value the value to aggregate * @returns the {@link Any} {@link StandardAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // return true if a category value is null * CategoryMissing: fields => Any(IsNull(fields.Category)), * // ... * } * ``` */ export declare function Any(value: EastFunction): StandardAggregationDefinition; /** * Create an {@link AggregationDefinition} that returns true if all values are `true` * * @param value the value to aggregate * @returns the {@link Every} {@link StandardAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // return true if all category values are null * AllCategoriesMissing: fields => Every( * IsNull(fields.Category) * ), * // ... * } * ``` */ export declare function Every(value: EastFunction): StandardAggregationDefinition; /** * Create an {@link AggregationDefinition} to find the most common, non-null value of `value` (which defaults to `field`). If a tie is encountered, any one of the most-common values is chosen. * * @param value the value to aggregate * @returns the {@link Mode} {@link StandardAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // return the mode of prices * ModalPrice: fields => Mode(fields.Price), * // ... * } * ``` */ export declare function Mode(value: EastFunction): StandardAggregationDefinition; /** * Create an {@link AggregationDefinition} to find the smallest, non-null value of `value` (which defaults to `field`). * * @param value the value to aggregate * @returns the {@link Minimum} {@link StandardAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // return the lowest Price * MinPrice: fields => Minimum(fields.Price), * // ... * } * ``` */ export declare function Minimum(value: EastFunction): StandardAggregationDefinition; /** * Create an {@link AggregationDefinition} to find the largest, non-null value of `value` (which defaults to `field`). * * @param value the value to aggregate * @returns the {@link Maximum} {@link StandardAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // return the highest Price * MaxPrice: fields => Maximum(fields.Price), * // ... * } * ``` */ export declare function Maximum(value: EastFunction): StandardAggregationDefinition; /** * Create an {@link AggregationDefinition} to find the span of the non-null values of `value` (which defaults to `field`), meaning the difference between the largest and smallest values. * * @param value the value to aggregate * @returns the {@link Span} {@link StandardAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // return the Price span * PriceSpan: fields => Span(fields.Price), * // ... * } * ``` */ export declare function Span(value: EastFunction): StandardAggregationDefinition; /** * Create an {@link AggregationDefinition} to find the value of `key`, in the key-value pair that has the smallest, non-null value of `value` (which defaults to `field`). * * @param value the value to aggregate * @param key the value to find the minimum of * @returns the {@link FindMinimum} {@link StandardAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // return Product with the lowest Price * LowestPriceProduct: fields => FindMinimum( * fields.Price, * fields.Product * ), * // ... * } * ``` */ export declare function FindMinimum(value: EastFunction, key: EastFunction): K extends NullType ? T extends NullType ? StandardAggregationDefinition : StandardAggregationDefinition> : StandardAggregationDefinition; /** * Create an {@link AggregationDefinition} to find the value of `key`, in the key-value pair that has the largest, non-null value of `value` (which defaults to `field`). * * @param value the value to aggregate * @param key the value to find the minimum of * @returns the {@link FindMaximum} {@link StandardAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // return Product with the lowest Price * HighestPriceProduct: fields => FindMaximum( * fields.Price, * fields.Product * ), * // ... * } * ``` */ export declare function FindMaximum(value: EastFunction, key: EastFunction): K extends NullType ? T extends NullType ? StandardAggregationDefinition : StandardAggregationDefinition> : StandardAggregationDefinition; /** * Create an {@link AggregationDefinition} to find the median of the non-null values of `value` (which defaults to `field`). In case of a tie, the greater number is returned. * * @param value the value to aggregate * @returns the {@link Median} {@link StandardAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // return the middle Price * MedianPrice: fields => Median(fields.Price), * // ... * } * ``` */ export declare function Median(value: EastFunction): StandardAggregationDefinition; /** * Create an {@link AggregationDefinition} to calculate the sum of the values of a "sparse" dictionary where missing elements are presumed to have value of zero. * * @param value_expr the value to aggregate * @returns the {@link SparseDictSum} {@link CustomAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // return the ... * PriceCardTotal: fields => SparseDictSum(fields.PriceCard, DictType(StringType,FloatType))), * // ... * } * ``` */ export declare function SparseDictSum(value_expr: Expression>): AggregationDefinition>; export declare function SparseDictSum(value_expr: Expression>): AggregationDefinition>; /** * Create an {@link AggregationDefinition} to calculate the average values of a "sparse" dictionary where missing elements are presumed to have value of zero. * * @param value_expr the value to aggregate * @returns the {@link SparseDictMean} {@link CustomAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // return the ... * PriceCardAvg: fields => SparseDictMean(fields.PriceCard, DictType(StringType,FloatType))), * // ... * } * ``` */ export declare function SparseDictMean(value_expr: Expression>): AggregationDefinition>; export declare function SparseDictMean(value_expr: Expression>): AggregationDefinition>; /** * Create an {@link AggregationDefinition} to calculate the variance of the values of a "sparse" dictionary where missing elements are presumed to have value of zero. * * @param value_expr the value to aggregate * @returns the {@link SparseDictMean} {@link CustomAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // return the ... * PriceCardVariance: fields => SparseDictVariance(fields.PriceCard, DictType(StringType,FloatType))), * // ... * } * ``` */ export declare function SparseDictVariance(value_expr: Expression>): AggregationDefinition>; export declare function SparseDictVariance(value_expr: Expression>): AggregationDefinition>; /** * Create an {@link AggregationDefinition} to calculate the covariance of the values of a "sparse" dictionary where missing elements are presumed to have value of zero. * * @param value_expr the value to aggregate * @returns the {@link SparseDictMean} {@link CustomAggregationDefinition} * * @category Aggregation * * @example * ```typescript * // ... * aggregations: { * // return the ... * PriceCardCoVariance: fields => SparseDictCovariance(fields.PriceCard, DictType(StringType,FloatType))), * // ... * } * ``` */ export declare function SparseDictCovariance(value_expr: Expression>): AggregationDefinition>>; export declare function SparseDictCovariance(value_expr: Expression>): AggregationDefinition>>; /** @internal */ export declare function toAggregation(def: AggregationDefinition, group_key: string, input_key: string): Aggregation; /** @internal */ export declare function checkAggregationInput(aggregation: Aggregation, variables: Record): void; export type SimpleAggregationTimeUnit = 'date' | 'day' | 'hours' | 'month' | 'year' | 'yearmonthdatehours' | 'yearmonthdate' | 'yearmonth'; export type SimpleAggregationKind = 'count' | 'distinct' | 'sum' | 'mean' | 'max' | 'min' | 'find_minimum' | 'find_maximum' | 'collect'; export type SimpleAggregationField = { field: string; timeUnit?: SimpleAggregationTimeUnit; }; export type SimpleAggregation = { aggregate: SimpleAggregationKind; value: SimpleAggregationField; key?: SimpleAggregationField | undefined; group?: SimpleAggregationField | undefined; }; export declare function fromTimeUnitToFunction(timeUnit: SimpleAggregationTimeUnit, variable: Variable): EastFunction | undefined; export type SimpleAggregationDefinition = { group: EastFunction | undefined; aggregation: StandardAggregationDefinition; }; export declare function fromAggregationEncoding = DictType>(encoding: SimpleAggregation, fields: TypeToFields): SimpleAggregationDefinition | undefined;