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 an unexpected result of the inlineprojection of TopN #58771

Merged
merged 6 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 11 additions & 1 deletion pkg/planner/core/operator/logicalop/logical_top_n.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ func (lt *LogicalTopN) PruneColumns(parentUsedCols []*expression.Column, opt *op
child := lt.Children()[0]
var cols []*expression.Column

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

lt.ByItems, cols = pruneByItems(lt, lt.ByItems, opt)
parentUsedCols = append(parentUsedCols, cols...)
Expand All @@ -92,6 +93,15 @@ func (lt *LogicalTopN) PruneColumns(parentUsedCols []*expression.Column, opt *op
if err != nil {
return nil, err
}
// If the length of parentUsedCols is 0, it means that the parent plan does not need this plan to output related
// results, such as: select count(*) from t
// So we set the schema of topN to 0. After inlineprojection, the schema of topN will be set to the shortest column
// in its child plan, and this column will not be used later.
if len(snapParentUsedCols) == 0 {
lt.SetSchema(nil)
}
lt.InlineProjection(snapParentUsedCols, opt)
EricZequan marked this conversation as resolved.
Show resolved Hide resolved

return lt, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ go_test(
srcs = [
"hash64_equals_test.go",
"logical_mem_table_predicate_extractor_test.go",
"plan_execute_test.go",
],
flaky = True,
shard_count = 33,
shard_count = 34,
deps = [
"//pkg/domain",
"//pkg/expression",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package logicalop_test

import (
"testing"

"github.com/pingcap/tidb/pkg/testkit"
)

func TestIssue58743(t *testing.T) {
store := testkit.CreateMockStore(t)

tk := testkit.NewTestKit(t, store)

tk.MustExec("use test")
tk.MustExec(`
CREATE TABLE tlf5d55361 (
col_9 time NOT NULL,
col_10 float NOT NULL,
col_11 json NOT NULL,
col_12 date NOT NULL,
col_13 json NOT NULL,
col_14 tinyint NOT NULL,
col_15 date DEFAULT NULL,
col_16 tinyblob NOT NULL,
col_17 time DEFAULT '03:51:26',
PRIMARY KEY (col_14, col_9, col_10) /*T![clustered_index] CLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci
PARTITION BY RANGE (col_14)
(
PARTITION p0 VALUES LESS THAN (-128),
PARTITION p1 VALUES LESS THAN (-84),
PARTITION p2 VALUES LESS THAN (-41),
PARTITION p3 VALUES LESS THAN (-30)
);
`)
tk.MustExec(`
CREATE TABLE td8d55878 (
col_26 datetime DEFAULT NULL,
col_27 time DEFAULT NULL,
col_28 json DEFAULT NULL,
col_29 char(186) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT '4-BJKi',
col_30 date NOT NULL DEFAULT '1998-07-28',
col_31 datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk COLLATE=gbk_chinese_ci;
`)

tk.MustQuery(`
WITH cte_3 (col_128) AS (
SELECT /*+ USE_INDEX_MERGE(td8d55878 tlf5d55361)*/
MIN(td8d55878.col_26) AS r0
FROM tlf5d55361
JOIN td8d55878
GROUP BY tlf5d55361.col_17
HAVING tlf5d55361.col_17 <= '20:22:14.00' OR tlf5d55361.col_17 BETWEEN '21:56:23.00' AND '19:42:43.00'
ORDER BY r0
LIMIT 772780933
),
cte_4 (col_129) AS (
SELECT /*+ HASH_AGG()*/
SUM(tlf5d55361.col_14) AS r0
FROM td8d55878
JOIN tlf5d55361 ON tlf5d55361.col_17 = td8d55878.col_27
GROUP BY td8d55878.col_30
HAVING ISNULL(td8d55878.col_30) OR td8d55878.col_30 BETWEEN '2009-09-08' AND '1980-11-17'
)
SELECT SUM((cte_4.col_129 IN (0.65, 564617.3335, 45, 0.319, 0.4427)) IS TRUE)
FROM cte_4
JOIN cte_3;
`).Equal(nil)
}