Files
romm/backend/tests/handler/database/test_roms_group_by_index.py
Sam Dornan 5c43a97687 feat(gallery): pick a game's default version by region priority
The group_by_meta_id dedup window ordered each sibling group by
is_main_sibling then fs_name_no_ext, so absent a manual override the
gallery entry went to whichever filename sorted first: "(Demo)" beat
"(Europe)" beat "(USA)".

Two ordering terms now sit between those: a full release outranks a
pre-release, then the configured scan.priority.region order decides.
Release status comes first because a demo is not a substitute for the
game, so a Japanese release wins over a USA demo.

scan.priority.region already existed but only steered metadata and
artwork selection, so its description is reworded across all locales.

Regions live in a JSON column that no covering index can carry, and 0107
measured what one uncovered reference costs this window: the plan drops
from `type=index, Using index` to `type=ALL, key=NULL`, four times per
gallery page load. 0108 adds a STORED generated column over regions[0]
and covers it in idx_roms_sibling_cover instead, so the ranking reads a
scalar. The engine keeps it in sync and ALTER TABLE populates existing
rows, so no scan hook and no backfill are needed.

Pre-release detection matches the filename rather than the parsed `tags`
column: tags keep the dumper's casing, and fs_name_no_ext is already in
the index.

Reordering the priority list changes which rom represents a group, so the
cached gallery sidecars are dropped when the setting changes.

Closes #1528

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-21 11:55:07 -05:00

123 lines
4.2 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, and adding a metadata provider is the way back into
it: a new id column joins the window's COALESCE chain, the index does not follow
it, and the gallery quietly returns to full scans.
"""
import re
from sqlalchemy import 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 models.rom import Rom
INDEX_COLUMNS = [
"platform_id",
"igdb_id",
"moby_id",
"ss_id",
"launchbox_id",
"ra_id",
"hasheous_id",
"tgdb_id",
"flashpoint_id",
"fs_name_no_ext",
"generated_primary_region",
"id",
]
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 _dedup_window_subquery() -> Select:
"""The narrow `roms` subquery the grouped query materializes for its window."""
query, _ = db_rom_handler.get_roms_query()
grouped = db_rom_handler.filter_roms(query=query, group_by_meta_id=True)
for subquery in _subqueries(grouped):
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_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() -> 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.
"""
sql = str(
_dedup_window_subquery().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")
}