Skip to content

Commit

Permalink
Bug#28573894 ALTER PARTITIONED TABLE ADD AUTO_INCREMENT DIFF RESULT D…
Browse files Browse the repository at this point in the history
…EPENDING ON ALGORITHM

For partitioned table, ensure that the AUTO_INCREMENT values will
be assigned from the same sequence. This is based on the following
change in MySQL 5.6.44:

commit aaba359c13d9200747a609730dafafc3b63cd4d6
Author: Rahul Malik <rahul.m.malik@oracle.com>
Date:   Mon Feb 4 13:31:41 2019 +0530

    Bug#28573894 ALTER PARTITIONED TABLE ADD AUTO_INCREMENT DIFF RESULT DEPENDING ON ALGORITHM

    Problem:
    When a partition table is in-place altered to add an auto-increment column,
    then its values are starting over for each partition.

    Analysis:
    In the case of in-place alter, InnoDB is creating a new sequence object
    for each partition. It is default initialized. So auto-increment columns
    start over for each partition.

    Fix:
    Assign old sequence of the partition to the sequence of next partition
    so it won't start over.

    RB#21148
    Reviewed by Bin Su <bin.x.su@oracle.com>
  • Loading branch information
dr-m committed Apr 25, 2019
1 parent 9e7bcb0 commit 1cd31bc
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 7 deletions.
42 changes: 42 additions & 0 deletions mysql-test/r/partition_innodb.result
Original file line number Diff line number Diff line change
Expand Up @@ -918,3 +918,45 @@ ERROR HY000: CHECK OPTION failed 'test.v'
SET GLOBAL innodb_stats_persistent= @save_isp;
DROP view v;
DROP TABLE t;
#
# Bug#28573894 ALTER PARTITIONED TABLE ADD AUTO_INCREMENT DIFF RESULT
#
CREATE TABLE t (a VARCHAR(10) NOT NULL,b INT,PRIMARY KEY (b)) ENGINE=INNODB
PARTITION BY RANGE (b)
(PARTITION pa VALUES LESS THAN (2),
PARTITION pb VALUES LESS THAN (20),
PARTITION pc VALUES LESS THAN (30),
PARTITION pd VALUES LESS THAN (40));
INSERT INTO t
VALUES('A',0),('B',1),('C',2),('D',3),('E',4),('F',5),('G',25),('H',35);
CREATE TABLE t_copy LIKE t;
INSERT INTO t_copy SELECT * FROM t;
ALTER TABLE t ADD COLUMN r INT UNSIGNED NOT NULL AUTO_INCREMENT,
ADD UNIQUE KEY (r,b);
affected rows: 0
info: Records: 0 Duplicates: 0 Warnings: 0
ALTER TABLE t_copy ADD COLUMN r INT UNSIGNED NOT NULL AUTO_INCREMENT,
ADD UNIQUE KEY (r,b), ALGORITHM=COPY;
affected rows: 8
info: Records: 8 Duplicates: 0 Warnings: 0
SELECT * FROM t;
a b r
A 0 1
B 1 2
C 2 3
D 3 4
E 4 5
F 5 6
G 25 7
H 35 8
SELECT * FROM t_copy;
a b r
A 0 1
B 1 2
C 2 3
D 3 4
E 4 5
F 5 6
G 25 7
H 35 8
DROP TABLE t,t_copy;
24 changes: 24 additions & 0 deletions mysql-test/t/partition_innodb.test
Original file line number Diff line number Diff line change
Expand Up @@ -1028,3 +1028,27 @@ SET GLOBAL innodb_stats_persistent= @save_isp;
DROP view v;
DROP TABLE t;

--echo #
--echo # Bug#28573894 ALTER PARTITIONED TABLE ADD AUTO_INCREMENT DIFF RESULT
--echo #
CREATE TABLE t (a VARCHAR(10) NOT NULL,b INT,PRIMARY KEY (b)) ENGINE=INNODB
PARTITION BY RANGE (b)
(PARTITION pa VALUES LESS THAN (2),
PARTITION pb VALUES LESS THAN (20),
PARTITION pc VALUES LESS THAN (30),
PARTITION pd VALUES LESS THAN (40));

INSERT INTO t
VALUES('A',0),('B',1),('C',2),('D',3),('E',4),('F',5),('G',25),('H',35);
CREATE TABLE t_copy LIKE t;
INSERT INTO t_copy SELECT * FROM t;

