Skip to content

Commit

Permalink
feat: add support for WITH ROLLUP
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay committed May 13, 2024
1 parent eb22cfb commit 7089aed
Show file tree
Hide file tree
Showing 35 changed files with 7,247 additions and 7,125 deletions.
8 changes: 5 additions & 3 deletions go/test/endtoend/vtgate/queries/random/query_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ func (sg *selectGenerator) createGroupBy(tables []tableT) (grouping []column) {
if col.typ == "date" && !testFailingQueries {
continue
}
sg.sel.GroupBy = append(sg.sel.GroupBy, col.getASTExpr())
sg.sel.AddGroupBy(col.getASTExpr())

// add to select
if rand.IntN(2) < 1 {
Expand Down Expand Up @@ -478,8 +478,10 @@ func (sg *selectGenerator) createAggregations(tables []tableT) (aggregates []col
// orders on all grouping expressions and on random SelectExprs
func (sg *selectGenerator) createOrderBy() {
// always order on grouping expressions
for _, expr := range sg.sel.GroupBy {
sg.sel.OrderBy = append(sg.sel.OrderBy, sqlparser.NewOrder(expr, getRandomOrderDirection()))
if sg.sel.GroupBy != nil {
for _, expr := range sg.sel.GroupBy.Exprs {
sg.sel.OrderBy = append(sg.sel.OrderBy, sqlparser.NewOrder(expr, getRandomOrderDirection()))
}
}

// randomly order on SelectExprs
Expand Down
7 changes: 5 additions & 2 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ type (
Comments *ParsedComments
SelectExprs SelectExprs
Where *Where
GroupBy GroupBy
GroupBy *GroupBy
Having *Where
Windows NamedWindows
OrderBy OrderBy
Expand Down Expand Up @@ -3451,7 +3451,10 @@ type ConvertType struct {
}

// GroupBy represents a GROUP BY clause.
type GroupBy []Expr
type GroupBy struct {
Exprs []Expr
WithRollup bool
}

// OrderBy represents an ORDER By clause.
type OrderBy []*Order
Expand Down
42 changes: 20 additions & 22 deletions go/vt/sqlparser/ast_clone.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 20 additions & 13 deletions go/vt/sqlparser/ast_copy_on_rewrite.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 25 additions & 26 deletions go/vt/sqlparser/ast_equals.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions go/vt/sqlparser/ast_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -1922,12 +1922,18 @@ func (node *When) Format(buf *TrackedBuffer) {
}

// Format formats the node.
func (node GroupBy) Format(buf *TrackedBuffer) {
func (node *GroupBy) Format(buf *TrackedBuffer) {
if node == nil {
return
}
prefix := " group by "
for _, n := range node {
for _, n := range node.Exprs {
buf.astPrintf(node, "%s%v", prefix, n)
prefix = ", "
}
if node.WithRollup {
buf.literal(" with rollup")
}
}

// Format formats the node.
Expand Down
10 changes: 8 additions & 2 deletions go/vt/sqlparser/ast_format_fast.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 17 additions & 4 deletions go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -788,10 +788,19 @@ func NewLimitWithoutOffset(rowCount int) *Limit {
}

// NewSelect is used to create a select statement
func NewSelect(comments Comments, exprs SelectExprs, selectOptions []string, into *SelectInto, from TableExprs, where *Where, groupBy GroupBy, having *Where, windows NamedWindows) *Select {
func NewSelect(
comments Comments,
exprs SelectExprs,
selectOptions []string,
into *SelectInto,
from TableExprs,
where *Where,
groupBy *GroupBy,
having *Where,
windows NamedWindows,
) *Select {
var cache *bool
var distinct, straightJoinHint, sqlFoundRows bool

for _, option := range selectOptions {
switch strings.ToLower(option) {
case DistinctStr:
Expand Down Expand Up @@ -1168,13 +1177,17 @@ func (node *Select) AddHaving(expr Expr) {

// AddGroupBy adds a grouping expression, unless it's already present
func (node *Select) AddGroupBy(expr Expr) {
for _, gb := range node.GroupBy {
if node.GroupBy == nil {
node.GroupBy = &GroupBy{Exprs: []Expr{expr}}
return
}
for _, gb := range node.GroupBy.Exprs {
if Equals.Expr(gb, expr) {
// group by columns are sets - duplicates don't add anything, so we can just skip these
return
}
}
node.GroupBy = append(node.GroupBy, expr)
node.GroupBy.Exprs = append(node.GroupBy.Exprs, expr)
}

// AddWhere adds the boolean expression to the
Expand Down
Loading

0 comments on commit 7089aed

Please sign in to comment.