ZhuLinsen/daily_stock_analysis · warning · ValueError

Phase-filtered results match too many rows; narrow the analy

Error message

Phase-filtered results match too many rows; narrow the analysis date range or stock code.

What it means

In the batched phase-filtered result listing, the service probes rows in batches while tracking a scanned budget of MAX_DYNAMIC_SUMMARY_ROWS+1. Before each batch it computes remaining_probe_rows; when the budget is exhausted before the phase filter matched enough rows, it raises this ValueError (HTTP 400). This protects the paginated results endpoint from unbounded scanning when a phase filter matches very few rows inside a huge candidate set.

Source

Thrown at src/services/backtest_service.py:737

        scanned = 0
        matched_total = 0
        page_rows: List[
            Tuple[
                BacktestResult,
                Optional[str],
                Optional[str],
                Optional[Dict[str, Any]],
                str,
                Optional[str],
                Optional[str],
                Optional[int],
            ]
        ] = []

        while True:
            remaining_probe_rows = self.MAX_DYNAMIC_SUMMARY_ROWS + 1 - scanned
            if remaining_probe_rows <= 0:
                raise ValueError("Phase-filtered results match too many rows; narrow the analysis date range or stock code.")
            batch_limit = min(batch_size, remaining_probe_rows)
            batch = self.repo.get_results_with_context_batch(
                code=code,
                eval_window_days=eval_window_days,
                engine_version=engine_version,
                analysis_date_from=analysis_date_from,
                analysis_date_to=analysis_date_to,
                days=None,
                offset=sql_offset,
                limit=batch_limit,
            )
            if not batch:
                break
            scanned += len(batch)
            if scanned > self.MAX_DYNAMIC_SUMMARY_ROWS:
                raise ValueError("Phase-filtered results match too many rows; narrow the analysis date range or stock code.")
            sql_offset += len(batch)
            for (

View on GitHub (pinned to 5159bd72e8)

Solutions

  1. Narrow with analysis_date_from/analysis_date_to or a code filter so the candidate set fits the scan budget.
  2. Drop the analysis_phase filter and use the plain paginated results endpoint, then filter client-side if the dataset is small.
  3. Backfill/normalize context_snapshot phase data for legacy rows so phase filtering is selective.

Example fix

# before
data = service.get_recent_evaluations(analysis_phase="premarket")

# after
data = service.get_recent_evaluations(
    analysis_phase="premarket",
    analysis_date_from="2026-08-01",
    code="600519",
)
Defensive patterns

Strategy: validation

Validate before calling

count = service.repo.count_results(code=code, eval_window_days=eval_window_days, engine_version=engine_version, analysis_date_from=dfrom, analysis_date_to=dto)
if count > service.MAX_DYNAMIC_SUMMARY_ROWS:
    return JSONResponse(status_code=400, content={"error": "too_many_rows", "hint": "narrow date range or code"})
data = service.get_recent_evaluations(code=code, analysis_phase=phase, analysis_date_from=dfrom, analysis_date_to=dto)

Try / catch

try:
    rows = service.get_recent_evaluations(analysis_phase=phase, analysis_date_from=dfrom, analysis_date_to=dto)
except ValueError as exc:
    if "too many rows" in str(exc):
        return JSONResponse(status_code=400, content={"error": "too_many_rows", "message": str(exc)})
    raise

Prevention

When it happens

Trigger: GET /api/v1/backtest/results?analysis_phase=unknown with a large unfiltered result set where matching-phase rows are sparse, so the scanner burns the whole probe budget on non-matching rows.

Common situations: Filtering for premarket/postmarket phases when most analyses were intraday; requesting the last page of a phase with few entries; legacy rows whose context_snapshot lacks phase info (bucketed as unknown among many).

Related errors


AI-assisted analysis of ZhuLinsen/daily_stock_analysis@5159bd72e8 (2026-08-15). Data as JSON: /api/errors/ea1e008b22bdc9d2. Report an issue: GitHub.