Skip to content

Commit

Permalink
Merge pull request #8746 from planetscale/fix-8694-in-11
Browse files Browse the repository at this point in the history
Backport to 11: Fixing a panic in vtgate with OLAP mode
  • Loading branch information
harshit-gangal authored Sep 6, 2021
2 parents e272029 + bcd3534 commit e972eea
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
20 changes: 20 additions & 0 deletions go/test/endtoend/vtgate/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion go/vt/vtgate/engine/rename_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}
}
Expand Down

0 comments on commit e972eea

Please sign in to comment.