Skip to content

Commit b42c0df

Browse files
Fix SET and START TRANSACTION in create procedure statements (vitessio#18279)
Signed-off-by: Manan Gupta <[email protected]>
1 parent fcfa0c8 commit b42c0df

File tree

13 files changed

+75
-12
lines changed

13 files changed

+75
-12
lines changed

go/test/endtoend/vtgate/unsharded/main_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,10 @@ BEGIN
151151
insert into allDefaults(id) values (128);
152152
select 128 into val from dual;
153153
END;
154-
`}
154+
`,
155+
`CREATE PROCEDURE p1 (in x BIGINT) BEGIN declare y DECIMAL(14,2); set y = 4.2; END`,
156+
`CREATE PROCEDURE p2 (in x BIGINT) BEGIN START TRANSACTION; SELECT 128 from dual; COMMIT; END`,
157+
}
155158
)
156159

157160
func TestMain(m *testing.M) {

go/vt/sqlparser/ast.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,8 +640,15 @@ type (
640640
// TxAccessMode is an enum for Transaction Access Mode
641641
TxAccessMode int8
642642

643+
// BeginType is an enum for the type of BEGIN statement.
644+
BeginType int8
645+
643646
// Begin represents a Begin statement.
644647
Begin struct {
648+
// We need to differentiate between BEGIN and START TRANSACTION statements
649+
// because inside a stored procedure the former is considered part of a BEGIN...END statement,
650+
// while the latter starts a transaction.
651+
Type BeginType
645652
TxAccessModes []TxAccessMode
646653
}
647654

go/vt/sqlparser/ast_equals.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/vt/sqlparser/ast_format.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ func (node *Commit) Format(buf *TrackedBuffer) {
11881188

11891189
// Format formats the node.
11901190
func (node *Begin) Format(buf *TrackedBuffer) {
1191-
if node.TxAccessModes == nil {
1191+
if node.Type == BeginStmt {
11921192
buf.literal("begin")
11931193
return
11941194
}

go/vt/sqlparser/ast_format_fast.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/vt/sqlparser/cached_size.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/vt/sqlparser/constants.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,12 @@ const (
997997
ReadOnly
998998
)
999999

1000+
// BEGIN statement type
1001+
const (
1002+
BeginStmt BeginType = iota
1003+
StartTransactionStmt
1004+
)
1005+
10001006
// Enum Types of WKT functions
10011007
const (
10021008
GeometryFromText GeomFromWktType = iota

go/vt/sqlparser/normalizer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func (nz *normalizer) determineQueryRewriteStrategy(in Statement) {
174174
func (nz *normalizer) walkDown(node, _ SQLNode) bool {
175175
switch node := node.(type) {
176176
case *Begin, *Commit, *Rollback, *Savepoint, *SRollback, *Release, *OtherAdmin, *Analyze,
177-
*PrepareStmt, *ExecuteStmt, *FramePoint, *ColName, TableName, *ConvertType:
177+
*PrepareStmt, *ExecuteStmt, *FramePoint, *ColName, TableName, *ConvertType, *CreateProcedure:
178178
// These statement do not need normalizing
179179
return false
180180
case *AssignmentExpr:

go/vt/sqlparser/normalizer_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,11 @@ func TestNormalize(t *testing.T) {
441441
"bv1": sqltypes.Int64BindVariable(1),
442442
"bv2": sqltypes.Int64BindVariable(0),
443443
},
444+
}, {
445+
// Verify we don't change anything in the normalization of create procedures.
446+
in: "CREATE PROCEDURE p2 (in x BIGINT) BEGIN declare y DECIMAL(14,2); START TRANSACTION; set y = 4.2; SELECT 128 from dual; COMMIT; END",
447+
outstmt: "create procedure p2 (in x BIGINT) begin declare y DECIMAL(14,2); start transaction; set y = 4.2; select 128 from dual; commit; end;",
448+
outbv: map[string]*querypb.BindVariable{},
444449
}}
445450
parser := NewTestParser()
446451
for _, tc := range testcases {

go/vt/sqlparser/parse_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,6 +2226,8 @@ var (
22262226
}, {
22272227
input: "create procedure ConditionWithSignalAndHandler() begin declare custom_error condition for sqlstate '45000'; declare exit handler for custom_error begin select 'Handled with custom condition and signal'; end; signal sqlstate '45000' set message_text = 'Custom signal triggered'; end;",
22282228
output: "create procedure ConditionWithSignalAndHandler () begin declare custom_error condition for sqlstate '45000'; declare exit handler for custom_error begin select 'Handled with custom condition and signal' from dual; end; signal sqlstate '45000' set message_text = 'Custom signal triggered'; end;",
2229+
}, {
2230+
input: "create procedure t1 (in x BIGINT) begin start transaction; insert into unsharded_a values (1, 'a', 'a'); commit; end;",
22292231
}, {
22302232
input: "create /*vt+ strategy=online */ or replace view v as select a, b, c from t",
22312233
}, {
@@ -2907,8 +2909,7 @@ var (
29072909
input: "begin;",
29082910
output: "begin",
29092911
}, {
2910-
input: "start transaction",
2911-
output: "begin",
2912+
input: "start transaction",
29122913
}, {
29132914
input: "start transaction with consistent snapshot",
29142915
}, {

0 commit comments

Comments
 (0)