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
- static _select_aliases(select_parts: list[str]) list[str][source]¶
Extract output aliases from ‘expression AS alias’ fragments.
The grouped SELECT is wrapped in an outer projection (#104), so json_object must reference the inner ALIASES, not re-evaluate the aggregate expressions (which would need the source data column).
- _ranked_group_query(current_table: str, select_parts: list[str], group_by_parts: list[str], json_args: str, json_output_func: str) tuple[str, list][source]¶
Build the $group query when $first/$last are present (#105).
A ranked CTE assigns per-group row numbers by insertion order (id), partitioned null-safely on the group key; $first/$last become MAX(CASE WHEN rn/rd = 1 …) aggregates over that ranking.
- static _grouped_source_sql(current_table: str, select_parts: list[str], group_by_clause: str) str[source]¶
FROM clause for the grouped output (#104).
With a GROUP BY, empty input naturally yields zero groups. A constant-key group (no GROUP BY) is a bare aggregate that always emits one row in SQLite — even over zero rows — so it computes COUNT(*) alongside and filters it out.
- _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> }, … }
}
}