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 index-out-of-range error when checking only_full_group_by and make sure limit outputs no more columns than its child #25693

Closed
Closed
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
28 changes: 28 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3205,3 +3205,31 @@ func (s *testIntegrationSerialSuite) TestMergeContinuousSelections(c *C) {
res.Check(testkit.Rows(output[i].Plan...))
}
}

func (s *testIntegrationSuite) TestIssue23839(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists BB")
tk.MustExec("CREATE TABLE `BB` (\n" +
" `col_int` int(11) DEFAULT NULL,\n" +
" `col_varchar_10` varchar(10) DEFAULT NULL,\n" +
" `pk` int(11) NOT NULL AUTO_INCREMENT,\n" +
" `col_int_not_null` int(11) NOT NULL,\n" +
" `col_decimal` decimal(10,0) DEFAULT NULL,\n" +
" `col_datetime` datetime DEFAULT NULL,\n" +
" `col_decimal_not_null` decimal(10,0) NOT NULL,\n" +
" `col_datetime_not_null` datetime NOT NULL,\n" +
" `col_varchar_10_not_null` varchar(10) NOT NULL,\n" +
" PRIMARY KEY (`pk`) /*T![clustered_index] CLUSTERED */\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=2000001")
tk.Exec("explain SELECT OUTR . col2 AS X FROM (SELECT INNR . col1 as col1, SUM( INNR . col2 ) as col2 FROM (SELECT INNR . `col_int_not_null` + 1 as col1, INNR . `pk` as col2 FROM BB AS INNR) AS INNR GROUP BY col1) AS OUTR2 INNER JOIN (SELECT INNR . col1 as col1, MAX( INNR . col2 ) as col2 FROM (SELECT INNR . `col_int_not_null` + 1 as col1, INNR . `pk` as col2 FROM BB AS INNR) AS INNR GROUP BY col1) AS OUTR ON OUTR2.col1 = OUTR.col1 GROUP BY OUTR . col1, OUTR2 . col1 HAVING X <> 'b'")
}

func (s *testIntegrationSuite) TestLimitWindowColPrune(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int)")
tk.MustExec("insert into t values(1)")
tk.MustQuery("select count(a) f1, row_number() over (order by count(a)) as f2 from t limit 1").Check(testkit.Rows("1 1"))
}
2 changes: 1 addition & 1 deletion planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2676,7 +2676,7 @@ func checkColFuncDepend(
Name: colInfo.Name,
}
pIdx, err := expression.FindFieldName(p.OutputNames(), pkName)
if err != nil {
if err != nil || pIdx < 0 {
primaryFuncDepend = false
break
}
Expand Down
10 changes: 8 additions & 2 deletions planner/core/rule_column_pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,14 @@ func (p *LogicalLimit) PruneColumns(parentUsedCols []*expression.Column) error {
return nil
}

p.inlineProjection(parentUsedCols)
return p.children[0].PruneColumns(parentUsedCols)
savedUsedCols := make([]*expression.Column, len(parentUsedCols))
copy(savedUsedCols, parentUsedCols)
if err := p.children[0].PruneColumns(parentUsedCols); err != nil {
return err
}
p.schema = nil
p.inlineProjection(savedUsedCols)
return nil
}

func (*columnPruner) name() string {
Expand Down