Skip to content
Snippets Groups Projects
Unverified Commit e7f1cb91 authored by ou yuanning's avatar ou yuanning Committed by GitHub
Browse files

fix prepare for update bug (#5099)

parent 40f16bb3
Branches
No related tags found
No related merge requests found
......@@ -106,14 +106,7 @@ func DeepCopyNode(node *plan.Node) *plan.Node {
newNode.UpdateCtxs[i].UpdateCols[j] = &plan.ColDef{
Name: col.Name,
Alg: col.Alg,
Typ: &plan.Type{
Id: col.Typ.Id,
Nullable: col.Typ.Nullable,
Width: col.Typ.Width,
Precision: col.Typ.Precision,
Size: col.Typ.Size,
Scale: col.Typ.Scale,
},
Typ: DeepCopyTyp(col.Typ),
Default: DeepCopyDefault(col.Default),
Primary: col.Primary,
Pkidx: col.Pkidx,
......@@ -201,18 +194,22 @@ func DeepCopyDefault(def *plan.Default) *plan.Default {
}
}
func DeepCopyTyp(typ *plan.Type) *plan.Type {
return &plan.Type{
Id: typ.Id,
Nullable: typ.Nullable,
Width: typ.Width,
Precision: typ.Precision,
Size: typ.Size,
Scale: typ.Scale,
}
}
func DeepCopyColDef(col *plan.ColDef) *plan.ColDef {
return &plan.ColDef{
Name: col.Name,
Alg: col.Alg,
Typ: &plan.Type{
Id: col.Typ.Id,
Nullable: col.Typ.Nullable,
Width: col.Typ.Width,
Precision: col.Typ.Precision,
Size: col.Typ.Size,
Scale: col.Typ.Scale,
},
Typ: DeepCopyTyp(col.Typ),
Default: DeepCopyDefault(col.Default),
Primary: col.Primary,
Pkidx: col.Pkidx,
......
......@@ -25,19 +25,21 @@ type VisitPlanRule interface {
type VisitPlan struct {
plan *Plan
isUpdatePlan bool
rules []VisitPlanRule
}
func NewVisitPlan(pl *Plan, rules []VisitPlanRule) *VisitPlan {
return &VisitPlan{
plan: pl,
isUpdatePlan: false,
rules: rules,
}
}
func (vq *VisitPlan) visitNode(qry *Query, node *Node) error {
func (vq *VisitPlan) visitNode(qry *Query, node *Node, idx int32) error {
for i := range node.Children {
if err := vq.visitNode(qry, qry.Nodes[node.Children[i]]); err != nil {
if err := vq.visitNode(qry, qry.Nodes[node.Children[i]], node.Children[i]); err != nil {
return err
}
}
......@@ -49,7 +51,7 @@ func (vq *VisitPlan) visitNode(qry *Query, node *Node) error {
return err
}
} else if rule.IsApplyExpr() {
err := vq.exploreNode(rule, node)
err := vq.exploreNode(rule, node, idx)
if err != nil {
return err
}
......@@ -59,7 +61,7 @@ func (vq *VisitPlan) visitNode(qry *Query, node *Node) error {
return nil
}
func (vq *VisitPlan) exploreNode(rule VisitPlanRule, node *Node) error {
func (vq *VisitPlan) exploreNode(rule VisitPlanRule, node *Node, idx int32) error {
var err error
if node.Limit != nil {
node.Limit, err = rule.ApplyExpr(node.Limit)
......@@ -90,7 +92,25 @@ func (vq *VisitPlan) exploreNode(rule VisitPlanRule, node *Node) error {
}
for i := range node.ProjectList {
// if prepare statement is: update set decimal_col = decimal_col + ? ;
// and then: 'set @a=12.22; execute stmt1 using @a;' decimal_col + ? will be float64
if vq.isUpdatePlan {
pl, _ := vq.plan.Plan.(*Plan_Query)
num := len(pl.Query.Nodes) - int(idx)
// last project Node
if num == 2 {
oldType := DeepCopyTyp(node.ProjectList[i].Typ)
node.ProjectList[i], err = rule.ApplyExpr(node.ProjectList[i])
if node.ProjectList[i].Typ.Id != oldType.Id {
node.ProjectList[i], err = makePlan2CastExpr(node.ProjectList[i], oldType)
}
} else {
node.ProjectList[i], err = rule.ApplyExpr(node.ProjectList[i])
}
} else {
node.ProjectList[i], err = rule.ApplyExpr(node.ProjectList[i])
}
if err != nil {
return err
}
......@@ -103,12 +123,14 @@ func (vq *VisitPlan) Visit() error {
switch pl := vq.plan.Plan.(type) {
case *Plan_Query:
qry := pl.Query
vq.isUpdatePlan = (pl.Query.StmtType == plan.Query_UPDATE)
if len(qry.Steps) == 0 {
return nil
}
for _, step := range qry.Steps {
err := vq.visitNode(qry, qry.Nodes[step])
err := vq.visitNode(qry, qry.Nodes[step], step)
if err != nil {
return err
}
......
......@@ -49,3 +49,10 @@ execute stmt2;
set @var_t1= 'aa';
execute stmt2;
deallocate prepare stmt2;
create table t1 (a decimal(20,4), b int);
insert into t1 values (12.2222, 1);
prepare stmt1 from 'update t1 set a=a+? where b = 1';
set @a=0.1111;
execute stmt1 using @a;
select a, b from t1;
\ No newline at end of file
......@@ -54,16 +54,13 @@ CREATE TABLE test_table
dou DOUBLE
);
-- @bvt:issue#4484
set @float1_num=1.2345678;
set @float2_num=1.8765432;
set @double_num1=1.223344556677889900;
set @double_num2=1.223344556677889900;
INSERT INTO test_table VALUES(0, @float1_num, @double_num1), (1, @float2_num, @double_num2);
-- @bvt:issue
-- @bvt:issue#4487
INSERT INTO test_table VALUES(0, 1.2345678, 1.223344556677889900), (1, 1.876599999432, 1.223344556677889900);
select * from test_table;
select * from test_table where fl=1.2345678;
......@@ -87,7 +84,6 @@ EXECUTE s2 USING @dou_not_hit;
DEALLOCATE PREPARE s1;
DEALLOCATE PREPARE s2;
-- @bvt:issue
DROP TABLE test_table;
......@@ -130,7 +126,6 @@ select * from t1;
DEALLOCATE PREPARE s2;
-- @bvt:issue#4526
prepare s3 from 'select * from t1 where str1 like ?';
prepare s4 from 'select * from t1 where str2 not like ?';
......@@ -139,7 +134,6 @@ execute s3 using @a;
DEALLOCATE PREPARE s3;
DEALLOCATE PREPARE s4;
-- @bvt:issue
prepare s5 from 'select * from t1 where str2=?';
......
......@@ -76,3 +76,12 @@ execute stmt2;
@var_t1
aa
deallocate prepare stmt2;
create table t1 (a decimal(20,4), b int);
insert into t1 values (12.2222, 1);
prepare stmt1 from 'update t1 set a=a+? where b = 1';
set @a=0.1111;
execute stmt1 using @a;
select a, b from t1;
a b
12.3333 1
\ No newline at end of file
......@@ -48,7 +48,7 @@ set @double_num1=1.223344556677889900;
set @double_num2=1.223344556677889900;
INSERT INTO test_table VALUES(0, @float1_num, @double_num1), (1, @float2_num, @double_num2);
INSERT INTO test_table VALUES(0, 1.2345678, 1.223344556677889900), (1, 1.876599999432, 1.223344556677889900);
Duplicate entry '0' for key 'test_table.PRIMARY'
tae data: duplicate
select * from test_table;
pk fl dou
0 1.23457 1.22334455667789
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment