Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport to 11: Fixing a panic in vtgate with OLAP mode #8746

Merged
merged 6 commits into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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