Debian Codegraph @

packages/search/lib/commands/AGGREGATE.ts

raw
1 import { RedisCommandArgument, RedisCommandArguments } from '@redis/client/dist/lib/commands';
2 import { pushVerdictArgument, transformTuplesReply } from '@redis/client/dist/lib/commands/generic-transformers';
3 import { Params, PropertyName, pushArgumentsWithLength, pushParamsArgs, pushSortByArguments, SortByProperty } from '.';
4
5 export enum AggregateSteps {
6 GROUPBY = 'GROUPBY',
7 SORTBY = 'SORTBY',
8 APPLY = 'APPLY',
9 LIMIT = 'LIMIT',
10 FILTER = 'FILTER'
11 }
12
13 interface AggregateStep<T extends AggregateSteps> {
14 type: T;
15 }
16
17 export enum AggregateGroupByReducers {
18 COUNT = 'COUNT',
19 COUNT_DISTINCT = 'COUNT_DISTINCT',
20 COUNT_DISTINCTISH = 'COUNT_DISTINCTISH',
21 SUM = 'SUM',
22 MIN = 'MIN',
23 MAX = 'MAX',
24 AVG = 'AVG',
25 STDDEV = 'STDDEV',
26 QUANTILE = 'QUANTILE',
27 TOLIST = 'TOLIST',
28 TO_LIST = 'TOLIST',
29 FIRST_VALUE = 'FIRST_VALUE',
30 RANDOM_SAMPLE = 'RANDOM_SAMPLE'
31 }
32
33 interface GroupByReducer<T extends AggregateGroupByReducers> {
34 type: T;
35 AS?: string;
36 }
37
38 type CountReducer = GroupByReducer<AggregateGroupByReducers.COUNT>;
39
40 interface CountDistinctReducer extends GroupByReducer<AggregateGroupByReducers.COUNT_DISTINCT> {
41 property: PropertyName;
42 }
43
44 interface CountDistinctishReducer extends GroupByReducer<AggregateGroupByReducers.COUNT_DISTINCTISH> {
45 property: PropertyName;
46 }
47
48 interface SumReducer extends GroupByReducer<AggregateGroupByReducers.SUM> {
49 property: PropertyName;
50 }
51
52 interface MinReducer extends GroupByReducer<AggregateGroupByReducers.MIN> {
53 property: PropertyName;
54 }
55
56 interface MaxReducer extends GroupByReducer<AggregateGroupByReducers.MAX> {
57 property: PropertyName;
58 }
59
60 interface AvgReducer extends GroupByReducer<AggregateGroupByReducers.AVG> {
61 property: PropertyName;
62 }
63
64 interface StdDevReducer extends GroupByReducer<AggregateGroupByReducers.STDDEV> {
65 property: PropertyName;
66 }
67
68 interface QuantileReducer extends GroupByReducer<AggregateGroupByReducers.QUANTILE> {
69 property: PropertyName;
70 quantile: number;
71 }
72
73 interface ToListReducer extends GroupByReducer<AggregateGroupByReducers.TOLIST> {
74 property: PropertyName;
75 }
76
77 interface FirstValueReducer extends GroupByReducer<AggregateGroupByReducers.FIRST_VALUE> {
78 property: PropertyName;
79 BY?: PropertyName | {
80 property: PropertyName;
81 direction?: 'ASC' | 'DESC';
82 };
83 }
84
85 interface RandomSampleReducer extends GroupByReducer<AggregateGroupByReducers.RANDOM_SAMPLE> {
86 property: PropertyName;
87 sampleSize: number;
88 }
89
90 type GroupByReducers = CountReducer | CountDistinctReducer | CountDistinctishReducer | SumReducer | MinReducer | MaxReducer | AvgReducer | StdDevReducer | QuantileReducer | ToListReducer | FirstValueReducer | RandomSampleReducer;
91
92 interface GroupByStep extends AggregateStep<AggregateSteps.GROUPBY> {
93 properties?: PropertyName | Array<PropertyName>;
94 REDUCE: GroupByReducers | Array<GroupByReducers>;
95 }
96
97 interface SortStep extends AggregateStep<AggregateSteps.SORTBY> {
98 BY: SortByProperty | Array<SortByProperty>;
99 MAX?: number;
100 }
101
102 interface ApplyStep extends AggregateStep<AggregateSteps.APPLY> {
103 expression: string;
104 AS: string;
105 }
106
107 interface LimitStep extends AggregateStep<AggregateSteps.LIMIT> {
108 from: number;
109 size: number;
110 }
111
112 interface FilterStep extends AggregateStep<AggregateSteps.FILTER> {
113 expression: string;
114 }
115
116 type LoadField = PropertyName | {
117 identifier: PropertyName;
118 AS?: string;
119 }
120
121 export interface AggregateOptions {
122 VERBATIM?: true;
123 LOAD?: LoadField | Array<LoadField>;
124 STEPS?: Array<GroupByStep | SortStep | ApplyStep | LimitStep | FilterStep>;
125 PARAMS?: Params;
126 DIALECT?: number;
127 TIMEOUT?: number;
128 }
129
130 export const FIRST_KEY_INDEX = 1;
131
132 export const IS_READ_ONLY = true;
133
134 export function transformArguments(
135 index: string,
136 query: string,
137 options?: AggregateOptions
138 ): RedisCommandArguments {
139 return pushAggregatehOptions(
140 ['FT.AGGREGATE', index, query],
141 options
142 );
143 }
144
145 export function pushAggregatehOptions(
146 args: RedisCommandArguments,
147 options?: AggregateOptions
148 ): RedisCommandArguments {
149 if (options?.VERBATIM) {
150 args.push('VERBATIM');
151 }
152
153 if (options?.LOAD) {
154 args.push('LOAD');
155 pushArgumentsWithLength(args, () => {
156 if (Array.isArray(options.LOAD)) {
157 for (const load of options.LOAD) {
158 pushLoadField(args, load);
159 }
160 } else {
161 pushLoadField(args, options.LOAD!);
162 }
163 });
164 }
165
166 if (options?.STEPS) {
167 for (const step of options.STEPS) {
168 switch (step.type) {
169 case AggregateSteps.GROUPBY:
170 args.push('GROUPBY');
171 if (!step.properties) {
172 args.push('0');
173 } else {
174 pushVerdictArgument(args, step.properties);
175 }
176
177 if (Array.isArray(step.REDUCE)) {
178 for (const reducer of step.REDUCE) {
179 pushGroupByReducer(args, reducer);
180 }
181 } else {
182 pushGroupByReducer(args, step.REDUCE);
183 }
184
185 break;
186
187 case AggregateSteps.SORTBY:
188 pushSortByArguments(args, 'SORTBY', step.BY);
189
190 if (step.MAX) {
191 args.push('MAX', step.MAX.toString());
192 }
193
194 break;
195
196 case AggregateSteps.APPLY:
197 args.push('APPLY', step.expression, 'AS', step.AS);
198 break;
199
200 case AggregateSteps.LIMIT:
201 args.push('LIMIT', step.from.toString(), step.size.toString());
202 break;
203
204 case AggregateSteps.FILTER:
205 args.push('FILTER', step.expression);
206 break;
207 }
208 }
209 }
210
211 pushParamsArgs(args, options?.PARAMS);
212
213 if (options?.DIALECT) {
214 args.push('DIALECT', options.DIALECT.toString());
215 }
216
217 if (options?.TIMEOUT !== undefined) {
218 args.push('TIMEOUT', options.TIMEOUT.toString());
219 }
220
221 return args;
222 }
223
224 function pushLoadField(args: RedisCommandArguments, toLoad: LoadField): void {
225 if (typeof toLoad === 'string') {
226 args.push(toLoad);
227 } else {
228 args.push(toLoad.identifier);
229
230 if (toLoad.AS) {
231 args.push('AS', toLoad.AS);
232 }
233 }
234 }
235
236 function pushGroupByReducer(args: RedisCommandArguments, reducer: GroupByReducers): void {
237 args.push('REDUCE', reducer.type);
238
239 switch (reducer.type) {
240 case AggregateGroupByReducers.COUNT:
241 args.push('0');
242 break;
243
244 case AggregateGroupByReducers.COUNT_DISTINCT:
245 case AggregateGroupByReducers.COUNT_DISTINCTISH:
246 case AggregateGroupByReducers.SUM:
247 case AggregateGroupByReducers.MIN:
248 case AggregateGroupByReducers.MAX:
249 case AggregateGroupByReducers.AVG:
250 case AggregateGroupByReducers.STDDEV:
251 case AggregateGroupByReducers.TOLIST:
252 args.push('1', reducer.property);
253 break;
254
255 case AggregateGroupByReducers.QUANTILE:
256 args.push('2', reducer.property, reducer.quantile.toString());
257 break;
258
259 case AggregateGroupByReducers.FIRST_VALUE: {
260 pushArgumentsWithLength(args, () => {
261 args.push(reducer.property);
262
263 if (reducer.BY) {
264 args.push('BY');
265 if (typeof reducer.BY === 'string') {
266 args.push(reducer.BY);
267 } else {
268 args.push(reducer.BY.property);
269
270 if (reducer.BY.direction) {
271 args.push(reducer.BY.direction);
272 }
273 }
274 }
275 });
276 break;
277 }
278
279 case AggregateGroupByReducers.RANDOM_SAMPLE:
280 args.push('2', reducer.property, reducer.sampleSize.toString());
281 break;
282 }
283
284 if (reducer.AS) {
285 args.push('AS', reducer.AS);
286 }
287 }
288
289 export type AggregateRawReply = [
290 total: number,
291 ...results: Array<Array<RedisCommandArgument>>
292 ];
293
294 export interface AggregateReply {
295 total: number;
296 results: Array<Record<string, RedisCommandArgument>>;
297 }
298
299 export function transformReply(rawReply: AggregateRawReply): AggregateReply {
300 const results: Array<Record<string, RedisCommandArgument>> = [];
301 for (let i = 1; i < rawReply.length; i++) {
302 results.push(
303 transformTuplesReply(rawReply[i] as Array<RedisCommandArgument>)
304 );
305 }
306
307 return {
308 total: rawReply[0],
309 results
310 };
311 }