Skip to content

Commit 5b1a12f

Browse files
authored
no implicit comits for temporary tables. (#2954)
1 parent 2d6449f commit 5b1a12f

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

enginetest/queries/transaction_queries.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,127 @@ var TransactionTests = []TransactionTest{
11171117
},
11181118
},
11191119
},
1120+
{
1121+
Name: "certain ddl queries on temporary tables 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 tmp (pk int primary key);",
1153+
Expected: []sql.Row{{types.OkResult{}}},
1154+
},
1155+
{
1156+
Query: "/* client b */ select * from t;",
1157+
Expected: []sql.Row{},
1158+
},
1159+
1160+
{
1161+
// This should not implicitly commit the transaction
1162+
Query: "/* client a */ insert into tmp values (1), (2), (3);",
1163+
Expected: []sql.Row{{types.OkResult{RowsAffected: 3}}},
1164+
},
1165+
{
1166+
Query: "/* client b */ select * from t;",
1167+
Expected: []sql.Row{},
1168+
},
1169+
1170+
{
1171+
// This should not implicitly commit the transaction
1172+
Query: "/* client a */ drop temporary table tmp;",
1173+
Expected: []sql.Row{{types.OkResult{}}},
1174+
},
1175+
{
1176+
Query: "/* client b */ select * from t;",
1177+
Expected: []sql.Row{},
1178+
},
1179+
1180+
{
1181+
// This should not implicitly commit the transaction
1182+
Query: "/* client a */ create temporary table tmp (pk int primary key);",
1183+
Expected: []sql.Row{{types.OkResult{}}},
1184+
},
1185+
{
1186+
// Oddly, this does implicitly commit the transaction
1187+
Query: "/* client a */ drop table tmp;",
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+
Query: "/* client a */ delete from t where true;",
1200+
Expected: []sql.Row{{types.OkResult{RowsAffected: 3}}},
1201+
},
1202+
1203+
{
1204+
// This should commit and reset table t
1205+
Query: "/* client a */ start transaction;",
1206+
Expected: []sql.Row{},
1207+
},
1208+
{
1209+
// This should not implicitly commit the transaction
1210+
Query: "/* client a */ insert into t values (1), (2), (3);",
1211+
Expected: []sql.Row{{types.OkResult{RowsAffected: 3}}},
1212+
},
1213+
{
1214+
// This should not implicitly commit the transaction
1215+
Query: "/* client a */ create temporary table tmp (pk int primary key);",
1216+
Expected: []sql.Row{{types.OkResult{}}},
1217+
},
1218+
{
1219+
Query: "/* client b */ select * from t;",
1220+
Expected: []sql.Row{},
1221+
},
1222+
{
1223+
// TODO: turns out we can't alter temporary tables; unskip tests when that is fixed
1224+
// Oddly, this does implicitly commit the transaction
1225+
Skip: true,
1226+
Query: "/* client a */ alter table tmp add column j int;",
1227+
Expected: []sql.Row{{types.OkResult{}}},
1228+
},
1229+
{
1230+
// TODO: turns out we can't alter temporary tables; unskip tests when that is fixed
1231+
Query: "/* client b */ select * from t;",
1232+
Skip: true,
1233+
Expected: []sql.Row{
1234+
{1},
1235+
{2},
1236+
{3},
1237+
},
1238+
},
1239+
},
1240+
},
11201241
{
11211242
Name: "alter table queries are implicitly committed",
11221243
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: 4 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")

0 commit comments

Comments
 (0)