diff --git a/go/vt/sqlparser/ast_rewriting.go b/go/vt/sqlparser/ast_rewriting.go index 7791f1cfd8f..bc0e8653ae3 100644 --- a/go/vt/sqlparser/ast_rewriting.go +++ b/go/vt/sqlparser/ast_rewriting.go @@ -237,6 +237,7 @@ func (er *expressionRewriter) sysVarRewrite(cursor *Cursor, node *ColName) { sysvars.SessionUUID.Name, sysvars.ReadAfterWriteGTID.Name, sysvars.ReadAfterWriteTimeOut.Name, + sysvars.VitessVersion.Name, sysvars.SessionTrackGTIDs.Name: cursor.Replace(bindVarExpression("__vt" + lowered)) er.bindVars.AddSysVar(lowered) diff --git a/go/vt/sqlparser/ast_rewriting_test.go b/go/vt/sqlparser/ast_rewriting_test.go index 802f632f627..64f185c42da 100644 --- a/go/vt/sqlparser/ast_rewriting_test.go +++ b/go/vt/sqlparser/ast_rewriting_test.go @@ -32,7 +32,7 @@ type myTestCase struct { ddlStrategy, sessionUUID bool udv int autocommit, clientFoundRows, skipQueryPlanCache bool - sqlSelectLimit, transactionMode, workload bool + sqlSelectLimit, transactionMode, workload, vitessVersion bool } func TestRewrites(in *testing.T) { @@ -40,6 +40,10 @@ func TestRewrites(in *testing.T) { in: "SELECT 42", expected: "SELECT 42", // no bindvar needs + }, { + in: "SELECT @@vitess_version", + expected: "SELECT :__vtvitess_version as `@@vitess_version`", + vitessVersion: true, }, { in: "SELECT last_insert_id()", expected: "SELECT :__lastInsertId as `last_insert_id()`", @@ -198,6 +202,7 @@ func TestRewrites(in *testing.T) { assert.Equal(tc.rawGTID, result.NeedsSysVar(sysvars.ReadAfterWriteGTID.Name), "should need rawGTID") assert.Equal(tc.rawTimeout, result.NeedsSysVar(sysvars.ReadAfterWriteTimeOut.Name), "should need rawTimeout") assert.Equal(tc.sessTrackGTID, result.NeedsSysVar(sysvars.SessionTrackGTIDs.Name), "should need sessTrackGTID") + assert.Equal(tc.vitessVersion, result.NeedsSysVar(sysvars.VitessVersion.Name), "should need Vitess version") }) } } diff --git a/go/vt/sysvars/sysvars.go b/go/vt/sysvars/sysvars.go index b0e60b61d6d..2c6515ca28e 100644 --- a/go/vt/sysvars/sysvars.go +++ b/go/vt/sysvars/sysvars.go @@ -55,7 +55,8 @@ var ( Names = SystemVariable{Name: "names", Default: utf8, IdentifierAsString: true} SessionUUID = SystemVariable{Name: "session_uuid", IdentifierAsString: true} // Online DDL - DDLStrategy = SystemVariable{Name: "ddl_strategy", IdentifierAsString: true} + DDLStrategy = SystemVariable{Name: "ddl_strategy", IdentifierAsString: true} + VitessVersion = SystemVariable{Name: "vitess_version", IdentifierAsString: true} // Read After Write settings ReadAfterWriteGTID = SystemVariable{Name: "read_after_write_gtid"} diff --git a/go/vt/vtgate/executor.go b/go/vt/vtgate/executor.go index 88864f13d2d..7dcd4aa73e2 100644 --- a/go/vt/vtgate/executor.go +++ b/go/vt/vtgate/executor.go @@ -29,6 +29,8 @@ import ( "sync" "time" + "vitess.io/vitess/go/vt/servenv" + "vitess.io/vitess/go/vt/sysvars" "context" @@ -315,6 +317,8 @@ func (e *Executor) addNeededBindVars(bindVarNeeds *sqlparser.BindVarNeeds, bindV } }) bindVars[key] = sqltypes.StringBindVariable(v) + case sysvars.VitessVersion.Name: + bindVars[key] = sqltypes.StringBindVariable(servenv.AppVersion.String()) } }