-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
executor, session: Add Detach() method to detach the record set #54091
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
FIXME
will be fixed in #54114