Skip to content

Commit f3df8ac

Browse files
author
James Cor
committed
no implicit commits for temporary tables
1 parent 2d6449f commit f3df8ac

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

enginetest/queries/transaction_queries.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,86 @@ var TransactionTests = []TransactionTest{
11171117
},
11181118
},
11191119
},
1120+
{
1121+
Name: "create temporary table queries are not implicitly committed",
1122+
Assertions: []ScriptTestAssertion{
1123+
{
1124+
Query: "/* client a */ create table t (pk int primary key);",
1125+
Expected: []sql.Row{{types.OkResult{}}},
1126+
},
1127+
{
1128+
Query: "/* client b */ select * from t;",
1129+
Expected: []sql.Row{},
1130+
},
1131+
1132+
{
1133+
Query: "/* client a */ set @@autocommit = 0;",
1134+
Expected: []sql.Row{{}},
1135+
},
1136+
{
1137+
Query: "/* client a */ start transaction;",
1138+
Expected: []sql.Row{},
1139+
},
1140+
{
1141+
// This should not appear for client b until a transaction is committed
1142+
Query: "/* client a */ insert into t values (1), (2), (3);",
1143+
Expected: []sql.Row{{types.OkResult{RowsAffected: 3}}},
1144+
},
1145+
{
1146+
Query: "/* client b */ select * from t;",
1147+
Expected: []sql.Row{},
1148+
},
1149+
1150+
{
1151+
// This should not implicitly commit the transaction
1152+
Query: "/* client a */ create temporary table tmp1 (pk int primary key);",
1153+
Expected: []sql.Row{{types.OkResult{}}},
1154+
},
1155+
{
1156+
// This should not implicitly commit the transaction
1157+
Query: "/* client a */ create temporary table tmp2 (pk int primary key);",
1158+
Expected: []sql.Row{{types.OkResult{}}},
1159+
},
1160+
{
1161+
Query: "/* client b */ select * from t;",
1162+
Expected: []sql.Row{},
1163+
},
1164+
1165+
{
1166+
// This should not implicitly commit the transaction
1167+
Query: "/* client a */ insert into tmp1 values (1), (2), (3);",
1168+
Expected: []sql.Row{{types.OkResult{RowsAffected: 3}}},
1169+
},
1170+
{
1171+
Query: "/* client b */ select * from t;",
1172+
Expected: []sql.Row{},
1173+
},
1174+
1175+
{
1176+
// This should not implicitly commit the transaction
1177+
Query: "/* client a */ drop temporary table tmp1;",
1178+
Expected: []sql.Row{{types.OkResult{}}},
1179+
},
1180+
{
1181+
Query: "/* client b */ select * from t;",
1182+
Expected: []sql.Row{},
1183+
},
1184+
1185+
{
1186+
// Oddly, this does commit the transaction
1187+
Query: "/* client a */ drop table tmp2;",
1188+
Expected: []sql.Row{{types.OkResult{}}},
1189+
},
1190+
{
1191+
Query: "/* client b */ select * from t;",
1192+
Expected: []sql.Row{
1193+
{1},
1194+
{2},
1195+
{3},
1196+
},
1197+
},
1198+
},
1199+
},
11201200
{
11211201
Name: "alter table queries are implicitly committed",
11221202
Assertions: []ScriptTestAssertion{

sql/planbuilder/builder.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ func (b *Builder) buildSubquery(inScope *scope, stmt ast.Statement, subQuery str
233233
case *ast.Show:
234234
return b.buildShow(inScope, n)
235235
case *ast.DDL:
236-
b.qFlags.Set(sql.QFlagDDL)
237236
return b.buildDDL(inScope, subQuery, fullQuery, n)
238237
case *ast.AlterTable:
239238
b.qFlags.Set(sql.QFlagAlterTable)

sql/planbuilder/ddl.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ func (b *Builder) buildDDL(inScope *scope, subQuery string, fullQuery string, c
122122
if err := b.cat.AuthorizationHandler().HandleAuth(b.ctx, b.authQueryState, c.Auth); err != nil && b.authEnabled {
123123
b.handleErr(err)
124124
}
125+
if !c.Temporary {
126+
b.qFlags.Set(sql.QFlagDDL)
127+
}
125128

126129
outScope = inScope.push()
127130
switch strings.ToLower(c.Action) {
@@ -231,6 +234,7 @@ func (b *Builder) buildDropTable(inScope *scope, c *ast.DDL) (outScope *scope) {
231234
if dbName == "" {
232235
dbName = b.currentDb().Name()
233236
}
237+
234238
for _, t := range c.FromTables {
235239
if t.DbQualifier.String() != "" && t.DbQualifier.String() != dbName {
236240
err := sql.ErrUnsupportedFeature.New("dropping tables on multiple databases in the same statement")
@@ -360,6 +364,11 @@ func (b *Builder) buildCreateTable(inScope *scope, c *ast.DDL) (outScope *scope)
360364
database, c.Table.Name.String(), c.IfNotExists, c.Temporary, tableSpec)
361365
}
362366

367+
// Temporary tables do not cause implicit commits
368+
if c.Temporary {
369+
b.qFlags.Unset(sql.QFlagDDL)
370+
}
371+
363372
return
364373
}
365374

0 commit comments

Comments
 (0)