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 extract correlated column for expand in building phase will panic. #54988

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions pkg/planner/core/logical_expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ func (p *LogicalExpand) ExhaustPhysicalPlans(prop *property.PhysicalProperty) ([

// ExtractCorrelatedCols implements base.LogicalPlan.<15th> interface.
func (p *LogicalExpand) ExtractCorrelatedCols() []*expression.CorrelatedColumn {
// if p.LevelExprs is nil, it means the GenLevelProjections has not been called yet,
// which is done in logical optimizing phase. While for building correlated subquery
// plan, the ExtractCorrelatedCols will be called once after building, so we should
// distinguish the case here.
if p.LevelExprs == nil {
// since level projections generation don't produce any correlated columns, just
// return nil.
return nil
}
corCols := make([]*expression.CorrelatedColumn, 0, len(p.LevelExprs[0]))
for _, lExpr := range p.LevelExprs {
for _, expr := range lExpr {
Expand Down
23 changes: 23 additions & 0 deletions tests/integrationtest/r/executor/expand.result
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,26 @@ GROUP BY product WITH ROLLUP) AS tmp
WHERE product is null;
product SUM
NULL 7785

SELECT product FROM t1 WHERE EXISTS
(SELECT product, country_id , SUM(profit) FROM t1 AS t2
WHERE t1.product=t2.product GROUP BY product, country_id WITH ROLLUP
HAVING SUM(profit) > 6000);
product
Computer
Computer
Computer
Computer
Computer

SELECT product, country_id , year, SUM(profit) FROM t1
GROUP BY product, country_id, year HAVING country_id is NULL;
product country_id year SUM(profit)
SELECT CONCAT(':',product,':'), SUM(profit), AVG(profit) FROM t1
GROUP BY product WITH ROLLUP;
CONCAT(':',product,':') SUM(profit) AVG(profit)
NULL 7785 519.0000
:TV: 600 120.0000
:Calculator: 275 68.7500
:Phone: 10 10.0000
:Computer: 6900 1380.0000
19 changes: 19 additions & 0 deletions tests/integrationtest/t/executor/expand.test
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,22 @@ t1.country_id=t2.country_id GROUP BY product, country, year WITH ROLLUP;
SELECT product, `SUM` FROM (SELECT product, SUM(profit) AS 'sum' FROM t1
GROUP BY product WITH ROLLUP) AS tmp
WHERE product is null;

--echo
--sorted_result
SELECT product FROM t1 WHERE EXISTS
(SELECT product, country_id , SUM(profit) FROM t1 AS t2
WHERE t1.product=t2.product GROUP BY product, country_id WITH ROLLUP
HAVING SUM(profit) > 6000);

--echo
--sorted_result
# The following does not return the expected answer, but this is a limitation
# in the implementation so we should just document it
SELECT product, country_id , year, SUM(profit) FROM t1
GROUP BY product, country_id, year HAVING country_id is NULL;

--echo
--sorted_result
SELECT CONCAT(':',product,':'), SUM(profit), AVG(profit) FROM t1
GROUP BY product WITH ROLLUP;