From 31472406194450a258faba02f3477c29f2330b40 Mon Sep 17 00:00:00 2001 From: Harshit Gangal Date: Wed, 31 Jan 2024 10:33:03 +0530 Subject: [PATCH] `transaction_mode` variable to return flag default if unset (#15032) Signed-off-by: Harshit Gangal --- .../endtoend/vtgate/queries/misc/misc_test.go | 33 +++++++++++++++++++ go/vt/vtgate/executor.go | 6 +++- go/vt/vtgate/executor_select_test.go | 2 +- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/go/test/endtoend/vtgate/queries/misc/misc_test.go b/go/test/endtoend/vtgate/queries/misc/misc_test.go index 465b0adf883..eca46cfcc29 100644 --- a/go/test/endtoend/vtgate/queries/misc/misc_test.go +++ b/go/test/endtoend/vtgate/queries/misc/misc_test.go @@ -284,3 +284,36 @@ func TestAnalyze(t *testing.T) { }) } } + +// TestTransactionModeVar executes SELECT on `transaction_mode` variable +func TestTransactionModeVar(t *testing.T) { + utils.SkipIfBinaryIsBelowVersion(t, 19, "vtgate") + + mcmp, closer := start(t) + defer closer() + + tcases := []struct { + setStmt string + expRes string + }{{ + expRes: `[[VARCHAR("MULTI")]]`, + }, { + setStmt: `set transaction_mode = single`, + expRes: `[[VARCHAR("SINGLE")]]`, + }, { + setStmt: `set transaction_mode = multi`, + expRes: `[[VARCHAR("MULTI")]]`, + }, { + setStmt: `set transaction_mode = twopc`, + expRes: `[[VARCHAR("TWOPC")]]`, + }} + + for _, tcase := range tcases { + t.Run(tcase.setStmt, func(t *testing.T) { + if tcase.setStmt != "" { + utils.Exec(t, mcmp.VtConn, tcase.setStmt) + } + utils.AssertMatches(t, mcmp.VtConn, "select @@transaction_mode", tcase.expRes) + }) + } +} diff --git a/go/vt/vtgate/executor.go b/go/vt/vtgate/executor.go index c395fedacfe..867685b8fec 100644 --- a/go/vt/vtgate/executor.go +++ b/go/vt/vtgate/executor.go @@ -461,7 +461,11 @@ func (e *Executor) addNeededBindVars(vcursor *vcursorImpl, bindVarNeeds *sqlpars }) bindVars[key] = sqltypes.Int64BindVariable(v) case sysvars.TransactionMode.Name: - bindVars[key] = sqltypes.StringBindVariable(session.TransactionMode.String()) + txMode := session.TransactionMode + if txMode == vtgatepb.TransactionMode_UNSPECIFIED { + txMode = getTxMode() + } + bindVars[key] = sqltypes.StringBindVariable(txMode.String()) case sysvars.Workload.Name: var v string ifOptionsExist(session, func(options *querypb.ExecuteOptions) { diff --git a/go/vt/vtgate/executor_select_test.go b/go/vt/vtgate/executor_select_test.go index dd214f9dfae..9603b5da5bc 100644 --- a/go/vt/vtgate/executor_select_test.go +++ b/go/vt/vtgate/executor_select_test.go @@ -801,7 +801,7 @@ func TestSelectSystemVariables(t *testing.T) { sqltypes.NewInt64(0), sqltypes.NewInt64(0), sqltypes.NewInt64(0), - sqltypes.NewVarChar("UNSPECIFIED"), + sqltypes.NewVarChar("MULTI"), sqltypes.NewVarChar(""), // these have been set at the beginning of the test sqltypes.NewVarChar("a fine gtid"),