Skip to content

Commit d678460

Browse files
authored
refactor and parse table options, support auto_increment table option (#2401)
1 parent 734c46b commit d678460

27 files changed

+715
-618
lines changed

enginetest/memory_engine_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ func TestJoinOps(t *testing.T) {
121121
}
122122

123123
func TestJoinStats(t *testing.T) {
124-
enginetest.TestJoinStats(t, enginetest.NewDefaultMemoryHarness())
124+
harness := enginetest.NewDefaultMemoryHarness()
125+
if harness.IsUsingServer() {
126+
t.Skip("join stats don't work with bindvars")
127+
}
128+
enginetest.TestJoinStats(t, harness)
125129
}
126130

127131
// TestJSONTableQueries runs the canonical test queries against a single threaded index enabled harness.

enginetest/queries/alter_table_queries.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ var AlterTableAddAutoIncrementScripts = []ScriptTest{
10231023
" `j` int,\n" +
10241024
" `pk` int NOT NULL AUTO_INCREMENT,\n" +
10251025
" PRIMARY KEY (`pk`)\n" +
1026-
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
1026+
") ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
10271027
},
10281028
{
10291029
Query: "select pk from t1 order by pk",
@@ -1056,7 +1056,7 @@ var AlterTableAddAutoIncrementScripts = []ScriptTest{
10561056
" `i` int,\n" +
10571057
" `j` int,\n" +
10581058
" PRIMARY KEY (`pk`)\n" +
1059-
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
1059+
") ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
10601060
},
10611061
{
10621062
Query: "select pk from t1 order by pk",
@@ -1086,7 +1086,7 @@ var AlterTableAddAutoIncrementScripts = []ScriptTest{
10861086
" `j` int AUTO_INCREMENT,\n" +
10871087
" PRIMARY KEY (`i`),\n" +
10881088
" UNIQUE KEY `j` (`j`)\n" +
1089-
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
1089+
") ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
10901090
},
10911091
{
10921092
Query: "select * from t1 order by i",
@@ -1402,8 +1402,12 @@ var AddDropPrimaryKeyScripts = []ScriptTest{
14021402
Expected: []sql.Row{{types.NewOkResult(0)}},
14031403
},
14041404
{
1405-
Query: "show create table t;",
1406-
Expected: []sql.Row{{"t", "CREATE TABLE `t` (\n `id` int NOT NULL AUTO_INCREMENT,\n `c1` varchar(255),\n UNIQUE KEY `id` (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
1405+
Query: "show create table t;",
1406+
Expected: []sql.Row{{"t", "CREATE TABLE `t` (\n" +
1407+
" `id` int NOT NULL AUTO_INCREMENT,\n" +
1408+
" `c1` varchar(255),\n" +
1409+
" UNIQUE KEY `id` (`id`)\n" +
1410+
") ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
14071411
},
14081412
{
14091413
Query: "insert into t (c1) values('two');",
@@ -1437,8 +1441,12 @@ var AddDropPrimaryKeyScripts = []ScriptTest{
14371441
Expected: []sql.Row{{types.NewOkResult(0)}},
14381442
},
14391443
{
1440-
Query: "show create table t;",
1441-
Expected: []sql.Row{{"t", "CREATE TABLE `t` (\n `id` int NOT NULL AUTO_INCREMENT,\n `c1` varchar(255),\n KEY `id` (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
1444+
Query: "show create table t;",
1445+
Expected: []sql.Row{{"t", "CREATE TABLE `t` (\n" +
1446+
" `id` int NOT NULL AUTO_INCREMENT,\n" +
1447+
" `c1` varchar(255),\n" +
1448+
" KEY `id` (`id`)\n" +
1449+
") ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
14421450
},
14431451
{
14441452
Query: "insert into t (c1) values('two');",
@@ -1488,8 +1496,14 @@ var AddDropPrimaryKeyScripts = []ScriptTest{
14881496
Expected: []sql.Row{{1, -1, "one"}, {2, -2, "two"}},
14891497
},
14901498
{
1491-
Query: "show create table t;",
1492-
Expected: []sql.Row{{"t", "CREATE TABLE `t` (\n `id1` int NOT NULL AUTO_INCREMENT,\n `id2` int NOT NULL,\n `c1` varchar(255),\n KEY `c1id1` (`c1`,`id1`),\n KEY `id1c1` (`id1`,`c1`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
1499+
Query: "show create table t;",
1500+
Expected: []sql.Row{{"t", "CREATE TABLE `t` (\n" +
1501+
" `id1` int NOT NULL AUTO_INCREMENT,\n" +
1502+
" `id2` int NOT NULL,\n" +
1503+
" `c1` varchar(255),\n" +
1504+
" KEY `c1id1` (`c1`,`id1`),\n" +
1505+
" KEY `id1c1` (`id1`,`c1`)\n" +
1506+
") ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
14931507
},
14941508
},
14951509
},

enginetest/queries/create_table_queries.go

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ var CreateTableAutoIncrementTests = []ScriptTest{
755755
" `b` int NOT NULL,\n" +
756756
" PRIMARY KEY (`b`),\n" +
757757
" UNIQUE KEY `a` (`a`)\n" +
758-
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
758+
") ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
759759
},
760760
{
761761
Query: "select * from t1 order by b",
@@ -790,7 +790,7 @@ var CreateTableAutoIncrementTests = []ScriptTest{
790790
" `b` int NOT NULL,\n" +
791791
" PRIMARY KEY (`b`),\n" +
792792
" UNIQUE KEY `a` (`a`)\n" +
793-
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
793+
") ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
794794
},
795795
{
796796
Query: "select * from t1 order by b",
@@ -808,6 +808,51 @@ var CreateTableAutoIncrementTests = []ScriptTest{
808808
},
809809
},
810810
},
811+
{
812+
Name: "table with auto_increment table option",
813+
SetUpScript: []string{},
814+
Assertions: []ScriptTestAssertion{
815+
{
816+
// this just ignores the auto_increment argument
817+
Query: "create table t1 (i int) auto_increment=10;",
818+
Expected: []sql.Row{{types.NewOkResult(0)}},
819+
},
820+
{
821+
Query: "show create table t1",
822+
Expected: []sql.Row{
823+
{"t1", "CREATE TABLE `t1` (\n" +
824+
" `i` int\n" +
825+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"},
826+
},
827+
},
828+
829+
{
830+
Query: "create table t2 (i int auto_increment primary key) auto_increment=10;",
831+
Expected: []sql.Row{{types.NewOkResult(0)}},
832+
},
833+
{
834+
Query: "show create table t2",
835+
Expected: []sql.Row{
836+
{"t2", "CREATE TABLE `t2` (\n" +
837+
" `i` int NOT NULL AUTO_INCREMENT,\n" +
838+
" PRIMARY KEY (`i`)\n" +
839+
") ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"},
840+
},
841+
},
842+
{
843+
Query: "insert into t2 values (null), (null), (null)",
844+
Expected: []sql.Row{{types.OkResult{RowsAffected: 3, InsertID: 10}}},
845+
},
846+
{
847+
Query: "select * from t2",
848+
Expected: []sql.Row{
849+
{10},
850+
{11},
851+
{12},
852+
},
853+
},
854+
},
855+
},
811856
}
812857

813858
var BrokenCreateTableQueries = []WriteQueryTest{

enginetest/queries/script_queries.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7429,7 +7429,7 @@ var CreateDatabaseScripts = []ScriptTest{
74297429
Assertions: []ScriptTestAssertion{
74307430
{
74317431
Query: "CREATE DATABASE newtestdb CHARACTER SET utf8mb4 ENCRYPTION='N'",
7432-
Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 0, Info: nil}}},
7432+
Expected: []sql.Row{{types.NewOkResult(1)}},
74337433
},
74347434
{
74357435
SkipResultCheckOnServerEngine: true, // tracking issue here, https://github.com/dolthub/dolt/issues/6921. Also for when run with prepares, the warning is added twice
@@ -7438,13 +7438,16 @@ var CreateDatabaseScripts = []ScriptTest{
74387438
},
74397439
{
74407440
Query: "CREATE DATABASE newtest1db DEFAULT COLLATE binary ENCRYPTION='Y'",
7441-
Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 0, Info: nil}}},
7441+
Expected: []sql.Row{{types.NewOkResult(1)}},
74427442
},
74437443
{
74447444
SkipResultCheckOnServerEngine: true, // tracking issue here, https://github.com/dolthub/dolt/issues/6921.
74457445
// TODO: There should only be one warning (the warnings are not clearing for create database query) AND 'PREPARE' statements should not create warning from its query
7446-
Query: "SHOW WARNINGS /* 2 */",
7447-
Expected: []sql.Row{{"Warning", 1235, "Setting CHARACTER SET, COLLATION and ENCRYPTION are not supported yet"}, {"Warning", 1235, "Setting CHARACTER SET, COLLATION and ENCRYPTION are not supported yet"}},
7446+
Query: "SHOW WARNINGS /* 2 */",
7447+
Expected: []sql.Row{
7448+
{"Warning", 1235, "Setting CHARACTER SET, COLLATION and ENCRYPTION are not supported yet"},
7449+
{"Warning", 1235, "Setting CHARACTER SET, COLLATION and ENCRYPTION are not supported yet"},
7450+
},
74487451
},
74497452
{
74507453
Query: "CREATE DATABASE mydb",

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/dolthub/go-icu-regex v0.0.0-20230524105445-af7e7991c97e
77
github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71
88
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81
9-
github.com/dolthub/vitess v0.0.0-20240312232959-8ee510931c7b
9+
github.com/dolthub/vitess v0.0.0-20240319195706-89f7db8b6ee8
1010
github.com/go-kit/kit v0.10.0
1111
github.com/go-sql-driver/mysql v1.7.2-0.20231213112541-0004702b931d
1212
github.com/gocraft/dbr/v2 v2.7.2

go.sum

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,8 @@ github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71 h1:bMGS25NWAGTE
5858
github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71/go.mod h1:2/2zjLQ/JOOSbbSboojeg+cAwcRV0fDLzIiWch/lhqI=
5959
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81 h1:7/v8q9XGFa6q5Ap4Z/OhNkAMBaK5YeuEzwJt+NZdhiE=
6060
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81/go.mod h1:siLfyv2c92W1eN/R4QqG/+RjjX5W2+gCTRjZxBjI3TY=
61-
github.com/dolthub/vitess v0.0.0-20240307173128-5f7f58927aec h1:H1tHx26oby26Ig8SkFLIu/L5w3bFhAHOONMhJc0iE4A=
62-
github.com/dolthub/vitess v0.0.0-20240307173128-5f7f58927aec/go.mod h1:IwjNXSQPymrja5pVqmfnYdcy7Uv7eNJNBPK/MEh9OOw=
63-
github.com/dolthub/vitess v0.0.0-20240308234654-9bc43e5f8cd4 h1:iwY0Ek3OVFJh0AbKx88l57KVAcfIASoaqFO7EROziGo=
64-
github.com/dolthub/vitess v0.0.0-20240308234654-9bc43e5f8cd4/go.mod h1:IwjNXSQPymrja5pVqmfnYdcy7Uv7eNJNBPK/MEh9OOw=
65-
github.com/dolthub/vitess v0.0.0-20240312192751-96a83d3b5144 h1:Q9ibQsvJSc1Mmnbw98xbZOW2lD3xClrmZFXMHo6TPFc=
66-
github.com/dolthub/vitess v0.0.0-20240312192751-96a83d3b5144/go.mod h1:IwjNXSQPymrja5pVqmfnYdcy7Uv7eNJNBPK/MEh9OOw=
67-
github.com/dolthub/vitess v0.0.0-20240312231636-6621ed350367 h1:ydfzoj2Ibh+raCf6tJdKHG680YoGX3GGQWevi3NrDSo=
68-
github.com/dolthub/vitess v0.0.0-20240312231636-6621ed350367/go.mod h1:IwjNXSQPymrja5pVqmfnYdcy7Uv7eNJNBPK/MEh9OOw=
69-
github.com/dolthub/vitess v0.0.0-20240312232959-8ee510931c7b h1:e9ziIpupCojwRFCV85GSAjoiWN3aouKJN4pLufcdGvk=
70-
github.com/dolthub/vitess v0.0.0-20240312232959-8ee510931c7b/go.mod h1:IwjNXSQPymrja5pVqmfnYdcy7Uv7eNJNBPK/MEh9OOw=
61+
github.com/dolthub/vitess v0.0.0-20240319195706-89f7db8b6ee8 h1:MxITfv984XWKIs3kPmoMM9rgqpslU9gX6eP/MxQXg+A=
62+
github.com/dolthub/vitess v0.0.0-20240319195706-89f7db8b6ee8/go.mod h1:IwjNXSQPymrja5pVqmfnYdcy7Uv7eNJNBPK/MEh9OOw=
7163
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
7264
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
7365
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=

memory/table_data.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ func (td *TableData) truncate(schema sql.PrimaryKeySchema) *TableData {
176176
if schema.HasAutoIncrement() {
177177
td.autoIncVal = 1
178178
}
179+
for i, col := range schema.Schema {
180+
if col.AutoIncrement {
181+
td.autoColIdx = i
182+
break
183+
}
184+
}
179185

180186
return td
181187
}

sql/analyzer/resolve_columns.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func indexColumns(_ *sql.Context, _ *Analyzer, n sql.Node, scope *plan.Scope) (m
183183

184184
switch node := n.(type) {
185185
case *plan.CreateTable: // For this node in particular, the columns will only come into existence after the analyzer step, so we forge them here.
186-
for _, col := range node.CreateSchema.Schema {
186+
for _, col := range node.PkSchema().Schema {
187187
columns[tableCol{
188188
table: "",
189189
col: strings.ToLower(col.Name),

sql/analyzer/resolve_create_select.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,11 @@ func resolveCreateSelect(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.
2121
return nil, transform.SameTree, err
2222
}
2323

24-
// Get the correct schema of the CREATE TABLE based on the select query
25-
inputSpec := ct.TableSpec()
26-
2724
// We don't want to carry any information about keys, constraints, defaults, etc. from a `create table as select`
2825
// statement. When the underlying select node is a table, we must remove all such info from its schema. The only
2926
// exception is NOT NULL constraints, which we leave alone.
3027
selectSchema := stripSchema(analyzedSelect.Schema())
31-
mergedSchema := mergeSchemas(inputSpec.Schema.Schema, selectSchema)
28+
mergedSchema := mergeSchemas(ct.PkSchema().Schema, selectSchema)
3229
newSch := make(sql.Schema, len(mergedSchema))
3330

3431
for i, col := range mergedSchema {
@@ -44,9 +41,9 @@ func resolveCreateSelect(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.
4441
}
4542
}
4643

47-
newSpec := inputSpec.WithSchema(sql.NewPrimaryKeySchema(newSch, pkOrdinals...))
48-
// CREATE ... SELECT will always inherit the database collation for the table
49-
newSpec.Collation = plan.GetDatabaseCollation(ctx, ct.Database())
44+
newSpec := &plan.TableSpec{
45+
Schema: sql.NewPrimaryKeySchema(newSch, pkOrdinals...),
46+
}
5047

5148
newCreateTable := plan.NewCreateTable(ct.Database(), ct.Name(), ct.IfNotExists(), ct.Temporary(), newSpec)
5249
analyzedCreate, err := a.Analyze(ctx, newCreateTable, scope)

0 commit comments

Comments
 (0)