Skip to content

Commit

Permalink
support 'GROUP BY' column number
Browse files Browse the repository at this point in the history
  • Loading branch information
dt committed Jan 4, 2016
1 parent 1dd9fe4 commit 5e6fd66
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
17 changes: 16 additions & 1 deletion sql/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ func (p *planner) groupBy(n *parser.Select, s *scanNode) (*groupNode, error) {
// that determination is made during validation, which will require matching
// expressions.
for i := range n.GroupBy {
// If a col offset is specified, replace it with that expression first.
if col, err := s.colOffset(n.GroupBy[i], false); err != nil {
return nil, err
} else if col >= 0 {
n.GroupBy[i] = s.render[col]
}

norm, err := s.resolveQNames(n.GroupBy[i])
if err != nil {
return nil, err
Expand Down Expand Up @@ -192,7 +199,6 @@ func (n *groupNode) Next() bool {
}

func (n *groupNode) computeAggregates() {
n.intialized = true
var scratch []byte

// Loop over the rows passing the values into the corresponding aggregation
Expand Down Expand Up @@ -229,6 +235,8 @@ func (n *groupNode) computeAggregates() {
n.buckets[""] = struct{}{}
}

n.intialized = true

// Render the results.
n.values.rows = make([]parser.DTuple, 0, len(n.buckets))
for k := range n.buckets {
Expand Down Expand Up @@ -562,6 +570,13 @@ func (a *aggregateFunc) TypeCheck(args parser.MapArgs) (parser.Datum, error) {
}

func (a *aggregateFunc) Eval(ctx parser.EvalContext) (parser.Datum, error) {
// During init of the group (ie bucket filling), grouped expressions (ie wrapped
// qvalues) are Eval()'ed to determine the bucket for a row, so pass these
// calls through to the underlying `arg` expr eval until the group is init'ed.
if !a.group.intialized {
return a.arg.Eval(ctx)
}

found, ok := a.buckets[a.group.currentBucket]
if !ok {
found = a.create()
Expand Down
17 changes: 17 additions & 0 deletions sql/testdata/aggregate
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ SELECT COUNT(*), k FROM kv GROUP BY k
1 7
1 8

query II rowsort
SELECT COUNT(*), k FROM kv GROUP BY 2
----
1 1
1 3
1 5
1 6
1 7
1 8

query I rowsort
SELECT 1 GROUP BY 'a';
----
Expand Down Expand Up @@ -56,6 +66,13 @@ SELECT COUNT(*), UPPER(s) FROM kv GROUP BY UPPER(s)
2 B
3 A

query IT rowsort
SELECT COUNT(*), UPPER(s) FROM kv GROUP BY 2
----
1 NULL
2 B
3 A

query IT rowsort
SELECT COUNT(*), UPPER(s) FROM kv GROUP BY s
----
Expand Down

0 comments on commit 5e6fd66

Please sign in to comment.