2018-11-20 18:38:36 +00:00
|
|
|
package reads_test
|
2018-10-05 22:02:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/influxdata/platform/models"
|
2018-12-10 20:53:16 +00:00
|
|
|
"github.com/influxdata/platform/pkg/data/gen"
|
2018-11-20 18:38:36 +00:00
|
|
|
"github.com/influxdata/platform/storage/reads"
|
2018-10-05 22:02:31 +00:00
|
|
|
"github.com/influxdata/platform/storage/reads/datatypes"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGroupGroupResultSetSorting(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2018-11-20 18:38:36 +00:00
|
|
|
cur reads.SeriesCursor
|
2018-10-05 22:02:31 +00:00
|
|
|
group datatypes.ReadRequest_Group
|
|
|
|
keys []string
|
2018-11-20 18:38:36 +00:00
|
|
|
exp string
|
2018-10-05 22:02:31 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "group by tag1 in all series",
|
|
|
|
cur: &sliceSeriesCursor{
|
|
|
|
rows: newSeriesRows(
|
2018-11-20 18:38:36 +00:00
|
|
|
"cpu,tag0=val00,tag1=val10",
|
|
|
|
"cpu,tag0=val00,tag1=val11",
|
|
|
|
"cpu,tag0=val00,tag1=val12",
|
|
|
|
"cpu,tag0=val01,tag1=val10",
|
|
|
|
"cpu,tag0=val01,tag1=val11",
|
|
|
|
"cpu,tag0=val01,tag1=val12",
|
2018-10-05 22:02:31 +00:00
|
|
|
)},
|
|
|
|
group: datatypes.GroupBy,
|
|
|
|
keys: []string{"tag1"},
|
2018-11-20 18:38:36 +00:00
|
|
|
exp: `group:
|
|
|
|
tag key : _m,tag0,tag1
|
|
|
|
partition key: val10
|
|
|
|
series: _m=cpu,tag0=val00,tag1=val10
|
|
|
|
series: _m=cpu,tag0=val01,tag1=val10
|
|
|
|
group:
|
|
|
|
tag key : _m,tag0,tag1
|
|
|
|
partition key: val11
|
|
|
|
series: _m=cpu,tag0=val00,tag1=val11
|
|
|
|
series: _m=cpu,tag0=val01,tag1=val11
|
|
|
|
group:
|
|
|
|
tag key : _m,tag0,tag1
|
|
|
|
partition key: val12
|
|
|
|
series: _m=cpu,tag0=val00,tag1=val12
|
|
|
|
series: _m=cpu,tag0=val01,tag1=val12
|
|
|
|
`,
|
2018-10-05 22:02:31 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "group by tag1 in partial series",
|
|
|
|
cur: &sliceSeriesCursor{
|
|
|
|
rows: newSeriesRows(
|
2018-11-20 18:38:36 +00:00
|
|
|
"aaa,tag0=val00",
|
|
|
|
"aaa,tag0=val01",
|
|
|
|
"cpu,tag0=val00,tag1=val10",
|
|
|
|
"cpu,tag0=val00,tag1=val11",
|
|
|
|
"cpu,tag0=val00,tag1=val12",
|
|
|
|
"cpu,tag0=val01,tag1=val10",
|
|
|
|
"cpu,tag0=val01,tag1=val11",
|
|
|
|
"cpu,tag0=val01,tag1=val12",
|
2018-10-05 22:02:31 +00:00
|
|
|
)},
|
|
|
|
group: datatypes.GroupBy,
|
|
|
|
keys: []string{"tag1"},
|
2018-11-20 18:38:36 +00:00
|
|
|
exp: `group:
|
|
|
|
tag key : _m,tag0,tag1
|
|
|
|
partition key: val10
|
|
|
|
series: _m=cpu,tag0=val00,tag1=val10
|
|
|
|
series: _m=cpu,tag0=val01,tag1=val10
|
|
|
|
group:
|
|
|
|
tag key : _m,tag0,tag1
|
|
|
|
partition key: val11
|
|
|
|
series: _m=cpu,tag0=val01,tag1=val11
|
|
|
|
series: _m=cpu,tag0=val00,tag1=val11
|
|
|
|
group:
|
|
|
|
tag key : _m,tag0,tag1
|
|
|
|
partition key: val12
|
|
|
|
series: _m=cpu,tag0=val01,tag1=val12
|
|
|
|
series: _m=cpu,tag0=val00,tag1=val12
|
|
|
|
group:
|
|
|
|
tag key : _m,tag0
|
|
|
|
partition key: <nil>
|
|
|
|
series: _m=aaa,tag0=val00
|
|
|
|
series: _m=aaa,tag0=val01
|
|
|
|
`,
|
2018-10-05 22:02:31 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "group by tag2,tag1 with partial series",
|
|
|
|
cur: &sliceSeriesCursor{
|
|
|
|
rows: newSeriesRows(
|
2018-11-20 18:38:36 +00:00
|
|
|
"aaa,tag0=val00",
|
|
|
|
"aaa,tag0=val01",
|
|
|
|
"cpu,tag0=val00,tag1=val10",
|
|
|
|
"cpu,tag0=val00,tag1=val11",
|
|
|
|
"cpu,tag0=val00,tag1=val12",
|
|
|
|
"mem,tag1=val10,tag2=val20",
|
|
|
|
"mem,tag1=val11,tag2=val20",
|
|
|
|
"mem,tag1=val11,tag2=val21",
|
2018-10-05 22:02:31 +00:00
|
|
|
)},
|
|
|
|
group: datatypes.GroupBy,
|
2018-11-20 18:38:36 +00:00
|
|
|
keys: []string{"tag2", "tag1"},
|
|
|
|
exp: `group:
|
|
|
|
tag key : _m,tag1,tag2
|
|
|
|
partition key: val20,val10
|
|
|
|
series: _m=mem,tag1=val10,tag2=val20
|
|
|
|
group:
|
|
|
|
tag key : _m,tag1,tag2
|
|
|
|
partition key: val20,val11
|
|
|
|
series: _m=mem,tag1=val11,tag2=val20
|
|
|
|
group:
|
|
|
|
tag key : _m,tag1,tag2
|
|
|
|
partition key: val21,val11
|
|
|
|
series: _m=mem,tag1=val11,tag2=val21
|
|
|
|
group:
|
|
|
|
tag key : _m,tag0,tag1
|
|
|
|
partition key: <nil>,val10
|
|
|
|
series: _m=cpu,tag0=val00,tag1=val10
|
|
|
|
group:
|
|
|
|
tag key : _m,tag0,tag1
|
|
|
|
partition key: <nil>,val11
|
|
|
|
series: _m=cpu,tag0=val00,tag1=val11
|
|
|
|
group:
|
|
|
|
tag key : _m,tag0,tag1
|
|
|
|
partition key: <nil>,val12
|
|
|
|
series: _m=cpu,tag0=val00,tag1=val12
|
|
|
|
group:
|
|
|
|
tag key : _m,tag0
|
|
|
|
partition key: <nil>,<nil>
|
|
|
|
series: _m=aaa,tag0=val00
|
|
|
|
series: _m=aaa,tag0=val01
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "group by tag0,tag2 with partial series",
|
|
|
|
cur: &sliceSeriesCursor{
|
|
|
|
rows: newSeriesRows(
|
|
|
|
"aaa,tag0=val00",
|
|
|
|
"aaa,tag0=val01",
|
|
|
|
"cpu,tag0=val00,tag1=val10",
|
|
|
|
"cpu,tag0=val00,tag1=val11",
|
|
|
|
"cpu,tag0=val00,tag1=val12",
|
|
|
|
"mem,tag1=val10,tag2=val20",
|
|
|
|
"mem,tag1=val11,tag2=val20",
|
|
|
|
"mem,tag1=val11,tag2=val21",
|
|
|
|
)},
|
|
|
|
group: datatypes.GroupBy,
|
|
|
|
keys: []string{"tag0", "tag2"},
|
|
|
|
exp: `group:
|
|
|
|
tag key : _m,tag0,tag1
|
|
|
|
partition key: val00,<nil>
|
|
|
|
series: _m=aaa,tag0=val00
|
|
|
|
series: _m=cpu,tag0=val00,tag1=val10
|
|
|
|
series: _m=cpu,tag0=val00,tag1=val11
|
|
|
|
series: _m=cpu,tag0=val00,tag1=val12
|
|
|
|
group:
|
|
|
|
tag key : _m,tag0
|
|
|
|
partition key: val01,<nil>
|
|
|
|
series: _m=aaa,tag0=val01
|
|
|
|
group:
|
|
|
|
tag key : _m,tag1,tag2
|
|
|
|
partition key: <nil>,val20
|
|
|
|
series: _m=mem,tag1=val10,tag2=val20
|
|
|
|
series: _m=mem,tag1=val11,tag2=val20
|
|
|
|
group:
|
|
|
|
tag key : _m,tag1,tag2
|
|
|
|
partition key: <nil>,val21
|
|
|
|
series: _m=mem,tag1=val11,tag2=val21
|
|
|
|
`,
|
2018-10-05 22:02:31 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
2018-11-20 18:38:36 +00:00
|
|
|
newCursor := func() (reads.SeriesCursor, error) {
|
2018-10-05 22:02:31 +00:00
|
|
|
return tt.cur, nil
|
|
|
|
}
|
|
|
|
|
2018-11-20 18:38:36 +00:00
|
|
|
var hints datatypes.HintFlags
|
|
|
|
hints.SetHintSchemaAllTime()
|
|
|
|
rs := reads.NewGroupResultSet(context.Background(), &datatypes.ReadRequest{Group: tt.group, GroupKeys: tt.keys, Hints: hints}, newCursor)
|
2018-10-05 22:02:31 +00:00
|
|
|
|
2018-11-20 18:38:36 +00:00
|
|
|
sb := new(strings.Builder)
|
|
|
|
GroupResultSetToString(sb, rs, SkipNilCursor())
|
2018-10-05 22:02:31 +00:00
|
|
|
|
2018-11-20 18:38:36 +00:00
|
|
|
if got := sb.String(); !cmp.Equal(got, tt.exp) {
|
|
|
|
t.Errorf("unexpected value; -got/+exp\n%s", cmp.Diff(strings.Split(got, "\n"), strings.Split(tt.exp, "\n")))
|
2018-10-05 22:02:31 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-20 18:38:36 +00:00
|
|
|
func TestNewGroupResultSet_Sorting(t *testing.T) {
|
2018-10-05 22:02:31 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2018-11-20 18:38:36 +00:00
|
|
|
keys []string
|
|
|
|
opts []reads.GroupOption
|
2018-10-05 22:02:31 +00:00
|
|
|
exp string
|
|
|
|
}{
|
|
|
|
{
|
2018-11-20 18:38:36 +00:00
|
|
|
name: "nil hi",
|
|
|
|
keys: []string{"tag0", "tag2"},
|
|
|
|
exp: `group:
|
|
|
|
tag key : _m,tag0,tag1
|
|
|
|
partition key: val00,<nil>
|
|
|
|
series: _m=aaa,tag0=val00
|
|
|
|
series: _m=cpu,tag0=val00,tag1=val10
|
|
|
|
series: _m=cpu,tag0=val00,tag1=val11
|
|
|
|
series: _m=cpu,tag0=val00,tag1=val12
|
|
|
|
group:
|
|
|
|
tag key : _m,tag0
|
|
|
|
partition key: val01,<nil>
|
|
|
|
series: _m=aaa,tag0=val01
|
|
|
|
group:
|
|
|
|
tag key : _m,tag1,tag2
|
|
|
|
partition key: <nil>,val20
|
|
|
|
series: _m=mem,tag1=val10,tag2=val20
|
|
|
|
series: _m=mem,tag1=val11,tag2=val20
|
|
|
|
group:
|
|
|
|
tag key : _m,tag1,tag2
|
|
|
|
partition key: <nil>,val21
|
|
|
|
series: _m=mem,tag1=val11,tag2=val21
|
|
|
|
`,
|
2018-10-05 22:02:31 +00:00
|
|
|
},
|
|
|
|
{
|
2018-11-20 18:38:36 +00:00
|
|
|
name: "nil lo",
|
|
|
|
keys: []string{"tag0", "tag2"},
|
|
|
|
opts: []reads.GroupOption{reads.GroupOptionNilSortLo()},
|
|
|
|
exp: `group:
|
|
|
|
tag key : _m,tag1,tag2
|
|
|
|
partition key: <nil>,val20
|
|
|
|
series: _m=mem,tag1=val11,tag2=val20
|
|
|
|
series: _m=mem,tag1=val10,tag2=val20
|
|
|
|
group:
|
|
|
|
tag key : _m,tag1,tag2
|
|
|
|
partition key: <nil>,val21
|
|
|
|
series: _m=mem,tag1=val11,tag2=val21
|
|
|
|
group:
|
|
|
|
tag key : _m,tag0,tag1
|
|
|
|
partition key: val00,<nil>
|
|
|
|
series: _m=cpu,tag0=val00,tag1=val10
|
|
|
|
series: _m=cpu,tag0=val00,tag1=val11
|
|
|
|
series: _m=cpu,tag0=val00,tag1=val12
|
|
|
|
series: _m=aaa,tag0=val00
|
|
|
|
group:
|
|
|
|
tag key : _m,tag0
|
|
|
|
partition key: val01,<nil>
|
|
|
|
series: _m=aaa,tag0=val01
|
|
|
|
`,
|
2018-10-05 22:02:31 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2018-11-20 18:38:36 +00:00
|
|
|
newCursor := func() (reads.SeriesCursor, error) {
|
|
|
|
return &sliceSeriesCursor{
|
|
|
|
rows: newSeriesRows(
|
|
|
|
"aaa,tag0=val00",
|
|
|
|
"aaa,tag0=val01",
|
|
|
|
"cpu,tag0=val00,tag1=val10",
|
|
|
|
"cpu,tag0=val00,tag1=val11",
|
|
|
|
"cpu,tag0=val00,tag1=val12",
|
|
|
|
"mem,tag1=val10,tag2=val20",
|
|
|
|
"mem,tag1=val11,tag2=val20",
|
|
|
|
"mem,tag1=val11,tag2=val21",
|
|
|
|
)}, nil
|
2018-10-05 22:02:31 +00:00
|
|
|
}
|
|
|
|
|
2018-11-20 18:38:36 +00:00
|
|
|
var hints datatypes.HintFlags
|
|
|
|
hints.SetHintSchemaAllTime()
|
|
|
|
rs := reads.NewGroupResultSet(context.Background(), &datatypes.ReadRequest{Group: datatypes.GroupBy, GroupKeys: tt.keys, Hints: hints}, newCursor, tt.opts...)
|
2018-10-05 22:02:31 +00:00
|
|
|
|
2018-11-20 18:38:36 +00:00
|
|
|
sb := new(strings.Builder)
|
|
|
|
GroupResultSetToString(sb, rs, SkipNilCursor())
|
|
|
|
|
|
|
|
if got := sb.String(); !cmp.Equal(got, tt.exp) {
|
|
|
|
t.Errorf("unexpected value; -got/+exp\n%s", cmp.Diff(strings.Split(got, "\n"), strings.Split(tt.exp, "\n")))
|
2018-10-05 22:02:31 +00:00
|
|
|
}
|
2018-11-20 18:38:36 +00:00
|
|
|
})
|
2018-10-05 22:02:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type sliceSeriesCursor struct {
|
2018-11-20 18:38:36 +00:00
|
|
|
rows []reads.SeriesRow
|
2018-10-05 22:02:31 +00:00
|
|
|
i int
|
|
|
|
}
|
|
|
|
|
2018-11-20 18:38:36 +00:00
|
|
|
func newSeriesRows(keys ...string) []reads.SeriesRow {
|
|
|
|
rows := make([]reads.SeriesRow, len(keys))
|
2018-10-05 22:02:31 +00:00
|
|
|
for i := range keys {
|
|
|
|
rows[i].Name, rows[i].SeriesTags = models.ParseKeyBytes([]byte(keys[i]))
|
|
|
|
rows[i].Tags = rows[i].SeriesTags.Clone()
|
|
|
|
rows[i].Tags.Set([]byte("_m"), rows[i].Name)
|
|
|
|
}
|
|
|
|
return rows
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sliceSeriesCursor) Close() {}
|
|
|
|
func (s *sliceSeriesCursor) Err() error { return nil }
|
|
|
|
|
2018-11-20 18:38:36 +00:00
|
|
|
func (s *sliceSeriesCursor) Next() *reads.SeriesRow {
|
2018-10-05 22:02:31 +00:00
|
|
|
if s.i < len(s.rows) {
|
|
|
|
s.i++
|
|
|
|
return &s.rows[s.i-1]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-11-20 18:55:04 +00:00
|
|
|
|
|
|
|
func BenchmarkNewGroupResultSet_GroupBy(b *testing.B) {
|
|
|
|
card := []int{10, 10, 10}
|
2018-12-10 20:53:16 +00:00
|
|
|
vals := make([]gen.CountableSequence, len(card))
|
2018-11-20 18:55:04 +00:00
|
|
|
for i := range card {
|
|
|
|
vals[i] = gen.NewCounterByteSequenceCount(card[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
tags := gen.NewTagsValuesSequenceValues("tag", vals)
|
|
|
|
rows := make([]reads.SeriesRow, tags.Count())
|
|
|
|
for i := range rows {
|
|
|
|
tags.Next()
|
|
|
|
t := tags.Value().Clone()
|
|
|
|
rows[i].SeriesTags = t
|
|
|
|
rows[i].Tags = t
|
|
|
|
rows[i].Name = []byte("m0")
|
|
|
|
}
|
|
|
|
|
|
|
|
cur := &sliceSeriesCursor{rows: rows}
|
|
|
|
newCursor := func() (reads.SeriesCursor, error) {
|
|
|
|
cur.i = 0
|
|
|
|
return cur, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
var hints datatypes.HintFlags
|
|
|
|
hints.SetHintSchemaAllTime()
|
|
|
|
rs := reads.NewGroupResultSet(context.Background(), &datatypes.ReadRequest{Group: datatypes.GroupBy, GroupKeys: []string{"tag2"}, Hints: hints}, newCursor)
|
|
|
|
rs.Close()
|
|
|
|
}
|
|
|
|
}
|