Files
romm/backend/utils/sql_dialect.py
Zach Clendenen 10865b8663 perf(roms): faster smart collection and filter value queries (#5069)
* perf(roms): stop MariaDB rescanning the library for smart collections

MariaDB plans the smart collection membership IN (subquery) as a semi-join that rescans every rom per page query. A NO_SEMIJOIN hint keeps it a plain subquery; other dialects don't emit the hint.

* perf(roms): parse each distinct filter value list once

The filter dropdown query decoded every JSON column of every row. The columns are now read as text, deduplicated, and each distinct list is parsed once. PostgreSQL decodes JSON in the driver, so its lists are merged as returned.

* test(roms): cover repeated filter value lists in pr-ready code review

* refactor(roms): pr-ready simplify pass

* style(roms): pr-ready polish

* fix(roms): address Greptile review

Fold filter value rows in batches of 5000 instead of materializing the whole result, keeping the parse-each-distinct-list-once dedup across batches, so /api/roms/filters no longer holds one row per ROM in memory (Greptile: filter requests retain every row). The repeated-lists test now runs with a batch size of 2 to cover the cross-batch path.

* fix(roms): pr-ready code review fixes

- Cast filter JSON columns to text in SQL, so PostgreSQL gets the
  parse-once saving and a scalar JSONB value no longer crashes json.loads
- Stream filter rows with yield_per instead of only partitioning
- Name the JSON filter columns instead of hard-coding positions
- Add optimizer_hint_on_mysql next to force_index_on_mysql, with tests
- Make the repeated-lists test span batches regardless of row order

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* refactor(roms): pr-ready simplify pass

Collect each filter column's distinct JSON texts and parse them once at
the end, and key the JSON filter columns by their RomFiltersDict field so
the positional unpack and per-field return go away.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* style(roms): pr-ready polish

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-03 10:29:25 -04:00

206 lines
7.3 KiB
Python

"""Per-engine SQL spellings, chosen when the statement compiles."""
import json
from collections.abc import Callable, Collection, Sequence
from typing import Any
import sqlalchemy as sa
from sqlalchemy import SQLColumnExpression
from sqlalchemy.dialects import mysql as sa_mysql
from sqlalchemy.dialects import postgresql as sa_pg
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql import ClauseElement, ColumnElement, func
from sqlalchemy.sql.compiler import DDLCompiler, SQLCompiler
from sqlalchemy.sql.ddl import ExecutableDDLElement
from sqlalchemy.sql.elements import ClauseList
from sqlalchemy.sql.operators import OperatorType, comma_op
from sqlalchemy.sql.selectable import FromClause, Select
from sqlalchemy.sql.visitors import InternalTraversal
# A JSON column holding an array: `Mapped[list[str] | None]`, `Mapped[set[int]]`...
type JsonArrayColumn = SQLColumnExpression[Collection[Any] | None]
type JsonArrayValues = Sequence[str] | Sequence[int]
_MYSQL_FAMILY = ("mysql", "mariadb")
def _compiles_on_mysql_family[F: Callable[..., str]](
construct: type[ClauseElement],
) -> Callable[[F], F]:
"""Register a compiler for MySQL and MariaDB; `"mysql"` alone misses MariaDB."""
def decorate(fn: F) -> F:
for dialect_name in _MYSQL_FAMILY:
fn = compiles(construct, dialect_name)(fn)
return fn
return decorate
class DialectCase[T](ColumnElement[T]):
"""`mysql` on MySQL and MariaDB, `postgresql` elsewhere, typed as `postgresql`."""
inherit_cache = True
# Both branches are traversed, so their binds are part of the cache key.
_traverse_internals = [
("postgresql", InternalTraversal.dp_clauseelement),
("mysql", InternalTraversal.dp_clauseelement),
]
def __init__(self, *, postgresql: ColumnElement[T], mysql: ClauseElement) -> None:
self.postgresql = postgresql
self.mysql = mysql
self.type = postgresql.type
# FROM inference and correlation see the tables either branch reads.
@property
def _from_objects(self) -> list[FromClause]:
return [*self.postgresql._from_objects, *self.mysql._from_objects]
# Group and negate each branch on its own, so `NOT (a OR b)` keeps its parens;
# a comma list stays flat, since a grouped ORDER BY list is a row value.
def self_group(self, against: OperatorType | None = None) -> ColumnElement[T]:
if against is comma_op:
return self
return DialectCase(
postgresql=self.postgresql.self_group(against=against),
mysql=self.mysql.self_group(against=against),
)
def _negate(self) -> ColumnElement[T]:
return DialectCase(
postgresql=self.postgresql._negate(), mysql=self.mysql._negate()
)
@compiles(DialectCase)
def _dialect_case_default(
element: DialectCase[Any], compiler: SQLCompiler, **kw: Any
) -> str:
return compiler.process(element.postgresql, **kw)
@_compiles_on_mysql_family(DialectCase)
def _dialect_case_mysql(
element: DialectCase[Any], compiler: SQLCompiler, **kw: Any
) -> str:
return compiler.process(element.mysql, **kw)
def nulls_last[T](sort_key: SQLColumnExpression[T], descending: bool) -> DialectCase[T]:
"""An ORDER BY term that sorts NULL values of `sort_key` after every other value."""
directed = sort_key.desc() if descending else sort_key.asc()
# MySQL and MariaDB have no NULLS LAST. DESC already puts NULLs last there;
# ASC needs the `IS NULL` term leading.
return DialectCase(
# PostgreSQL's `idx_roms_<column>_desc` indexes are declared with exactly
# this spelling, so the descending sort reads out of them.
postgresql=directed.nulls_last(),
mysql=directed if descending else ClauseList(sort_key.is_(None), directed),
)
def force_index_on_mysql[S: Select[*tuple[Any, ...]]](
statement: S, table: FromClause | type[Any], index_name: str
) -> S:
"""Make MySQL and MariaDB read `table` through `index_name`; PostgreSQL plans freely."""
hint = f"FORCE INDEX ({index_name})"
for dialect_name in _MYSQL_FAMILY:
statement = statement.with_hint(table, hint, dialect_name)
return statement
def optimizer_hint_on_mysql[S: Select[*tuple[Any, ...]]](statement: S, hint: str) -> S:
"""Prefix the SELECT with an optimizer `hint` on MySQL and MariaDB only."""
for dialect_name in _MYSQL_FAMILY:
statement = statement.prefix_with(f"/*+ {hint} */", dialect=dialect_name)
return statement
def fulltext_match(*columns: ColumnElement[Any], boolean_query: str) -> sa_mysql.match:
"""A boolean-mode match over one FULLTEXT index's columns, for a `mysql` branch."""
return sa_mysql.match(*columns, against=boolean_query).in_boolean_mode()
def _jsonb(column: JsonArrayColumn) -> ColumnElement[Any]:
return sa.type_coerce(column, sa_pg.JSONB)
def _jsonb_contains(column: JsonArrayColumn, value: str | int) -> ColumnElement[bool]:
return _jsonb(column).contains(
func.cast(sa.literal(value, sa_pg.JSONB), sa_pg.JSONB)
)
def _jsonb_text_array(values: JsonArrayValues) -> ColumnElement[Any]:
return sa.type_coerce(values, sa_pg.ARRAY(sa_pg.TEXT))
def json_array_contains_value(
column: JsonArrayColumn, value: str | int
) -> ColumnElement[bool]:
"""Check if a JSON array column contains the given value."""
return DialectCase(
# `?` only matches strings; `@>` handles every other JSON type.
postgresql=(
_jsonb(column).has_key(value)
if isinstance(value, str)
else _jsonb_contains(column, value)
),
# JSON_CONTAINS takes JSON text, even for a number.
mysql=func.json_contains(column, json.dumps(value)),
)
def json_array_contains_any(
column: JsonArrayColumn, values: JsonArrayValues
) -> ColumnElement[bool]:
"""Check if a JSON array column contains any of the given values."""
if not values:
return sa.false()
if len(values) == 1:
return json_array_contains_value(column, values[0])
return DialectCase(
postgresql=(
_jsonb(column).has_any(_jsonb_text_array(values))
if isinstance(values[0], str)
else sa.or_(*(_jsonb_contains(column, v) for v in values))
),
mysql=func.json_overlaps(column, json.dumps(values)),
)
def json_array_contains_all(
column: JsonArrayColumn, values: JsonArrayValues
) -> ColumnElement[bool]:
"""Check if a JSON array column contains all of the given values."""
if not values:
return sa.false()
return DialectCase(
postgresql=(
_jsonb(column).has_all(_jsonb_text_array(values))
if isinstance(values[0], str)
else sa.and_(*(_jsonb_contains(column, v) for v in values))
),
mysql=func.json_contains(column, json.dumps(values)),
)
class Analyze(ExecutableDDLElement):
"""Refresh the planner statistics of `table`."""
def __init__(self, table: str) -> None:
self.table = table
@compiles(Analyze)
def _analyze_default(element: Analyze, compiler: DDLCompiler, **kw: Any) -> str:
return f"ANALYZE {compiler.preparer.quote(element.table)}"
@_compiles_on_mysql_family(Analyze)
def _analyze_mysql(element: Analyze, compiler: DDLCompiler, **kw: Any) -> str:
return f"ANALYZE TABLE {compiler.preparer.quote(element.table)}"