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: don't decorrelate the APPLY when the inner's projection reference no column #32370

Merged
merged 5 commits into from
Feb 23, 2022
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
Prev Previous commit
Next Next commit
let the ORDER BY case be decorrelated
  • Loading branch information
winoros committed Feb 16, 2022
commit 1027655cca8f7d6f55326aa9c3f1417f3d671f31
2 changes: 1 addition & 1 deletion expression/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func ExtractColumnsAndCorColumnsFromExpressions(result []*Column, list []Express
}

// ExtractColumnSet extracts the different values of `UniqueId` for columns in expressions.
func ExtractColumnSet(exprs []Expression) *intsets.Sparse {
func ExtractColumnSet(exprs ...Expression) *intsets.Sparse {
set := &intsets.Sparse{}
for _, expr := range exprs {
extractColumnSet(expr, set)
Expand Down
9 changes: 8 additions & 1 deletion planner/core/rule_decorrelate.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,14 @@ func (s *decorrelateSolver) optimize(ctx context.Context, p LogicalPlan, opt *lo
return s.optimize(ctx, p, opt)
}
} else if proj, ok := innerPlan.(*LogicalProjection); ok {
if colSet := expression.ExtractColumnSet(proj.Exprs); colSet.IsEmpty() && apply.JoinType == LeftOuterJoin {
allConst := true
for _, expr := range proj.Exprs {
if len(expression.ExtractCorColumns(expr)) > 0 || !expression.ExtractColumnSet(expr).IsEmpty() {
allConst = false
break
}
}
if allConst && apply.JoinType == LeftOuterJoin {
// If the projection just references some constant. We cannot directly pull it up when the APPLY is an outer join.
// e.g. select (select 1 from t1 where t1.a=t2.a) from t2; When the t1.a=t2.a is false the join's output is NULL.
// But if we pull the projection upon the APPLY. It will return 1 since the projection is evaluated after the join.
Expand Down
2 changes: 1 addition & 1 deletion planner/core/rule_partition_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ func makePartitionByFnCol(sctx sessionctx.Context, columns []*expression.Column,
monotonous = getMonotoneMode(raw.FuncName.L)
// Check the partitionExpr is in the form: fn(col, ...)
// There should be only one column argument, and it should be the first parameter.
if expression.ExtractColumnSet(args).Len() == 1 {
if expression.ExtractColumnSet(args...).Len() == 1 {
if col1, ok := args[0].(*expression.Column); ok {
col = col1
}
Expand Down
2 changes: 1 addition & 1 deletion planner/core/testdata/plan_suite_unexported_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@
"Err": false
},
{
"Best": "Apply{UnionAll{UnionAll{Dual->Projection->Dual->Projection}->Aggr(firstrow(Column#4))->Projection->Dual->Projection}->Dual->Projection}->Sort->Projection",
"Best": "Join{UnionAll{UnionAll{Dual->Projection->Dual->Projection}->Aggr(firstrow(Column#4))->Projection->Dual->Projection}->Dual}->Sort->Projection",
"Err": false
}
]
Expand Down
2 changes: 1 addition & 1 deletion util/ranger/detacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func extractIndexPointRangesForCNF(sctx sessionctx.Context, conds []expression.E
offset := int(-1)
for i, cond := range conds {
tmpConds := []expression.Expression{cond}
colSets := expression.ExtractColumnSet(tmpConds)
colSets := expression.ExtractColumnSet(cond)
if colSets.Len() == 0 {
continue
}
Expand Down