| 1 |
from typing import Union |
| 2 |
|
| 3 |
from .aggregation import Asc, Desc, Reducer, SortDirection |
| 4 |
|
| 5 |
|
| 6 |
class FieldOnlyReducer(Reducer): |
| 7 |
"""See https://valkey.io/docs/interact/search-and-query/search/aggregations/""" |
| 8 |
|
| 9 |
def __init__(self, field: str) -> None: |
| 10 |
super().__init__(field) |
| 11 |
self._field = field |
| 12 |
|
| 13 |
|
| 14 |
class count(Reducer): |
| 15 |
""" |
| 16 |
Counts the number of results in the group |
| 17 |
""" |
| 18 |
|
| 19 |
NAME = "COUNT" |
| 20 |
|
| 21 |
def __init__(self) -> None: |
| 22 |
super().__init__() |
| 23 |
|
| 24 |
|
| 25 |
class sum(FieldOnlyReducer): |
| 26 |
""" |
| 27 |
Calculates the sum of all the values in the given fields within the group |
| 28 |
""" |
| 29 |
|
| 30 |
NAME = "SUM" |
| 31 |
|
| 32 |
def __init__(self, field: str) -> None: |
| 33 |
super().__init__(field) |
| 34 |
|
| 35 |
|
| 36 |
class min(FieldOnlyReducer): |
| 37 |
""" |
| 38 |
Calculates the smallest value in the given field within the group |
| 39 |
""" |
| 40 |
|
| 41 |
NAME = "MIN" |
| 42 |
|
| 43 |
def __init__(self, field: str) -> None: |
| 44 |
super().__init__(field) |
| 45 |
|
| 46 |
|
| 47 |
class max(FieldOnlyReducer): |
| 48 |
""" |
| 49 |
Calculates the largest value in the given field within the group |
| 50 |
""" |
| 51 |
|
| 52 |
NAME = "MAX" |
| 53 |
|
| 54 |
def __init__(self, field: str) -> None: |
| 55 |
super().__init__(field) |
| 56 |
|
| 57 |
|
| 58 |
class avg(FieldOnlyReducer): |
| 59 |
""" |
| 60 |
Calculates the mean value in the given field within the group |
| 61 |
""" |
| 62 |
|
| 63 |
NAME = "AVG" |
| 64 |
|
| 65 |
def __init__(self, field: str) -> None: |
| 66 |
super().__init__(field) |
| 67 |
|
| 68 |
|
| 69 |
class tolist(FieldOnlyReducer): |
| 70 |
""" |
| 71 |
Returns all the matched properties in a list |
| 72 |
""" |
| 73 |
|
| 74 |
NAME = "TOLIST" |
| 75 |
|
| 76 |
def __init__(self, field: str) -> None: |
| 77 |
super().__init__(field) |
| 78 |
|
| 79 |
|
| 80 |
class count_distinct(FieldOnlyReducer): |
| 81 |
""" |
| 82 |
Calculate the number of distinct values contained in all the results in |
| 83 |
the group for the given field |
| 84 |
""" |
| 85 |
|
| 86 |
NAME = "COUNT_DISTINCT" |
| 87 |
|
| 88 |
def __init__(self, field: str) -> None: |
| 89 |
super().__init__(field) |
| 90 |
|
| 91 |
|
| 92 |
class count_distinctish(FieldOnlyReducer): |
| 93 |
""" |
| 94 |
Calculate the number of distinct values contained in all the results in the |
| 95 |
group for the given field. This uses a faster algorithm than |
| 96 |
`count_distinct` but is less accurate |
| 97 |
""" |
| 98 |
|
| 99 |
NAME = "COUNT_DISTINCTISH" |
| 100 |
|
| 101 |
|
| 102 |
class quantile(Reducer): |
| 103 |
""" |
| 104 |
Return the value for the nth percentile within the range of values for the |
| 105 |
field within the group. |
| 106 |
""" |
| 107 |
|
| 108 |
NAME = "QUANTILE" |
| 109 |
|
| 110 |
def __init__(self, field: str, pct: float) -> None: |
| 111 |
super().__init__(field, str(pct)) |
| 112 |
self._field = field |
| 113 |
|
| 114 |
|
| 115 |
class stddev(FieldOnlyReducer): |
| 116 |
""" |
| 117 |
Return the standard deviation for the values within the group |
| 118 |
""" |
| 119 |
|
| 120 |
NAME = "STDDEV" |
| 121 |
|
| 122 |
def __init__(self, field: str) -> None: |
| 123 |
super().__init__(field) |
| 124 |
|
| 125 |
|
| 126 |
class first_value(Reducer): |
| 127 |
""" |
| 128 |
Selects the first value within the group according to sorting parameters |
| 129 |
""" |
| 130 |
|
| 131 |
NAME = "FIRST_VALUE" |
| 132 |
|
| 133 |
def __init__(self, field: str, *byfields: Union[Asc, Desc]) -> None: |
| 134 |
""" |
| 135 |
Selects the first value of the given field within the group. |
| 136 |
|
| 137 |
### Parameter |
| 138 |
|
| 139 |
- **field**: Source field used for the value |
| 140 |
- **byfields**: How to sort the results. This can be either the |
| 141 |
*class* of `aggregation.Asc` or `aggregation.Desc` in which |
| 142 |
case the field `field` is also used as the sort input. |
| 143 |
|
| 144 |
`byfields` can also be one or more *instances* of `Asc` or `Desc` |
| 145 |
indicating the sort order for these fields |
| 146 |
""" |
| 147 |
|
| 148 |
fieldstrs = [] |
| 149 |
if ( |
| 150 |
len(byfields) == 1 |
| 151 |
and isinstance(byfields[0], type) |
| 152 |
and issubclass(byfields[0], SortDirection) |
| 153 |
): |
| 154 |
byfields = [byfields[0](field)] |
| 155 |
|
| 156 |
for f in byfields: |
| 157 |
fieldstrs += [f.field, f.DIRSTRING] |
| 158 |
|
| 159 |
args = [field] |
| 160 |
if fieldstrs: |
| 161 |
args += ["BY"] + fieldstrs |
| 162 |
super().__init__(*args) |
| 163 |
self._field = field |
| 164 |
|
| 165 |
|
| 166 |
class random_sample(Reducer): |
| 167 |
""" |
| 168 |
Returns a random sample of items from the dataset, from the given property |
| 169 |
""" |
| 170 |
|
| 171 |
NAME = "RANDOM_SAMPLE" |
| 172 |
|
| 173 |
def __init__(self, field: str, size: int) -> None: |
| 174 |
""" |
| 175 |
### Parameter |
| 176 |
|
| 177 |
**field**: Field to sample from |
| 178 |
**size**: Return this many items (can be less) |
| 179 |
""" |
| 180 |
args = [field, str(size)] |
| 181 |
super().__init__(*args) |
| 182 |
self._field = field |