Skip to content

Commit

Permalink
Update unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Lord <[email protected]>
  • Loading branch information
mattlord committed Jan 31, 2025
1 parent 3632c05 commit 1b597ec
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
13 changes: 7 additions & 6 deletions go/vt/vttablet/tabletserver/vstreamer/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ type Plan struct {
// of the table.
Filters []Filter

// WHERE clauses in the Filter query that we can push down to
// Predicates in the Filter query that we can push down to
// MySQL to reduce the returned rows we need to filter.
// This will contain any valid expressions in the Filter's
// WHERE clause with the exception of the in_keyspace()
// function which is a filter that must be applied by the
// vstreamer (it's not a valid MySQL function).
whereExprsToPushDown []sqlparser.Expr

// Convert any integer values seen in the binlog events for ENUM or SET
Expand Down Expand Up @@ -606,10 +610,7 @@ func (plan *Plan) analyzeWhere(vschema *localVSchema, where *sqlparser.Where) er
if !ok {
return fmt.Errorf("unexpected: %v", sqlparser.String(expr))
}
// Add it to the column expressions so that it's passed down to mysqld.
if plan.whereExprsToPushDown == nil {
plan.whereExprsToPushDown = make([]sqlparser.Expr, 0)
}
// Add it to the expressions that get pushed down to mysqld.
log.Errorf("DEBUG: adding to list of pushdown expressions: %v", sqlparser.String(expr))
plan.whereExprsToPushDown = append(plan.whereExprsToPushDown, expr)
// StrVal is varbinary, we do not support varchar since we would have to implement all collation types
Expand Down Expand Up @@ -659,7 +660,7 @@ func (plan *Plan) analyzeWhere(vschema *localVSchema, where *sqlparser.Where) er
Opcode: IsNotNull,
ColNum: colnum,
})
// Add it to the column expressions so that it's passed down to mysqld.
// Add it to the expressions that get pushed down to mysqld.
plan.whereExprsToPushDown = append(plan.whereExprsToPushDown, expr)
default:
return fmt.Errorf("unsupported constraint: %v", sqlparser.String(expr))
Expand Down
51 changes: 49 additions & 2 deletions go/vt/vttablet/tabletserver/vstreamer/planbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/mysql/collations"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/test/utils"
"vitess.io/vitess/go/vt/proto/topodata"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vtenv"
Expand Down Expand Up @@ -451,6 +450,54 @@ func TestPlanBuilder(t *testing.T) {
VindexColumns: nil,
KeyRange: nil,
}},
whereExprsToPushDown: []sqlparser.Expr{
sqlparser.NewComparisonExpr(sqlparser.EqualOp, sqlparser.Expr(sqlparser.NewColName("id")), sqlparser.Expr(sqlparser.NewIntLiteral("1")), nil),
},
env: vtenv.NewTestEnv(),
},
}, {
inTable: t1,
inRule: &binlogdatapb.Rule{Match: "t1", Filter: "select val, id from t1 where id > 1 and id < 10"},
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),
},
}},
Filters: []Filter{
{
Opcode: GreaterThan,
ColNum: 0,
Value: sqltypes.NewInt64(1),
Vindex: nil,
VindexColumns: nil,
KeyRange: nil,
},
{
Opcode: LessThan,
ColNum: 0,
Value: sqltypes.NewInt64(10),
Vindex: nil,
VindexColumns: nil,
KeyRange: nil,
},
},
whereExprsToPushDown: []sqlparser.Expr{
sqlparser.NewComparisonExpr(sqlparser.GreaterThanOp, sqlparser.Expr(sqlparser.NewColName("id")), sqlparser.Expr(sqlparser.NewIntLiteral("1")), nil),
sqlparser.NewComparisonExpr(sqlparser.LessThanOp, sqlparser.Expr(sqlparser.NewColName("id")), sqlparser.Expr(sqlparser.NewIntLiteral("10")), nil),
},
env: vtenv.NewTestEnv(),
},
}, {
Expand Down Expand Up @@ -661,7 +708,7 @@ func TestPlanBuilder(t *testing.T) {
plan.Filters[ind].Vindex = nil
plan.Filters[ind].Vindex = nil
}
utils.MustMatch(t, tcase.outPlan, plan)
require.EqualValues(t, tcase.outPlan, plan)
})
}
}
Expand Down
1 change: 0 additions & 1 deletion go/vt/vttablet/tabletserver/vstreamer/rowstreamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ func (rs *rowStreamer) buildSelect(st *binlogdatapb.MinimalTable) (string, error
}

addPushdownExpressions := func() {
// First we add any predicates that should be pushed down.
for i, expr := range rs.plan.whereExprsToPushDown {
if i != 0 {
buf.Myprintf(" and ")
Expand Down

0 comments on commit 1b597ec

Please sign in to comment.