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

*: fix the sysvar value may be corrupted after set by subquery #41003

Merged
merged 5 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 10 additions & 1 deletion executor/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,16 @@ func (e *SetExecutor) getVarValue(ctx context.Context, v *expression.VarAssignme
if err != nil || nativeVal.IsNull() {
return "", err
}
return nativeVal.ToString()

value, err = nativeVal.ToString()
if err != nil {
return "", err
}

// We need to clone the string because the value is constructed by `hack.String` in Datum which reuses the under layer `[]byte`
// instead of allocating some new spaces. The `[]byte` in Datum will be reused in `chunk.Chunk` by different statements in session.
// If we do not clone the value, the system variable will have a risk to be modified by other statements.
return strings.Clone(value), nil
}

func (e *SetExecutor) loadSnapshotInfoSchemaIfNeeded(name string, snapshotTS uint64) error {
Expand Down
35 changes: 35 additions & 0 deletions server/tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2950,3 +2950,38 @@ func TestSandBoxMode(t *testing.T) {
require.NoError(t, err)
}
}

func TestIssue40979(t *testing.T) {
ts := createTidbTestSuite(t)

db, err := sql.Open("mysql", ts.getDSN())
require.NoError(t, err)
defer func() {
require.NoError(t, db.Close())
}()

conn, err := db.Conn(context.Background())
require.NoError(t, err)
defer func() {
require.NoError(t, conn.Close())
}()

rs, err := conn.QueryContext(context.Background(), "show tables in test")
ts.Rows(t, rs)

_, err = conn.ExecContext(context.Background(), "set @@time_zone=(select 'Asia/Shanghai')")
require.NoError(t, err)

rs, err = conn.QueryContext(context.Background(), "select TIDB_TABLE_ID from information_schema.tables where TABLE_SCHEMA='aaaa'")
ts.Rows(t, rs)

rs, err = conn.QueryContext(context.Background(), "select @@time_zone")
require.NoError(t, err)
defer func() {
require.NoError(t, rs.Close())
}()

rows := ts.Rows(t, rs)
require.Equal(t, 1, len(rows))
require.Equal(t, "Asia/Shanghai", rows[0])
}