Skip to content

Commit 6281905

Browse files
[release-18.0] Fix Incorrect Optimization with LIMIT and GROUP BY (vitessio#16263) (vitessio#16266)
Signed-off-by: Andres Taylor <[email protected]> Signed-off-by: Florent Poinsard <[email protected]> Co-authored-by: Andrés Taylor <[email protected]>
1 parent 398529d commit 6281905

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

go/test/endtoend/vtgate/queries/aggregation/aggregation_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package aggregation
1818

1919
import (
2020
"fmt"
21+
"math/rand"
2122
"slices"
2223
"sort"
2324
"strings"
@@ -66,6 +67,22 @@ func start(t *testing.T) (utils.MySQLCompare, func()) {
6667
}
6768
}
6869

70+
func TestAggrWithLimit(t *testing.T) {
71+
version, err := cluster.GetMajorVersion("vtgate")
72+
require.NoError(t, err)
73+
if version != 18 {
74+
t.Skip("Test requires VTGate version 18")
75+
}
76+
mcmp, closer := start(t)
77+
defer closer()
78+
79+
for i := 0; i < 1000; i++ {
80+
r := rand.Intn(50)
81+
mcmp.Exec(fmt.Sprintf("insert into aggr_test(id, val1, val2) values(%d, 'a', %d)", i, r))
82+
}
83+
mcmp.Exec("select val2, count(*) from aggr_test group by val2 order by count(*), val2 limit 10")
84+
}
85+
6986
func TestAggregateTypes(t *testing.T) {
7087
mcmp, closer := start(t)
7188
defer closer()

go/vt/vtgate/planbuilder/operators/query_planning.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,11 @@ func setUpperLimit(in *Limit) (ops.Operator, *rewrite.ApplyResult, error) {
518518
case *Join, *ApplyJoin, *SubQueryContainer, *SubQuery:
519519
// we can't push limits down on either side
520520
return rewrite.SkipChildren
521+
case *Aggregator:
522+
if len(op.Grouping) > 0 {
523+
// we can't push limits down if we have a group by
524+
return rewrite.SkipChildren
525+
}
521526
case *Route:
522527
newSrc := &Limit{
523528
Source: op.Source,
@@ -527,9 +532,8 @@ func setUpperLimit(in *Limit) (ops.Operator, *rewrite.ApplyResult, error) {
527532
op.Source = newSrc
528533
result = result.Merge(rewrite.NewTree("push limit under route", newSrc))
529534
return rewrite.SkipChildren
530-
default:
531-
return rewrite.VisitChildren
532535
}
536+
return rewrite.VisitChildren
533537
}
534538

535539
_, err := rewrite.TopDown(in.Source, TableID, visitor, shouldVisit)

go/vt/vtgate/planbuilder/testdata/memory_sort_cases.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
},
148148
"FieldQuery": "select a, b, count(*) as k, weight_string(a) from `user` where 1 != 1 group by a, weight_string(a)",
149149
"OrderBy": "(0|3) ASC",
150-
"Query": "select a, b, count(*) as k, weight_string(a) from `user` group by a, weight_string(a) order by a asc limit :__upper_limit",
150+
"Query": "select a, b, count(*) as k, weight_string(a) from `user` group by a, weight_string(a) order by a asc",
151151
"Table": "`user`"
152152
}
153153
]

0 commit comments

Comments
 (0)