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

core: add the implemet of setparamtype at show, showddl, datasource, limit plan ; executor: add new case 'LogicalLimit' at Next() #62

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions executor/prepared.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ func (e *PrepareExec) Next(ctx context.Context, req *chunk.Chunk) error {
err = SetUpdateParamType(p.(*plannercore.Update), &prepared.Params)
case *plannercore.LogicalSort:
err = SetSortType(p.(*plannercore.LogicalSort), &prepared.Params)
case *plannercore.LogicalLimit:
err = SetLimitType(p.(*plannercore.LogicalLimit), &prepared.Params)
}
if err != nil {
return err
Expand Down Expand Up @@ -397,6 +399,11 @@ func SetSortType(sort *plannercore.LogicalSort, i *[]ast.ParamMarkerExpr) error
return sort.SetParamType(i)
}

// SetLimitType set the parameter type of limit plan
func SetLimitType(limit *plannercore.LogicalLimit, i *[]ast.ParamMarkerExpr) error {
return limit.SetParamType(i)
}

// ExecuteExec represents an EXECUTE executor.
// It cannot be executed by itself, all it needs to do is to build
// another Executor from a prepared statement.
Expand Down
62 changes: 62 additions & 0 deletions executor/prepared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,65 @@ func (s *testPrepareSuite) TestPlanCacheWithDifferentVariableTypes(c *C) {
}
}
}

type testParamType struct {
sql string
expectType []byte
paramCount int
}

//
func (s *testPrepareSuite) TestGetPrepareParamType(c *C) {
store, _, err := newStoreWithBootstrap()
c.Assert(err, IsNil)

se, err := session.CreateSession4Test(store)
c.Assert(err, IsNil)
tk := testkit.NewTestKit(c, store)
tk.Se = se

tk.Se.Auth(&auth.UserIdentity{Username: "root", Hostname: "localhost", CurrentUser: true, AuthUsername: "root", AuthHostname: "%"}, nil, []byte("012345678901234567890"))

sm := &mockSessionManager2{
se: se,
}
se.SetSessionManager(sm)

tk.MustExec("use test")
tk.MustExec("drop table if exists student")

tk.MustExec("create table student (id int primary key, name varchar(255), age int)")
tk.MustExec("insert into student values(1, \"zhangsan\", 18)")

sqlList := []*testParamType{
{
// limit and offset followed by parameters are constants,
// their kind be set to "0" by default.
sql: "select * from student where name = $1 limit $2 offset 1",
paramCount: 2,
expectType: []byte{mysql.TypeVarchar, mysql.TypeDecimal},
}, {
// If limit and offset followed by parameters are ParamMakerExpr,
// TiDB-Server will generate a LogicalTableDual,
// We can't get any param type.
// todo get param type in this case.
sql: "select * from student where name = $1 limit $2 offset $3",
paramCount: 3,
expectType: []byte{mysql.TypeDecimal, mysql.TypeDecimal, mysql.TypeDecimal},
},
}

for _, v1 := range sqlList {
stmtID, paramCount, _, err := tk.Se.PrepareStmt(v1.sql, "")
c.Assert(paramCount, Equals, v1.paramCount)
c.Assert(err, IsNil)

if cachedStmt, ok := tk.Se.GetSessionVars().PreparedStmts[stmtID].(*plannercore.CachedPrepareStmt); ok {
cachedParams := cachedStmt.PreparedAst.Params
for i, _ := range cachedParams {
paramType := cachedParams[i].GetType().Tp
c.Assert(paramType, Equals, v1.expectType[i])
}
}
}
}
27 changes: 22 additions & 5 deletions planner/core/logical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,8 @@ type DataSource struct {
isForUpdateRead bool
}

// SetParamType todo 设置参数类型
// SetParamType set the parameter type of dataSource plan
// dataSource plan is always the last node of o plan tree and it will not contain any parameter.Just return nil
func (ds *DataSource) SetParamType(paramExprs *[]ast.ParamMarkerExpr) (err error) {
return err
}
Expand Down Expand Up @@ -1098,8 +1099,17 @@ type LogicalLimit struct {
limitHints limitHintInfo
}

// SetParamType todo 设置参数类型
func (p *LogicalLimit) SetParamType(paramExprs *[]ast.ParamMarkerExpr) (err error) {
// SetParamType set the parameter type of logicalLimit plan
// when send "limit $1 offset $2",planbuilder will convert it to a tableDual plan.see why at /planner/core/logical_plan_builder.go buildLimit
func (ll *LogicalLimit) SetParamType(paramExprs *[]ast.ParamMarkerExpr) (err error) {
if childs := ll.children; childs != nil {
for _, child := range childs {
err = child.SetParamType(paramExprs)
if err != nil {
return err
}
}
}
return err
}

Expand Down Expand Up @@ -1240,7 +1250,11 @@ type LogicalShow struct {
ShowContents
}

// SetParamType todo 设置参数类型
// SetParamType set the parameter type of logicalShow plan
// https://www.postgresql.org/docs/current/sql-show.html
// According to the postgresql document, any parameter won't follow the show statement.
// Besides, show statement won't be the sub statement of any other sql statement.
// So, Show statement doesn't need actual implement. Just return nil.
func (p *LogicalShow) SetParamType(paramExprs *[]ast.ParamMarkerExpr) (err error) {
return err
}
Expand All @@ -1252,7 +1266,10 @@ type LogicalShowDDLJobs struct {
JobNumber int64
}

// SetParamType todo 设置参数类型
// SetParamType set the parameter type of logicalShowDDLJobs plan
// https://www.postgresql.org/docs/current/sql-show.html
// Actually, postgresql doesn't support the syntax like "show create table tname"
// And showddl statement won't contain parameter. Just return nil
func (p *LogicalShowDDLJobs) SetParamType(paramExprs *[]ast.ParamMarkerExpr) (err error) {
return err
}
13 changes: 9 additions & 4 deletions planner/core/physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -916,8 +916,11 @@ type PhysicalShow struct {
ShowContents
}

// SetParamType set the parameter type in ParamMarkerExpr from PhysicalShow
// todo 从PhysicalShow计划中获取参数类型
// SetParamType set the parameter type of physicalShow plan
// https://www.postgresql.org/docs/current/sql-show.html
// According to the postgresql document, any parameter won't follow the show statement.
// Besides, show statement won't be the sub statement of any other sql statement.
// So, Show statement doesn't need actual implement. Just return nil.
func (show *PhysicalShow) SetParamType(paramExprs *[]ast.ParamMarkerExpr) (err error) {
return err
}
Expand All @@ -929,8 +932,10 @@ type PhysicalShowDDLJobs struct {
JobNumber int64
}

// SetParamType set the parameter type in ParamMarkerExpr from PhysicalShowDDLJobs
// todo 从PhysicalShowDDLJobs中获取参数类型
// SetParamType set the parameter type of physicalShowDDLJobs plan
// https://www.postgresql.org/docs/current/sql-show.html
// Actually, postgresql doesn't support the syntax like "show create table tname"
// And showddl statement won't contain parameter. Just return nil
func (ddl *PhysicalShowDDLJobs) SetParamType(paramExprs *[]ast.ParamMarkerExpr) (err error) {
return err
}
Expand Down