diff --git a/go/test/endtoend/vtgate/misc_test.go b/go/test/endtoend/vtgate/misc_test.go index ae737b8db06..0110bd8f43b 100644 --- a/go/test/endtoend/vtgate/misc_test.go +++ b/go/test/endtoend/vtgate/misc_test.go @@ -528,6 +528,13 @@ func TestSQLSelectLimit(t *testing.T) { utils.AssertMatches(t, conn, "select uid, msg from t7_xxhash order by uid", `[[VARCHAR("1") VARCHAR("a")] [VARCHAR("2") VARCHAR("b")]]`) utils.AssertMatches(t, conn, "(select uid, msg from t7_xxhash order by uid)", `[[VARCHAR("1") VARCHAR("a")] [VARCHAR("2") VARCHAR("b")]]`) utils.AssertMatches(t, conn, "select uid, msg from t7_xxhash order by uid limit 4", `[[VARCHAR("1") VARCHAR("a")] [VARCHAR("2") VARCHAR("b")] [VARCHAR("3") NULL] [VARCHAR("4") VARCHAR("a")]]`) + + // Don't LIMIT subqueries + utils.AssertMatches(t, conn, "select count(*) from (select uid, msg from t7_xxhash order by uid) as subquery", `[[INT64(6)]]`) + utils.AssertMatches(t, conn, "select count(*) from (select 1 union all select 2 union all select 3) as subquery", `[[INT64(3)]]`) + + utils.AssertMatches(t, conn, "select 1 union all select 2 union all select 3", `[[INT64(1)] [INT64(2)]]`) + /* planner does not support query with order by in union query. without order by the results are not deterministic for testing purpose utils.AssertMatches(t, conn, "select uid, msg from t7_xxhash union all select uid, msg from t7_xxhash order by uid", ``) diff --git a/go/vt/sqlparser/normalizer.go b/go/vt/sqlparser/normalizer.go index a985b99f6a7..ef83bc5b250 100644 --- a/go/vt/sqlparser/normalizer.go +++ b/go/vt/sqlparser/normalizer.go @@ -186,7 +186,7 @@ func (nz *normalizer) walkDown(node, _ SQLNode) bool { nz.inDerived++ case *Select: nz.inSelect++ - if nz.selectLimit > 0 && node.Limit == nil { + if nz.selectLimit > 0 && node.Limit == nil && nz.inSelect == 1 { node.Limit = &Limit{Rowcount: NewIntLiteral(strconv.Itoa(nz.selectLimit))} } case *AliasedExpr: @@ -591,7 +591,7 @@ func shouldRewriteDatabaseFunc(in Statement) bool { // rewriteUnion sets the SELECT limit for UNION statements if not already set. func (nz *normalizer) rewriteUnion(node *Union) { - if nz.selectLimit > 0 && node.Limit == nil { + if nz.selectLimit > 0 && node.Limit == nil && nz.inSelect == 0 { node.Limit = &Limit{Rowcount: NewIntLiteral(strconv.Itoa(nz.selectLimit))} } }