Skip to content

Commit 2a3542c

Browse files
authored
planner: fix an unexpected result of the inlineprojection of TopN (pingcap#58771)
ref pingcap#58743
1 parent 510d003 commit 2a3542c

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed

pkg/planner/core/operator/logicalop/logical_top_n.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ func (lt *LogicalTopN) PruneColumns(parentUsedCols []*expression.Column, opt *op
8383
child := lt.Children()[0]
8484
var cols []*expression.Column
8585

86-
lt.InlineProjection(parentUsedCols, opt)
86+
snapParentUsedCols := make([]*expression.Column, 0, len(parentUsedCols))
87+
snapParentUsedCols = append(snapParentUsedCols, parentUsedCols...)
8788

8889
lt.ByItems, cols = pruneByItems(lt, lt.ByItems, opt)
8990
parentUsedCols = append(parentUsedCols, cols...)
@@ -92,6 +93,15 @@ func (lt *LogicalTopN) PruneColumns(parentUsedCols []*expression.Column, opt *op
9293
if err != nil {
9394
return nil, err
9495
}
96+
// If the length of parentUsedCols is 0, it means that the parent plan does not need this plan to output related
97+
// results, such as: select count(*) from t
98+
// So we set the schema of topN to 0. After inlineprojection, the schema of topN will be set to the shortest column
99+
// in its child plan, and this column will not be used later.
100+
if len(snapParentUsedCols) == 0 {
101+
lt.SetSchema(nil)
102+
}
103+
lt.InlineProjection(snapParentUsedCols, opt)
104+
95105
return lt, nil
96106
}
97107

pkg/planner/core/operator/logicalop/logicalop_test/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ go_test(
66
srcs = [
77
"hash64_equals_test.go",
88
"logical_mem_table_predicate_extractor_test.go",
9+
"plan_execute_test.go",
910
],
1011
flaky = True,
11-
shard_count = 33,
12+
shard_count = 34,
1213
deps = [
1314
"//pkg/domain",
1415
"//pkg/expression",
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2019 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package logicalop_test
16+
17+
import (
18+
"testing"
19+
20+
"github.com/pingcap/tidb/pkg/testkit"
21+
)
22+
23+
func TestIssue58743(t *testing.T) {
24+
store := testkit.CreateMockStore(t)
25+
26+
tk := testkit.NewTestKit(t, store)
27+
28+
tk.MustExec("use test")
29+
tk.MustExec(`
30+
CREATE TABLE tlf5d55361 (
31+
col_9 time NOT NULL,
32+
col_10 float NOT NULL,
33+
col_11 json NOT NULL,
34+
col_12 date NOT NULL,
35+
col_13 json NOT NULL,
36+
col_14 tinyint NOT NULL,
37+
col_15 date DEFAULT NULL,
38+
col_16 tinyblob NOT NULL,
39+
col_17 time DEFAULT '03:51:26',
40+
PRIMARY KEY (col_14, col_9, col_10) /*T![clustered_index] CLUSTERED */
41+
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci
42+
PARTITION BY RANGE (col_14)
43+
(
44+
PARTITION p0 VALUES LESS THAN (-128),
45+
PARTITION p1 VALUES LESS THAN (-84),
46+
PARTITION p2 VALUES LESS THAN (-41),
47+
PARTITION p3 VALUES LESS THAN (-30)
48+
);
49+
`)
50+
tk.MustExec(`
51+
CREATE TABLE td8d55878 (
52+
col_26 datetime DEFAULT NULL,
53+
col_27 time DEFAULT NULL,
54+
col_28 json DEFAULT NULL,
55+
col_29 char(186) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT '4-BJKi',
56+
col_30 date NOT NULL DEFAULT '1998-07-28',
57+
col_31 datetime NOT NULL
58+
) ENGINE=InnoDB DEFAULT CHARSET=gbk COLLATE=gbk_chinese_ci;
59+
`)
60+
61+
tk.MustQuery(`
62+
WITH cte_3 (col_128) AS (
63+
SELECT /*+ USE_INDEX_MERGE(td8d55878 tlf5d55361)*/
64+
MIN(td8d55878.col_26) AS r0
65+
FROM tlf5d55361
66+
JOIN td8d55878
67+
GROUP BY tlf5d55361.col_17
68+
HAVING tlf5d55361.col_17 <= '20:22:14.00' OR tlf5d55361.col_17 BETWEEN '21:56:23.00' AND '19:42:43.00'
69+
ORDER BY r0
70+
LIMIT 772780933
71+
),
72+
cte_4 (col_129) AS (
73+
SELECT /*+ HASH_AGG()*/
74+
SUM(tlf5d55361.col_14) AS r0
75+
FROM td8d55878
76+
JOIN tlf5d55361 ON tlf5d55361.col_17 = td8d55878.col_27
77+
GROUP BY td8d55878.col_30
78+
HAVING ISNULL(td8d55878.col_30) OR td8d55878.col_30 BETWEEN '2009-09-08' AND '1980-11-17'
79+
)
80+
SELECT SUM((cte_4.col_129 IN (0.65, 564617.3335, 45, 0.319, 0.4427)) IS TRUE)
81+
FROM cte_4
82+
JOIN cte_3;
83+
`).Equal(nil)
84+
}

0 commit comments

Comments
 (0)