Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,55 @@ func TestBuildPlayerPlan(t *testing.T) {
},
},
},
}, {
input: &binlogdatapb.Filter{
Rules: []*binlogdatapb.Rule{{
Match: "t1",
Filter: "select c1, convert(c using utf8mb4) as c2 from t1",
}},
},
plan: &TestReplicatorPlan{
VStreamFilter: &binlogdatapb.Filter{
Rules: []*binlogdatapb.Rule{{
Match: "t1",
Filter: "select c1, convert(c using utf8mb4) as c2 from t1",
}},
},
TargetTables: []string{"t1"},
TablePlans: map[string]*TestTablePlan{
"t1": {
TargetName: "t1",
SendRule: "t1",
PKReferences: []string{"c1"},
InsertFront: "insert into t1(c1,c2)",
InsertValues: "(:a_c1,convert(:a_c using utf8mb4))",
Insert: "insert into t1(c1,c2) values (:a_c1,convert(:a_c using utf8mb4))",
Update: "update t1 set c2=convert(:a_c using utf8mb4) where c1=:b_c1",
Delete: "delete from t1 where c1=:b_c1",
},
},
},
planpk: &TestReplicatorPlan{
VStreamFilter: &binlogdatapb.Filter{
Rules: []*binlogdatapb.Rule{{
Match: "t1",
Filter: "select c1, convert(c using utf8mb4) as c2, pk1, pk2 from t1",
}},
},
TargetTables: []string{"t1"},
TablePlans: map[string]*TestTablePlan{
"t1": {
TargetName: "t1",
SendRule: "t1",
PKReferences: []string{"c1", "pk1", "pk2"},
InsertFront: "insert into t1(c1,c2)",
InsertValues: "(:a_c1,convert(:a_c using utf8mb4))",
Insert: "insert into t1(c1,c2) select :a_c1, convert(:a_c using utf8mb4) from dual where (:a_pk1,:a_pk2) <= (1,'aaa')",
Update: "update t1 set c2=convert(:a_c using utf8mb4) where c1=:b_c1 and (:b_pk1,:b_pk2) <= (1,'aaa')",
Delete: "delete from t1 where c1=:b_c1 and (:b_pk1,:b_pk2) <= (1,'aaa')",
},
},
},
}, {
// Keywords as names.
input: &binlogdatapb.Filter{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,23 @@ func (tpb *tablePlanBuilder) analyzeExpr(selExpr sqlparser.SelectExpr) (*colExpr
references: make(map[string]bool),
}
if expr, ok := aliased.Expr.(*sqlparser.ConvertUsingExpr); ok {
var colName sqlparser.IdentifierCI

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment here (a short paragraph) explaining why the following code is necessary.

err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
switch node := node.(type) {
case *sqlparser.ColName:
if !node.Qualifier.IsEmpty() {
return false, fmt.Errorf("unsupported qualifier for column: %v", sqlparser.String(node))
}
colName = node.Name
}
return true, nil
}, aliased.Expr)
if err != nil {
return nil, fmt.Errorf("failed to find column name for convert using expression: %v", sqlparser.String(aliased.Expr))
}
selExpr := &sqlparser.ConvertUsingExpr{
Type: "utf8mb4",
Expr: &sqlparser.ColName{Name: as},
Expr: &sqlparser.ColName{Name: colName},
}
cexpr.expr = expr
cexpr.operation = opExpr
Expand Down
16 changes: 15 additions & 1 deletion go/vt/vttablet/tabletserver/vstreamer/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,21 @@ func (plan *Plan) analyzeExpr(vschema *localVSchema, selExpr sqlparser.SelectExp
FixedValue: sqltypes.NewInt64(num),
}, nil
case *sqlparser.ConvertUsingExpr:
colnum, err := findColumn(plan.Table, aliased.As)
var colName sqlparser.IdentifierCI

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly please add a comment here as well.

err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
switch node := node.(type) {
case *sqlparser.ColName:
if !node.Qualifier.IsEmpty() {
return false, fmt.Errorf("unsupported qualifier for column: %v", sqlparser.String(node))
}
colName = node.Name
}
return true, nil
}, aliased.Expr)
if err != nil {
return ColExpr{}, fmt.Errorf("failed to find column name for convert using expression: %v", sqlparser.String(aliased.Expr))
}
colnum, err := findColumn(plan.Table, colName)
if err != nil {
return ColExpr{}, err
}
Expand Down
23 changes: 23 additions & 0 deletions go/vt/vttablet/tabletserver/vstreamer/planbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,29 @@ func TestPlanBuilder(t *testing.T) {
KeyRange: nil,
}},
},
}, {
inTable: t1,
inRule: &binlogdatapb.Rule{Match: "t1", Filter: "select convert(val using utf8mb4) as val2, id as id from t1"},
outPlan: &Plan{
ColExprs: []ColExpr{{
ColNum: 1,
Field: &querypb.Field{
Name: "val",
Type: sqltypes.VarBinary,
Charset: collations.CollationBinaryID,
Flags: uint32(querypb.MySqlFlag_BINARY_FLAG),
},
}, {
ColNum: 0,
Field: &querypb.Field{
Name: "id",
Type: sqltypes.Int64,
Charset: collations.CollationBinaryID,
Flags: uint32(querypb.MySqlFlag_NUM_FLAG),
},
}},
convertUsingUTF8Columns: map[string]bool{"val": true},
},
}, {
inTable: regional,
inRule: &binlogdatapb.Rule{Match: "regional", Filter: "select id, keyspace_id() from regional"},
Expand Down