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

ddl:support drop index for the partitioned table. #7306

Merged
merged 6 commits into from
Aug 17, 2018

Conversation

ciscoxll
Copy link
Contributor

@ciscoxll ciscoxll commented Aug 7, 2018

What have you changed? (mandatory)

support drop index for the partitioned table

Take this as an example:

mysql> set @@tidb_enable_table_partition = 1;
Query OK, 0 rows affected (0.00 sec)

mysql> create table tr(
    ->         id int, name varchar(50),
    ->         purchased date
    ->     )
    ->     partition by range( year(purchased) ) (
    ->         partition p0 values less than (1990),
    ->         partition p1 values less than (1995),
    ->         partition p2 values less than (2000),
    ->         partition p3 values less than (2005),
    ->         partition p4 values less than (2010),
    ->         partition p5 values less than (2015)
    ->        );
Query OK, 0 rows affected (0.01 sec)

mysql>  INSERT INTO tr VALUES
    ->     (1, 'desk organiser', '2003-10-15'),
    ->     (2, 'alarm clock', '1997-11-05'),
    ->     (3, 'chair', '2009-03-10'),
    ->     (4, 'bookcase', '1989-01-10'),
    ->     (5, 'exercise bike', '2014-05-09'),
    ->     (6, 'sofa', '1987-06-05'),
    ->     (7, 'espresso maker', '2011-11-22'),
    ->     (8, 'aquarium', '1992-08-04'),
    ->     (9, 'study desk', '2006-09-16'),
    ->     (10, 'lava lamp', '1998-12-25');
Query OK, 10 rows affected (0.10 sec)

mysql> create index idx1 on tr (purchased);
Query OK, 0 rows affected (0.04 sec)

mysql> select count(purchased) from tr use index(idx1);
+------------------+
| count(purchased) |
+------------------+
|               10 |
+------------------+
1 row in set (0.00 sec)

mysql> drop index idx1 on tr;
Query OK, 0 rows affected (0.03 sec)

mysql> select count(purchased) from tr use index(idx1);
ERROR 1176 (42000): Key 'idx1' doesn't exist in table 'tr'

What is the type of the changes? (mandatory)

New feature

  • New feature (non-breaking change which adds functionality)

How has this PR been tested? (mandatory)

Unit tests.

Does this PR affect documentation (docs/docs-cn) update? (mandatory)

No

Does this PR affect tidb-ansible update? (mandatory)

No

Does this PR need to be added to the release notes? (mandatory)

No

