Use filters
Use Tablestore SDK for Python to filter rows by attribute column values on the server and reduce the amount of data returned to the client.
Prerequisites
Install the Tablestore SDK for Python and initialize a client.
Function description
Filters are applied after the server reads data. Only rows that meet the conditions are returned to the client. Filtering does not reduce the number of rows read by the server or the read CUs consumed. Use filters after you narrow the primary key range.
The following filter conditions are supported:
SingleColumnConditioncompares the value of one attribute column.SingleColumnRegexConditionextracts a substring from a string attribute column and compares the extracted value.CompositeColumnConditioncombines up to 32 conditions withAND,OR, orNOT.
The following example returns only rows whose latest value in the status attribute column is online during a range read.
column_filter = SingleColumnCondition(
"status",
"online",
ComparatorType.EQUAL,
pass_if_missing=False,
)
start_primary_key = [("partition", "device"), ("id", INF_MIN)]
end_primary_key = [("partition", "device"), ("id", INF_MAX)]
consumed, next_start_primary_key, rows, next_token = client.get_range(
"example_table",
Direction.FORWARD,
start_primary_key,
end_primary_key,
column_filter=column_filter,
)
Parameters
Single-column value condition
Call SingleColumnCondition(column_name, column_value, comparator, pass_if_missing=True, latest_version_only=True) to create a single-column value condition.
|
Name |
Type |
Description |
|
column_name (required) |
|
The name of the attribute column to evaluate. |
|
column_value (required) |
|
The target value. Its type must match the attribute column value type. |
|
comparator (required) |
|
The comparison operator. Valid values are |
|
pass_if_missing (optional) |
|
Specifies whether a row passes the filter if the target attribute column is missing. Default value: |
|
latest_version_only (optional) |
|
Specifies whether to evaluate only the latest version. Default value: |
Regular expression condition
Call SingleColumnRegexCondition(column_name, comparator, column_value=None, regex_rule=None, latest_version_only=True) to create a regular expression condition. Only string attribute columns support regular expression filtering.
|
Name |
Type |
Description |
|
column_name (required) |
|
The name of the string attribute column to evaluate. |
|
comparator (required) |
|
The comparison operator. Valid values are |
|
column_value (optional) |
|
The target value. Omit this parameter if |
|
regex_rule (optional) |
|
The substring extraction and type conversion rule. |
|
latest_version_only (optional) |
|
Specifies whether to evaluate only the latest version. Default value: |
If the target attribute column is missing, a row never passes a regular expression filter. pass_if_missing cannot be configured separately for this condition.
Regular expression rule
The regex_rule parameter is of the RegexRule type and contains the following parameters.
|
Name |
Type |
Description |
|
regex_input (required) |
|
The regular expression, up to 256 bytes. Perl-compatible expressions and single-byte characters are supported. Chinese characters are not supported. If the expression contains capturing groups, the first group is extracted. Otherwise, the entire match is extracted. |
|
cast_type (required) |
|
The type to which the extracted value is converted. Valid values are |
Composite condition
Call CompositeColumnCondition(combinator) to create a composite condition, and then call add_sub_condition to add conditions.
|
Name |
Type |
Description |
|
combinator (required) |
|
The logical operator. Valid values are |
|
sub_conditions (required) |
|
The conditions to combine. Single-column value conditions, regular expression conditions, and nested composite conditions are supported, with a maximum of 32 conditions. |
Examples
Filter with a regular expression
The following example extracts the number from values such as user_004 in the account attribute column, converts the number to an integer, and returns only rows whose extracted value is greater than 3.
regex_rule = RegexRule(r"user_(\d+)", CastType.VT_INTEGER)
column_filter = SingleColumnRegexCondition(
"account",
ComparatorType.GREATER_THAN,
3,
regex_rule,
)
consumed, next_start_primary_key, rows, next_token = client.get_range(
"example_table",
Direction.FORWARD,
start_primary_key,
end_primary_key,
column_filter=column_filter,
)
Combine filters
The following example constructs the filter (category == "sensor" OR category == "gateway") AND score > 3.
category_filter = CompositeColumnCondition(LogicalOperator.OR)
category_filter.add_sub_condition(
SingleColumnCondition("category", "sensor", ComparatorType.EQUAL)
)
category_filter.add_sub_condition(
SingleColumnCondition("category", "gateway", ComparatorType.EQUAL)
)
column_filter = CompositeColumnCondition(LogicalOperator.AND)
column_filter.add_sub_condition(category_filter)
column_filter.add_sub_condition(
SingleColumnCondition("score", 3, ComparatorType.GREATER_THAN)
)
Evaluate historical versions
The following example sets latest_version_only to False. A row is returned if any version of the status attribute column is online. Rows without the status column are not returned.
column_filter = SingleColumnCondition(
"status",
"online",
ComparatorType.EQUAL,
pass_if_missing=False,
latest_version_only=False,
)