Skip to content

Commit

Permalink
Merge pull request #384 from supertenant/group_by_with_totals
Browse files Browse the repository at this point in the history
fix(query): add with totals case (issue #382)
  • Loading branch information
ernado authored Feb 22, 2024
2 parents 1e16ba3 + 667f281 commit db4dad6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion query.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ func (c *Client) Do(ctx context.Context, q Query) (err error) {
return errors.Wrap(err, "packet")
}
switch code {
case proto.ServerCodeData:
case proto.ServerCodeData, proto.ServerCodeTotals:
if err := c.decodeBlock(ctx, decodeOptions{
Handler: onResult,
Result: q.Result,
Expand Down
31 changes: 31 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,37 @@ func requireEqual[T any](t *testing.T, a, b proto.ColumnOf[T]) {
}
}

func TestWithTotals(t *testing.T) {
t.Parallel()
ctx := context.Background()
conn := Conn(t)
var n proto.ColUInt64
var c proto.ColUInt64

var data []uint64
query := Query{
Body: `
SELECT
number AS n,
COUNT() AS c
FROM (
SELECT number FROM system.numbers LIMIT 100
) GROUP BY n WITH TOTALS
`,
Result: proto.Results{
{Name: "n", Data: &n},
{Name: "c", Data: &c},
},
OnResult: func(ctx context.Context, b proto.Block) error {
data = append(data, c...)
return nil
},
}
require.NoError(t, conn.Do(ctx, query))
require.Equal(t, 101, len(data))
require.Equal(t, uint64(100), data[100])
}

func TestDateTimeOverflow(t *testing.T) {
t.Parallel()
ctx := context.Background()
Expand Down

0 comments on commit db4dad6

Please sign in to comment.