--enable_info
ALTER TABLE t ADD COLUMN r INT UNSIGNED NOT NULL AUTO_INCREMENT,
ADD UNIQUE KEY (r,b);
ALTER TABLE t_copy ADD COLUMN r INT UNSIGNED NOT NULL AUTO_INCREMENT,
ADD UNIQUE KEY (r,b), ALGORITHM=COPY;
--disable_info
SELECT * FROM t;
SELECT * FROM t_copy;
DROP TABLE t,t_copy;
11 changes: 8 additions & 3 deletions sql/ha_partition.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Copyright (c) 2005, 2017, Oracle and/or its affiliates.
Copyright (c) 2009, 2017, MariaDB
Copyright (c) 2005, 2019, Oracle and/or its affiliates.
Copyright (c) 2009, 2019, MariaDB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -8329,7 +8329,12 @@ bool ha_partition::inplace_alter_table(TABLE *altered_table,

for (index= 0; index < m_tot_parts && !error; index++)
{
ha_alter_info->handler_ctx= part_inplace_ctx->handler_ctx_array[index];
if ((ha_alter_info->handler_ctx=
part_inplace_ctx->handler_ctx_array[index]) != NULL
&& index != 0)
ha_alter_info->handler_ctx->set_shared_data
(*part_inplace_ctx->handler_ctx_array[index - 1]);

if (m_file[index]->ha_inplace_alter_table(altered_table,
ha_alter_info))
error= true;
Expand Down
5 changes: 3 additions & 2 deletions sql/handler.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef HANDLER_INCLUDED
#define HANDLER_INCLUDED
/*
Copyright (c) 2000, 2016, Oracle and/or its affiliates.
Copyright (c) 2009, 2018, MariaDB
Copyright (c) 2000, 2019, Oracle and/or its affiliates.
Copyright (c) 2009, 2019, MariaDB
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -1833,6 +1833,7 @@ class inplace_alter_handler_ctx : public Sql_alloc
inplace_alter_handler_ctx() {}

virtual ~inplace_alter_handler_ctx() {}
virtual void set_shared_data(const inplace_alter_handler_ctx& ctx) {}
};


Expand Down
19 changes: 18 additions & 1 deletion storage/innobase/handler/handler0alter.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*****************************************************************************
Copyright (c) 2005, 2018, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2005, 2019, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2013, 2019, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify it under
Expand Down Expand Up @@ -2178,6 +2178,23 @@ struct ha_innobase_inplace_ctx : public inplace_alter_handler_ctx
@return whether the table will be rebuilt */
bool need_rebuild () const { return(old_table != new_table); }

/** Share context between partitions.
@param[in] ctx context from another partition of the table */
void set_shared_data(const inplace_alter_handler_ctx& ctx)
{
if (add_autoinc != ULINT_UNDEFINED) {
const ha_innobase_inplace_ctx& ha_ctx =
static_cast<const ha_innobase_inplace_ctx&>
(ctx);
/* When adding an AUTO_INCREMENT column to a
partitioned InnoDB table, we must share the
sequence for all partitions. */
ut_ad(ha_ctx.add_autoinc == add_autoinc);
ut_ad(ha_ctx.sequence.last());
sequence = ha_ctx.sequence;
}
}

private:
// Disable copying
ha_innobase_inplace_ctx(const ha_innobase_inplace_ctx&);
Expand Down
19 changes: 18 additions & 1 deletion storage/xtradb/handler/handler0alter.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*****************************************************************************
Copyright (c) 2005, 2018, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2005, 2019, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2017, 2019, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify it under
Expand Down Expand Up @@ -2181,6 +2181,23 @@ struct ha_innobase_inplace_ctx : public inplace_alter_handler_ctx
@return whether the table will be rebuilt */
bool need_rebuild () const { return(old_table != new_table); }

/** Share context between partitions.
@param[in] ctx context from another partition of the table */
void set_shared_data(const inplace_alter_handler_ctx& ctx)
{
if (add_autoinc != ULINT_UNDEFINED) {
const ha_innobase_inplace_ctx& ha_ctx =
static_cast<const ha_innobase_inplace_ctx&>
(ctx);
/* When adding an AUTO_INCREMENT column to a
partitioned InnoDB table, we must share the
sequence for all partitions. */
ut_ad(ha_ctx.add_autoinc == add_autoinc);
ut_ad(ha_ctx.sequence.last());
sequence = ha_ctx.sequence;
}
}

private:
// Disable copying
ha_innobase_inplace_ctx(const ha_innobase_inplace_ctx&);
Expand Down

0 comments on commit 1cd31bc

Please sign in to comment.