Skip to content

Commit

Permalink
test: 添加alter table表分区语法测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
hanchuanchuan committed Nov 28, 2020
1 parent f5c39b3 commit 9b24954
Show file tree
Hide file tree
Showing 7 changed files with 407 additions and 7 deletions.
17 changes: 17 additions & 0 deletions session/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ type TableInfo struct {
// 索引
Indexes []*IndexInfo

// 分区信息
Partitions []*PartitionInfo

// 是否已删除
IsDeleted bool
// 备份库是否已创建
Expand Down Expand Up @@ -249,6 +252,20 @@ type IndexInfo struct {
IsDeleted bool `gorm:"-"`
}

// PartitionInfo 分区信息
type PartitionInfo struct {
gorm.Model

Table string `gorm:"Column:Table"`
PartName string `gorm:"Column:PARTITION_NAME"`
PartMethod string `gorm:"Column:PARTITION_METHOD"`
PartExpression string `gorm:"Column:PARTITION_EXPRESSION"`
PartDescription string `gorm:"Column:PARTITION_DESCRIPTION"`
TableRows int `gorm:"Column:TABLE_ROWS"`

IsDeleted bool `gorm:"-"`
}

// DBInfo 库信息
type DBInfo struct {
Name string
Expand Down
22 changes: 22 additions & 0 deletions session/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,28 @@ func (s *session) rawScan(sqlStr string, dest interface{}) (err error) {
return
}

// Raw 执行sql语句,连接失败时自动重连,自动重置当前数据库
func (s *session) rawDB(dest interface{}, sqlStr string, values ...interface{}) (err error) {
// 连接断开无效时,自动重试
for i := 0; i < maxBadConnRetries; i++ {
err = s.db.Raw(sqlStr, values...).Scan(dest).Error
if err == nil {
return
}
if err == mysqlDriver.ErrInvalidConn {
log.Errorf("con:%d %v", s.sessionVars.ConnectionID, err)
err1 := s.initConnection()
if err1 != nil {
return err1
}
s.appendErrorMessage(mysqlDriver.ErrInvalidConn.Error())
continue
}
return
}
return
}

// initConnection 连接失败时自动重连,重连后重置当前数据库
func (s *session) initConnection() (err error) {
name := s.dbName
Expand Down
15 changes: 15 additions & 0 deletions session/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ const (
ErrViewSupport
ErrViewColumnCount
ErrIncorrectDateTimeValue
ErrSameNamePartition
ErrRepeatConstDefinition
ErrPartitionNotExisted
ER_ERROR_LAST
)

Expand Down Expand Up @@ -389,6 +392,9 @@ var ErrorsDefault = map[ErrorCode]string{
ErrViewSupport: "Not allowed to create or use views '%s'.",
ErrViewColumnCount: "View's SELECT and view's field list have different column counts",
ErrIncorrectDateTimeValue: "Incorrect datetime value: '%v'(column '%s')",
ErrSameNamePartition: "Duplicate partition name %-.192s",
ErrRepeatConstDefinition: "Duplicate partition constant definition: '%v'",
ErrPartitionNotExisted: "Partition '%-.64s' does not exist",
ER_ERROR_LAST: "TheLastError,ByeBye",
}

Expand Down Expand Up @@ -559,6 +565,9 @@ var ErrorsChinese = map[ErrorCode]string{
ErrViewSupport: "不允许创建或使用视图 '%s'.",
ErrViewColumnCount: "视图的SELECT和视图字段列表具有不同的列数",
ErrIncorrectDateTimeValue: "不正确的时间:'%v'(列 '%s')",
ErrSameNamePartition: "分区名重复: %-.192s",
ErrRepeatConstDefinition: "重复的分区范围定义: '%v'",
ErrPartitionNotExisted: "分区 '%-.64s' 不存在",
}

func GetErrorLevel(code ErrorCode) uint8 {
Expand Down Expand Up @@ -1059,6 +1068,12 @@ func (e ErrorCode) String() string {
return "er_view_support"
case ErrIncorrectDateTimeValue:
return "er_incorrect_datetime_value"
case ErrSameNamePartition:
return "er_same_name_partition"
case ErrRepeatConstDefinition:
return "er_repeat_const_definition"
case ErrPartitionNotExisted:
return "er_partition_not_existed"
case ER_ERROR_LAST:
return "er_error_last"
}
Expand Down
2 changes: 2 additions & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,8 @@ func createSession(store kv.Storage) (*session, error) {
lowerCaseTableNames: 1,
}

// s.sessionVars.EnableTablePartition = true

if plannercore.PreparedPlanCacheEnabled() {
s.preparedPlanCache = kvcache.NewSimpleLRUCache(plannercore.PreparedPlanCacheCapacity)
}
Expand Down
91 changes: 84 additions & 7 deletions session/session_inception.go
Original file line number Diff line number Diff line change
Expand Up @@ -2726,10 +2726,6 @@ func (s *session) checkCreateTable(node *ast.CreateTableStmt, sql string) {
}
}

if node.Partition != nil {
s.appendErrorNo(ER_PARTITION_NOT_ALLOWED)
}

if node.Select != nil {
if s.enforeGtidConsistency {
s.appendErrorMessage("Statement violates GTID consistency: CREATE TABLE ... SELECT.")
Expand Down Expand Up @@ -2805,6 +2801,17 @@ func (s *session) checkCreateTable(node *ast.CreateTableStmt, sql string) {
}
}

if node.Partition != nil {
if !s.inc.EnablePartitionTable {
s.appendErrorNo(ER_PARTITION_NOT_ALLOWED)
} else {
s.checkPartitionNameUnique(node.Partition.Definitions)
if !s.hasError() {
s.buildPartitionInfo(node.Partition, table)
}
}
}

if !s.hasError() && s.inc.ColumnsMustHaveIndex != "" {
s.checkColumnsMustHaveindex(table)
}
Expand Down Expand Up @@ -2991,6 +2998,42 @@ func (s *session) buildTableInfo(node *ast.CreateTableStmt) *TableInfo {
return table
}

func (s *session) buildPartitionInfo(def *ast.PartitionOptions,
t *TableInfo) []*PartitionInfo {
log.Debug("buildPartitionInfo")

parts := make([]*PartitionInfo, len(def.Definitions))
for index, part := range def.Definitions {
p := &PartitionInfo{
Table: t.Name,
PartName: part.Name.String(),
PartMethod: def.PartitionMethod.Tp.String(),
}
switch clause := part.Clause.(type) {
case *ast.PartitionDefinitionClauseIn:
if clause.Values == nil {
continue
}
partValues := make([]string, 0)
for _, values := range clause.Values {
for _, v := range values {
key := fmt.Sprintf("%v", v.GetValue())
partValues = append(partValues, key)
}
}
p.PartDescription = strings.Join(partValues, ",")

case *ast.PartitionDefinitionClauseLessThan:
for _, v := range clause.Exprs {
p.PartDescription = fmt.Sprintf("%v", v.GetValue())
break
}
}
parts[index] = p
}
return parts
}

func (s *session) checkAlterTable(node *ast.AlterTableStmt, sql string) {
log.Debug("checkAlterTable")

Expand Down Expand Up @@ -3163,10 +3206,23 @@ func (s *session) checkAlterTable(node *ast.AlterTableStmt, sql string) {
s.checkAlterTableRenameIndex(table, alter)
}

case ast.AlterTableAddPartitions,
ast.AlterTableAlterPartition,
case ast.AlterTableAddPartitions:
if !s.inc.EnablePartitionTable {
s.appendErrorNo(ER_PARTITION_NOT_ALLOWED)
} else {
s.fetchPartitionFromDB(table)
s.checkPartitionNameUnique(alter.PartDefinitions)
s.checkPartitionNameExists(table, alter.PartDefinitions)
}
case ast.AlterTableDropPartition:
if !s.inc.EnablePartitionTable {
s.appendErrorNo(ER_PARTITION_NOT_ALLOWED)
} else {
s.fetchPartitionFromDB(table)
s.checkPartitionDrop(table, alter.PartitionNames)
}
case ast.AlterTableAlterPartition,
ast.AlterTableCoalescePartitions,
ast.AlterTableDropPartition,
ast.AlterTableTruncatePartition,
ast.AlterTableRebuildPartition,
ast.AlterTableReorganizePartition,
Expand All @@ -3177,6 +3233,7 @@ func (s *session) checkAlterTable(node *ast.AlterTableStmt, sql string) {
ast.AlterTableImportPartitionTablespace,
ast.AlterTableDiscardPartitionTablespace:
s.appendErrorNo(ER_PARTITION_NOT_ALLOWED)
s.fetchPartitionFromDB(table)

case ast.AlterTableLock,
ast.AlterTableAlgorithm,
Expand Down Expand Up @@ -6966,6 +7023,26 @@ func (s *session) queryTableFromDB(db string, tableName string, reportNotExists
return rows
}

func (s *session) fetchPartitionFromDB(t *TableInfo) error {
if t.IsNew || t.IsDeleted || t.Partitions != nil {
return nil
}

var rows []*PartitionInfo
sql := "SELECT PARTITION_NAME, PARTITION_METHOD, PARTITION_EXPRESSION, PARTITION_DESCRIPTION, TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA = ? AND TABLE_NAME= ?"
if err := s.rawDB(&rows, sql, t.Schema, t.Name); err != nil {
if myErr, ok := err.(*mysqlDriver.MySQLError); ok {
log.Errorf("con:%d %v", s.sessionVars.ConnectionID, err)
s.appendErrorMessage(myErr.Message + ".")
} else {
s.appendErrorMessage(err.Error() + ".")
}
return err
}
t.Partitions = rows
return nil
}

func (s *session) queryIndexFromDB(db string, tableName string, reportNotExists bool) []*IndexInfo {
if db == "" {
db = s.dbName
Expand Down
Loading

0 comments on commit 9b24954

Please sign in to comment.