Files
romm/backend/tests/handler/database/test_roms_group_by_index.py
Georges-Antoine Assi 68709e79a7 fix(siblings): group versions by connected component everywhere (#5430)
* fix(siblings): group versions by connected component everywhere

The game page's siblings matched on any shared provider id, while the
gallery's group_by_meta_id window partitioned on the first non-null id in
a fixed provider order. A version matched by IGDB and ScreenScraper and
one matched by ScreenScraper alone were siblings on the game page but two
gallery cards. The recommendation surfaces didn't group at all: the
cold-start row ranks by rating, which every version shares, so one game
filled it.

rom_sibling_groups now stores each ROM's connected component over
rom_identity_keys, kept in step by add_rom/update_rom/delete_rom and
rebuilt after a scan. The sibling_roms view, the gallery window and the
recommendations all read it: the feed and similar games collapse each
game to one entry, exclude the other versions of a played game, and
show the version the grouped gallery shows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(siblings): answer the Versions filter from rom_sibling_groups

Only a ROM with another version has a group row, so "has versions" is a
primary-key probe rather than an EXISTS over the sibling_roms self-join.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(siblings): break representative ties by id; restore Versions filter

Versions often share a filename stem and region, and without a final key
the version that represents a group could change between queries.

The "Versions" filter goes back to EXISTS over sibling_roms: measured on
a 100k-ROM library it takes the same time as the direct group-row probe
(107 ms each), since the view merges into the query.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(migrations): renumber rom_sibling_groups to 0158 after master's 0157

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(recommendations): address Greptile review on sibling grouping

- Swap each recommendation to its visible representative before the
  visibility cut, so a game whose top-ranked version is hidden shows
  through a visible one instead of disappearing.
- Read candidates in batches until enough distinct games remain, so a
  game with more versions than one overfetch batch no longer crowds out
  the rest of the feed; the cold-start query pages with an id tiebreak.
- Shorten the 0158 migration and RomSiblingGroup docstrings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(recommendations): use the shared auth helpers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: code-review

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: simplify

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: prose

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: prose (code-review fixes)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(recommendations): hide only the hidden version, as the gallery does

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(siblings): regroup only on identity changes and scope the post-scan repair

add_rom skips the regroup when the merge changed no identity column. The
post-scan repair regroups the scanned ROMs for a ROM-scoped scan and
rebuilds only the scanned platforms otherwise, since no group spans two
platforms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 12:07:57 -05:00

156 lines
5.8 KiB
Python

"""The "Group ROMs" dedup window and the index that has to cover it.
Grouping collapses versions of the same game into one gallery entry with a
window function. The window materializes a narrow subquery first, because
carrying the wide `roms` row (every provider's raw metadata) through the window
spills its sort to disk. That narrowing only pays off if the index carries every
column the subquery reads: a covering index is all-or-nothing, so one missing
column drops the plan to a full table scan.
`idx_roms_sibling_cover` was added in 0090 for the `sibling_roms` self-join and
did not cover `flashpoint_id` or `fs_name_no_ext`, which the window needs for
its partition tail and its sort tiebreaker. The gallery issues the grouped query
up to four times per page load (page, count, char index, id index), so the scan
was paid four times over. 0107 widened the index to close the gap.
0108 added `generated_primary_region` for the same reason: region priority ranks
each sibling, and `roms.regions` is JSON that no covering index can carry.
The failure mode is silent: a `roms` column the window starts reading that the
index does not follow quietly returns the gallery to full scans.
"""
import re
from typing import Any, cast
from sqlalchemy import Table, inspect
from sqlalchemy.dialects import mysql
from sqlalchemy.sql import Subquery
from sqlalchemy.sql.expression import Select
from tests.conftest import engine
from handler.database import db_rom_handler
from handler.database.rom_filters import RomFilterParams
from models.rom import Rom
# Taken off the model rather than repeated, so these checks read as "the
# migrations and the window agree with the declared index".
INDEX_COLUMNS = [
column.name
for column in next(
index
for index in cast(Table, Rom.__table__).indexes
if index.name == "idx_roms_sibling_cover"
).columns
]
def _subqueries(clause, found: list[Subquery] | None = None) -> list[Subquery]:
found = found if found is not None else []
for child in clause.get_children():
if isinstance(child, Subquery):
found.append(child)
_subqueries(child.element, found)
elif hasattr(child, "get_children"):
_subqueries(child, found)
return found
def _grouped_query(order_by: str = "", user_id: int | None = None):
query, _ = db_rom_handler.get_roms_query(order_by=order_by, user_id=user_id)
return db_rom_handler.filter_roms(
query=query,
filters=RomFilterParams(group_by_meta_id=True),
order_by=order_by,
user_id=user_id,
)
def _dedup_window_subquery(
order_by: str = "", user_id: int | None = None
) -> Select[*tuple[Any, ...]]:
"""The narrow `roms` subquery the grouped query materializes for its window."""
for subquery in _subqueries(_grouped_query(order_by, user_id)):
if not isinstance(subquery.element, Select):
continue
if any(
getattr(column, "table", None) is Rom.__table__
for column in subquery.element.selected_columns
):
return subquery.element
raise AssertionError("the grouped query no longer materializes a roms subquery")
def _dedup_window_select(
order_by: str = "", user_id: int | None = None
) -> Select[*tuple[Any, ...]]:
"""The SELECT that computes `row_num` (and any group aggregate)."""
for subquery in _subqueries(_grouped_query(order_by, user_id)):
if not isinstance(subquery.element, Select):
continue
if any(
getattr(column, "key", None) == "row_num"
for column in subquery.element.selected_columns
):
return subquery.element
raise AssertionError("the grouped query no longer builds a row_number window")
def _dedup_window_columns() -> set[str]:
"""The `roms` columns the window selects directly."""
return {
column.name
for column in _dedup_window_subquery().selected_columns
if getattr(column, "table", None) is Rom.__table__
}
def _dedup_window_referenced_columns(
order_by: str = "", user_id: int | None = None
) -> set[str]:
"""Every `roms` column the window's SQL reads, expressions included.
Selecting a bare column is only one way in: a CASE or a function call over
an uncovered column reads it just the same while carrying no `.table`, so
the compiled SQL is what has to be inspected. Compiled at the window level
(which nests the narrow roms subquery), so a sort key aggregated straight
off `roms` is caught too.
"""
sql = str(
_dedup_window_select(order_by=order_by, user_id=user_id).compile(
dialect=mysql.dialect(), compile_kwargs={"literal_binds": True}
)
)
return set(re.findall(r"\broms\.(\w+)", sql))
class TestSiblingCoverIndex:
def test_migrations_create_covering_index(self):
indexes = [
index["column_names"] for index in inspect(engine).get_indexes("roms")
]
assert INDEX_COLUMNS in indexes
class TestGroupByMetaIdCoverage:
def test_dedup_window_reads_only_covered_columns(self):
assert _dedup_window_columns() <= set(INDEX_COLUMNS)
def test_dedup_window_expressions_read_only_covered_columns(self):
assert _dedup_window_referenced_columns() <= set(INDEX_COLUMNS)
def test_dedup_window_excludes_the_wide_metadata_columns(self):
assert not {
column for column in _dedup_window_columns() if column.endswith("_metadata")
}
def test_rom_user_sorted_window_reads_only_covered_columns(self):
# The grouped sort aggregate reads its key off the window's rom_user
# join; a roms-side key would break out of the covering index here.
referenced = _dedup_window_referenced_columns(order_by="last_played", user_id=1)
assert referenced <= set(INDEX_COLUMNS)