fix(tradein): estimate 500 — AmbiguousParameter на NULL-параметрах

Второй баг из #385 (#388 закрыл только алиас distance_m). В
_fetch_analogs ORDER BY использует :target_year и :target_house_type.
Когда год постройки / тип дома не заданы, они приходят NULL — psycopg
шлёт их без типа, PostgreSQL не может определить тип параметра →
psycopg.errors.AmbiguousParameter ($8) → 500 на каждый estimate.

Фикс: явный CAST(:target_year AS integer) и CAST(:target_house_type
AS text). Проверено на прод-БД через psycopg3: без CAST падает с
AmbiguousParameter, с CAST — успешно (7 строк).
This commit is contained in:
TradeIn Deploy 2026-05-22 10:03:17 +05:00
parent 137c8bbb08
commit 923513f906

View file

@ -347,10 +347,13 @@ def _fetch_analogs(
-- термом) PostgreSQL трактует имя как входную колонку listings,
-- которой нет "column distance_m does not exist". Инлайним ST_Distance.
ST_Distance(geom::geography, ST_MakePoint(:lon, :lat)::geography) / 1000.0
+ CASE WHEN :target_year IS NOT NULL AND year_built IS NOT NULL
THEN abs(year_built - :target_year) / 12.0 ELSE 0 END
+ CASE WHEN :target_house_type IS NOT NULL AND house_type IS NOT NULL
AND house_type <> :target_house_type
-- CAST обязателен: target_year / target_house_type приходят NULL
-- без типа PostgreSQL "could not determine data type of parameter"
-- (AmbiguousParameter). Явный тип снимает неоднозначность.
+ CASE WHEN CAST(:target_year AS integer) IS NOT NULL AND year_built IS NOT NULL
THEN abs(year_built - CAST(:target_year AS integer)) / 12.0 ELSE 0 END
+ CASE WHEN CAST(:target_house_type AS text) IS NOT NULL AND house_type IS NOT NULL
AND house_type <> CAST(:target_house_type AS text)
THEN 1.5 ELSE 0 END
)
LIMIT 50