Skip to content

Commit

Permalink
add predicates no matter the opcode
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay committed Jan 25, 2021
1 parent dfc53d5 commit b062f53
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
32 changes: 20 additions & 12 deletions go/vt/vtgate/planbuilder/route_planning.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,23 @@ type vindexPlusPredicates struct {
// addPredicate clones this routePlan and returns a new one with these predicates added to it. if the predicates can help,
// they will improve the routeOpCode
func (rp *routePlan) addPredicate(predicates ...sqlparser.Expr) error {
newVindexFound, err := rp.searchForNewVindexes(predicates)
if err != nil {
return err
}

// if we didn't open up any new vindex options, no need to enter here
if newVindexFound {
rp.pickBestAvailableVindex()
}

// any predicates that cover more than a single table need to be added here
rp.predicates = append(rp.predicates, predicates...)

return nil
}

func (rp *routePlan) searchForNewVindexes(predicates []sqlparser.Expr) (bool, error) {
newVindexFound := false
for _, filter := range predicates {
switch node := filter.(type) {
Expand All @@ -248,7 +265,7 @@ func (rp *routePlan) addPredicate(predicates ...sqlparser.Expr) error {
// since we know that nothing returns true when compared to NULL,
// so we can safely bail out here
rp.routeOpCode = engine.SelectNone
return nil
return false, nil
}
// TODO(Manan,Andres): Remove the predicates that are repeated eg. Id=1 AND Id=1
for _, v := range rp.vindexPreds {
Expand All @@ -269,7 +286,7 @@ func (rp *routePlan) addPredicate(predicates ...sqlparser.Expr) error {
continue
}
// something else went wrong, return the error
return err
return false, err
}
if ok {
for _, col := range v.vindex.Columns {
Expand All @@ -286,16 +303,7 @@ func (rp *routePlan) addPredicate(predicates ...sqlparser.Expr) error {
}
}
}

// if we didn't open up any new vindex options, no need to enter here
if newVindexFound {
rp.pickBestAvailableVindex()
}

// any predicates that cover more than a single table need to be added here
rp.predicates = append(rp.predicates, predicates...)

return nil
return newVindexFound, nil
}

// pickBestAvailableVindex goes over the available vindexes for this route and picks the best one available.
Expand Down
6 changes: 3 additions & 3 deletions go/vt/vtgate/planbuilder/testdata/filter_cases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Gen4 plan same as above
"Sharded": true
},
"FieldQuery": "select id from user where 1 != 1",
"Query": "select id from user",
"Query": "select id from user where someColumn = null",
"Table": "user"
}
}
Expand Down Expand Up @@ -1664,7 +1664,7 @@ Gen4 plan same as above
"Sharded": true
},
"FieldQuery": "select id from music where 1 != 1",
"Query": "select id from music",
"Query": "select id from music where id = null",
"Table": "music"
}
}
Expand Down Expand Up @@ -1738,7 +1738,7 @@ Gen4 plan same as above
"Sharded": true
},
"FieldQuery": "select id from music where 1 != 1",
"Query": "select id from music",
"Query": "select id from music where user_id = 4 and id = null",
"Table": "music"
}
}
Expand Down

0 comments on commit b062f53

Please sign in to comment.