Skip to content

Commit

Permalink
planner: do not remove the first row func if it is a constant (#50287)
Browse files Browse the repository at this point in the history
close #38756
  • Loading branch information
ti-chi-bot authored Feb 1, 2024
1 parent bac49de commit 25dd34f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/executor/test/executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4299,3 +4299,15 @@ func TestProcessInfoOfSubQuery(t *testing.T) {
tk2.MustQuery("select 1 from information_schema.processlist where TxnStart != '' and info like 'select%sleep% from t%'").Check(testkit.Rows("1"))
wg.Wait()
}

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"))
}
6 changes: 6 additions & 0 deletions pkg/planner/core/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -1893,6 +1893,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 25dd34f

Please sign in to comment.