ddl/db_test.go Outdated
(9, 'study desk', '2006-09-16'),
(10, 'lava lamp', '1998-12-25');`)

tk.MustExec("create index idx1 on tr (purchased)")
Copy link
Contributor

Choose a reason for hiding this comment

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

It test drop index, so how about create the table with index together?

ddl/db_test.go Outdated
tk.MustQuery("select count(purchased) from tr use index(idx1)").Check(testkit.Rows("10"))
tk.MustExec("admin check table tr")
// Wait for add index done.
time.Sleep(waitForCleanDataInterval)
Copy link
Contributor

Choose a reason for hiding this comment

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

There is no need to wait.
create index idx1 on tr (purchased) blocks for a while, as long as it returns, add index will be done.

@@ -436,7 +436,7 @@ func onDropIndex(t *meta.Meta, job *model.Job) (ver int64, _ error) {
job.Args[0] = indexInfo.ID
} else {
job.FinishTableJob(model.JobStateDone, model.StateNone, ver, tblInfo)
job.Args = append(job.Args, indexInfo.ID)
job.Args = append(job.Args, indexInfo.ID, getPartitionIDs(tblInfo))
Copy link
Contributor

Choose a reason for hiding this comment

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

If the table is not a partition, what will getPartitionIDs return?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

getPartitionIDs will return []int64{}.

@ciscoxll ciscoxll force-pushed the drop-partition-index branch from f8f0298 to 95355b8 Compare August 10, 2018 02:14
@@ -81,10 +81,10 @@ func MockTableFromMeta(tblInfo *model.TableInfo) table.Table {

var t Table
initTableCommon(&t.tableCommon, tblInfo, tblInfo.ID, columns, nil)
if err := initTableIndices(&t.tableCommon); 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.

Is this change relevant to the PR?

ddl/db_test.go Outdated
c.Assert(err, IsNil, Commentf("err:%v", errors.ErrorStack(err)))
case <-ticker.C:
step := 10
for i := num; i < num+step; i++ {
Copy link
Contributor

Choose a reason for hiding this comment

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

The random data will always locate in the first partition...

@ciscoxll ciscoxll force-pushed the drop-partition-index branch from 95355b8 to 56b719e Compare August 10, 2018 09:37
@ciscoxll
Copy link
Contributor Author

@tiancaiamao @winkyao PTAL.

@tiancaiamao
Copy link
Contributor

LGTM

@tiancaiamao tiancaiamao added the status/LGT1 Indicates that a PR has LGTM 1. label Aug 13, 2018
@ciscoxll
Copy link
Contributor Author

@winkyao @zimulala PTAL.

}
}
return nil
}
Copy link
Member

Choose a reason for hiding this comment

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

if len(partitionIDs) > 0 {
} else {
}
This could make code more clear.

@@ -279,9 +279,20 @@ func insertJobIntoDeleteRangeTable(ctx sessionctx.Context, job *model.Job) error
tableID := job.TableID
var indexName interface{}
var indexID int64
if err := job.DecodeArgs(&indexName, &indexID); err != nil {
var partitionIDs []int64
if err := job.DecodeArgs(&indexName, &indexID, &partitionIDs); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

We have the job.TableID. Could we get the partition IDs from the info schema instead of job arguments?

Copy link
Contributor

Choose a reason for hiding this comment

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

When modifying table schema, there is a new table info and an old one.
It's easy to get the stale IDs if we get it from the info schema. @shenli

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@shenli Existing function getPartitionIDs.

func getPartitionIDs(table *model.TableInfo) []int64 {
	if table.GetPartitionInfo() == nil {
		return []int64{}
	}
	partitionIDs := make([]int64, 0, len(table.Partition.Definitions))
	for _, def := range table.Partition.Definitions {
		partitionIDs = append(partitionIDs, def.ID)
	}
	return partitionIDs
}

Is it clearer.

Copy link
Contributor

Choose a reason for hiding this comment

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

In drop index, because the ddl of same table is serial executed, so get partition IDs from info schema is ok.
But I remain neutral, get the partition IDs from the info schema or use job arguments are both ok for me. >v<

Copy link
Member

Choose a reason for hiding this comment

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

"It's easy to get the stale IDs if we get it from the info schema." Can not understand this. We should guarantee that we will get the correct schema when doing DDL.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can ignore the comment @shenli , I just give a hint on how to achieve this...

We should guarantee that we will get the correct schema when doing DDL.

Copy link
Member

Choose a reason for hiding this comment

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

@ciscoxll Please address the comment. We do not need to put partition IDs in the job argument.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do agree with shenli. please address the comment.

Copy link
Contributor

Choose a reason for hiding this comment

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

@shenli @winkyao
I think it's OK. The processing here is unified with other places.

ddl/db_test.go Outdated
for i := num; i < num+step; i++ {
rnd := rand.Intn(num)
s.mustExec(c, "update partition_drop_idx set c2 = 1 where c1 = ?", rnd)
s.mustExec(c, "insert into partition_drop_idx values (?, ?, ?)", i, i, i)
Copy link
Contributor

Choose a reason for hiding this comment

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

i may more than 20, then, insert into partition_drop_idx values (20,20,20)" should return error: (1604, 'locate partition failed')

@ciscoxll
Copy link
Contributor Author

@winkyao @zimulala PTAL.

@winkyao
Copy link
Contributor

winkyao commented Aug 15, 2018

You can use interface to get InfoSchema to solve the cycle import problem.

@ciscoxll
Copy link
Contributor Author

@shenli @winkyao @crazycs520 PTAL.

Copy link
Contributor

@crazycs520 crazycs520 left a comment

Choose a reason for hiding this comment

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

LGTM

@ciscoxll ciscoxll added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Aug 16, 2018
Copy link
Contributor

@zimulala zimulala left a comment

Choose a reason for hiding this comment

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

LGTM

@zimulala zimulala added status/LGT3 The PR has already had 3 LGTM. and removed status/LGT2 Indicates that a PR has LGTM 2. labels Aug 17, 2018
@zimulala
Copy link
Contributor

/run-all-tests

Copy link
Contributor

@zhexuany zhexuany left a comment

Choose a reason for hiding this comment

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

LGTM

@zhexuany zhexuany merged commit 8199f60 into pingcap:master Aug 17, 2018
@ciscoxll ciscoxll deleted the drop-partition-index branch August 17, 2018 06:41
@sre-bot sre-bot added the contribution This PR is from a community contributor. label Dec 18, 2019
@you06 you06 added the sig/sql-infra SIG: SQL Infra label Mar 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution This PR is from a community contributor. sig/sql-infra SIG: SQL Infra status/LGT3 The PR has already had 3 LGTM.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants