SDIFF
The SDIFF
command in DiceDB is used to compute the difference between multiple sets. It returns the members of the set resulting from the difference between the first set and all the successive sets. This command is useful when you need to find elements that are unique to the first set compared to other sets.
Syntax
Parameters
Parameter | Description | Type | Required |
---|---|---|---|
key1 | The key of the first set. | String | Yes |
key2..keyN | The keys of the subsequent sets to be compared with the first set. | String | No |
Return Values
The SDIFF
command returns an array of elements that are present in the first set but not in any of the subsequent sets. If the first set does not exist, it is considered an empty set. If none of the sets exist, an empty array is returned.
Condition | Return Value |
---|---|
Elements present in the first set but not in any subsequent sets | Array of elements |
First set does not exist | Empty array |
None of the sets exist | Empty array |
Syntax or specified constraints are invalid | error |
Behaviour
- DiceDB retrieves the set associated with
key1
. - DiceDB retrieves the sets associated with
key2
throughkeyN
. - DiceDB computes the difference by removing elements found in
key2
throughkeyN
from the set found inkey1
. - The resulting set, containing elements unique to
key1
, is returned.
Errors
-
Wrong Type Error
:- Error Message:
(error) WRONGTYPE Operation against a key holding the wrong kind of value
- If any of the keys provided do not hold a set, DiceDB will return an error of type
WRONGTYPE
. This error indicates that the operation against a key holding the wrong kind of value was attempted.
- Error Message:
-
Wrong number of arguments
:- Error Message:
(error) ERR wrong number of arguments for 'sdiff' command
- If no keys are provided, DiceDB will return a syntax error indicating that at least one key must be specified.
- Error Message:
-
Syntax Error
: If no keys are provided, DiceDB will return a syntax error indicating that at least one key must be specified.
Example Usage
Basic Usage
In this example, the difference between set1 and the union of set2 and set3 is computed. The element “b” is unique to set1.
Single Set
In this example, since only one set is provided, the command returns all elements of set1
.
Non-Existent Sets
In this example, since neither set1
nor set2
exist, the command returns an empty array.
Wrong Type Error
In this example, not_a_set
is not a set, so DiceDB returns a WRONGTYPE
error.
Syntax Error
In this example, no keys are provided, so DiceDB returns a syntax error.