neosqlite.collection.expr_evaluator.sql_converters module

SQL converters for expression evaluation.

This module contains the SQL tier conversion methods for the ExprEvaluator. These methods convert MongoDB $expr operators to SQL expressions.

This is designed as a mixin class to be composed into ExprEvaluator.

class neosqlite.collection.expr_evaluator.sql_converters.SqlConvertersMixin[source]

Bases: object

Mixin class providing SQL conversion methods for expression evaluation.

This class is designed to be composed into ExprEvaluator and provides all the _convert_* methods for converting MongoDB operators to SQL.

Required attributes from parent class:
  • data_column: Name of the JSON data column

  • json_function_prefix: ‘json’ or ‘jsonb’ based on support

  • json_each_function: ‘json_each’ or ‘jsonb_each’

  • json_group_array_function: ‘json_group_array’ or ‘jsonb_group_array’

  • _jsonb_supported: Boolean indicating JSONB support

  • _log2_warned: Boolean tracking if $log2 warning was issued

data_column: str
_jsonb_supported: bool
_log2_warned: bool
_current_context: AggregationContext | None
_convert_expr_to_sql(expr: dict[str, Any]) tuple[str, list[Any]][source]

Convert a $expr expression to SQL.

Parameters:

expr – Expression dictionary

Returns:

Tuple of (SQL expression, parameters)

Raises:
  • NotImplementedError – If operator is not supported in SQL

  • ValueError – If expression structure is invalid

_convert_logical_operator(operator: str, operands: Any) tuple[str, list[Any]][source]

Convert logical operators ($and, $or, $not, $nor) to SQL.

_convert_comparison_operator(operator: str, operands: list[Any]) tuple[str, list[Any]][source]

Convert comparison operators to SQL.

_convert_cmp_operator(operands: list[Any]) tuple[str, list[Any]][source]

Convert $cmp operator to SQL CASE statement.

_convert_arithmetic_operator(operator: str, operands: list[Any]) tuple[str, list[Any]][source]

Convert arithmetic operators to SQL.

_convert_cond_operator(operands: dict[str, Any]) tuple[str, list[Any]][source]

Convert $cond operator to SQL CASE statement.

_convert_ifNull_operator(operands: list[Any]) tuple[str, list[Any]][source]

Convert $ifNull operator to SQL COALESCE.

_convert_array_operator(operator: str, operands: Any) tuple[str, list[Any]][source]

Convert array operators to SQL.

_convert_set_operator(operator: str, operands: Any) tuple[str, list[Any]][source]

Convert set operators to SQL using json_each.

_build_pattern_with_options(regex: str, options: str) str[source]

Build regex pattern with inline flags.

_convert_string_operator(operator: str, operands: list[Any]) tuple[str, list[Any]][source]

Convert string operators to SQL.

_convert_math_operator(operator: str, operands: Any) tuple[str, list[Any]][source]

Convert math operators to SQL.

_convert_trig_operator(operator: str, operands: Any) tuple[str, list[Any]][source]

Convert trigonometric and hyperbolic operators to SQL.

Parameters:
  • operator – The trig operator ($sin, $cos, etc.)

  • operands – The operand(s). Can be: - A single value (string, number) for simple cases like {“$sin”: “$angle”} - A list of values for array format like {“$sin”: [“$angle”]}

_convert_angle_operator(operator: str, operands: Any) tuple[str, list[Any]][source]

Convert angle conversion operators to SQL.

_convert_date_operator(operator: str, operands: Any) tuple[str, list[Any]][source]

Convert date operators to SQL using strftime.

_convert_date_arithmetic_operator(operator: str, operands: list[Any]) tuple[str, list[Any]][source]

Convert $dateAdd/$dateSubtract operators to SQL.

MongoDB syntax: {$dateAdd: [date, amount, unit]} or

{$dateAdd: {startDate: date, amount: N, unit: “day”}}

SQLite: datetime(date, ‘+N unit’ or ‘-N unit’)

_convert_date_diff_operator(operands: list[Any]) tuple[str, list[Any]][source]

Convert $dateDiff operator to SQL.

MongoDB syntax: {$dateDiff: [date1, date2, unit]} or

{$dateDiff: {startDate: date1, endDate: date2, unit: “day”}}

SQLite: julianday(date2) - julianday(date1) for days

_convert_object_operator(operator: str, operands: Any) tuple[str, list[Any]][source]

Convert object operators to SQL.

Note: json_patch() works with both JSON and JSONB data types. Only json_extract/jsonb_extract, json_set/jsonb_set have JSONB variants.

_convert_let_operator(operands: Any) tuple[str, list[Any]][source]

Convert $let operator to SQL by inlining variables.

MongoDB syntax: { $let: { vars: { <var1>: <expr1>, … }, in: <expr> } }

_convert_data_size_operator(operator: str, operands: Any) tuple[str, list[Any]][source]

Convert $binarySize and $bsonSize operators to SQL.

_get_operator_return_type(operator: str) str | None[source]

Infer the BSON return type of a MongoDB operator.

Returns:

BSON type name (e.g., ‘number’, ‘bool’, ‘string’, ‘array’, ‘object’) or None if the return type is ambiguous or unknown.

_get_literal_bson_type(value: Any) str | None[source]

Get the BSON type name for a literal value.

_convert_type_operator(operator: str, operands: Any) tuple[str, list[Any]][source]

Convert type conversion operators to SQL.

_convert_operand_to_sql(operand: Any) tuple[str, list[Any]][source]

Convert an operand to SQL expression.

Handles: - Field references: “$field” → json_extract/jsonb_extract expression - Literals: numbers, strings, booleans - Nested expressions: {“$operator”: […]}

_map_comparison_operator(op: str) str[source]

Map MongoDB comparison operators to SQL.

_map_arithmetic_operator(op: str) str[source]

Map MongoDB arithmetic operators to SQL.