"""Shared helpers for sound-recording stream logic modules.""" from collections import defaultdict from typing import Any, Mapping def rows_to_dicts(rows): """Convert SQLAlchemy/Snowflake rows to plain dicts.""" return [ dict(row._mapping) if hasattr(row, "_mapping") else dict(row) for row in rows ] def group_by_isrc(rows): """Group row dicts by their ``isrc`` key.""" grouped = defaultdict(list) for row in rows: grouped[row["isrc"]].append(row) return grouped def build_bulk_query_input( isrcs, store_ids, query_params: Mapping[str, Any], permissions: Mapping[str, Any], ): """Build the common query input dict for bulk streams queries. ``transfer_product_ownership_enabled`` is threaded through unchanged so the SQL templates can both swap to the ``_PRODUCT_TRANSFER`` rollup variant via ``transfer_product_ownership_table_name`` and select the ``rollup_grain``-aware branch of ``permissions_filter`` (which does not require ``download_activity_date``). """ return { **permissions, "isrcs": isrcs, "store_ids": store_ids, "distributors": query_params["distributors"], "country_ids": query_params.get("country_ids", []), "transfer_product_ownership_enabled": query_params.get( "transfer_product_ownership_enabled", False ), }