Skip to content

Commit

Permalink
refactor: introduce helper method to extract logic
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay committed May 14, 2024
1 parent 42e97a0 commit d1a9c9f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
8 changes: 8 additions & 0 deletions go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,14 @@ func (node *Select) AddGroupBy(expr Expr) {
node.GroupBy.Exprs = append(node.GroupBy.Exprs, expr)
}

// GroupByExprs returns the group by expressions
func (node *Select) GroupByExprs() []Expr {
if node.GroupBy == nil {
return nil
}
return node.GroupBy.Exprs
}

// AddWhere adds the boolean expression to the
// WHERE clause as an AND condition.
func (node *Update) AddWhere(expr Expr) {
Expand Down
22 changes: 10 additions & 12 deletions go/vt/vtgate/semantics/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,19 +382,17 @@ func (b *binder) searchInSelectExpressions(colName *sqlparser.ColName, deps depe
return dependency{certain: true, direct: direct, recursive: recursive, typ: typ}
}
}
if stmt.GroupBy != nil {
for _, gb := range stmt.GroupBy.Exprs {
selectCol, ok := gb.(*sqlparser.ColName)
if !ok || !selectCol.Name.Equal(colName.Name) {
continue
}
for _, gb := range stmt.GroupByExprs() {
selectCol, ok := gb.(*sqlparser.ColName)
if !ok || !selectCol.Name.Equal(colName.Name) {
continue
}

_, direct, _ := b.org.depsForExpr(selectCol)
if deps.direct == direct {
// we have found the ColName in the GROUP BY expressions, so it's safe to use here
direct, recursive, typ := b.org.depsForExpr(gb)
return dependency{certain: true, direct: direct, recursive: recursive, typ: typ}
}
_, direct, _ := b.org.depsForExpr(selectCol)
if deps.direct == direct {
// we have found the ColName in the GROUP BY expressions, so it's safe to use here
direct, recursive, typ := b.org.depsForExpr(gb)
return dependency{certain: true, direct: direct, recursive: recursive, typ: typ}
}
}
return dependency{}
Expand Down

0 comments on commit d1a9c9f

Please sign in to comment.