Skip to content

Commit

Permalink
planner: do not remove the first row func if it is a constant | tidb-…
Browse files Browse the repository at this point in the history
…test=pr/2283 (#50020) (#50285)

close #38756
  • Loading branch information
ti-chi-bot authored Jan 23, 2024
1 parent 254e430 commit c641ca4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ func checkFileName(s string) bool {
return false
}

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

tk.MustExec("use test")
tk.MustExec("create table t (c1 int)")
tk.MustExec("insert into t values (1), (2), (3)")
tk.MustQuery("SELECT SQRT(1) FROM t").Check(testkit.Rows("1", "1", "1"))
tk.MustQuery("(SELECT DISTINCT SQRT(1) FROM t)").Check(testkit.Rows("1"))
tk.MustQuery("SELECT DISTINCT cast(1 as double) FROM t").Check(testkit.Rows("1"))
}

func TestPessimisticSelectForUpdate(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
Expand Down
6 changes: 6 additions & 0 deletions planner/core/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -1883,6 +1883,12 @@ func RemoveUnnecessaryFirstRow(
// the firstrow in root task can not be removed.
break
}
// Skip if it's a constant.
// For SELECT DISTINCT SQRT(1) FROM t.
// We shouldn't remove the firstrow(SQRT(1)).
if _, ok := gbyExpr.(*expression.Constant); ok {
continue
}
if gbyExpr.Equal(sctx, aggFunc.Args[0]) {
canOptimize = true
firstRowFuncMap[aggFunc].Args[0] = finalGbyItems[j]
Expand Down

0 comments on commit c641ca4

Please sign in to comment.