Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: update PlanBuilder.windowSpecs when building subquery #30878

Merged
merged 2 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ func (er *expressionRewriter) buildSubquery(ctx context.Context, subq *ast.Subqu
er.b.outerNames = er.b.outerNames[0 : len(er.b.outerNames)-1]
}()
}
outerWindowSpecs := er.b.windowSpecs
defer func() {
er.b.windowSpecs = outerWindowSpecs
}()

np, err := er.b.buildResultSetNode(ctx, subq.Query)
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5020,3 +5020,17 @@ func (s *testIntegrationSerialSuite) TestIssue30271(c *C) {
tk.MustQuery("select * from t where (a>'a' and b='a') or (b = 'A' and a < 'd') order by a,c;").Check(testkit.Rows("b a 1", "b A 2", "c a 3"))

}

func (s *testIntegrationSuite) TestIssue30804(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1, t2")
tk.MustExec("create table t1(a int, b int)")
tk.MustExec("create table t2(a int, b int)")
// minimal reproduction of https://github.com/pingcap/tidb/issues/30804
tk.MustExec("select avg(0) over w from t1 window w as (order by (select 1))")
// named window cannot be used in subquery
err := tk.ExecToErr("select avg(0) over w from t1 where b > (select sum(t2.a) over w from t2) window w as (partition by t1.b)")
c.Assert(core.ErrWindowNoSuchWindow.Equal(err), IsTrue)
tk.MustExec("select avg(0) over w1 from t1 where b > (select sum(t2.a) over w2 from t2 window w2 as (partition by t2.b)) window w1 as (partition by t1.b)")
}