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: fix group_concat function couldn't resolve the index of its order-by item (#46419) #47121

Merged
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 expression/integration_test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ go_test(
"main_test.go",
],
flaky = True,
<<<<<<< HEAD
shard_count = 50,
=======
shard_count = 27,
>>>>>>> b823a1deb97 (planner: fix group_concat function couldn't resolve the index of its order-by item (#46419))
deps = [
"//config",
"//domain",
Expand Down
11 changes: 11 additions & 0 deletions expression/integration_test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7960,3 +7960,14 @@ func TestIssue45410(t *testing.T) {
tk.MustExec("INSERT INTO t1 VALUES (0);")
tk.MustQuery("SELECT c1>=CAST('-787360724' AS TIME) FROM t1;").Check(testkit.Rows("1"))
}

func TestIssue41986(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)

tk.MustExec("use test")
tk.MustExec("CREATE TABLE poi_clearing_time_topic (effective_date datetime DEFAULT NULL , clearing_time int(11) DEFAULT NULL);")
tk.MustExec("insert into poi_clearing_time_topic values ('2023:08:25', 1)")
// shouldn't report they can't find column error and return the right result.
tk.MustQuery("SELECT GROUP_CONCAT(effective_date order by stlmnt_hour DESC) FROM ( SELECT (COALESCE(pct.clearing_time, 0)/3600000) AS stlmnt_hour ,COALESCE(pct.effective_date, '1970-01-01 08:00:00') AS effective_date FROM poi_clearing_time_topic pct ORDER BY pct.effective_date DESC ) a;").Check(testkit.Rows("2023-08-25 00:00:00"))
}
35 changes: 35 additions & 0 deletions planner/core/rule_aggregation_push_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/pingcap/tidb/expression/aggregation"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/planner/util"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
)
Expand Down Expand Up @@ -559,6 +560,8 @@ func (a *aggregationPushDownSolver) aggPushDown(p LogicalPlan, opt *logicalOptim
}
oldAggFuncsArgs := make([][]expression.Expression, 0, len(agg.AggFuncs))
newAggFuncsArgs := make([][]expression.Expression, 0, len(agg.AggFuncs))
oldAggOrderItems := make([][]*util.ByItems, 0, len(agg.AggFuncs))
newAggOrderItems := make([][]*util.ByItems, 0, len(agg.AggFuncs))
if noSideEffects {
for _, aggFunc := range agg.AggFuncs {
oldAggFuncsArgs = append(oldAggFuncsArgs, aggFunc.Args)
Expand All @@ -571,6 +574,27 @@ func (a *aggregationPushDownSolver) aggPushDown(p LogicalPlan, opt *logicalOptim
break
}
newAggFuncsArgs = append(newAggFuncsArgs, newArgs)
// for ordeByItems, treat it like agg func's args, if it can be substituted by underlying projection's expression recording them temporarily.
if len(aggFunc.OrderByItems) != 0 {
oldAggOrderItems = append(oldAggOrderItems, aggFunc.OrderByItems)
newOrderByItems := make([]expression.Expression, 0, len(aggFunc.OrderByItems))
for _, oby := range aggFunc.OrderByItems {
newOrderByItems = append(newOrderByItems, expression.ColumnSubstitute(oby.Expr, proj.schema, proj.Exprs))
}
if ExprsHasSideEffects(newOrderByItems) {
noSideEffects = false
break
}
oneAggOrderByItems := make([]*util.ByItems, 0, len(aggFunc.OrderByItems))
for i, obyExpr := range newOrderByItems {
oneAggOrderByItems = append(oneAggOrderByItems, &util.ByItems{Expr: obyExpr, Desc: aggFunc.OrderByItems[i].Desc})
}
newAggOrderItems = append(newAggOrderItems, oneAggOrderByItems)
} else {
// occupy the pos for convenience of subscript index
oldAggOrderItems = append(oldAggOrderItems, nil)
newAggOrderItems = append(newAggOrderItems, nil)
}
}
}
for i, funcsArgs := range oldAggFuncsArgs {
Expand All @@ -580,6 +604,16 @@ func (a *aggregationPushDownSolver) aggPushDown(p LogicalPlan, opt *logicalOptim
break
}
}
for j, item := range newAggOrderItems {
if item == nil {
continue
}
// substitution happened, check the eval type compatibility.
if oldAggOrderItems[i][j].Expr.GetType().EvalType() != newAggOrderItems[i][j].Expr.GetType().EvalType() {
noSideEffects = false
break
}
}
if !noSideEffects {
break
}
Expand All @@ -588,6 +622,7 @@ func (a *aggregationPushDownSolver) aggPushDown(p LogicalPlan, opt *logicalOptim
agg.GroupByItems = newGbyItems
for i, aggFunc := range agg.AggFuncs {
aggFunc.Args = newAggFuncsArgs[i]
aggFunc.OrderByItems = newAggOrderItems[i]
}
projChild := proj.children[0]
agg.SetChildren(projChild)
Expand Down