neosqlite.collection.expr_evaluator.python_evaluators module

Python evaluation methods for NeoSQLite $expr operator.

This module contains the PythonEvaluatorsMixin class which provides all the _evaluate_*_python methods for evaluating MongoDB $expr expressions in Python as a fallback when SQL evaluation is not possible.

class neosqlite.collection.expr_evaluator.python_evaluators.PythonEvaluatorsMixin[source]

Bases: object

Mixin class providing Python evaluation methods for $expr expressions.

This mixin provides fallback evaluation capabilities when SQL-based evaluation (Tier 1 and Tier 2) is not possible or when the kill switch is activated.

_log2_warned: bool
evaluate_python(expr: dict[str, Any], document: dict[str, Any]) bool[source]

Python fallback evaluation for $expr.

This ensures identical results to SQL evaluation and provides the kill switch functionality.

Parameters:
  • expr – The $expr expression

  • document – Document to evaluate against

Returns:

Boolean result of expression evaluation

_evaluate_expr_python(expr: dict[str, Any], document: dict[str, Any]) Any[source]

Recursively evaluate expression in Python.

_evaluate_logical_python(operator: str, operands: list[Any], document: dict[str, Any]) bool[source]

Evaluate logical operators in Python.

_evaluate_comparison_python(operator: str, operands: list[Any], document: dict[str, Any]) bool[source]

Evaluate comparison operators in Python.

_evaluate_cmp_python(operands: list[Any], document: dict[str, Any]) int[source]

Evaluate $cmp operator in Python.

_evaluate_arithmetic_python(operator: str, operands: list[Any], document: dict[str, Any]) float | None[source]

Evaluate arithmetic operators in Python.

Note: In MongoDB, arithmetic operations with null return null.

_evaluate_math_python(operator: str, operands: list[Any], document: dict[str, Any]) float | None[source]

Evaluate math operators in Python.

_evaluate_pow_python(operands: list[Any], document: dict[str, Any]) float | None[source]

Evaluate $pow operator in Python.

_evaluate_sqrt_python(operands: list[Any], document: dict[str, Any]) float | None[source]

Evaluate $sqrt operator in Python.

_evaluate_trig_python(operator: str, operands: list[Any], document: dict[str, Any]) float | None[source]

Evaluate trigonometric operators in Python.

_evaluate_angle_python(operator: str, operands: Any, document: dict[str, Any]) float | None[source]

Evaluate angle conversion operators in Python.

_evaluate_cond_python(operands: dict[str, Any], document: dict[str, Any]) Any[source]

Evaluate $cond operator in Python.

_evaluate_ifNull_python(operands: list[Any], document: dict[str, Any]) Any[source]

Evaluate $ifNull operator in Python.

_evaluate_switch_python(operands: dict[str, Any], document: dict[str, Any]) Any[source]

Evaluate $switch operator in Python.

_evaluate_array_python(operator: str, operands: list[Any], document: dict[str, Any]) Any[source]

Evaluate array operators in Python.

_evaluate_array_transform_python(operator: str, operands: Any, document: dict[str, Any]) Any[source]

Evaluate $filter, $map, $reduce operators in Python.

These operators use variable scoping: - $filter: {input: <array>, as: <var>, cond: <expr>} - $map: {input: <array>, as: <var>, in: <expr>} - $reduce: {input: <array>, initialValue: <val>, in: <expr>}

_evaluate_string_python(operator: str, operands: Any, document: dict[str, Any]) Any[source]

Evaluate string operators in Python.

Parameters:
  • operator – The string operator ($toUpper, $toLower, etc.)

  • operands – The operand(s). Can be: - A single value for simple cases like {“$toUpper”: “$field”} - A list of values for array format - A dict for operators like $trim, $regexMatch

  • document – The document to evaluate against

_evaluate_date_python(operator: str, operands: list[Any], document: dict[str, Any]) int | None[source]

Evaluate date operators in Python.

MongoDB compatibility: Date operators require the field to be stored as BSON Date/datetime type. String dates are NOT automatically converted, matching MongoDB’s behavior.

_evaluate_date_arithmetic_python(operator: str, operands: list[Any], document: dict[str, Any]) Any[source]

Evaluate $dateAdd, $dateSubtract, $dateDiff operators in Python.

_evaluate_object_python(operator: str, operands: Any, document: dict[str, Any]) Any[source]

Evaluate object operators in Python.

_evaluate_data_size_python(operator: str, operands: Any, document: dict[str, Any]) int[source]

Evaluate data size operators ($binarySize, $bsonSize) in Python.

_evaluate_type_python(operator: str, operands: list[Any], document: dict[str, Any]) Any[source]

Evaluate type conversion operators in Python.

static _convert_to_int(value: Any) Any[source]

Convert value to int.

static _convert_to_long(value: Any) Any[source]

Convert value to long (64-bit int).

static _convert_to_double(value: Any) Any[source]

Convert value to double (float).

static _convert_to_decimal(value: Any) Any[source]

Convert value to decimal (float, as SQLite lacks Decimal128).

static _convert_to_string(value: Any) Any[source]

Convert value to string.

static _convert_to_bool(value: Any) Any[source]

Convert value to bool.

static _convert_to_objectid(value: Any) Any[source]

Convert value to ObjectId.

static _convert_to_bindata(value: Any) Any[source]

Convert value to Binary (binData).

static _convert_to_bsonbindata(value: Any) Any[source]

Convert value to Binary (bsonBinData).

static _convert_to_regex(value: Any) Any[source]

Convert value to regex pattern.

static _convert_to_bsonregex(value: Any) Any[source]

Convert value to regex pattern (bsonRegex).

static _convert_to_date(value: Any) Any[source]

Convert value to date.

static _convert_to_null(value: Any) None[source]

Convert any value to None.

_get_bson_type(value: Any) str[source]

Get BSON type name for a value.

_evaluate_literal_python(operands: Any, document: dict[str, Any]) Any[source]

Evaluate $literal operator in Python.

_evaluate_operand_python(operand: Any, document: dict[str, Any]) Any[source]

Evaluate an operand in Python context.