neosqlite.collection.temporary_table_aggregation.operators_group module

class neosqlite.collection.temporary_table_aggregation.operators_group.OperatorsGroupMixin[source]

Bases: OperatorsBaseMixin

_process_group_stage(create_temp: Callable, current_table: str, group_spec: dict[str, Any]) str[source]

Process a $group stage using temporary tables.

This method implements the $group aggregation stage which groups documents by a specified key and performs accumulator operations.

Supports these accumulators in SQL tier: - $sum, $avg, $min, $max: Standard SQL aggregators - $count: COUNT(*) - $first, $last: Using subqueries (LIMITATION: requires no preceding $sort) - $addToSet: Using json_group_array(DISTINCT …) - $push: Using json_group_array(…) - Expression keys: Using SQLTranslator for expression evaluation

Limitation: - $first/$last with preceding $sort stage falls back to Python for correctness.

The current implementation uses correlated subqueries that don’t preserve sort order across groups.

Parameters:
  • create_temp (Callable) – Function to create temporary tables

  • current_table (str) – Name of the current temporary table containing input data

  • group_spec (dict[str, Any]) – The $group stage specification

Returns:

Name of the newly created temporary table with grouped results

Return type:

str

_id_to_json_object_args(select_parts: list[str]) str[source]

Convert SELECT parts to json_object arguments.

Parameters:

select_parts – List of SELECT column expressions (e.g., [“expr1 AS field1”, “expr2 AS field2”])

Returns:

Comma-separated list of ‘key’, value pairs for json_object

_get_results_from_table(table_name: str, is_count: bool = False, count_field: str | None = None, batch_size: int = 101) list[dict[str, Any]][source]

Get results from a temporary table.

This method retrieves all documents from a temporary table and converts them back into their Python dictionary representation using the collection’s document loading mechanism.

For $count optimization, if is_count is True, it returns a single document with the count from the table using SQL COUNT(*) instead of loading all documents.

Parameters:
  • table_name (str) – Name of the temporary table to retrieve results from

  • is_count (bool) – If True, return count document instead of all documents

  • count_field (str | None) – The field name for the count if is_count is True

Returns:

List of documents retrieved from the temporary table,

with each document represented as a dictionary

Return type:

list[dict[str, Any]]

_process_bucket_stage(create_temp, current_table, bucket_spec)[source]

Process $bucket stage - groups documents by boundaries.

MongoDB syntax: {

$bucket: {

groupBy: <expression>, boundaries: [<lowerbound1>, <lowerbound2>, …], default: <literal>, // optional output: { <output1>: { <$accumulator expression> }, … }

}

}

_build_group_by_expr(group_by)[source]

Build SQL expression for groupBy field.

_process_bucket_auto_stage(create_temp, current_table, bucket_auto_spec)[source]

Process $bucketAuto stage - auto-sized buckets.

MongoDB syntax: {

$bucketAuto: {

groupBy: <expression>, buckets: <number>, output: { <output1>: { <$accumulator expression> }, … }, granularity: <string> // optional

}

}