Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: add SORT_RO command. #1528

Merged
merged 15 commits into from
Jul 4, 2024
50 changes: 50 additions & 0 deletions python/python/glide/async_commands/cluster_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ async def sort(
) -> List[bytes]:
"""
Sorts the elements in the list, set, or sorted set at `key` and returns the result.
This command is routed to primary nodes only.
To store the result into a new key, see `sort_store`.

By default, sorting is numeric, and elements are compared by their value interpreted as double precision floating point numbers.
Expand Down Expand Up @@ -728,6 +729,55 @@ async def sort(
result = await self._execute_command(RequestType.Sort, args)
return cast(List[bytes], result)

async def sort_ro(
self,
key: TEncodable,
limit: Optional[Limit] = None,
order: Optional[OrderBy] = None,
alpha: Optional[bool] = None,
) -> List[bytes]:
"""
Sorts the elements in the list, set, or sorted set at `key` and returns the result.
The `sort_ro` command can be used to sort elements based on different criteria and apply transformations on sorted elements.
This command is routed depending on the client's `ReadFrom` strategy.
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved

GilboaAWS marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider mentioning what is the difference between sort and sort_ro in the client (not in redis)

Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
By default, sorting is numeric, and elements are compared by their value interpreted as double precision floating point numbers.

See https://valkey.io/commands/sort for more details.

Args:
key (TEncodable): The key of the list, set, or sorted set to be sorted.
limit (Optional[Limit]): Limiting the range of the query by setting offset and result count. See `Limit` class for more information.
order (Optional[OrderBy]): Specifies the order to sort the elements.
Can be `OrderBy.ASC` (ascending) or `OrderBy.DESC` (descending).
alpha (Optional[bool]): When `True`, sorts elements lexicographically. When `False` (default), sorts elements numerically.
Use this when the list, set, or sorted set contains string values that cannot be converted into double precision floating point numbers.

Returns:
List[bytes]: A list of sorted elements.

Examples:
>>> await client.lpush("mylist", '3', '1', '2')
>>> await client.sort_ro("mylist")
[b'1', b'2', b'3']

>>> await client.sort_ro("mylist", order=OrderBy.DESC)
[b'3', b'2', b'1']

>>> await client.lpush("mylist", '2', '1', '2', '3', '3', '1')
>>> await client.sort_ro("mylist", limit=Limit(2, 3))
[b'1', b'2', b'2']

>>> await client.lpush("mylist", "a", "b", "c", "d")
>>> await client.sort_ro("mylist", limit=Limit(2, 2), order=OrderBy.DESC, alpha=True)
[b'b', b'a']

Since: Redis version 7.0.0.
"""
args = _build_sort_args(key, None, limit, None, order, alpha)
result = await self._execute_command(RequestType.SortReadOnly, args)
return cast(List[bytes], result)

async def sort_store(
self,
key: TEncodable,
Expand Down
66 changes: 66 additions & 0 deletions python/python/glide/async_commands/standalone_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ async def sort(
"""
Sorts the elements in the list, set, or sorted set at `key` and returns the result.
The `sort` command can be used to sort elements based on different criteria and apply transformations on sorted elements.
This command is routed to primary nodes only.
To store the result into a new key, see `sort_store`.

