Skip to content

Commit

Permalink
Merge pull request #7313 from planetscale/v4-subquery
Browse files Browse the repository at this point in the history
Gen4: Handling subquery in query graph
  • Loading branch information
systay authored Jan 18, 2021
2 parents 915f80a + 5dbda83 commit d09dffe
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 125 deletions.
47 changes: 46 additions & 1 deletion go/vt/vtgate/planbuilder/querygraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ type (

// noDeps contains the predicates that can be evaluated anywhere.
noDeps sqlparser.Expr

// subqueries contains the subqueries that depend on this query graph
subqueries map[*sqlparser.Subquery][]*queryGraph
}

// queryTable is a single FROM table, including all predicates particular to this table
Expand Down Expand Up @@ -79,9 +82,38 @@ func createQGFromSelect(sel *sqlparser.Select, semTable *semantics.SemTable) (*q
return qg, nil
}

func createQGFromSelectStatement(selStmt sqlparser.SelectStatement, semTable *semantics.SemTable) ([]*queryGraph, error) {
switch stmt := selStmt.(type) {
case *sqlparser.Select:
qg, err := createQGFromSelect(stmt, semTable)
if err != nil {
return nil, err
}
return []*queryGraph{qg}, err
case *sqlparser.Union:
qg, err := createQGFromSelectStatement(stmt.FirstStatement, semTable)
if err != nil {
return nil, err
}
for _, sel := range stmt.UnionSelects {
qgr, err := createQGFromSelectStatement(sel.Statement, semTable)
if err != nil {
return nil, err
}
qg = append(qg, qgr...)
}
return qg, nil
case *sqlparser.ParenSelect:
return createQGFromSelectStatement(stmt.Select, semTable)
}

return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "BUG: not reachable %T", selStmt)
}

func newQueryGraph() *queryGraph {
return &queryGraph{
crossTable: map[semantics.TableSet][]sqlparser.Expr{},
subqueries: map[*sqlparser.Subquery][]*queryGraph{},
}
}

Expand Down Expand Up @@ -156,7 +188,20 @@ func (qg *queryGraph) collectPredicate(predicate sqlparser.Expr, semTable *seman
}
qg.crossTable[deps] = allPredicates
}
return nil
err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
switch subQuery := node.(type) {
case *sqlparser.Subquery:

qgr, err := createQGFromSelectStatement(subQuery.Select, semTable)
if err != nil {
return false, err
}
qg.subqueries[subQuery] = qgr
}
return true, nil
}, predicate)

return err
}

func (qg *queryGraph) addToSingleTable(table semantics.TableSet, predicate sqlparser.Expr) bool {
Expand Down
Loading

0 comments on commit d09dffe

Please sign in to comment.