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: support push part of order property down to the partition table #36108

Merged
merged 31 commits into from
Nov 29, 2022

Conversation

winoros
Copy link
Member

@winoros winoros commented Jul 11, 2022

What problem does this PR solve?

Issue Number: ref #26166 (a quick solution)

Problem Summary:

The partition table shields the partition information in the execution phase. So we cannot convert the order by index limit x to LIMIT with the order property pushed to the index kv.

What is changed and how it works?

This time we don't change the codes of the execution. So we cannot push the full order property down to the index kv.
We just change the plan from TopN(TiDB)->Reader(TiDB)->TopN(TiKV)->ScanKV(TiKV, no order) to TopN(TiDB)->Reader(TiDB)->Limit(TiKV)->ScanKV(TiKV, in order).

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

push part of the order property down to the TiKV.

@ti-chi-bot
Copy link
Member

ti-chi-bot commented Jul 11, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • AilinKid
  • fixdb

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added do-not-merge/needs-linked-issue release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 11, 2022
@winoros winoros added the sig/planner SIG: Planner label Jul 11, 2022
}

// If we reach here, the index matches the order property of the top-n, we could push a limit down.
copTsk.keepOrder = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like we don't need to set copTask.keepOrder to true.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for partitions maybe

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func buildIndexLookUpTask(ctx sessionctx.Context, t *copTask) *rootTask {
	newTask := &rootTask{cst: t.cst}
	p := PhysicalIndexLookUpReader{
		tablePlan:        t.tablePlan,
		indexPlan:        t.indexPlan,
		ExtraHandleCol:   t.extraHandleCol,
		CommonHandleCols: t.commonHandleCols,
		expectedCnt:      t.expectCnt,
		keepOrder:        t.keepOrder,
	}.Init(ctx, t.tablePlan.SelectBlockOffset())

This func uses it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh actually we don't need the keep order, remove it.

return nil, false
}
}
copTsk.keepOrder = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto.

}
tblScan = finalTblScanPlan.(*PhysicalTableScan)
}
if !copTsk.indexPlanFinished {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We didn't consider that idxScan might be nil in the logic below.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When indexPlanFinished is not true, there should be a index scan to be finished later.

planner/core/task.go Outdated Show resolved Hide resolved
Comment on lines 1042 to 1043
// pushTopNDownToDynamicPartition is a temp solution for partition table. It actually does the same thing as DataSource's isMatchProp.
func (p *PhysicalTopN) pushTopNDownToDynamicPartition(copTsk *copTask) (task, bool) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need more detailed comments since it's a tricky way to modify the plan.
I think we should clarify what the resulting plan would be like and why we have to use this method as a temporary solution.

Comment on lines +1159 to +1161
if tblScan.HandleCols.IsInt() {
pk := tblScan.HandleCols.GetCol(0)
if len(colsProp.SortItems) != 1 || !colsProp.SortItems[0].Col.Equal(p.SCtx(), pk) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is different from (*DataSource).isMatchProp. According to isMatchProp, tiflash doesn't support desc scan.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change only affects tikv part.

Comment on lines 1137 to 1148
if copTsk.tablePlan != nil && !idxScan.Table.IsCommonHandle && copTsk.extraHandleCol == nil {
// The clustered index would always add handle, so we don't need to consider that case.
copTsk.extraHandleCol = &expression.Column{
RetType: types.NewFieldType(mysql.TypeLonglong),
ID: model.ExtraHandleID,
UniqueID: idxScan.ctx.GetSessionVars().AllocPlanColumnID(),
}
tblScan.Schema().Append(copTsk.extraHandleCol)
tblScan.Columns = append(tblScan.Columns, model.NewExtraHandleColInfo())
copTsk.needExtraProj = true
copTsk.originSchema = idxScan.dataSourceSchema
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We missed the case that the int primary key is not _tidb_rowid but a column specified by the user.
I think we can modify the (*PhysicalTableScan).appendExtraHandleCol a bit and reuse that.

@sre-bot
Copy link
Contributor

sre-bot commented Jul 13, 2022

func (p *PhysicalTopN) pushTopNDownToDynamicPartition(copTsk *copTask) (task, bool) {
var err error
copTsk = copTsk.copy().(*copTask)
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the err isn't assigned anything before

planner/core/task.go Show resolved Hide resolved
}

// If we reach here, the index matches the order property of the top-n, we could push a limit down.
copTsk.keepOrder = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for partitions maybe

if len(constColsByCond) == 0 || idxPos > len(constColsByCond) || !constColsByCond[idxPos] {
found = false
break
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missed set found = true at the end? (indicating that we are sure that this col is constant, then just go to outer's continued column)

In:
order by (a,b,c), once index(a,c) exists and b=1, we can still access this path right?
reader -> limit -> selection -> indexScan

order by (a,c), once index(a,b,c) exists and b=1 is what actually it already does.

@ti-chi-bot ti-chi-bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 19, 2022
@winoros winoros requested a review from AilinKid July 20, 2022 09:32
@ti-chi-bot ti-chi-bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Nov 23, 2022
return true
}
if len(j) == 0 {
return false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the len(j) = 0 has different logic with the above?

@@ -310,17 +310,20 @@ func TestListPartitionPruner(t *testing.T) {
partitionPrunerData.LoadTestCases(t, &input, &output)
valid := false
for i, tt := range input {
if tt == "select * from t1 where (id = 1 and a = 1) or a is null" {
fmt.Println("1")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the debug info

return nil, false
}
finalTblScanPlan := copTsk.tablePlan
if len(finalTblScanPlan.Children()) > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/ if / for?

}
tblScan.Desc = isDesc
childProfile := copTsk.plan().statsInfo()
newCount := p.Offset + p.Count
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we consider there's a selection on the table side? the count here may won't pass the selection

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In such case, the plan should be Limit->Selection->TableScan. The selection should not matter

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get!make sense

Copy link
Contributor

@AilinKid AilinKid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rest LGTM

@AilinKid
Copy link
Contributor

/merge

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: 953273f

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Nov 29, 2022
Copy link
Contributor

@fixdb fixdb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@ti-chi-bot ti-chi-bot merged commit f9a6e47 into pingcap:master Nov 29, 2022
@sre-bot
Copy link
Contributor

sre-bot commented Nov 29, 2022

TiDB MergeCI notify

🔴 Bad News! New failing [1] after this pr merged.
These new failed integration tests seem to be caused by the current PR, please try to fix these new failed integration tests, thanks!

CI Name Result Duration Compare with Parent commit
idc-jenkins-ci-tidb/integration-common-test 🟥 failed 1, success 16, total 17 30 min New failing
idc-jenkins-ci-tidb/sqllogic-test-2 ✅ all 28 tests passed 6 min 28 sec Fixed
idc-jenkins-ci/integration-cdc-test 🟢 all 40 tests passed 20 min Existing passed
idc-jenkins-ci-tidb/common-test 🟢 all 11 tests passed 11 min Existing passed
idc-jenkins-ci-tidb/integration-ddl-test 🟢 all 6 tests passed 6 min 19 sec Existing passed
idc-jenkins-ci-tidb/mybatis-test 🟢 all 1 tests passed 6 min 9 sec Existing passed
idc-jenkins-ci-tidb/sqllogic-test-1 🟢 all 26 tests passed 5 min 50 sec Existing passed
idc-jenkins-ci-tidb/tics-test 🟢 all 1 tests passed 5 min 30 sec Existing passed
idc-jenkins-ci-tidb/integration-compatibility-test 🟢 all 1 tests passed 3 min 28 sec Existing passed
idc-jenkins-ci-tidb/plugin-test 🟢 build success, plugin test success 4min Existing passed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/planner SIG: Planner size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants