sql_select_limit is applied to every branch of a UNION, changing UNION DISTINCT / ORDER BY results
Overview of the Issue
When a session has sql_select_limit set (directly, or through a JDBC setMaxRows), the normalizer injects the limit into every SELECT that is a direct branch of a top-level UNION, in addition to the UNION itself. Each branch is truncated before the UNION DISTINCT dedup and before any global ORDER BY, so the statement can return fewer rows or the wrong rows compared to MySQL.
The mechanism is in go/vt/sqlparser/normalizer.go:
walkDowninjects the limit into a*Selectwhennz.inSelect == 1(https://github.com/vitessio/vitess/blob/f278dc7cd29c1d19bcaf9fe846fbf4b3329f75c6/go/vt/sqlparser/normalizer.go#L189)*Uniondoes not incrementinSelect, so every branch of a statement-level union is visited atinSelect == 1and gets its own limitrewriteUnionthen adds the limit to the union itself on the way back up (https://github.com/vitessio/vitess/blob/f278dc7cd29c1d19bcaf9fe846fbf4b3329f75c6/go/vt/sqlparser/normalizer.go#L589)
With sql_select_limit = 10 the normalizer produces:
select a from t1 union select a from t2
=> (select a from t1 limit :vtg1) union (select a from t2 limit :vtg2) limit 10
select a from t1 union select a from t2 order by a
=> (select a from t1 limit :vtg1) union (select a from t2 limit :vtg2) order by a asc limit 10
select a from t1 union select a from t2 limit 3 -- user's own limit
=> (select a from t1 limit :vtg1) union (select a from t2 limit :vtg2) limit 3
select a from t1 union (select a from t2 union select a from t3)
=> (select a from t1 limit :vtg1) union ((select a from t2 limit :vtg2) union (select a from t3 limit :vtg3) limit 10) limit 10Note the third case: even when the user supplies an explicit LIMIT on the union, the branches still get the session limit pushed into them.
Wrong-result examples (sql_select_limit = 10):
- UNION DISTINCT.
t1has 10 rows with value1;t2has 10 rows with value1followed by values2..20. MySQL returns 10 distinct rows. Vitess truncates each branch to 10 rows first, both branches dedup to{1}, and the query returns 1 row. - Global ORDER BY.
select a from t1 union all select a from t2 order by awith the limit pushed into each branch sorts an arbitrary 10 rows per branch instead of the globally smallest 10.
#18716 (v23) narrowed the previous behaviour, which injected the limit into derived tables and subqueries as well, but it did not cover union branches. Its end-to-end test (select 1 union all select 2 union all select 3) cannot catch this because every branch is a single row.
Reproduction Steps
Unit-level (no cluster needed):
stmt, _ := NewTestParser().Parse("select a from t1 union select a from t2")
res, _ := Normalize(stmt, NewReservedVars("vtg", nil), map[string]*querypb.BindVariable{}, true, "ks", 10, "", nil, nil, nil)
fmt.Println(String(res.AST))
// (select a from t1 limit :vtg1 /* INT64 */) union (select a from t2 limit :vtg2 /* INT64 */) limit 10Cluster-level, against the go/test/endtoend/vtgate fixtures (t7_xxhash has uid 1..6, msg = a, b, null, a, a, b):
set sql_select_limit = 2;
(select msg from t7_xxhash where uid in ('1', '4', '6') order by uid)
union
(select msg from t7_xxhash where uid in ('1', '4', '6') order by uid);
-- Each branch is (a, a, b). MySQL: 2 rows (a, b). Vitess: 1 row, because each branch is cut to (a, a) before the dedup.Binary Version
main @ 83eb933047b4fd87df9805f7634bafa180d8cd4d (2026-09-25). Also affects v22.0.0 (where the limit was additionally pushed into derived tables and subqueries, see #18716) and any release carrying #18716.
Operating System and Environment details
Reproduced with the sqlparser package unit tests on macOS arm64. The bug is in query rewriting and is platform-independent.
Log Fragments
N/A. The rewritten query is visible in the normalizer output above.
Source: vitessio/vitess