#21234·vitess

sql_select_limit is applied to every branch of a UNION, changing UNION DISTINCT / ORDER BY results

Author: e-akashsainiCreated Sep 25, 2026Updated Sep 29, 2026
LabelsType: BugComponent: Query Serving

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:

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 10

Note 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. t1 has 10 rows with value 1; t2 has 10 rows with value 1 followed by values 2..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 a with 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):

go
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 10

Cluster-level, against the go/test/endtoend/vtgate fixtures (t7_xxhash has uid 1..6, msg = a, b, null, a, a, b):

sql
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.