AGGREGATE.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.transformReply = exports.pushAggregatehOptions = exports.transformArguments = exports.IS_READ_ONLY = exports.FIRST_KEY_INDEX = exports.AggregateGroupByReducers = exports.AggregateSteps = void 0;
  4. const generic_transformers_1 = require("@redis/client/dist/lib/commands/generic-transformers");
  5. const _1 = require(".");
  6. var AggregateSteps;
  7. (function (AggregateSteps) {
  8. AggregateSteps["GROUPBY"] = "GROUPBY";
  9. AggregateSteps["SORTBY"] = "SORTBY";
  10. AggregateSteps["APPLY"] = "APPLY";
  11. AggregateSteps["LIMIT"] = "LIMIT";
  12. AggregateSteps["FILTER"] = "FILTER";
  13. })(AggregateSteps || (exports.AggregateSteps = AggregateSteps = {}));
  14. var AggregateGroupByReducers;
  15. (function (AggregateGroupByReducers) {
  16. AggregateGroupByReducers["COUNT"] = "COUNT";
  17. AggregateGroupByReducers["COUNT_DISTINCT"] = "COUNT_DISTINCT";
  18. AggregateGroupByReducers["COUNT_DISTINCTISH"] = "COUNT_DISTINCTISH";
  19. AggregateGroupByReducers["SUM"] = "SUM";
  20. AggregateGroupByReducers["MIN"] = "MIN";
  21. AggregateGroupByReducers["MAX"] = "MAX";
  22. AggregateGroupByReducers["AVG"] = "AVG";
  23. AggregateGroupByReducers["STDDEV"] = "STDDEV";
  24. AggregateGroupByReducers["QUANTILE"] = "QUANTILE";
  25. AggregateGroupByReducers["TOLIST"] = "TOLIST";
  26. AggregateGroupByReducers["TO_LIST"] = "TOLIST";
  27. AggregateGroupByReducers["FIRST_VALUE"] = "FIRST_VALUE";
  28. AggregateGroupByReducers["RANDOM_SAMPLE"] = "RANDOM_SAMPLE";
  29. })(AggregateGroupByReducers || (exports.AggregateGroupByReducers = AggregateGroupByReducers = {}));
  30. exports.FIRST_KEY_INDEX = 1;
  31. exports.IS_READ_ONLY = true;
  32. function transformArguments(index, query, options) {
  33. return pushAggregatehOptions(['FT.AGGREGATE', index, query], options);
  34. }
  35. exports.transformArguments = transformArguments;
  36. function pushAggregatehOptions(args, options) {
  37. if (options?.VERBATIM) {
  38. args.push('VERBATIM');
  39. }
  40. if (options?.LOAD) {
  41. args.push('LOAD');
  42. (0, _1.pushArgumentsWithLength)(args, () => {
  43. if (Array.isArray(options.LOAD)) {
  44. for (const load of options.LOAD) {
  45. pushLoadField(args, load);
  46. }
  47. }
  48. else {
  49. pushLoadField(args, options.LOAD);
  50. }
  51. });
  52. }
  53. if (options?.STEPS) {
  54. for (const step of options.STEPS) {
  55. switch (step.type) {
  56. case AggregateSteps.GROUPBY:
  57. args.push('GROUPBY');
  58. if (!step.properties) {
  59. args.push('0');
  60. }
  61. else {
  62. (0, generic_transformers_1.pushVerdictArgument)(args, step.properties);
  63. }
  64. if (Array.isArray(step.REDUCE)) {
  65. for (const reducer of step.REDUCE) {
  66. pushGroupByReducer(args, reducer);
  67. }
  68. }
  69. else {
  70. pushGroupByReducer(args, step.REDUCE);
  71. }
  72. break;
  73. case AggregateSteps.SORTBY:
  74. (0, _1.pushSortByArguments)(args, 'SORTBY', step.BY);
  75. if (step.MAX) {
  76. args.push('MAX', step.MAX.toString());
  77. }
  78. break;
  79. case AggregateSteps.APPLY:
  80. args.push('APPLY', step.expression, 'AS', step.AS);
  81. break;
  82. case AggregateSteps.LIMIT:
  83. args.push('LIMIT', step.from.toString(), step.size.toString());
  84. break;
  85. case AggregateSteps.FILTER:
  86. args.push('FILTER', step.expression);
  87. break;
  88. }
  89. }
  90. }
  91. (0, _1.pushParamsArgs)(args, options?.PARAMS);
  92. if (options?.DIALECT) {
  93. args.push('DIALECT', options.DIALECT.toString());
  94. }
  95. if (options?.TIMEOUT !== undefined) {
  96. args.push('TIMEOUT', options.TIMEOUT.toString());
  97. }
  98. return args;
  99. }
  100. exports.pushAggregatehOptions = pushAggregatehOptions;
  101. function pushLoadField(args, toLoad) {
  102. if (typeof toLoad === 'string') {
  103. args.push(toLoad);
  104. }
  105. else {
  106. args.push(toLoad.identifier);
  107. if (toLoad.AS) {
  108. args.push('AS', toLoad.AS);
  109. }
  110. }
  111. }
  112. function pushGroupByReducer(args, reducer) {
  113. args.push('REDUCE', reducer.type);
  114. switch (reducer.type) {
  115. case AggregateGroupByReducers.COUNT:
  116. args.push('0');
  117. break;
  118. case AggregateGroupByReducers.COUNT_DISTINCT:
  119. case AggregateGroupByReducers.COUNT_DISTINCTISH:
  120. case AggregateGroupByReducers.SUM:
  121. case AggregateGroupByReducers.MIN:
  122. case AggregateGroupByReducers.MAX:
  123. case AggregateGroupByReducers.AVG:
  124. case AggregateGroupByReducers.STDDEV:
  125. case AggregateGroupByReducers.TOLIST:
  126. args.push('1', reducer.property);
  127. break;
  128. case AggregateGroupByReducers.QUANTILE:
  129. args.push('2', reducer.property, reducer.quantile.toString());
  130. break;
  131. case AggregateGroupByReducers.FIRST_VALUE: {
  132. (0, _1.pushArgumentsWithLength)(args, () => {
  133. args.push(reducer.property);
  134. if (reducer.BY) {
  135. args.push('BY');
  136. if (typeof reducer.BY === 'string') {
  137. args.push(reducer.BY);
  138. }
  139. else {
  140. args.push(reducer.BY.property);
  141. if (reducer.BY.direction) {
  142. args.push(reducer.BY.direction);
  143. }
  144. }
  145. }
  146. });
  147. break;
  148. }
  149. case AggregateGroupByReducers.RANDOM_SAMPLE:
  150. args.push('2', reducer.property, reducer.sampleSize.toString());
  151. break;
  152. }
  153. if (reducer.AS) {
  154. args.push('AS', reducer.AS);
  155. }
  156. }
  157. function transformReply(rawReply) {
  158. const results = [];
  159. for (let i = 1; i < rawReply.length; i++) {
  160. results.push((0, generic_transformers_1.transformTuplesReply)(rawReply[i]));
  161. }
  162. return {
  163. total: rawReply[0],
  164. results
  165. };
  166. }
  167. exports.transformReply = transformReply;