fix(mysql): page custom queries in a stable order when ORDER BY is only in a subquery or window #4831
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
ShivamSharma43
Relates to: no issue
The MySQL connector pages an advanced sync rule query with
LIMIT ... OFFSET ...._update_query_with_pagination_attributesappendsORDER BY <primary keys>first, unless the query already has an ORDER BY. It decided that withre.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:For each of these, the paged queries are
<query> LIMIT 5000 OFFSET nwith 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 (...)andWINDOW w AS (...)definitions). It skips string literals, quoted identifiers and comments while scanning. The queries above now getORDER 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_attributesgets eleven cases. Seven fail onmain: 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 oftest_mysql.pypasses.Checklists
Pre-Review Checklist
config.yml.example)test_mysql.py,ruff checkandruff format --checkon the changed files; not tested against a live MySQL server)v7.13.2,v7.14.0,v8.0.0)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