-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executor, session: Add Detach() method to detach the record set (#54091)
close #54090
- Loading branch information
Showing
7 changed files
with
323 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "staticrecordset", | ||
srcs = ["recordset.go"], | ||
importpath = "github.com/pingcap/tidb/pkg/executor/staticrecordset", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/executor/internal/exec", | ||
"//pkg/parser/ast", | ||
"//pkg/util", | ||
"//pkg/util/chunk", | ||
"//pkg/util/logutil", | ||
"//pkg/util/sqlexec", | ||
"@org_uber_go_zap//:zap", | ||
], | ||
) | ||
|
||
go_test( | ||
name = "staticrecordset_test", | ||
timeout = "short", | ||
srcs = ["integration_test.go"], | ||
flaky = True, | ||
shard_count = 4, | ||
deps = [ | ||
"//pkg/testkit", | ||
"//pkg/util/sqlexec", | ||
"@com_github_stretchr_testify//require", | ||
"@com_github_tikv_client_go_v2//tikv", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// Copyright 2024 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package staticrecordset_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/pingcap/tidb/pkg/testkit" | ||
"github.com/pingcap/tidb/pkg/util/sqlexec" | ||
"github.com/stretchr/testify/require" | ||
"github.com/tikv/client-go/v2/tikv" | ||
) | ||
|
||
func TestStaticRecordSet(t *testing.T) { | ||
store := testkit.CreateMockStore(t) | ||
tk := testkit.NewTestKit(t, store) | ||
|
||
tk.MustExec("use test") | ||
tk.MustExec("create table t(id int)") | ||
tk.MustExec("insert into t values (1), (2), (3)") | ||
|
||
rs, err := tk.Exec("select * from t") | ||
require.NoError(t, err) | ||
drs := rs.(sqlexec.DetachableRecordSet) | ||
srs, ok, err := drs.TryDetach() | ||
require.True(t, ok) | ||
require.NoError(t, err) | ||
|
||
// check schema | ||
require.Len(t, srs.Fields(), 1) | ||
require.Equal(t, "id", srs.Fields()[0].Column.Name.O) | ||
|
||
// check data | ||
chk := srs.NewChunk(nil) | ||
err = srs.Next(context.Background(), chk) | ||
require.NoError(t, err) | ||
require.Equal(t, 3, chk.NumRows()) | ||
require.Equal(t, int64(1), chk.GetRow(0).GetInt64(0)) | ||
require.Equal(t, int64(2), chk.GetRow(1).GetInt64(0)) | ||
require.Equal(t, int64(3), chk.GetRow(2).GetInt64(0)) | ||
|
||
require.NoError(t, srs.Close()) | ||
} | ||
|
||
func TestStaticRecordSetWithTxn(t *testing.T) { | ||
store := testkit.CreateMockStore(t) | ||
tk := testkit.NewTestKit(t, store) | ||
|
||
tk.MustExec("use test") | ||
tk.MustExec("create table t(id int)") | ||
tk.MustExec("insert into t values (1), (2), (3)") | ||
|
||
rs, err := tk.Exec("select * from t") | ||
require.NoError(t, err) | ||
txn, err := tk.Session().Txn(false) | ||
require.NoError(t, err) | ||
require.True(t, txn.Valid()) | ||
drs := rs.(sqlexec.DetachableRecordSet) | ||
srs, ok, err := drs.TryDetach() | ||
require.True(t, ok) | ||
require.NoError(t, err) | ||
|
||
// The transaction should have been committed. | ||
txn, err = tk.Session().Txn(false) | ||
require.NoError(t, err) | ||
require.False(t, txn.Valid()) | ||
|
||
// Now, it's fine to run another statement on the session | ||
// remove all existing data in the table | ||
tk.MustExec("truncate table t") | ||
|
||
// check data | ||
chk := srs.NewChunk(nil) | ||
err = srs.Next(context.Background(), chk) | ||
require.NoError(t, err) | ||
require.Equal(t, 3, chk.NumRows()) | ||
require.Equal(t, int64(1), chk.GetRow(0).GetInt64(0)) | ||
require.Equal(t, int64(2), chk.GetRow(1).GetInt64(0)) | ||
require.Equal(t, int64(3), chk.GetRow(2).GetInt64(0)) | ||
|
||
require.NoError(t, srs.Close()) | ||
} | ||
|
||
func TestStaticRecordSetExceedGCTime(t *testing.T) { | ||
store := testkit.CreateMockStore(t) | ||
tk := testkit.NewTestKit(t, store) | ||
|
||
tk.MustExec("use test") | ||
tk.MustExec("create table t(id int)") | ||
tk.MustExec("insert into t values (1), (2), (3)") | ||
|
||
rs, err := tk.Exec("select * from t") | ||
require.NoError(t, err) | ||
// Get the startTS | ||
txn, err := tk.Session().Txn(false) | ||
require.NoError(t, err) | ||
startTS := txn.StartTS() | ||
|
||
// Detach the record set | ||
drs := rs.(sqlexec.DetachableRecordSet) | ||
srs, ok, err := drs.TryDetach() | ||
require.True(t, ok) | ||
require.NoError(t, err) | ||
|
||
// Now, it's fine to run another statement on the session | ||
// remove all existing data in the table | ||
tk.MustExec("truncate table t") | ||
|
||
// Update the safe point | ||
store.(tikv.Storage).UpdateSPCache(startTS+1, time.Now()) | ||
|
||
// Check data, it'll get an error | ||
chk := srs.NewChunk(nil) | ||
err = srs.Next(context.Background(), chk) | ||
require.Error(t, err) | ||
require.NoError(t, srs.Close()) | ||
} | ||
|
||
func TestDetachError(t *testing.T) { | ||
store := testkit.CreateMockStore(t) | ||
tk := testkit.NewTestKit(t, store) | ||
|
||
tk.MustExec("use test") | ||
tk.MustExec("create table t(id int)") | ||
tk.MustExec("insert into t values (1), (2), (3)") | ||
|
||
// explicit transaction is not allowed | ||
tk.MustExec("begin") | ||
rs, err := tk.Exec("select * from t") | ||
require.NoError(t, err) | ||
drs2 := rs.(sqlexec.DetachableRecordSet) | ||
_, ok, err := drs2.TryDetach() | ||
require.False(t, ok) | ||
require.NoError(t, err) | ||
tk.MustExec("commit") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2024 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package staticrecordset | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/pingcap/tidb/pkg/executor/internal/exec" | ||
"github.com/pingcap/tidb/pkg/parser/ast" | ||
"github.com/pingcap/tidb/pkg/util" | ||
"github.com/pingcap/tidb/pkg/util/chunk" | ||
"github.com/pingcap/tidb/pkg/util/logutil" | ||
"github.com/pingcap/tidb/pkg/util/sqlexec" | ||
"go.uber.org/zap" | ||
) | ||
|
||
var _ sqlexec.RecordSet = &staticRecordSet{} | ||
|
||
type staticRecordSet struct { | ||
fields []*ast.ResultField | ||
executor exec.Executor | ||
|
||
sqlText string | ||
} | ||
|
||
// New creates a new staticRecordSet | ||
func New(fields []*ast.ResultField, executor exec.Executor, sqlText string) sqlexec.RecordSet { | ||
return &staticRecordSet{ | ||
fields: fields, | ||
executor: executor, | ||
sqlText: sqlText, | ||
} | ||
} | ||
|
||
func (s *staticRecordSet) Fields() []*ast.ResultField { | ||
return s.fields | ||
} | ||
|
||
func (s *staticRecordSet) Next(ctx context.Context, req *chunk.Chunk) (err error) { | ||
defer func() { | ||
r := recover() | ||
if r == nil { | ||
return | ||
} | ||
err = util.GetRecoverError(r) | ||
logutil.Logger(ctx).Error("execute sql panic", zap.String("sql", s.sqlText), zap.Stack("stack")) | ||
}() | ||
|
||
return exec.Next(ctx, s.executor, req) | ||
} | ||
|
||
// NewChunk create a chunk base on top-level executor's exec.NewFirstChunk(). | ||
func (s *staticRecordSet) NewChunk(alloc chunk.Allocator) *chunk.Chunk { | ||
if alloc == nil { | ||
return exec.NewFirstChunk(s.executor) | ||
} | ||
|
||
return alloc.Alloc(s.executor.RetFieldTypes(), s.executor.InitCap(), s.executor.MaxChunkSize()) | ||
} | ||
|
||
// Close closes the executor. | ||
func (s *staticRecordSet) Close() error { | ||
err := exec.Close(s.executor) | ||
s.executor = nil | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters