From ce9697818241bdeb1460b106176d29d0a98750ec Mon Sep 17 00:00:00 2001 From: shenli Date: Wed, 16 Mar 2016 22:09:11 +0800 Subject: [PATCH] *: Add index option in IndexInfo 1. Add index option in IndexInfo. 2. Show index option content when running show index statement. --- ast/ddl.go | 22 +++---- ddl/ddl.go | 11 +++- executor/executor_ddl.go | 2 +- executor/show.go | 4 +- executor/show_test.go | 14 +++++ model/model.go | 23 ++++++++ parser/parser.go | 121 ++++++++++++++++++++++++++++++++------- parser/parser.y | 107 ++++++++++++++++++++++++++++------ 8 files changed, 252 insertions(+), 52 deletions(-) diff --git a/ast/ddl.go b/ast/ddl.go index d1e1a8a84287d..2be6e1a958979 100644 --- a/ast/ddl.go +++ b/ast/ddl.go @@ -14,6 +14,7 @@ package ast import ( + "github.com/pingcap/tidb/model" "github.com/pingcap/tidb/util/types" ) @@ -198,15 +199,6 @@ func (n *ColumnOption) Accept(v Visitor) (Node, bool) { return v.Leave(n) } -// IndexType is the type of index -type IndexType int - -// IndexTypes -const ( - IndexTypeBtree = iota + 1 - IndexTypeHash -) - // IndexOption is the index options. // KEY_BLOCK_SIZE [=] value // | index_type @@ -217,7 +209,7 @@ type IndexOption struct { node KeyBlockSize uint64 - Tp IndexType + Tp model.IndexType Comment string } @@ -259,6 +251,9 @@ type Constraint struct { // Used for foreign key. Refer *ReferenceDef + + // Index Options + Option *IndexOption } // Accept implements Node Accept interface. @@ -282,6 +277,13 @@ func (n *Constraint) Accept(v Visitor) (Node, bool) { } n.Refer = node.(*ReferenceDef) } + if n.Option != nil { + node, ok := n.Option.Accept(v) + if !ok { + return n, false + } + n.Option = node.(*IndexOption) + } return v.Leave(n) } diff --git a/ddl/ddl.go b/ddl/ddl.go index 4b154faf5d7a3..c5b779144cb56 100644 --- a/ddl/ddl.go +++ b/ddl/ddl.go @@ -44,7 +44,7 @@ import ( type DDL interface { CreateSchema(ctx context.Context, name model.CIStr, charsetInfo *ast.CharsetOpt) error DropSchema(ctx context.Context, schema model.CIStr) error - CreateTable(ctx context.Context, ident ast.Ident, cols []*ast.ColumnDef, constrs []*ast.Constraint) error + CreateTable(ctx context.Context, ident ast.Ident, cols []*ast.ColumnDef, constrs []*ast.Constraint, options []*ast.TableOption) error DropTable(ctx context.Context, tableIdent ast.Ident) (err error) CreateIndex(ctx context.Context, tableIdent ast.Ident, unique bool, indexName model.CIStr, columnNames []*ast.IndexColName) error DropIndex(ctx context.Context, tableIdent ast.Ident, indexName model.CIStr) error @@ -680,6 +680,13 @@ func (d *ddl) buildTableInfo(tableName model.CIStr, cols []*column.Col, constrai case ast.ConstraintUniq, ast.ConstraintUniqKey, ast.ConstraintUniqIndex: idxInfo.Unique = true } + if constr.Option != nil { + idxInfo.Comment = constr.Option.Comment + idxInfo.Tp = constr.Option.Tp + } else { + // Use btree as default index type. + idxInfo.Tp = model.IndexTypeBtree + } idxInfo.ID, err = d.genGlobalID() if err != nil { return nil, errors.Trace(err) @@ -689,7 +696,7 @@ func (d *ddl) buildTableInfo(tableName model.CIStr, cols []*column.Col, constrai return } -func (d *ddl) CreateTable(ctx context.Context, ident ast.Ident, colDefs []*ast.ColumnDef, constraints []*ast.Constraint) (err error) { +func (d *ddl) CreateTable(ctx context.Context, ident ast.Ident, colDefs []*ast.ColumnDef, constraints []*ast.Constraint, options []*ast.TableOption) (err error) { is := d.GetInformationSchema() schema, ok := is.SchemaByName(ident.Schema) if !ok { diff --git a/executor/executor_ddl.go b/executor/executor_ddl.go index 328c60ae0ed1a..710193f938316 100644 --- a/executor/executor_ddl.go +++ b/executor/executor_ddl.go @@ -108,7 +108,7 @@ func (e *DDLExec) executeCreateDatabase(s *ast.CreateDatabaseStmt) error { func (e *DDLExec) executeCreateTable(s *ast.CreateTableStmt) error { ident := ast.Ident{Schema: s.Table.Schema, Name: s.Table.Name} - err := sessionctx.GetDomain(e.ctx).DDL().CreateTable(e.ctx, ident, s.Cols, s.Constraints) + err := sessionctx.GetDomain(e.ctx).DDL().CreateTable(e.ctx, ident, s.Cols, s.Constraints, s.Options) if terror.ErrorEqual(err, infoschema.TableExists) { if s.IfNotExists { return nil diff --git a/executor/show.go b/executor/show.go index 0c32367566aac..7a4a2bdc313a4 100644 --- a/executor/show.go +++ b/executor/show.go @@ -252,9 +252,9 @@ func (e *ShowExec) fetchShowIndex() error { subPart, // Sub_part nil, // Packed "YES", // Null - "BTREE", // Index_type + idx.Tp.String(), // Index_type "", // Comment - "", // Index_comment + idx.Comment, // Index_comment ) e.rows = append(e.rows, &Row{Data: data}) } diff --git a/executor/show_test.go b/executor/show_test.go index 7c19511e2da6a..2fd14c9b35d5d 100644 --- a/executor/show_test.go +++ b/executor/show_test.go @@ -37,4 +37,18 @@ func (s *testSuite) TestShow(c *C) { testSQL = "SHOW VARIABLES LIKE 'character_set_results';" result = tk.MustQuery(testSQL) c.Check(result.Rows(), HasLen, 1) + + // Test case for index type and comment + tk.MustExec(`create table show_index (c int, index cIdx using hash (c) comment "index_comment_for_cIdx");`) + testSQL = "SHOW index from show_index;" + result = tk.MustQuery(testSQL) + c.Check(result.Rows(), HasLen, 1) + expectedRow := []interface{}{ + "show_index", int64(1), "cIdx", int64(1), "c", "utf8_bin", + int64(0), nil, nil, "YES", "HASH", "", "index_comment_for_cIdx"} + row := result.Rows()[0] + c.Check(row, HasLen, len(expectedRow)) + for i, r := range row { + c.Check(r, Equals, expectedRow[i]) + } } diff --git a/model/model.go b/model/model.go index 0159b541a23e9..6c19baead0014 100644 --- a/model/model.go +++ b/model/model.go @@ -83,6 +83,7 @@ type TableInfo struct { Indices []*IndexInfo `json:"index_info"` State SchemaState `json:"state"` PKIsHandle bool `json:"pk_is_handle"` + Comment string `json:"comment"` } // Clone clones TableInfo. @@ -114,6 +115,26 @@ func (i *IndexColumn) Clone() *IndexColumn { return &ni } +// IndexType is the type of index +type IndexType int + +// String implements Stringer interface. +func (t IndexType) String() string { + switch t { + case IndexTypeBtree: + return "BTREE" + case IndexTypeHash: + return "HASH" + } + return "" +} + +// IndexTypes +const ( + IndexTypeBtree IndexType = iota + 1 + IndexTypeHash +) + // IndexInfo provides meta data describing a DB index. // It corresponds to the statement `CREATE INDEX Name ON Table (Column);` // See: https://dev.mysql.com/doc/refman/5.7/en/create-index.html @@ -125,6 +146,8 @@ type IndexInfo struct { Unique bool `json:"is_unique"` // Whether the index is unique. Primary bool `json:"is_primary"` // Whether the index is primary key. State SchemaState `json:"state"` + Comment string `json:"comment"` // Comment + Tp IndexType `json:"index_type"` // Index type: Btree or Hash } // Clone clones IndexInfo. diff --git a/parser/parser.go b/parser/parser.go index 1ae7aafb8aaed..e673a6abbe395 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -754,8 +754,8 @@ var ( 57373: 366, // by (3x) 57681: 367, // ByItem (3x) 57685: 368, // ColumnDef (3x) - 57699: 369, // Constraint (3x) - 57396: 370, // constraint (3x) + 57396: 369, // constraint (3x) + 57699: 370, // Constraint (3x) 57701: 371, // ConstraintKeywordOpt (3x) 57723: 372, // DeleteFromStmt (3x) 57748: 373, // FieldOpt (3x) @@ -1333,8 +1333,8 @@ var ( "by", "ByItem", "ColumnDef", - "Constraint", "constraint", + "Constraint", "ConstraintKeywordOpt", "DeleteFromStmt", "FieldOpt", @@ -2259,7 +2259,7 @@ var ( 713: {485, 1}, 714: {534, 1}, 715: {534, 3}, - 716: {369, 2}, + 716: {370, 2}, 717: {450, 1}, 718: {450, 1}, 719: {450, 4}, @@ -5417,7 +5417,7 @@ var ( // 1200 {1005, 949, 4: 950, 971, 957, 9: 1010, 978, 1006, 1008, 1009, 1007, 1021, 968, 1011, 1012, 1015, 967, 986, 988, 979, 970, 1029, 997, 958, 963, 962, 1039, 1019, 1048, 1022, 1053, 1055, 1057, 977, 1016, 1062, 990, 994, 995, 999, 1032, 952, 959, 960, 961, 964, 965, 966, 972, 1003, 1058, 981, 982, 984, 985, 993, 1002, 953, 955, 954, 956, 1004, 1024, 969, 973, 987, 1018, 974, 1028, 975, 998, 1013, 1026, 1023, 1027, 983, 989, 991, 992, 1020, 1025, 996, 1030, 1031, 1000, 951, 1063, 1033, 1034, 1035, 1036, 1038, 1037, 1040, 1041, 1042, 1043, 1044, 1045, 1017, 1046, 1047, 946, 1049, 1050, 1051, 1052, 1054, 976, 1056, 1059, 1060, 980, 1061, 1014, 1001, 1064, 1065, 1066, 1067, 1068, 1069, 1070, 1071, 1072, 206: 1073, 948, 947, 303: 2077}, {133: 2078}, - {1005, 949, 4: 950, 971, 957, 9: 1010, 978, 1006, 1008, 1009, 1007, 1021, 968, 1011, 1012, 1015, 967, 986, 988, 979, 970, 1029, 997, 958, 963, 962, 1039, 1019, 1048, 1022, 1053, 1055, 1057, 977, 1016, 1062, 990, 994, 995, 999, 1032, 952, 959, 960, 961, 964, 965, 966, 972, 1003, 1058, 981, 982, 984, 985, 993, 1002, 953, 955, 954, 956, 1004, 1024, 969, 973, 987, 1018, 974, 1028, 975, 998, 1013, 1026, 1023, 1027, 983, 989, 991, 992, 1020, 1025, 996, 1030, 1031, 1000, 951, 1063, 1033, 1034, 1035, 1036, 1038, 1037, 1040, 1041, 1042, 1043, 1044, 1045, 1017, 1046, 1047, 946, 1049, 1050, 1051, 1052, 1054, 976, 1056, 1059, 1060, 980, 1061, 1014, 1001, 1064, 1065, 1066, 1067, 1068, 1069, 1070, 1071, 1072, 203: 856, 856, 2085, 1105, 948, 947, 242: 2080, 244: 856, 856, 249: 856, 251: 856, 368: 2083, 2084, 2079, 2082, 450: 2086, 537: 2081}, + {1005, 949, 4: 950, 971, 957, 9: 1010, 978, 1006, 1008, 1009, 1007, 1021, 968, 1011, 1012, 1015, 967, 986, 988, 979, 970, 1029, 997, 958, 963, 962, 1039, 1019, 1048, 1022, 1053, 1055, 1057, 977, 1016, 1062, 990, 994, 995, 999, 1032, 952, 959, 960, 961, 964, 965, 966, 972, 1003, 1058, 981, 982, 984, 985, 993, 1002, 953, 955, 954, 956, 1004, 1024, 969, 973, 987, 1018, 974, 1028, 975, 998, 1013, 1026, 1023, 1027, 983, 989, 991, 992, 1020, 1025, 996, 1030, 1031, 1000, 951, 1063, 1033, 1034, 1035, 1036, 1038, 1037, 1040, 1041, 1042, 1043, 1044, 1045, 1017, 1046, 1047, 946, 1049, 1050, 1051, 1052, 1054, 976, 1056, 1059, 1060, 980, 1061, 1014, 1001, 1064, 1065, 1066, 1067, 1068, 1069, 1070, 1071, 1072, 203: 856, 856, 2085, 1105, 948, 947, 242: 2080, 244: 856, 856, 249: 856, 251: 856, 368: 2083, 2079, 2084, 2082, 450: 2086, 537: 2081}, {1005, 949, 4: 950, 971, 957, 9: 1010, 978, 1006, 1008, 1009, 1007, 1021, 968, 1011, 1012, 1015, 967, 986, 988, 979, 970, 1029, 997, 958, 963, 962, 1039, 1019, 1048, 1022, 1053, 1055, 1057, 977, 1016, 1062, 990, 994, 995, 999, 1032, 952, 959, 960, 961, 964, 965, 966, 972, 1003, 1058, 981, 982, 984, 985, 993, 1002, 953, 955, 954, 956, 1004, 1024, 969, 973, 987, 1018, 974, 1028, 975, 998, 1013, 1026, 1023, 1027, 983, 989, 991, 992, 1020, 1025, 996, 1030, 1031, 1000, 951, 1063, 1033, 1034, 1035, 1036, 1038, 1037, 1040, 1041, 1042, 1043, 1044, 1045, 1017, 1046, 1047, 946, 1049, 1050, 1051, 1052, 1054, 976, 1056, 1059, 1060, 980, 1061, 1014, 1001, 1064, 1065, 1066, 1067, 1068, 1069, 1070, 1071, 1072, 203: 855, 855, 206: 2371, 948, 947, 244: 855, 855, 249: 855, 251: 855, 449: 2370}, {26: 2281, 28: 2278, 2277, 40: 2280, 61: 2260, 2254, 2253, 75: 2268, 80: 2274, 2279, 153: 2267, 197: 2262, 247: 87, 253: 2255, 2251, 256: 87, 259: 2252, 2270, 263: 2259, 2266, 2237, 2238, 2257, 274: 2239, 2250, 2272, 2276, 2271, 2249, 2275, 2256, 284: 2258, 2248, 2240, 288: 2269, 2247, 2273, 2242, 2241, 2263, 465: 2246, 2264, 478: 2236, 490: 2244, 2245, 498: 2243, 504: 2261, 2234, 535: 2235, 541: 2265, 544: 2233}, // 1205 @@ -5529,7 +5529,7 @@ var ( {2: 822, 822, 7: 822, 822}, {2178, 2177, 138, 138, 6: 782, 9: 2184, 2182, 2179, 2181, 2183, 2180, 2187, 2175, 2185, 2186, 2191, 136: 2174, 149: 782, 209: 782, 333: 2176, 349: 2188, 353: 2190, 382: 2189, 2173}, // 1295 - {1005, 949, 4: 950, 971, 957, 9: 1010, 978, 1006, 1008, 1009, 1007, 1021, 968, 1011, 1012, 1015, 967, 986, 988, 979, 970, 1029, 997, 958, 963, 962, 1039, 1019, 1048, 1022, 1053, 1055, 1057, 977, 1016, 1062, 990, 994, 995, 999, 1032, 952, 959, 960, 961, 964, 965, 966, 972, 1003, 1058, 981, 982, 984, 985, 993, 1002, 953, 955, 954, 956, 1004, 1024, 969, 973, 987, 1018, 974, 1028, 975, 998, 1013, 1026, 1023, 1027, 983, 989, 991, 992, 1020, 1025, 996, 1030, 1031, 1000, 951, 1063, 1033, 1034, 1035, 1036, 1038, 1037, 1040, 1041, 1042, 1043, 1044, 1045, 1017, 1046, 1047, 946, 1049, 1050, 1051, 1052, 1054, 976, 1056, 1059, 1060, 980, 1061, 1014, 1001, 1064, 1065, 1066, 1067, 1068, 1069, 1070, 1071, 1072, 203: 856, 856, 2085, 1105, 948, 947, 242: 2080, 244: 856, 856, 249: 856, 251: 856, 368: 2083, 2084, 2079, 2082, 450: 2172}, + {1005, 949, 4: 950, 971, 957, 9: 1010, 978, 1006, 1008, 1009, 1007, 1021, 968, 1011, 1012, 1015, 967, 986, 988, 979, 970, 1029, 997, 958, 963, 962, 1039, 1019, 1048, 1022, 1053, 1055, 1057, 977, 1016, 1062, 990, 994, 995, 999, 1032, 952, 959, 960, 961, 964, 965, 966, 972, 1003, 1058, 981, 982, 984, 985, 993, 1002, 953, 955, 954, 956, 1004, 1024, 969, 973, 987, 1018, 974, 1028, 975, 998, 1013, 1026, 1023, 1027, 983, 989, 991, 992, 1020, 1025, 996, 1030, 1031, 1000, 951, 1063, 1033, 1034, 1035, 1036, 1038, 1037, 1040, 1041, 1042, 1043, 1044, 1045, 1017, 1046, 1047, 946, 1049, 1050, 1051, 1052, 1054, 976, 1056, 1059, 1060, 980, 1061, 1014, 1001, 1064, 1065, 1066, 1067, 1068, 1069, 1070, 1071, 1072, 203: 856, 856, 2085, 1105, 948, 947, 242: 2080, 244: 856, 856, 249: 856, 251: 856, 368: 2083, 2079, 2084, 2082, 450: 2172}, {7: 155, 155}, {2: 786, 786}, {6: 781, 149: 781, 209: 781}, @@ -5801,7 +5801,7 @@ var ( // 1520 {2: 873, 873, 8: 2420}, {2: 872, 872, 8: 872}, - {863, 863, 4: 863, 863, 863, 9: 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 203: 856, 856, 244: 856, 856, 249: 856, 251: 856, 369: 2414, 2079, 2082, 396: 2407, 2413}, + {863, 863, 4: 863, 863, 863, 9: 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 203: 856, 856, 244: 856, 856, 249: 856, 251: 856, 369: 2079, 2414, 2082, 396: 2407, 2413}, {863, 863, 4: 863, 863, 863, 9: 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 863, 203: 2402, 244: 2405, 2406, 249: 2404, 396: 2407, 2401, 500: 2403}, {2: 858, 858, 8: 858}, // 1525 @@ -6257,51 +6257,122 @@ yynewstate: } case 54: { - yyVAL.item = &ast.Constraint{Tp: ast.ConstraintPrimaryKey, Keys: yyS[yypt-2].item.([]*ast.IndexColName)} + c := &ast.Constraint{ + Tp: ast.ConstraintPrimaryKey, + Keys: yyS[yypt-2].item.([]*ast.IndexColName), + } + if yyS[yypt-0].item != nil { + c.Option = yyS[yypt-0].item.(*ast.IndexOption) + } + if yyS[yypt-4].item != nil { + if c.Option == nil { + c.Option = &ast.IndexOption{} + } + c.Option.Tp = yyS[yypt-4].item.(model.IndexType) + } + yyVAL.item = c } case 55: { - yyVAL.item = &ast.Constraint{ + c := &ast.Constraint{ Tp: ast.ConstraintFulltext, Keys: yyS[yypt-2].item.([]*ast.IndexColName), Name: yyS[yypt-4].item.(string), } + if yyS[yypt-0].item != nil { + c.Option = yyS[yypt-0].item.(*ast.IndexOption) + } + yyVAL.item = c } case 56: { - yyVAL.item = &ast.Constraint{ + c := &ast.Constraint{ Tp: ast.ConstraintIndex, Keys: yyS[yypt-2].item.([]*ast.IndexColName), Name: yyS[yypt-5].item.(string), } + if yyS[yypt-0].item != nil { + c.Option = yyS[yypt-0].item.(*ast.IndexOption) + } + if yyS[yypt-4].item != nil { + if c.Option == nil { + c.Option = &ast.IndexOption{} + } + c.Option.Tp = yyS[yypt-4].item.(model.IndexType) + } + yyVAL.item = c } case 57: { - yyVAL.item = &ast.Constraint{ + c := &ast.Constraint{ Tp: ast.ConstraintKey, Keys: yyS[yypt-2].item.([]*ast.IndexColName), - Name: yyS[yypt-5].item.(string)} + Name: yyS[yypt-5].item.(string), + } + if yyS[yypt-0].item != nil { + c.Option = yyS[yypt-0].item.(*ast.IndexOption) + } + if yyS[yypt-4].item != nil { + if c.Option == nil { + c.Option = &ast.IndexOption{} + } + c.Option.Tp = yyS[yypt-4].item.(model.IndexType) + } + yyVAL.item = c } case 58: { - yyVAL.item = &ast.Constraint{ + c := &ast.Constraint{ Tp: ast.ConstraintUniq, Keys: yyS[yypt-2].item.([]*ast.IndexColName), - Name: yyS[yypt-5].item.(string)} + Name: yyS[yypt-5].item.(string), + } + if yyS[yypt-0].item != nil { + c.Option = yyS[yypt-0].item.(*ast.IndexOption) + } + if yyS[yypt-4].item != nil { + if c.Option == nil { + c.Option = &ast.IndexOption{} + } + c.Option.Tp = yyS[yypt-4].item.(model.IndexType) + } + yyVAL.item = c } case 59: { - yyVAL.item = &ast.Constraint{ + c := &ast.Constraint{ Tp: ast.ConstraintUniqIndex, Keys: yyS[yypt-2].item.([]*ast.IndexColName), - Name: yyS[yypt-5].item.(string)} + Name: yyS[yypt-5].item.(string), + } + if yyS[yypt-0].item != nil { + c.Option = yyS[yypt-0].item.(*ast.IndexOption) + } + if yyS[yypt-4].item != nil { + if c.Option == nil { + c.Option = &ast.IndexOption{} + } + c.Option.Tp = yyS[yypt-4].item.(model.IndexType) + } + yyVAL.item = c } case 60: { - yyVAL.item = &ast.Constraint{ + c := &ast.Constraint{ Tp: ast.ConstraintUniqKey, Keys: yyS[yypt-2].item.([]*ast.IndexColName), - Name: yyS[yypt-5].item.(string)} + Name: yyS[yypt-5].item.(string), + } + if yyS[yypt-0].item != nil { + c.Option = yyS[yypt-0].item.(*ast.IndexOption) + } + if yyS[yypt-4].item != nil { + if c.Option == nil { + c.Option = &ast.IndexOption{} + } + c.Option.Tp = yyS[yypt-4].item.(model.IndexType) + } + yyVAL.item = c } case 61: { @@ -6828,6 +6899,10 @@ yynewstate: //"index name" yyVAL.item = yyS[yypt-0].item.(string) } + case 186: + { + yyVAL.item = nil + } case 187: { yyVAL.item = &ast.IndexOption{ @@ -6837,7 +6912,7 @@ yynewstate: case 188: { yyVAL.item = &ast.IndexOption{ - Tp: yyS[yypt-0].item.(ast.IndexType), + Tp: yyS[yypt-0].item.(model.IndexType), } } case 189: @@ -6848,11 +6923,15 @@ yynewstate: } case 190: { - yyVAL.item = ast.IndexTypeBtree + yyVAL.item = model.IndexTypeBtree } case 191: { - yyVAL.item = ast.IndexTypeHash + yyVAL.item = model.IndexTypeHash + } + case 192: + { + yyVAL.item = nil } case 193: { diff --git a/parser/parser.y b/parser/parser.y index fad068af021ca..f3642f24eda8c 100644 --- a/parser/parser.y +++ b/parser/parser.y @@ -918,51 +918,122 @@ ColumnOptionListOpt: ConstraintElem: "PRIMARY" "KEY" IndexTypeOpt '(' IndexColNameList ')' IndexOption { - $$ = &ast.Constraint{Tp: ast.ConstraintPrimaryKey, Keys: $5.([]*ast.IndexColName)} + c := &ast.Constraint{ + Tp: ast.ConstraintPrimaryKey, + Keys: $5.([]*ast.IndexColName), + } + if $7 != nil { + c.Option = $7.(*ast.IndexOption) + } + if $3 != nil { + if c.Option == nil { + c.Option = &ast.IndexOption{} + } + c.Option.Tp = $3.(model.IndexType) + } + $$ = c } | "FULLTEXT" "KEY" IndexName '(' IndexColNameList ')' IndexOption { - $$ = &ast.Constraint{ + c := &ast.Constraint{ Tp: ast.ConstraintFulltext, Keys: $5.([]*ast.IndexColName), Name: $3.(string), } + if $7 != nil { + c.Option = $7.(*ast.IndexOption) + } + $$ = c } | "INDEX" IndexName IndexTypeOpt '(' IndexColNameList ')' IndexOption { - $$ = &ast.Constraint{ + c := &ast.Constraint{ Tp: ast.ConstraintIndex, Keys: $5.([]*ast.IndexColName), Name: $2.(string), } + if $7 != nil { + c.Option = $7.(*ast.IndexOption) + } + if $3 != nil { + if c.Option == nil { + c.Option = &ast.IndexOption{} + } + c.Option.Tp = $3.(model.IndexType) + } + $$ = c } | "KEY" IndexName IndexTypeOpt '(' IndexColNameList ')' IndexOption { - $$ = &ast.Constraint{ + c := &ast.Constraint{ Tp: ast.ConstraintKey, Keys: $5.([]*ast.IndexColName), - Name: $2.(string)} + Name: $2.(string), + } + if $7 != nil { + c.Option = $7.(*ast.IndexOption) + } + if $3 != nil { + if c.Option == nil { + c.Option = &ast.IndexOption{} + } + c.Option.Tp = $3.(model.IndexType) + } + $$ = c } | "UNIQUE" IndexName IndexTypeOpt '(' IndexColNameList ')' IndexOption { - $$ = &ast.Constraint{ + c := &ast.Constraint{ Tp: ast.ConstraintUniq, Keys: $5.([]*ast.IndexColName), - Name: $2.(string)} + Name: $2.(string), + } + if $7 != nil { + c.Option = $7.(*ast.IndexOption) + } + if $3 != nil { + if c.Option == nil { + c.Option = &ast.IndexOption{} + } + c.Option.Tp = $3.(model.IndexType) + } + $$ = c } | "UNIQUE" "INDEX" IndexName IndexTypeOpt '(' IndexColNameList ')' IndexOption { - $$ = &ast.Constraint{ + c := &ast.Constraint{ Tp: ast.ConstraintUniqIndex, Keys: $6.([]*ast.IndexColName), - Name: $3.(string)} + Name: $3.(string), + } + if $8 != nil { + c.Option = $8.(*ast.IndexOption) + } + if $4 != nil { + if c.Option == nil { + c.Option = &ast.IndexOption{} + } + c.Option.Tp = $4.(model.IndexType) + } + $$ = c } | "UNIQUE" "KEY" IndexName IndexTypeOpt '(' IndexColNameList ')' IndexOption { - $$ = &ast.Constraint{ + c := &ast.Constraint{ Tp: ast.ConstraintUniqKey, Keys: $6.([]*ast.IndexColName), - Name: $3.(string)} + Name: $3.(string), + } + if $8 != nil { + c.Option = $8.(*ast.IndexOption) + } + if $4 != nil { + if c.Option == nil { + c.Option = &ast.IndexOption{} + } + c.Option.Tp = $4.(model.IndexType) + } + $$ = c } | "FOREIGN" "KEY" IndexName '(' IndexColNameList ')' ReferDef { @@ -1683,7 +1754,9 @@ IndexName: } IndexOption: - {} + { + $$ = nil + } | "KEY_BLOCK_SIZE" EqOpt LengthNum { $$ = &ast.IndexOption{ @@ -1693,7 +1766,7 @@ IndexOption: | IndexType { $$ = &ast.IndexOption { - Tp: $1.(ast.IndexType), + Tp: $1.(model.IndexType), } } | "COMMENT" stringLit @@ -1706,15 +1779,17 @@ IndexOption: IndexType: "USING" "BTREE" { - $$ = ast.IndexTypeBtree + $$ = model.IndexTypeBtree } | "USING" "HASH" { - $$ = ast.IndexTypeHash + $$ = model.IndexTypeHash } IndexTypeOpt: - {} + { + $$ = nil + } | IndexType { $$ = $1