Skip to content

Commit

Permalink
feat: add support for WITH ROLLUP (#15930)
Browse files Browse the repository at this point in the history
  • Loading branch information
systay committed May 14, 2024
1 parent eb22cfb commit 473c49a
Show file tree
Hide file tree
Showing 37 changed files with 8,410 additions and 8,200 deletions.
21 changes: 13 additions & 8 deletions go/test/endtoend/vtgate/queries/aggregation/aggregation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,19 @@ func TestGroupBy(t *testing.T) {
mcmp, closer := start(t)
defer closer()
mcmp.Exec("insert into t3(id5, id6, id7) values(1,1,2), (2,2,4), (3,2,4), (4,1,2), (5,1,2), (6,3,6)")
// test ordering and group by int column
mcmp.AssertMatches("select id6, id7, count(*) k from t3 group by id6, id7 order by k", `[[INT64(3) INT64(6) INT64(1)] [INT64(2) INT64(4) INT64(2)] [INT64(1) INT64(2) INT64(3)]]`)
mcmp.AssertMatches("select id6+id7, count(*) k from t3 group by id6+id7 order by k", `[[INT64(9) INT64(1)] [INT64(6) INT64(2)] [INT64(3) INT64(3)]]`)

// Test the same queries in streaming mode
utils.Exec(t, mcmp.VtConn, "set workload = olap")
mcmp.AssertMatches("select id6, id7, count(*) k from t3 group by id6, id7 order by k", `[[INT64(3) INT64(6) INT64(1)] [INT64(2) INT64(4) INT64(2)] [INT64(1) INT64(2) INT64(3)]]`)
mcmp.AssertMatches("select id6+id7, count(*) k from t3 group by id6+id7 order by k", `[[INT64(9) INT64(1)] [INT64(6) INT64(2)] [INT64(3) INT64(3)]]`)

// run queries in both workloads
workloads := []string{"oltp", "olap"}
for _, workload := range workloads {
utils.Exec(t, mcmp.VtConn, fmt.Sprintf("set workload = %s", workload))
// test ordering and group by int column
mcmp.AssertMatches("select id6, id7, count(*) k from t3 group by id6, id7 order by k", `[[INT64(3) INT64(6) INT64(1)] [INT64(2) INT64(4) INT64(2)] [INT64(1) INT64(2) INT64(3)]]`)
mcmp.AssertMatches("select id6+id7, count(*) k from t3 group by id6+id7 order by k", `[[INT64(9) INT64(1)] [INT64(6) INT64(2)] [INT64(3) INT64(3)]]`)
if utils.BinaryIsAtLeastAtVersion(20, "vtgate") &&
utils.BinaryIsAtLeastAtVersion(20, "vttablet") {
mcmp.Exec("select id6, id7, count(*) k from t3 group by id6, id7 with rollup")
}
}
}

func TestEqualFilterOnScatter(t *testing.T) {
Expand Down
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.

13 changes: 10 additions & 3 deletions go/vt/sqlparser/ast_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ func (node *Select) Format(buf *TrackedBuffer) {

buf.astPrintf(node, "%v%v%v",
node.Where,
node.GroupBy, node.Having)
node.GroupBy,
node.Having)

if node.Windows != nil {
buf.astPrintf(node, " %v", node.Windows)
Expand Down Expand Up @@ -1922,12 +1923,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 || len(node.Exprs) == 0 {
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.

0 comments on commit 473c49a

Please sign in to comment.