githubEdit

Expression Filters

Expression filter reference for langchain-mariadb, documenting the operator enum, filter builder classes, and MariaDBFilterExpressionConverter for metadata-based vector queries.

Version: langchain-mariadb v0.0.21

A flexible and composable filter expression system for building SQL-like queries.

This module provides a builder pattern implementation for creating complex filter expressions that can be converted to various query formats.

Example:

# Dict filter value
filter = {
   '$or': [{'status': {'$eq': 'active'}}, {'status': {'$eq': 'pending'}}],
   'age': {'$gte': 18}
   'country': {'$in': ['US', 'CA', 'UK']}
}

# Convert to SQL-like string (with a proper converter implementation)
converter = SQLFilterExpressionConverter()  # Some converter
sql_where = converter.convert_expression(filter)
print(sql_where)
# Output:
# (status = 'active' OR status = 'pending')
# AND age >= 18 AND country IN ['US','CA','UK']

Operator

Enumeration of supported filter operations

Attributes

  • AND:

  • OR:

  • EQ:

  • NE:

  • GT:

  • GTE:

  • LT:

  • LTE:

  • LIKE:

  • NLIKE:

  • IN:

  • NIN:

  • NOT:


Key

Represents a key in a filter expression

Constructor

Attributes

  • key:


Value

Represents a value in a filter expression

Constructor

Attributes

  • value:


Expression

Represents a boolean filter expression with a specific structure.

Constructor

Attributes

  • type:

  • left:

  • right:


Group

Represents a grouped collection of filter expressions.

Constructor

Attributes

  • content:


StringBuilder

Simple StringBuilder implementation for efficient string concatenation

Constructor

Methods

append

Attributes

  • buffer (List[str]):


FilterExpressionConverter

Abstract base class defining the interface for converting filter expressions into various string-based query representations

Methods

convert_expression

Convert a complete expression into its string representation

convert_symbol_to_context

Determine the appropriate operation symbol for a given expression

convert_operand_to_context

Convert an operand into a string representation within a given context

convert_expression_to_context

Convert an expression to its string representation in the given context

convert_key_to_context

Convert a key to its string representation in the given context

convert_value_to_context

Convert a value to its string representation in the given context

convert_single_value_to_context

Convert a single value to its string representation in the given context

write_group_start

Write the start of a group in the given context

write_group_end

Write the end of a group in the given context

write_value_range_start

Write the start of a value range in the given context

write_value_range_end

Write the end of a value range in the given context

write_value_range_separator

Write the separator between values in a range in the given context


BaseFilterExpressionConverter

Base implementation of the FilterExpressionConverter interface providing common functionality for converting filter expressions to string representations

Methods

convert_expression

convert_symbol_to_context

convert_operand_to_context

convert_value_to_context

convert_single_value_to_context

write_value_range_start

write_value_range_end

write_value_range_separator


MariaDBFilterExpressionConverter

Converter for MariaDB filter expressions.

Constructor

Methods

convert_expression_to_context

convert_key_to_context

write_value_range_start

write_value_range_end

write_group_start

write_group_end

Attributes

  • metadata_field_name:


eq

Check if a key equals a value.

Parameters:

  • key (str): The metadata field name to filter on

  • value (ValueType): The value to compare against

Returns:

Expression - Expression representing key == value

ne

Check if a key does not equal a value.

Parameters:

  • key (str): The metadata field name to filter on

  • value (ValueType): The value to compare against

Returns:

Expression - Expression representing key != value

gt

Check if a key is greater than a value.

Parameters:

  • key (str): The metadata field name to filter on

  • value (Union[int, str, float]): The numeric or string value to compare against

Returns:

Expression - Expression representing key > value

gte

Check if a key is greater than or equal to a value.

Parameters:

  • key (str): The metadata field name to filter on

  • value (Union[int, str, float]): The numeric or string value to compare against

Returns:

Expression - Expression representing key >= value

lt

Check if a key is less than a value.

Parameters:

  • key (str): The metadata field name to filter on

  • value (Union[int, str, float]): The numeric or string value to compare against

Returns:

Expression - Expression representing key < value

lte

Check if a key is less than or equal to a value.

Parameters:

  • key (str): The metadata field name to filter on

  • value (Union[int, str, float]): The numeric or string value to compare against

Returns:

Expression - Expression representing key <= value

like

Check if a key matches a pattern (SQL LIKE).

Parameters:

  • key (str): The metadata field name to filter on

  • value (Union[int, str, float]): The pattern to match (use % as wildcard)

Returns:

Expression - Expression representing key LIKE value

nlike

Check if a key does not match a pattern (SQL NOT LIKE).

Parameters:

  • key (str): The metadata field name to filter on

  • value (Union[int, str, float]): The pattern to not match (use % as wildcard)

Returns:

Expression - Expression representing key NOT LIKE value

includes

Check if a key's value is in a list of values (SQL IN operator).

Parameters:

  • key (str): The metadata field name to filter on

  • values (Union[List[int], List[str], List[bool], List[float]]): List of values to check against

Returns:

Expression - Expression representing key IN values

excludes

Check if a key's value is not in a list of values (SQL NOT IN operator).

Parameters:

  • key (str): The metadata field name to filter on

  • values (Union[List[int], List[str], List[bool], List[float]]): List of values to exclude

Returns:

Expression - Expression representing key NOT IN values

both

Combine two expressions with logical AND.

Parameters:

  • left (Operand): First expression

  • right (Operand): Second expression

Returns:

Expression - Expression representing (left AND right)

either

Combine two expressions with logical OR.

Parameters:

  • left (Operand): First expression

  • right (Operand): Second expression

Returns:

Expression - Expression representing (left OR right)

negate

Negate an expression with logical NOT.

Parameters:

  • content (Expression): Expression to negate

Returns:

Expression - Expression representing NOT content

group

Group an expression with parentheses for precedence control.

Parameters:

  • content (Expression): Expression to group

Returns:

Group - Group wrapping the expression

Last updated

Was this helpful?