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 flaky test TestPhysicalTableScanExtractCorrelatedCols #51355

Merged
merged 1 commit into from
Feb 27, 2024
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
39 changes: 30 additions & 9 deletions pkg/planner/core/physical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/pingcap/tidb/pkg/domain"
"github.com/pingcap/tidb/pkg/expression"
"github.com/pingcap/tidb/pkg/infoschema"
"github.com/pingcap/tidb/pkg/kv"
"github.com/pingcap/tidb/pkg/parser"
Expand Down Expand Up @@ -459,31 +460,51 @@ func TestPhysicalTableScanExtractCorrelatedCols(t *testing.T) {
p, ok := info.Plan.(core.Plan)
require.True(t, ok)

var findTableScan func(p core.Plan) *core.PhysicalTableScan
findTableScan = func(p core.Plan) *core.PhysicalTableScan {
var findSelection func(p core.Plan) *core.PhysicalSelection
findSelection = func(p core.Plan) *core.PhysicalSelection {
if p == nil {
return nil
}
switch v := p.(type) {
case *core.PhysicalTableScan:
if v.Table.Name.L == "t1" {
return v
case *core.PhysicalSelection:
if len(v.Children()) == 1 {
if ts, ok := v.Children()[0].(*core.PhysicalTableScan); ok && ts.Table.Name.L == "t1" {
return v
}
}
return nil
case *core.PhysicalTableReader:
return findTableScan(v.TablePlans[0])
for _, child := range v.TablePlans {
if sel := findSelection(child); sel != nil {
return sel
}
}
return nil
default:
physicayPlan := p.(core.PhysicalPlan)
for _, child := range physicayPlan.Children() {
if ts := findTableScan(child); ts != nil {
return ts
if sel := findSelection(child); sel != nil {
return sel
}
}
return nil
}
}
ts := findTableScan(p)
sel := findSelection(p)
require.NotNil(t, sel)
ts := sel.Children()[0].(*core.PhysicalTableScan)
require.NotNil(t, ts)
// manually push down the condition `client_no = c.company_no`
var selected expression.Expression
for _, cond := range sel.Conditions {
if sf, ok := cond.(*expression.ScalarFunction); ok && sf.Function.PbCode() == tipb.ScalarFuncSig_EQString {
selected = cond
break
}
}
if selected != nil {
core.PushedDown(sel, ts, []expression.Expression{selected}, 0.1)
}

pb, err := ts.ToPB(tk.Session().GetPlanCtx(), kv.TiFlash)
require.NoError(t, err)
Expand Down
12 changes: 9 additions & 3 deletions pkg/planner/core/tiflash_selection_late_materialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,16 @@ func predicatePushDownToTableScanImpl(sctx PlanContext, physicalSelection *Physi
return
}
logutil.BgLogger().Debug("planner: push down conditions to table scan", zap.String("table", physicalTableScan.Table.Name.L), zap.String("conditions", string(expression.SortedExplainExpressionList(sctx, selectedConds))))
PushedDown(physicalSelection, physicalTableScan, selectedConds, selectedSelectivity)
}

// PushedDown is used to push down the selected conditions from PhysicalSelection to PhysicalTableScan.
// Used in unit test, so it is exported.
func PushedDown(sel *PhysicalSelection, ts *PhysicalTableScan, selectedConds []expression.Expression, selectedSelectivity float64) {
// remove the pushed down conditions from selection
removeSpecificExprsFromSelection(physicalSelection, selectedConds)
removeSpecificExprsFromSelection(sel, selectedConds)
// add the pushed down conditions to table scan
physicalTableScan.LateMaterializationFilterCondition = selectedConds
ts.LateMaterializationFilterCondition = selectedConds
// Update the row count of table scan after pushing down the conditions.
physicalTableScan.StatsInfo().RowCount *= selectedSelectivity
ts.StatsInfo().RowCount *= selectedSelectivity
}