From d1a9c9f854a51fdd5154498f72a58ec0a476b787 Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Tue, 14 May 2024 14:39:03 +0200 Subject: [PATCH] refactor: introduce helper method to extract logic Signed-off-by: Andres Taylor --- go/vt/sqlparser/ast_funcs.go | 8 ++++++++ go/vt/vtgate/semantics/binder.go | 22 ++++++++++------------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/go/vt/sqlparser/ast_funcs.go b/go/vt/sqlparser/ast_funcs.go index 42edc5bd987..df201676fae 100644 --- a/go/vt/sqlparser/ast_funcs.go +++ b/go/vt/sqlparser/ast_funcs.go @@ -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) { diff --git a/go/vt/vtgate/semantics/binder.go b/go/vt/vtgate/semantics/binder.go index b87e63a325c..8b4d6d2163d 100644 --- a/go/vt/vtgate/semantics/binder.go +++ b/go/vt/vtgate/semantics/binder.go @@ -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{}