-
Notifications
You must be signed in to change notification settings - Fork 469
Expand file tree
/
Copy pathnumeric_nested_test.go
More file actions
98 lines (94 loc) · 2.74 KB
/
Copy pathnumeric_nested_test.go
File metadata and controls
98 lines (94 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) 2026 The Bluge Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package riot
import (
"context"
"testing"
"github.com/vcaesar/riot/numeric"
"github.com/vcaesar/riot/search"
"github.com/vcaesar/riot/search/aggregations"
)
func TestNumericColumnNestedAggregations(t *testing.T) {
w, err := OpenWriter(InMemoryOnlyConfig())
if err != nil {
t.Fatal(err)
}
defer func() {
if err := w.Close(); err != nil {
t.Error(err)
}
}()
doc := NewDocument("nested").
AddField(NewNumericField("outer", 1)).
AddField(NewNumericField("outer", 2)).
AddField(NewNumericField("inner", 100)).
AddField(NewNumericField("inner", 200))
if err := w.Insert(doc); err != nil {
t.Fatal(err)
}
r, err := w.Reader()
if err != nil {
t.Fatal(err)
}
defer func() {
if err := r.Close(); err != nil {
t.Error(err)
}
}()
terms := aggregations.NewTermsAggregation(search.Field("outer"), 10)
terms.AddAggregation("inner", aggregations.NewTermsAggregation(search.Field("inner"), 10))
ranges := aggregations.Ranges(search.Field("outer")).
AddRange(aggregations.NamedRange("one", 1, 2)).
AddRange(aggregations.NamedRange("two", 2, 3))
ranges.AddAggregation("sum", aggregations.Sum(search.Field("inner")))
req := NewTopNSearch(0, NewMatchAllQuery())
req.AddAggregation("terms", terms)
req.AddAggregation("ranges", ranges)
dmi, err := r.Search(context.Background(), req)
if err != nil {
t.Fatal(err)
}
for {
match, err := dmi.Next()
if err != nil {
t.Fatal(err)
}
if match == nil {
break
}
}
buckets := dmi.Aggregations().Buckets("terms")
if len(buckets) != 2 {
t.Fatalf("got %d outer terms", len(buckets))
}
seen := map[float64]bool{}
for _, bucket := range buckets {
n, err := numeric.PrefixCoded(bucket.Name()).Int64()
if err != nil {
t.Fatal(err)
}
seen[numeric.Int64ToFloat64(n)] = true
if bucket.Count() != 1 || len(bucket.Buckets("inner")) != 2 {
t.Fatalf("invalid nested bucket: %v", bucket)
}
}
if !seen[1] || !seen[2] {
t.Fatalf("outer keys overwritten: %v", seen)
}
for _, bucket := range dmi.Aggregations().Buckets("ranges") {
if bucket.Count() != 1 || bucket.Metric("sum") != 300 {
t.Fatalf("range %s: count %d, sum %v", bucket.Name(), bucket.Count(), bucket.Metric("sum"))
}
}
}