Skip to content

Commit

Permalink
planner: do not push down the aggregation function with correlated co…
Browse files Browse the repository at this point in the history
…lumn (#21453) (#21546)

Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
ti-srebot authored Jan 25, 2021
1 parent 858a6c9 commit b0122eb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
15 changes: 15 additions & 0 deletions expression/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,21 @@ func ContainVirtualColumn(exprs []Expression) bool {
return false
}

// ContainCorrelatedColumn checks if the expressions contain a correlated column
func ContainCorrelatedColumn(exprs []Expression) bool {
for _, expr := range exprs {
switch v := expr.(type) {
case *CorrelatedColumn:
return true
case *ScalarFunction:
if ContainCorrelatedColumn(v.GetArgs()) {
return true
}
}
}
return false
}

// ContainMutableConst checks if the expressions contain a lazy constant.
func ContainMutableConst(ctx sessionctx.Context, exprs []Expression) bool {
// Treat all constants immutable if plan cache is not enabled for this query.
Expand Down
11 changes: 11 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,17 @@ func (s *testIntegrationSuite) TestUpdateMultiUpdatePK(c *C) {
tk.MustQuery("SELECT * FROM t").Check(testkit.Rows("2 12"))
}

func (s *testIntegrationSuite) TestCorrelatedColumnAggFuncPushDown(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test;")
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int, b int);")
tk.MustExec("insert into t values (1,1);")
tk.MustQuery("select (select count(n.a + a) from t) from t n;").Check(testkit.Rows(
"1",
))
}

func (s *testIntegrationSuite) TestIssue22105(c *C) {
tk := testkit.NewTestKit(c, s.store)

Expand Down
3 changes: 2 additions & 1 deletion planner/core/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,8 @@ func CheckAggCanPushCop(sctx sessionctx.Context, aggFuncs []*aggregation.AggFunc
sc := sctx.GetSessionVars().StmtCtx
client := sctx.GetClient()
for _, aggFunc := range aggFuncs {
if expression.ContainVirtualColumn(aggFunc.Args) {
// if the aggFunc contain VirtualColumn or CorrelatedColumn, it can not be pushed down.
if expression.ContainVirtualColumn(aggFunc.Args) || expression.ContainCorrelatedColumn(aggFunc.Args) {
return false
}
pb := aggregation.AggFuncToPBExpr(sc, client, aggFunc)
Expand Down

0 comments on commit b0122eb

Please sign in to comment.