-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Online DDL: better support for range partitioning #15698
Changes from 14 commits
e8a537a
919fcd9
5dca6b4
49b31dc
89a1132
195c182
0f4f100
0943790
2720761
27afaf7
2b0cebf
ac0d601
21cf7ef
a102bda
b888e2b
5386217
b358c3c
c0b7a45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package schemadiff | ||
|
||
import "vitess.io/vitess/go/vt/sqlparser" | ||
|
||
// AlterTableRotatesRangePartition answers `true` when the given ALTER TABLE statemnts performas any sort | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Obviously minor, but There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
// of range partition rotation, that is applicable immediately and without moving data. | ||
// Such would be: | ||
// - Dropping any partition(s) | ||
// - Adding a new partition (empty, at the end of the list) | ||
func AlterTableRotatesRangePartition(alterTable *sqlparser.AlterTable) (bool, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As we discussed via Slack, I think that we should at least check that this is indeed a RANGE partition change (vs. e.g. LIST). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
spec := alterTable.PartitionSpec | ||
if spec == nil { | ||
return false, nil | ||
} | ||
errorResult := func(conflictingNode sqlparser.SQLNode) error { | ||
return &PartitionSpecNonExclusiveError{ | ||
Table: alterTable.Table.Name.String(), | ||
PartitionSpec: spec, | ||
ConflictingStatement: sqlparser.CanonicalString(conflictingNode), | ||
} | ||
} | ||
if len(alterTable.AlterOptions) > 0 { | ||
// This should never happen, unless someone programmatically tampered with the AlterTable AST. | ||
return false, errorResult(alterTable.AlterOptions[0]) | ||
} | ||
if alterTable.PartitionOption != nil { | ||
// This should never happen, unless someone programmatically tampered with the AlterTable AST. | ||
return false, errorResult(alterTable.PartitionOption) | ||
} | ||
switch spec.Action { | ||
case sqlparser.AddAction: | ||
if len(spec.Definitions) > 1 { | ||
// This should never happen, unless someone programmatically tampered with the AlterTable AST. | ||
return false, errorResult(spec.Definitions[1]) | ||
} | ||
return true, nil | ||
case sqlparser.DropAction: | ||
return true, nil | ||
default: | ||
return false, nil | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package schemadiff | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"vitess.io/vitess/go/vt/sqlparser" | ||
) | ||
|
||
// AnalyzePartitionRotation analyzes a given AlterTable statement to see whether it has partition rotation | ||
// commands, and if so, is the ALTER TABLE statement valid in MySQL. In MySQL, a single ALTER TABLE statement | ||
// cannot apply multiple rotation commands, nor can it mix rotation commands with other types of changes. | ||
func TestAlterTableRotatesRangePartition(t *testing.T) { | ||
tcases := []struct { | ||
alter string | ||
expect bool | ||
}{ | ||
{ | ||
alter: "ALTER TABLE t ADD PARTITION (PARTITION p1 VALUES LESS THAN (10))", | ||
expect: true, | ||
}, | ||
{ | ||
alter: "ALTER TABLE t DROP PARTITION p1", | ||
expect: true, | ||
}, | ||
{ | ||
alter: "ALTER TABLE t DROP PARTITION p1, p2", | ||
expect: true, | ||
}, | ||
{ | ||
alter: "ALTER TABLE t TRUNCATE PARTITION p3", | ||
}, | ||
{ | ||
alter: "ALTER TABLE t COALESCE PARTITION 3", | ||
}, | ||
{ | ||
alter: "ALTER TABLE t partition by range (id) (partition p1 values less than (10), partition p2 values less than (20), partition p3 values less than (30))", | ||
}, | ||
{ | ||
alter: "ALTER TABLE t ADD COLUMN c1 INT, DROP COLUMN c2", | ||
}, | ||
} | ||
|
||
for _, tcase := range tcases { | ||
t.Run(tcase.alter, func(t *testing.T) { | ||
stmt, err := sqlparser.NewTestParser().ParseStrictDDL(tcase.alter) | ||
require.NoError(t, err) | ||
alterTable, ok := stmt.(*sqlparser.AlterTable) | ||
require.True(t, ok) | ||
|
||
result, err := AlterTableRotatesRangePartition(alterTable) | ||
require.NoError(t, err) | ||
assert.Equal(t, tcase.expect, result) | ||
}) | ||
} | ||
} |
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.
Do we want to clean up this function in a follow up PR?
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.
Function is now removed.