All Products
Search
Document Center

Tablestore:Use a filter

Last Updated:Sep 18, 2024

Tablestore filters query results on the server and then returns the filtered results. Only the rows that meet the filter conditions are returned.

Prerequisites

Methods

When you call the GetRow, BatchGetRow, or GetRange operation to query data, you can use a filter to return only the rows that meet the filter conditions.

Filters include SingleColumnValueFilter and CompositeColumnValueFilter.

  • SingleColumnValueFilter: determines whether to return a row based only on the value of a reference column.

  • CompositeColumnValueFilter: determines whether to return a row based on a combination of filter conditions for values of multiple reference columns.

Note

For more information about filters, see Configure a filter.

Usage notes

  • Filter conditions support the following relational operators: =, !=, >, >=, <, and <=. Filter conditions also support the following logical operators: NOT, AND, and OR. You can specify up to 10 subconditions for a filter condition.

  • The reference columns that are used by a filter must be included in the returned columns. Otherwise, the filter cannot query the values of the reference columns.

  • When you use filters for the GetRow, BatchGetRow, and GetRange operations, the native semantics or limits of these operations are not affected.

    When you use the GetRange operation, up to 5,000 rows or 4 MB of data can be scanned at a time.

    If the scanned data does not match the filter conditions, the rows in the response are empty. However, the next_start_primary_key parameter may not be empty. If the next_start_primary_key parameter is not empty, you can use the parameter value to continue scanning until the NextStartPrimaryKey parameter is empty.

Parameters

You can configure the column_filter parameter to use filters for the GetRow, BatchGetRow, and GetRange operations.

    'column_filter' => <ColumnFilter>
  • SingleColumnValueFilter

    A SingleColumnValueFilter allows you to compare a column with a constant. The comparison between two columns or two constants is not supported.

        [
            'column_name' => '<string>',
            'value' => <ColumnValue>,
            'comparator' => <ComparatorType>,
            'pass_if_missing' => true || false,
            'latest_version_only' => true || false
        ]
            
  • CompositeColumnValueFilter

    A CompositeColumnValueFilter is of a tree structure. It contains the logical_operator inner node and the SingleColumnValueFilter leaf node.

        [
            'logical_operator' => <LogicalOperator>
            'sub_filters' => [
                <ColumnFilter>,
                <ColumnFilter>,
                <ColumnFilter>,
                // other conditions
            ]
        ]
            

Parameter

Description

column_name

The name of the reference column used by the filter.

value

The value of the reference column used by the filter.

Specify this parameter in the format of [Value, Type]. Valid values of Type are ColumnTypeConst::CONST_INTEGER, ColumnTypeConst::CONST_STRING, ColumnTypeConst::CONST_BINARY, ColumnTypeConst::CONST_BOOLEAN, and ColumnTypeConst::CONST_DOUBLE, which respectively specify the INTEGER, STRING (UTF-8 encoded string), BINARY, BOOLEAN, and DOUBLE types. If the type is BINARY, the type must be specified. If the type is not BINARY, you can specify only the value.

If the type is BINARY, you must specify both the type and value.

comparator

The relational operators used by the filter. For more information about relational operators, see ComparatorType.

Valid values: ComparatorTypeConst::CONST_EQUAL, ComparatorTypeConst::CONST_NOT_EQUAL, ComparatorTypeConst::CONST_GREATER_THAN, ComparatorTypeConst::CONST_GREATER_EQUAL, ComparatorTypeConst::CONST_LESS_THAN, and ComparatorTypeConst::CONST_LESS_EQUAL.

logical_operator

The logical operators used by the filter. For information about logical operators, see LogicalOperator.

Valid values: LogicalOperatorConst::CONST_NOT, LogicalOperatorConst::CONST_AND, and LogicalOperatorConst::CONST_OR.

pass_if_missing

Specifies whether to return a row if a reference column does not exist in the row. The value of this parameter is of the Boolean type. The default value is true, which indicates that if a reference column does not exist in a row, the row is returned.

A value of false indicates that if the reference column does not exist in a row, the row is not returned.

latest_version_only

Specifies whether to use only the latest version of data in a reference column for comparison when the reference column contains multiple versions of data. The value of this parameter is of the Boolean type. The default value is true, which indicates that if a reference column contains multiple versions of data, only the latest version of data is used for comparison.

A value of false indicates that if a reference column has multiple versions of data, all versions of data are used for comparison. If one version of data in the reference column meets the filter conditions, the row is returned.

sub_filters

Child nodes can be SingleColumnValueFilter or CompositeColumnValueFilter.

Different logical operators of the inner nodes require different number of child nodes that can be attached.

  • If the logical operator of an inner node is NOT, only one child node can be attached.

  • If the logical operator of an inner node is AND or OR, multiple child nodes can be attached.

Examples

Use SingleColumnValueFilter to filter data

The following sample code shows how to read a row of data from a data table and return the row of data if the value of the Col0 column in the row is 0.

$request = [
    'table_name' => 'MyTable',
    'primary_key' => [ // Specify the primary key. 
        ['PK0', 123],
        ['PK1', 'abc']
    ],
    'max_versions' => 1,                     // Set this parameter to 1 to read data of the latest version. 
    'columns_to_get' => ['Col0']             // Specify the column that you want to read. 
    
    // Specify a filter to return the row if the value of the Col0 column is 0. 
    'column_filter' => [
        'column_name' => 'col0',
        'value' => 0,
        'comparator' => ComparatorTypeConst::CONST_EQUAL,
        'pass_if_missing' => false                         / If the Col0 column does not exist in the row, the row is not returned. 
        'latest_version_only' => true                      // Specify that only the latest version is used for comparison. 
    ]
];
$response = $otsClient->getRow ($request); 

Use CompositeColumnValueFilter to filter data

The following sample code shows how to read a row of data from a data table and return the row of data if the value of the Col0 column in the row is 0 and the value of the Col1 column in the row is smaller than 100.

// Set the composite1 condition to (Col0 == 0) AND (Col1 > 100). 
    $composite1 = [
        'logical_operator' => LogicalOperatorConst::CONST_AND,
        'sub_filters' => [
            [
                'column_name' => 'Col0',
                'value' => 0,
                'comparator' => ComparatorTypeConst::CONST_EQUAL
            ],
            [
                'column_name' => 'Col1',
                'value' => 100,
                'comparator' => ComparatorTypeConst::CONST_GREATER_THAN
            ]
        ]
    ];
    // Set the composite2 condition to ( (Col0 == 0) AND (Col1 > 100) ) OR (Col2 <= 10). 
    $composite2 = [
        'logical_operator' => LogicalOperatorConst::CONST_OR,
        'sub_filters' => [
            $composite1,
            [
                'column_name' => 'Col2',
                'value' => 10,
                'comparator' => ComparatorTypeConst::CONST_LESS_EQUAL
            ]
        ]
    ];  
                    

References

  • If an application needs to use different attribute columns as query conditions to query data, you can specify these attribute columns as the primary key columns of a secondary index to accelerate data queries. For more information, see Secondary Index.

  • If your business requires multi-dimensional queries and data analysis, you can create a search index and specify the required attribute columns as the fields of the search index. Then, you can query and analyze data by using the search index. For example, you can use a search index to perform queries based on non-primary key columns, Boolean queries, and fuzzy queries. You can also use a search index to obtain the maximum and minimum values, collect statistics about the number of rows, and group query results. For more information, see Search index.

  • You can use the SQL query feature to query and analyze data in a table. For more information, see Query data.