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
*: index refactor for table partition #7062
Conversation
/run-all-tests |
LGTM |
/run-integration-ddl-test tidb-test=pr/587 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
table/tables/index.go
Outdated
idxInfo *model.IndexInfo | ||
prefix kv.Key | ||
} | ||
|
||
// NewIndex builds a new Index object. | ||
func NewIndex(tableInfo *model.TableInfo, indexInfo *model.IndexInfo) table.Index { | ||
func NewIndex(partitionID int64, indexInfo *model.IndexInfo) table.Index { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a problem with the naming of partitionID
. Is it always being paritonID
? Is it possible being table ID
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. Please comment it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be partition ID or table ID, depends on whether the table is a partitioned table or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If that is the case, then partitionID
is not a good choice. It would be nice if you could change partitionID
to ID
and add some comments like "ID could be partitionID or table ID".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reset LGTM
@@ -334,6 +338,11 @@ type PhysicalUnionScan struct { | |||
Conditions []expression.Expression | |||
} | |||
|
|||
// IsPartition returns true and partition ID if it works on a partition. | |||
func (p *PhysicalIndexScan) IsPartition() (bool, int64) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about s/IsPartition/GetPartitionID/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think IsPartition
maybe better.
succ, _ := IsPartition()
if succ {
doSomething()
}
It's also flexible when we need the partition id:
if succ, pid := IsPartition(); succ {
doSomthingWith(pid)
}
GetPartitionID
looks unnatural if we define:
GetPartitionID(pid int64, bool) // wow, it maybe not a partition
If we use partition id == 0
to represent it's not a partition:
GetPartitionID(pid int64)
This code introduce an implicit assumption with the caller.
plan/rule_partition_processor.go
Outdated
@@ -105,6 +105,12 @@ func (s *partitionProcessor) prune(ds *DataSource) (LogicalPlan, error) { | |||
newDataSource.baseLogicalPlan = newBaseLogicalPlan(ds.context(), TypeTableScan, &newDataSource) | |||
newDataSource.isPartition = true | |||
newDataSource.partitionID = pi.Definitions[i].ID | |||
// TODO: Find a better way to handle here. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can remove this TODO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any suggestion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if we haven't find a way to optimize the old logic, we don't need a TODO.
table/tables/index.go
Outdated
idxInfo *model.IndexInfo | ||
prefix kv.Key | ||
} | ||
|
||
// NewIndex builds a new Index object. | ||
func NewIndex(tableInfo *model.TableInfo, indexInfo *model.IndexInfo) table.Index { | ||
func NewIndex(partitionID int64, indexInfo *model.IndexInfo) table.Index { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the first parameter always the partition id? Can it be the table id?
table/tables/index.go
Outdated
idxInfo *model.IndexInfo | ||
prefix kv.Key | ||
} | ||
|
||
// NewIndex builds a new Index object. | ||
func NewIndex(tableInfo *model.TableInfo, indexInfo *model.IndexInfo) table.Index { | ||
func NewIndex(partitionID int64, indexInfo *model.IndexInfo) table.Index { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. Please comment it.
ret.recordPrefix = tablecodec.GenTableRecordPrefix(pid) | ||
ret.indexPrefix = tablecodec.GenTableIndexPrefix(pid) | ||
return &ret | ||
return t.partitions[pid] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why change here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If GetPartition
function create a Partition
and Index
everytime it's called, it's bad for performance.
So I store those information when PartitionedTable
is created, and just get it here.
table/tables/tables.go
Outdated
|
||
idx := NewIndex(tblInfo, idxInfo) | ||
t.indices = append(t.indices, idx) | ||
if err := initTableCommon(&t.tableCommon, tblInfo, tblInfo.ID, columns, alloc); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the case of partitions, it seems that this function repeatedly builds the index. The first time is here, the other time is when building the index(https://github.com/pingcap/tidb/pull/7062/files#diff-c709fb799ac0eac6d7ea40b2278c90d7R69).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
plan/rule_partition_processor.go
Outdated
@@ -105,6 +105,12 @@ func (s *partitionProcessor) prune(ds *DataSource) (LogicalPlan, error) { | |||
newDataSource.baseLogicalPlan = newBaseLogicalPlan(ds.context(), TypeTableScan, &newDataSource) | |||
newDataSource.isPartition = true | |||
newDataSource.partitionID = pi.Definitions[i].ID | |||
// Find a better way to handle here. | |||
// There are so many place that refer to the old DataSource including the schema |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment is confusion, I think you should directly explain why the newDataSource.id
is set to the ds.id
. It's not because we hold the reference of the Datasource
, it's because we use the id
as the FromID
of the Column
s in the Expression
of parent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"use the id as the FromID of the Columns in the Expression of parent"
This is a kind of reference!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I mean, we should statement the comment in a straightforward way.
a34e8dd
to
5484c0c
Compare
LGTM |
What have you changed? (mandatory)
index
is changed to partition prefix, soAddRecord
for index would work finePartitionedTable.AddRecord
tonewPartitionedTable
What is the type of the changes? (mandatory)
How has this PR been tested? (mandatory)
unit test
The test case is still very naive, more testes will be add when I finish
admin check table
in the following PRsPTAL @zimulala @winkyao @shenli