diff --git a/go/test/endtoend/vtgate/misc_test.go b/go/test/endtoend/vtgate/misc_test.go index 7bd41b5c9ac..4e813c0a715 100644 --- a/go/test/endtoend/vtgate/misc_test.go +++ b/go/test/endtoend/vtgate/misc_test.go @@ -704,6 +704,26 @@ ts12 TIMESTAMP DEFAULT LOCALTIME() exec(t, conn, "drop table function_default") } +func TestRenameFieldsOnOLAP(t *testing.T) { + defer cluster.PanicHandler(t) + ctx := context.Background() + conn, err := mysql.Connect(ctx, &vtParams) + require.NoError(t, err) + defer conn.Close() + + _ = exec(t, conn, "set workload = olap") + defer func() { + exec(t, conn, "set workload = oltp") + }() + + qr := exec(t, conn, "show tables") + require.Equal(t, 1, len(qr.Fields)) + assert.Equal(t, `Tables_in_ks`, fmt.Sprintf("%v", qr.Fields[0].Name)) + _ = exec(t, conn, "use mysql") + qr = exec(t, conn, "select @@workload") + assert.Equal(t, `[[VARBINARY("OLAP")]]`, fmt.Sprintf("%v", qr.Rows)) +} + func assertMatches(t *testing.T, conn *mysql.Conn, query, expected string) { t.Helper() qr := exec(t, conn, query) diff --git a/go/vt/vtgate/engine/rename_fields.go b/go/vt/vtgate/engine/rename_fields.go index 9983c5036e1..68b84f5155f 100644 --- a/go/vt/vtgate/engine/rename_fields.go +++ b/go/vt/vtgate/engine/rename_fields.go @@ -74,6 +74,9 @@ func (r *RenameFields) Execute(vcursor VCursor, bindVars map[string]*querypb.Bin func (r *RenameFields) renameFields(qr *sqltypes.Result) { for ind, index := range r.Indices { + if index >= len(qr.Fields) { + continue + } colName := r.Cols[ind] qr.Fields[index].Name = colName } @@ -84,7 +87,11 @@ func (r *RenameFields) StreamExecute(vcursor VCursor, bindVars map[string]*query if wantfields { innerCallback := callback callback = func(result *sqltypes.Result) error { - r.renameFields(result) + // Only the first callback will contain the fields. + // This check is to avoid going over the RenameFields indices when no fields are present in the result set. + if len(result.Fields) != 0 { + r.renameFields(result) + } return innerCallback(result) } }