Use AROP to reduce the values in an index range to a single number, on the server.
SUM, MIN, and MAX treat the values as numbers; AND, OR, and XOR fold them together as 64-bit integers, with fractional values truncated. All six ignore values that cannot be parsed as a number, and reply with null when the range contains nothing they could use, which distinguishes "no data" from a real result of 0. USED counts the occupied slots in the range and MATCH counts the slots whose value equals a given string; both always reply with an integer.
Running the reduction where the data lives keeps a range scan off the wire, which is the point when the array is a window of samples and you only need its total, extremes, or population.
See the array command overview for the data model these commands share.
Syntax#
Arguments#
| Argument | Required | Repeatable | Description |
|---|---|---|---|
<key> | Yes | No | Array key targeted by the command. |
<start> | Yes | No | First index of the range, inclusive. |
<end> | Yes | No | Last index of the range, inclusive. |
SUM | No | No | Sum of the numeric values in the range. |
MIN | No | No | Smallest numeric value in the range. |
MAX | No | No | Largest numeric value in the range. |
AND | No | No | Bitwise AND of the values in the range, as 64-bit integers. |
OR | No | No | Bitwise OR of the values in the range, as 64-bit integers. |
XOR | No | No | Bitwise XOR of the values in the range, as 64-bit integers. |
USED | No | No | Number of occupied slots in the range. |
MATCH <value> | No | No | Number of slots in the range whose value equals <value>. |
Important points#
- Exactly one operation must be given.
- Values that are not numbers are skipped by
SUM,MIN,MAX,AND,OR, andXOR, and counted normally byUSEDandMATCH. SUM,MIN,MAX,AND,OR, andXORreply with null when no value in the range could be used.USEDandMATCHreply with0.
Response#
The reply reports the result of the operation. Error replies have the same shape in RESP2 and RESP3 and are surfaced as exceptions by the SDKs below.
| Protocol | Reply |
|---|---|
| RESP2 | Bulk string for SUM, MIN, and MAX; Integer for AND, OR, XOR, USED, and MATCH; Null bulk string when there is nothing to aggregate |
| RESP3 | Bulk string for SUM, MIN, and MAX; Integer for AND, OR, XOR, USED, and MATCH; Null when there is nothing to aggregate |
Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.
Examples#
TCP examples use the TLS REDIS_URL from the Upstash console. REST examples use UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN.
Redis CLI
@upstash/redis
This command is not supported yet in @upstash/redis.
upstash_redis
This command is not supported yet in upstash_redis.