From dd3edf5994b6dc971c8f4e6065582a135e50bd22 Mon Sep 17 00:00:00 2001 From: guo-shaoge Date: Wed, 9 Jun 2021 10:48:29 +0800 Subject: [PATCH] executor: fix bug when use limit in CTE (#25261) --- executor/cte.go | 2 +- executor/cte_test.go | 71 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/executor/cte.go b/executor/cte.go index fb0aaccb3d..6ac26a5054 100644 --- a/executor/cte.go +++ b/executor/cte.go @@ -328,7 +328,7 @@ func (e *CTEExec) nextChunkLimit(req *chunk.Chunk) error { numRows := uint64(res.NumRows()) if e.cursor+numRows > e.limitEnd { numRows = e.limitEnd - e.cursor - req.Append(res.CopyConstructSel(), 0, int(numRows)+1) + req.Append(res.CopyConstructSel(), 0, int(numRows)) } else { req.SwapColumns(res.CopyConstructSel()) } diff --git a/executor/cte_test.go b/executor/cte_test.go index aa7c280442..4c6e1cf993 100644 --- a/executor/cte_test.go +++ b/executor/cte_test.go @@ -373,4 +373,75 @@ func (test *CTETestSuite) TestCTEWithLimit(c *check.C) { err = tk.QueryToErr("with recursive cte1 as (select 1/c1 c1 from t1 union select c1 + 1 c1 from cte1 where c1 < 2 limit 1) select * from cte1;") c.Assert(err, check.NotNil) c.Assert(err.Error(), check.Equals, "[executor:3636]Recursive query aborted after 1 iterations. Try increasing @@cte_max_recursion_depth to a larger value") + + tk.MustExec("set cte_max_recursion_depth = 1000;") + tk.MustExec("drop table if exists t1;") + tk.MustExec("create table t1(c1 int);") + tk.MustExec("insert into t1 values(1), (2), (3);") + + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union select c1 + 1 from cte1 limit 0 offset 2) select * from cte1;") + rows.Check(testkit.Rows()) + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union select c1 + 1 from cte1 limit 1 offset 2) select * from cte1;") + rows.Check(testkit.Rows("3")) + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union select c1 + 1 from cte1 limit 2 offset 2) select * from cte1;") + rows.Check(testkit.Rows("3", "4")) + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union select c1 + 1 from cte1 limit 3 offset 2) select * from cte1;") + rows.Check(testkit.Rows("3", "4", "5")) + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union select c1 + 1 from cte1 limit 4 offset 2) select * from cte1;") + rows.Check(testkit.Rows("3", "4", "5", "6")) + + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union select c1 + 1 from cte1 limit 0 offset 3) select * from cte1;") + rows.Check(testkit.Rows()) + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union select c1 + 1 from cte1 limit 1 offset 3) select * from cte1;") + rows.Check(testkit.Rows("4")) + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union select c1 + 1 from cte1 limit 2 offset 3) select * from cte1;") + rows.Check(testkit.Rows("4", "5")) + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union select c1 + 1 from cte1 limit 3 offset 3) select * from cte1;") + rows.Check(testkit.Rows("4", "5", "6")) + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union select c1 + 1 from cte1 limit 4 offset 3) select * from cte1;") + rows.Check(testkit.Rows("4", "5", "6", "7")) + + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union select c1 + 1 from cte1 limit 0 offset 4) select * from cte1;") + rows.Check(testkit.Rows()) + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union select c1 + 1 from cte1 limit 1 offset 4) select * from cte1;") + rows.Check(testkit.Rows("5")) + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union select c1 + 1 from cte1 limit 2 offset 4) select * from cte1;") + rows.Check(testkit.Rows("5", "6")) + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union select c1 + 1 from cte1 limit 3 offset 4) select * from cte1;") + rows.Check(testkit.Rows("5", "6", "7")) + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union select c1 + 1 from cte1 limit 4 offset 4) select * from cte1;") + rows.Check(testkit.Rows("5", "6", "7", "8")) + + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union all select c1 + 1 from cte1 limit 0 offset 2) select * from cte1;") + rows.Check(testkit.Rows()) + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union all select c1 + 1 from cte1 limit 1 offset 2) select * from cte1;") + rows.Check(testkit.Rows("3")) + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union all select c1 + 1 from cte1 limit 2 offset 2) select * from cte1;") + rows.Check(testkit.Rows("3", "2")) + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union all select c1 + 1 from cte1 limit 3 offset 2) select * from cte1;") + rows.Check(testkit.Rows("3", "2", "3")) + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union all select c1 + 1 from cte1 limit 4 offset 2) select * from cte1;") + rows.Check(testkit.Rows("3", "2", "3", "4")) + + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union all select c1 + 1 from cte1 limit 0 offset 3) select * from cte1;") + rows.Check(testkit.Rows()) + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union all select c1 + 1 from cte1 limit 1 offset 3) select * from cte1;") + rows.Check(testkit.Rows("2")) + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union all select c1 + 1 from cte1 limit 2 offset 3) select * from cte1;") + rows.Check(testkit.Rows("2", "3")) + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union all select c1 + 1 from cte1 limit 3 offset 3) select * from cte1;") + rows.Check(testkit.Rows("2", "3", "4")) + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union all select c1 + 1 from cte1 limit 4 offset 3) select * from cte1;") + rows.Check(testkit.Rows("2", "3", "4", "3")) + + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union all select c1 + 1 from cte1 limit 0 offset 4) select * from cte1;") + rows.Check(testkit.Rows()) + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union all select c1 + 1 from cte1 limit 1 offset 4) select * from cte1;") + rows.Check(testkit.Rows("3")) + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union all select c1 + 1 from cte1 limit 2 offset 4) select * from cte1;") + rows.Check(testkit.Rows("3", "4")) + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union all select c1 + 1 from cte1 limit 3 offset 4) select * from cte1;") + rows.Check(testkit.Rows("3", "4", "3")) + rows = tk.MustQuery("with recursive cte1(c1) as (select c1 from t1 union all select c1 + 1 from cte1 limit 4 offset 4) select * from cte1;") + rows.Check(testkit.Rows("3", "4", "3", "4")) }