Skip to content

Commit

Permalink
cherry pick #33519 to release-5.4
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <[email protected]>
  • Loading branch information
lcwangchao authored and ti-srebot committed Mar 30, 2022
1 parent 8effbbc commit a9737c7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
1 change: 0 additions & 1 deletion planner/core/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ func NewPlanCacheValue(plan Plan, names []*types.FieldName, srcMap map[*model.Ta
type CachedPrepareStmt struct {
PreparedAst *ast.Prepared
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 @@ -297,11 +296,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 @@ -343,23 +341,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 @@ -366,6 +366,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

0 comments on commit a9737c7

Please sign in to comment.