← Back to Pull requests

fix(mysql): page custom queries in a stable order when ORDER BY is only in a subquery or window #4831

Closed
unknown closed this pull request without merging on Nov 04, 2026 09:29 UTC
View on GitHub
unknown unknown commented on Oct 28, 2026 06:39 UTC
Author

Relates to: no issue

The MySQL connector pages an advanced sync rule query with LIMIT ... OFFSET .... _update_query_with_pagination_attributes appends ORDER BY <primary keys> first, unless the query already has an ORDER BY. It decided that with re.search(r"\bORDER\s+BY\b", query), which matches an ORDER BY anywhere in the text, including ones that don't order the rows the query returns:

SELECT id, ROW_NUMBER() OVER (ORDER BY marks) AS rank_no FROM employee
SELECT id, name FROM employee WHERE id IN (SELECT id FROM top_ids ORDER BY marks LIMIT 10)
SELECT t.id, t.name FROM (SELECT id, name FROM employee ORDER BY marks) AS t
SELECT id, name FROM employee WHERE note = 'order by date'
SELECT id, name FROM employee -- order by marks

For each of these, the paged queries are <query> LIMIT 5000 OFFSET n with no ORDER BY of their own. The order of a result is only defined by the ORDER BY of the query itself; the MySQL docs note that without one, LIMIT returns rows in a nondeterministic order. The optimizer also discards the ORDER BY of a derived table or subquery that has no LIMIT. Each page is a separate statement, so consecutive pages don't have to agree on that order, and a sync can skip some rows and index others twice.

This change adds has_top_level_order_by, which only counts an ORDER BY outside parentheses (subqueries, derived tables, OVER (...) and WINDOW w AS (...) definitions). It skips string literals, quoted identifiers and comments while scanning. The queries above now get ORDER BY <primary keys> appended, like a query without any ORDER BY. A query that orders its own result, including the ORDER BY of a UNION, is paged as before.

Tests: test_update_query_with_pagination_attributes gets eleven cases. Seven fail on main: the window function, subquery, derived table, string literal, quoted identifier, block comment and # comment cases. The other four check that the query's own ORDER BY is still found next to a literal containing ' and (, after a subquery and a window function, and on a UNION. The rest of test_mysql.py passes.

Checklists

Pre-Review Checklist


  • this PR does NOT contain credentials of any kind, such as API keys or username/passwords (double check config.yml.example)

  • this PR has a meaningful title

  • this PR links to all relevant github issues that it fixes or partially addresses

  • Covered the changes with automated tests

  • Tested the changes locally (ran test_mysql.py, ruff check and ruff format --check on the changed files; not tested against a live MySQL server)

  • Added a label for each target release version (example: v7.13.2, v7.14.0, v8.0.0)

  • For bugfixes: backport safely to all minor branches still receiving patch releases
  • Release Note

    MySQL advanced sync rule queries whose only ORDER BY is in a subquery, a window function, a string literal or a comment are now paged in primary key order, so syncs no longer skip or repeat rows between pages.

    🤖 Generated with Claude Code

    Diff: 394 lines (+295 / -99) across 4 files Reviewer: ShivamSharma43 Review duration: 10250m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.43) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.43 · 394 lines diff across 4 files
    0.43
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.358
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #4831"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 43.2% (MEDIUM) · Higher = larger blast radius
    43.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 17.1% · Safe to merge
    17.1%