-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
*: support create table with range type table partition. #6251
Changes from 7 commits
4f5c0ec
c9edaf8
e5723e5
da45638
f69d38a
bfd6b50
b178291
0ce2dc6
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 |
---|---|---|
|
@@ -740,14 +740,22 @@ func (d *ddl) CreateTableWithLike(ctx sessionctx.Context, ident, referIdent ast. | |
return errors.Trace(err) | ||
} | ||
|
||
func (d *ddl) CreateTable(ctx sessionctx.Context, ident ast.Ident, colDefs []*ast.ColumnDef, | ||
constraints []*ast.Constraint, options []*ast.TableOption) (err error) { | ||
func (d *ddl) CreateTable(ctx sessionctx.Context, s *ast.CreateTableStmt) (err error) { | ||
ident := ast.Ident{Schema: s.Table.Schema, Name: s.Table.Name} | ||
if s.ReferTable != nil { | ||
referIdent := ast.Ident{Schema: s.ReferTable.Schema, Name: s.ReferTable.Name} | ||
return d.CreateTableWithLike(ctx, ident, referIdent) | ||
} | ||
colDefs := s.Cols | ||
is := d.GetInformationSchema() | ||
schema, ok := is.SchemaByName(ident.Schema) | ||
if !ok { | ||
return infoschema.ErrDatabaseNotExists.GenByArgs(ident.Schema) | ||
} | ||
if is.TableExists(ident.Schema, ident.Name) { | ||
if s.IfNotExists { | ||
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. Could you add a test to cover this logic? |
||
return nil | ||
} | ||
return infoschema.ErrTableExists.GenByArgs(ident) | ||
} | ||
if err = checkTooLongTable(ident.Name); err != nil { | ||
|
@@ -766,7 +774,7 @@ func (d *ddl) CreateTable(ctx sessionctx.Context, ident ast.Ident, colDefs []*as | |
return errors.Trace(err) | ||
} | ||
|
||
cols, newConstraints, err := buildColumnsAndConstraints(ctx, colDefs, constraints) | ||
cols, newConstraints, err := buildColumnsAndConstraints(ctx, colDefs, s.Constraints) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
|
@@ -780,6 +788,42 @@ func (d *ddl) CreateTable(ctx sessionctx.Context, ident ast.Ident, colDefs []*as | |
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
if s.Partition != nil { | ||
pi := &model.PartitionInfo{ | ||
Type: s.Partition.Tp, | ||
Expr: s.Partition.Expr.Text(), | ||
} | ||
if s.Partition.Expr != nil { | ||
buf := new(bytes.Buffer) | ||
s.Partition.Expr.Format(buf) | ||
pi.Expr = buf.String() | ||
} else if s.Partition.ColumnNames != nil { | ||
pi.Columns = make([]model.CIStr, 0, len(s.Partition.ColumnNames)) | ||
for _, cn := range s.Partition.ColumnNames { | ||
pi.Columns = append(pi.Columns, cn.Name) | ||
} | ||
} | ||
for _, def := range s.Partition.Definitions { | ||
// TODO: generate multiple global ID for paritions. | ||
pid, err1 := d.genGlobalID() | ||
if err1 != nil { | ||
return errors.Trace(err1) | ||
} | ||
piDef := model.PartitionDefinition{ | ||
Name: def.Name, | ||
ID: pid, | ||
Comment: def.Comment, | ||
MaxValue: def.MaxValue, | ||
} | ||
for _, expr := range def.LessThan { | ||
buf := new(bytes.Buffer) | ||
expr.Format(buf) | ||
piDef.LessThan = append(piDef.LessThan, buf.String()) | ||
} | ||
pi.Definitions = append(pi.Definitions, piDef) | ||
} | ||
tbInfo.Partition = pi | ||
} | ||
|
||
job := &model.Job{ | ||
SchemaID: schema.ID, | ||
|
@@ -789,7 +833,7 @@ func (d *ddl) CreateTable(ctx sessionctx.Context, ident ast.Ident, colDefs []*as | |
Args: []interface{}{tbInfo}, | ||
} | ||
|
||
handleTableOptions(options, tbInfo) | ||
handleTableOptions(s.Options, tbInfo) | ||
err = checkCharsetAndCollation(tbInfo.Charset, tbInfo.Collate) | ||
if err != nil { | ||
return errors.Trace(err) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,6 +120,8 @@ type TableInfo struct { | |
|
||
// ShardRowIDBits specify if the implicit row ID is sharded. | ||
ShardRowIDBits uint64 | ||
|
||
Partition *PartitionInfo | ||
} | ||
|
||
// GetUpdateTime gets the table's updating time. | ||
|
@@ -221,6 +223,34 @@ func (t *TableInfo) ColumnIsInIndex(c *ColumnInfo) bool { | |
return false | ||
} | ||
|
||
// PartitionType is the type for PartitionInfo | ||
type PartitionType int | ||
|
||
// Partition types. | ||
const ( | ||
PartitionTypeRange PartitionType = 1 | ||
PartitionTypeHash PartitionType = 2 | ||
PartitionTypeList PartitionType = 3 | ||
) | ||
|
||
// PartitionInfo provides table partition info. | ||
type PartitionInfo struct { | ||
Type PartitionType | ||
Expr string | ||
Columns []CIStr | ||
|
||
Definitions []PartitionDefinition | ||
} | ||
|
||
// PartitionDefinition defines a single partition. | ||
type PartitionDefinition struct { | ||
ID int64 | ||
Name string | ||
LessThan []string | ||
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.
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. Yes 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. so how do we define a hash or list or key partition ? 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. It would be implemented later... @zz-jason 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. I think we should implement a more extensible struct to define partition info. 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. It's compatible when we add new fields later. |
||
Comment string `json:"omit_empty"` | ||
MaxValue bool | ||
} | ||
|
||
// IndexColumn provides index column info. | ||
type IndexColumn struct { | ||
Name CIStr `json:"name"` // Index name | ||
|
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.
s/PartitionDefinition/RangePartitionDefinition/ ?
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 can hold other partition types.