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

executor: fix execute failed when table schema changed #33519

Merged
merged 3 commits into from
Mar 30, 2022
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
1 change: 0 additions & 1 deletion planner/core/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ type CachedPrepareStmt struct {
PreparedAst *ast.Prepared
StmtDB string // which DB the statement will be processed over
VisitInfos []visitInfo
ColumnInfos interface{}
Executor interface{}
NormalizedSQL string
NormalizedPlan string
Expand Down
25 changes: 6 additions & 19 deletions server/driver_tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ func (ts *TiDBStatement) Execute(ctx context.Context, args []types.Datum) (rs Re
return
}
rs = &tidbResultSet{
recordSet: tidbRecordset,
preparedStmt: ts.ctx.GetSessionVars().PreparedStmts[ts.id].(*core.CachedPrepareStmt),
recordSet: tidbRecordset,
}
return
}
Expand Down Expand Up @@ -302,11 +301,10 @@ func (tc *TiDBContext) GetStmtStats() *stmtstats.StatementStats {
}

type tidbResultSet struct {
recordSet sqlexec.RecordSet
columns []*ColumnInfo
rows []chunk.Row
closed int32
preparedStmt *core.CachedPrepareStmt
recordSet sqlexec.RecordSet
columns []*ColumnInfo
rows []chunk.Row
closed int32
}

func (trs *tidbResultSet) NewChunk(alloc chunk.Allocator) *chunk.Chunk {
Expand Down Expand Up @@ -348,23 +346,12 @@ func (trs *tidbResultSet) Columns() []*ColumnInfo {
if trs.columns != nil {
return trs.columns
}
// for prepare statement, try to get cached columnInfo array
if trs.preparedStmt != nil {
ps := trs.preparedStmt
if colInfos, ok := ps.ColumnInfos.([]*ColumnInfo); ok {
trs.columns = colInfos
}
}

if trs.columns == nil {
fields := trs.recordSet.Fields()
for _, v := range fields {
trs.columns = append(trs.columns, convertColumnInfo(v))
}
if trs.preparedStmt != nil {
// if ColumnInfo struct has allocated object,
// here maybe we need deep copy ColumnInfo to do caching
trs.preparedStmt.ColumnInfos = trs.columns
}
}
return trs.columns
}
Expand Down
41 changes: 41 additions & 0 deletions server/tidb_serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,47 @@ func TestPrepareCount(t *testing.T) {
require.NoError(t, qctx.Close())
}

func TestPrepareExecute(t *testing.T) {
ts, cleanup := createTidbTestSuite(t)
defer cleanup()

qctx, err := ts.tidbdrv.OpenCtx(uint64(0), 0, uint8(tmysql.DefaultCollationID), "test", nil)
require.NoError(t, err)

ctx := context.Background()
_, err = qctx.Execute(ctx, "use test")
require.NoError(t, err)
_, err = qctx.Execute(ctx, "create table t1(id int primary key, v int)")
require.NoError(t, err)
_, err = qctx.Execute(ctx, "insert into t1 values(1, 100)")
require.NoError(t, err)

stmt, _, _, err := qctx.Prepare("select * from t1 where id=1")
require.NoError(t, err)
rs, err := stmt.Execute(ctx, nil)
require.NoError(t, err)
req := rs.NewChunk(nil)
require.NoError(t, rs.Next(ctx, req))
require.Equal(t, 2, req.NumCols())
require.Equal(t, req.NumCols(), len(rs.Columns()))
require.Equal(t, 1, req.NumRows())
require.Equal(t, int64(1), req.GetRow(0).GetInt64(0))
require.Equal(t, int64(100), req.GetRow(0).GetInt64(1))

// issue #33509
_, err = qctx.Execute(ctx, "alter table t1 drop column v")
require.NoError(t, err)

rs, err = stmt.Execute(ctx, nil)
require.NoError(t, err)
req = rs.NewChunk(nil)
require.NoError(t, rs.Next(ctx, req))
require.Equal(t, 1, req.NumCols())
require.Equal(t, req.NumCols(), len(rs.Columns()))
require.Equal(t, 1, req.NumRows())
require.Equal(t, int64(1), req.GetRow(0).GetInt64(0))
}

func TestDefaultCharacterAndCollation(t *testing.T) {
ts, cleanup := createTidbTestSuite(t)
defer cleanup()
Expand Down