See https://valkey.io/commands/sort for more details.
Expand Down Expand Up @@ -538,6 +539,71 @@ async def sort(
result = await self._execute_command(RequestType.Sort, args)
return cast(List[Optional[bytes]], result)

async def sort_ro(
self,
key: TEncodable,
by_pattern: Optional[TEncodable] = None,
limit: Optional[Limit] = None,
get_patterns: Optional[List[TEncodable]] = None,
order: Optional[OrderBy] = None,
alpha: Optional[bool] = None,
) -> List[Optional[bytes]]:
"""
Sorts the elements in the list, set, or sorted set at `key` and returns the result.
The `sort_ro` command can be used to sort elements based on different criteria and apply transformations on sorted elements.
This command is routed depending on the client's `ReadFrom` strategy.
GilboaAWS marked this conversation as resolved.
Show resolved Hide resolved

See https://valkey.io/commands/sort for more details.

GilboaAWS marked this conversation as resolved.
Show resolved Hide resolved
Args:
key (TEncodable): The key of the list, set, or sorted set to be sorted.
by_pattern (Optional[TEncodable]): A pattern to sort by external keys instead of by the elements stored at the key themselves.
The pattern should contain an asterisk (*) as a placeholder for the element values, where the value
from the key replaces the asterisk to create the key name. For example, if `key` contains IDs of objects,
`by_pattern` can be used to sort these IDs based on an attribute of the objects, like their weights or
timestamps.
E.g., if `by_pattern` is `weight_*`, the command will sort the elements by the values of the
keys `weight_<element>`.
If not provided, elements are sorted by their value.
limit (Optional[Limit]): Limiting the range of the query by setting offset and result count. See `Limit` class for more information.
get_pattern (Optional[TEncodable]): A pattern used to retrieve external keys' values, instead of the elements at `key`.
The pattern should contain an asterisk (*) as a placeholder for the element values, where the value
from `key` replaces the asterisk to create the key name. This allows the sorted elements to be
transformed based on the related keys values. For example, if `key` contains IDs of users, `get_pattern`
can be used to retrieve specific attributes of these users, such as their names or email addresses.
E.g., if `get_pattern` is `name_*`, the command will return the values of the keys `name_<element>`
for each sorted element. Multiple `get_pattern` arguments can be provided to retrieve multiple attributes.
The special value `#` can be used to include the actual element from `key` being sorted.
If not provided, only the sorted elements themselves are returned.
order (Optional[OrderBy]): Specifies the order to sort the elements.
Can be `OrderBy.ASC` (ascending) or `OrderBy.DESC` (descending).
alpha (Optional[bool]): When `True`, sorts elements lexicographically. When `False` (default), sorts elements numerically.
Use this when the list, set, or sorted set contains string values that cannot be converted into double precision floating point

Returns:
List[Optional[bytes]]: Returns a list of sorted elements.

Examples:
>>> await client.lpush("mylist", 3, 1, 2)
>>> await client.sort_ro("mylist")
[b'1', b'2', b'3']
>>> await client.sort_ro("mylist", order=OrderBy.DESC)
[b'3', b'2', b'1']
>>> await client.lpush("mylist2", 2, 1, 2, 3, 3, 1)
>>> await client.sort_ro("mylist2", limit=Limit(2, 3))
[b'2', b'2', b'3']
>>> await client.hset("user:1", "name", "Alice", "age", 30)
>>> await client.hset("user:2", "name", "Bob", "age", 25)
>>> await client.lpush("user_ids", 2, 1)
>>> await client.sort_ro("user_ids", by_pattern="user:*->age", get_patterns=["user:*->name"])
[b'Bob', b'Alice']

Since: Redis version 7.0.0.
"""
args = _build_sort_args(key, by_pattern, limit, get_patterns, order, alpha)
result = await self._execute_command(RequestType.SortReadOnly, args)
return cast(List[Optional[bytes]], result)

async def sort_store(
self,
key: TEncodable,
Expand Down
80 changes: 80 additions & 0 deletions python/python/glide/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -4722,6 +4722,55 @@ def sort(
args = _build_sort_args(key, by_pattern, limit, get_patterns, order, alpha)
return self.append_command(RequestType.Sort, args)

def sort_ro(
self: TTransaction,
key: TEncodable,
by_pattern: Optional[TEncodable] = None,
limit: Optional[Limit] = None,
get_patterns: Optional[List[TEncodable]] = None,
order: Optional[OrderBy] = None,
alpha: Optional[bool] = None,
) -> TTransaction:
"""
Sorts the elements in the list, set, or sorted set at `key` and returns the result.
The `sort_ro` command can be used to sort elements based on different criteria and apply transformations on sorted elements.
This command is routed depending on the client's `ReadFrom` strategy.

GilboaAWS marked this conversation as resolved.
Show resolved Hide resolved
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
See https://valkey.io/commands/sort for more details.

Args:
key (TEncodable): The key of the list, set, or sorted set to be sorted.
by_pattern (Optional[TEncodable]): A pattern to sort by external keys instead of by the elements stored at the key themselves.
The pattern should contain an asterisk (*) as a placeholder for the element values, where the value
from the key replaces the asterisk to create the key name. For example, if `key` contains IDs of objects,
`by_pattern` can be used to sort these IDs based on an attribute of the objects, like their weights or
timestamps.
E.g., if `by_pattern` is `weight_*`, the command will sort the elements by the values of the
keys `weight_<element>`.
If not provided, elements are sorted by their value.
limit (Optional[Limit]): Limiting the range of the query by setting offset and result count. See `Limit` class for more information.
get_pattern (Optional[TEncodable]): A pattern used to retrieve external keys' values, instead of the elements at `key`.
The pattern should contain an asterisk (*) as a placeholder for the element values, where the value
from `key` replaces the asterisk to create the key name. This allows the sorted elements to be
transformed based on the related keys values. For example, if `key` contains IDs of users, `get_pattern`
can be used to retrieve specific attributes of these users, such as their names or email addresses.
E.g., if `get_pattern` is `name_*`, the command will return the values of the keys `name_<element>`
for each sorted element. Multiple `get_pattern` arguments can be provided to retrieve multiple attributes.
The special value `#` can be used to include the actual element from `key` being sorted.
If not provided, only the sorted elements themselves are returned.
order (Optional[OrderBy]): Specifies the order to sort the elements.
Can be `OrderBy.ASC` (ascending) or `OrderBy.DESC` (descending).
alpha (Optional[bool]): When `True`, sorts elements lexicographically. When `False` (default), sorts elements numerically.
Use this when the list, set, or sorted set contains string values that cannot be converted into double precision floating point numbers.

Command response:
List[Optional[bytes]]: Returns a list of sorted elements.

Since: Redis version 7.0.0.
"""
args = _build_sort_args(key, by_pattern, limit, get_patterns, order, alpha)
return self.append_command(RequestType.SortReadOnly, args)

def sort_store(
self: TTransaction,
key: TEncodable,
Expand Down Expand Up @@ -4843,6 +4892,7 @@ def sort(
) -> TTransaction:
"""
Sorts the elements in the list, set, or sorted set at `key` and returns the result.
This command is routed to primary only.
To store the result into a new key, see `sort_store`.

See https://valkey.io/commands/sort for more details.
Expand All @@ -4861,6 +4911,36 @@ def sort(
args = _build_sort_args(key, None, limit, None, order, alpha)
return self.append_command(RequestType.Sort, args)

def sort_ro(
self: TTransaction,
key: TEncodable,
limit: Optional[Limit] = None,
order: Optional[OrderBy] = None,
alpha: Optional[bool] = None,
) -> TTransaction:
"""
Sorts the elements in the list, set, or sorted set at `key` and returns the result.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Sorts the elements in the list, set, or sorted set at `key` and returns the result.
Sorts the elements in the list, set, or sorted set at `key` and returns the result.
The `sort_ro` command can be used to sort elements based on different criteria and apply transformations on sorted elements.

The `sort_ro` command can be used to sort elements based on different criteria and apply transformations on sorted elements.
This command is routed depending on the client's `ReadFrom` strategy.
GilboaAWS marked this conversation as resolved.
Show resolved Hide resolved

See https://valkey.io/commands/sort for more details.

Args:
key (TEncodable): The key of the list, set, or sorted set to be sorted.
limit (Optional[Limit]): Limiting the range of the query by setting offset and result count. See `Limit` class for more information.
order (Optional[OrderBy]): Specifies the order to sort the elements.
Can be `OrderBy.ASC` (ascending) or `OrderBy.DESC` (descending).
alpha (Optional[bool]): When `True`, sorts elements lexicographically. When `False` (default), sorts elements numerically.
Use this when the list, set, or sorted set contains string values that cannot be converted into double precision floating point numbers.

Command response:
List[bytes]: A list of sorted elements.

Since: Redis version 7.0.0.
"""
args = _build_sort_args(key, None, limit, None, order, alpha)
return self.append_command(RequestType.SortReadOnly, args)

def sort_store(
self: TTransaction,
key: TEncodable,
Expand Down
Loading
Loading