Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions go/test/endtoend/vtgate/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", ``)
Expand Down
4 changes: 2 additions & 2 deletions go/vt/sqlparser/normalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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))}
}
}
Expand Down
Loading