You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ha...@apache.org on 2026/04/30 05:11:20 UTC
(skywalking-banyandb) 16/24: fix(topn): deduplicate entities in TopN aggregation query (#1085)
This is an automated email from the ASF dual-hosted git repository.
hanahmily pushed a commit to branch v0.10.x
in repository https://gitbox.apache.org/repos/asf/skywalking-banyandb.git
commit af4c71362478c143f6ce309f584059e1042e3d08
Author: eye-gu <73...@qq.com>
AuthorDate: Thu Apr 23 19:03:25 2026 +0800
fix(topn): deduplicate entities in TopN aggregation query (#1085)
---
banyand/query/processor_topn.go | 42 +++-
pkg/query/logical/measure/topn_analyzer.go | 18 +-
pkg/query/logical/measure/topn_plan_distinct.go | 268 +++++++++++++++++++++
.../logical/measure/topn_plan_distinct_test.go | 173 +++++++++++++
.../service_instance_metric_topn_test.json | 41 ++++
.../testdata/topn_aggregations/topn_agg_test.json | 17 ++
test/cases/init.go | 1 +
.../service_instance_metric_topn_test_data.json | 152 ++++++++++++
test/cases/topn/data/data.go | 91 +++++++
test/cases/topn/data/input/topn_count.ql | 22 ++
test/cases/topn/data/input/topn_count.yaml | 22 ++
test/cases/topn/data/input/topn_max.ql | 22 ++
test/cases/topn/data/input/topn_max.yaml | 22 ++
test/cases/topn/data/input/topn_mean.ql | 22 ++
test/cases/topn/data/input/topn_mean.yaml | 22 ++
test/cases/topn/data/input/topn_min.ql | 22 ++
test/cases/topn/data/input/topn_min.yaml | 22 ++
test/cases/topn/data/input/topn_sum.ql | 22 ++
test/cases/topn/data/input/topn_sum.yaml | 22 ++
test/cases/topn/data/want/topn_count.yaml | 43 ++++
test/cases/topn/data/want/topn_max.yaml | 43 ++++
test/cases/topn/data/want/topn_mean.yaml | 35 +++
test/cases/topn/data/want/topn_min.yaml | 43 ++++
test/cases/topn/data/want/topn_sum.yaml | 43 ++++
test/cases/topn/topn.go | 16 +-
25 files changed, 1226 insertions(+), 20 deletions(-)
diff --git a/banyand/query/processor_topn.go b/banyand/query/processor_topn.go
index 33556434c..25c360013 100644
--- a/banyand/query/processor_topn.go
+++ b/banyand/query/processor_topn.go
@@ -32,6 +32,7 @@ import (
"github.com/apache/skywalking-banyandb/banyand/measure"
"github.com/apache/skywalking-banyandb/pkg/bus"
"github.com/apache/skywalking-banyandb/pkg/logger"
+ pbv1 "github.com/apache/skywalking-banyandb/pkg/pb/v1"
"github.com/apache/skywalking-banyandb/pkg/query"
"github.com/apache/skywalking-banyandb/pkg/query/executor"
logical_measure "github.com/apache/skywalking-banyandb/pkg/query/logical/measure"
@@ -181,7 +182,46 @@ func (t *topNQueryProcessor) Rev(ctx context.Context, message bus.Message) (resp
}
}()
- resp = bus.NewMessage(bus.MessageID(now), toTopNResponse(result))
+ if request.GetAgg() != modelv1.AggregationFunction_AGGREGATION_FUNCTION_UNSPECIFIED {
+ // Standalone mode: use TopNPostProcessor for aggregation
+ if len(result) == 0 {
+ resp = bus.NewMessage(bus.MessageID(now), &measurev1.TopNResponse{})
+ return
+ }
+ aggregator := measure.CreateTopNPostProcessor(
+ request.GetTopN(),
+ request.GetAgg(),
+ request.GetFieldValueSort(),
+ )
+ var tags []string
+ for _, dp := range result {
+ dpTags := dp.GetTagFamilies()[0].GetTags()
+ if tags == nil {
+ tags = make([]string, len(dpTags))
+ for i, tag := range dpTags {
+ tags[i] = tag.Key
+ }
+ }
+ entityValues := make(pbv1.EntityValues, 0, len(dpTags))
+ for _, tag := range dpTags {
+ entityValues = append(entityValues, tag.Value)
+ }
+ aggregator.Put(entityValues,
+ dp.GetFields()[0].GetValue().GetInt().GetValue(),
+ uint64(dp.GetTimestamp().AsTime().UnixMilli()),
+ dp.GetVersion())
+ }
+ lists, err := aggregator.Val(tags)
+ if err != nil {
+ ml.Error().Err(err).RawJSON("req", logger.Proto(request)).Msg("fail to aggregate topN results")
+ resp = bus.NewMessage(bus.MessageID(now), common.NewError("fail to aggregate topN results for measure %s: %v", request.Name, err))
+ return
+ }
+ resp = bus.NewMessage(bus.MessageID(now), &measurev1.TopNResponse{Lists: lists})
+ } else {
+ // Standalone mode without aggregation or distributed DataNode mode: return top N distinct entities directly
+ resp = bus.NewMessage(bus.MessageID(now), toTopNResponse(result))
+ }
if !request.Trace && t.slowQuery > 0 {
latency := time.Since(n)
if latency > t.slowQuery {
diff --git a/pkg/query/logical/measure/topn_analyzer.go b/pkg/query/logical/measure/topn_analyzer.go
index cf2d87b68..66c29f9ca 100644
--- a/pkg/query/logical/measure/topn_analyzer.go
+++ b/pkg/query/logical/measure/topn_analyzer.go
@@ -109,23 +109,7 @@ func TopNAnalyze(criteria *measurev1.TopNRequest, sourceMeasureSchemaList []*dat
return nil, err
}
- if criteria.GetAgg() != 0 {
- groupByProjectionTags := sourceMeasureSchema.GetEntity().GetTagNames()
- groupByTags := [][]*logical.Tag{logical.NewTags(measure.TopNTagFamily, groupByProjectionTags...)}
- plan = newUnresolvedGroupBy(plan, groupByTags, false)
- plan = newUnresolvedAggregation(plan,
- &logical.Field{Name: topNAggSchema.FieldName},
- criteria.GetAgg(),
- true,
- false,
- false)
- }
-
- plan = top(plan, &measurev1.QueryRequest_Top{
- Number: criteria.GetTopN(),
- FieldName: topNAggSchema.FieldName,
- FieldValueSort: criteria.GetFieldValueSort(),
- })
+ plan = topNDistinct(plan, criteria.GetTopN(), criteria.GetFieldValueSort(), topNAggSchema.FieldName)
p, err := plan.Analyze(s)
if err != nil {
diff --git a/pkg/query/logical/measure/topn_plan_distinct.go b/pkg/query/logical/measure/topn_plan_distinct.go
new file mode 100644
index 000000000..698252e40
--- /dev/null
+++ b/pkg/query/logical/measure/topn_plan_distinct.go
@@ -0,0 +1,268 @@
+// Licensed to Apache Software Foundation (ASF) under one or more contributor
+// license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright
+// ownership. Apache Software Foundation (ASF) licenses this file to you 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 measure
+
+import (
+ "container/heap"
+ "context"
+ "fmt"
+ "sort"
+
+ "github.com/pkg/errors"
+ "go.uber.org/multierr"
+
+ measurev1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/measure/v1"
+ modelv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/model/v1"
+ pbv1 "github.com/apache/skywalking-banyandb/pkg/pb/v1"
+ "github.com/apache/skywalking-banyandb/pkg/query/executor"
+ "github.com/apache/skywalking-banyandb/pkg/query/logical"
+)
+
+// dedupHeapItem tracks an entity's best value and position in the deduplication heap.
+type dedupHeapItem struct {
+ idp *measurev1.InternalDataPoint
+ key string
+ val int64
+ index int
+}
+
+// entityDedupTopN is a heap-based structure that maintains top N distinct entities.
+// For DESC sorting, it uses a min-heap (root = smallest of top N).
+// For ASC sorting, it uses a max-heap (root = largest of top N).
+// The entityMap only tracks entities currently in the heap, so memory is bounded to O(topN).
+type entityDedupTopN struct {
+ entityMap map[string]*dedupHeapItem
+ items []*dedupHeapItem
+ topN int
+ sortDesc bool
+}
+
+// newEntityDedupTopN creates a new entity deduplication heap.
+func newEntityDedupTopN(topN int, sortDesc bool) *entityDedupTopN {
+ return &entityDedupTopN{
+ entityMap: make(map[string]*dedupHeapItem),
+ items: make([]*dedupHeapItem, 0, topN),
+ topN: topN,
+ sortDesc: sortDesc,
+ }
+}
+
+func (h *entityDedupTopN) Len() int { return len(h.items) }
+
+func (h *entityDedupTopN) Less(i, j int) bool {
+ if h.sortDesc {
+ return h.items[i].val < h.items[j].val
+ }
+ return h.items[i].val > h.items[j].val
+}
+
+func (h *entityDedupTopN) Swap(i, j int) {
+ h.items[i], h.items[j] = h.items[j], h.items[i]
+ h.items[i].index = i
+ h.items[j].index = j
+}
+
+func (h *entityDedupTopN) Push(x any) {
+ item := x.(*dedupHeapItem)
+ item.index = len(h.items)
+ h.entityMap[item.key] = item
+ h.items = append(h.items, item)
+}
+
+func (h *entityDedupTopN) Pop() any {
+ old := h.items
+ n := len(old)
+ item := old[n-1]
+ old[n-1] = nil
+ item.index = -1
+ delete(h.entityMap, item.key)
+ h.items = old[0 : n-1]
+ return item
+}
+
+func (h *entityDedupTopN) isBetter(newVal, existingVal int64) bool {
+ if h.sortDesc {
+ return newVal > existingVal
+ }
+ return newVal < existingVal
+}
+
+// Put inserts or updates an entity in the deduplication heap.
+func (h *entityDedupTopN) Put(key string, val int64, idp *measurev1.InternalDataPoint) {
+ if item, exists := h.entityMap[key]; exists {
+ if !h.isBetter(val, item.val) {
+ return
+ }
+ item.val = val
+ item.idp = idp
+ heap.Fix(h, item.index)
+ return
+ }
+ h.tryAddToHeap(key, val, idp)
+}
+
+func (h *entityDedupTopN) tryAddToHeap(key string, val int64, idp *measurev1.InternalDataPoint) {
+ if h.topN <= 0 {
+ return
+ }
+ if len(h.items) < h.topN {
+ heap.Push(h, &dedupHeapItem{key: key, val: val, idp: idp})
+ return
+ }
+ root := h.items[0]
+ if h.isBetter(val, root.val) {
+ heap.Pop(h)
+ heap.Push(h, &dedupHeapItem{key: key, val: val, idp: idp})
+ }
+}
+
+// Elements returns the top N distinct entities sorted by best value.
+func (h *entityDedupTopN) Elements() []*measurev1.InternalDataPoint {
+ result := make([]*dedupHeapItem, len(h.items))
+ copy(result, h.items)
+ sort.Slice(result, func(i, j int) bool {
+ if h.sortDesc {
+ return result[i].val > result[j].val
+ }
+ return result[i].val < result[j].val
+ })
+ idps := make([]*measurev1.InternalDataPoint, len(result))
+ for i, item := range result {
+ idps[i] = item.idp
+ }
+ return idps
+}
+
+var _ logical.UnresolvedPlan = (*unresolvedTopNDistinct)(nil)
+
+// unresolvedTopNDistinct is an unresolved plan for top N distinct entity selection.
+type unresolvedTopNDistinct struct {
+ unresolvedInput logical.UnresolvedPlan
+ fieldName string
+ topN int32
+ sort modelv1.Sort
+}
+
+func topNDistinct(input logical.UnresolvedPlan, topN int32, sort modelv1.Sort, fieldName string) logical.UnresolvedPlan {
+ return &unresolvedTopNDistinct{
+ unresolvedInput: input,
+ topN: topN,
+ sort: sort,
+ fieldName: fieldName,
+ }
+}
+
+func (u *unresolvedTopNDistinct) Analyze(measureSchema logical.Schema) (logical.Plan, error) {
+ prevPlan, analyzeErr := u.unresolvedInput.Analyze(measureSchema)
+ if analyzeErr != nil {
+ return nil, analyzeErr
+ }
+ fieldRefs, fieldErr := prevPlan.Schema().CreateFieldRef(logical.NewField(u.fieldName))
+ if fieldErr != nil {
+ return nil, fieldErr
+ }
+ if len(fieldRefs) == 0 {
+ return nil, errors.New("no field ref found for topN distinct")
+ }
+ sortDesc := u.sort == modelv1.Sort_SORT_DESC
+ return &topNDistinctOp{
+ Parent: &logical.Parent{
+ UnresolvedInput: u.unresolvedInput,
+ Input: prevPlan,
+ },
+ topN: int(u.topN),
+ sortDesc: sortDesc,
+ fieldIdx: fieldRefs[0].Spec.FieldIdx,
+ }, nil
+}
+
+var _ logical.Plan = (*topNDistinctOp)(nil)
+
+// topNDistinctOp selects top N distinct entities by best value.
+type topNDistinctOp struct {
+ *logical.Parent
+ topN int
+ sortDesc bool
+ fieldIdx int
+}
+
+func (t *topNDistinctOp) String() string {
+ dir := "DESC"
+ if !t.sortDesc {
+ dir = "ASC"
+ }
+ return fmt.Sprintf("TopNDistinct(topN=%d,sort=%s)", t.topN, dir)
+}
+
+func (t *topNDistinctOp) Children() []logical.Plan {
+ return []logical.Plan{t.Input}
+}
+
+func (t *topNDistinctOp) Schema() logical.Schema {
+ return t.Input.Schema()
+}
+
+func (t *topNDistinctOp) Execute(ctx context.Context) (mit executor.MIterator, err error) {
+ iter, execErr := t.Input.(executor.MeasureExecutable).Execute(ctx)
+ if execErr != nil {
+ return nil, execErr
+ }
+ defer func() {
+ err = multierr.Append(err, iter.Close())
+ }()
+ edHeap := newEntityDedupTopN(t.topN, t.sortDesc)
+ for iter.Next() {
+ dataPoints := iter.Current()
+ for _, idp := range dataPoints {
+ dp := idp.GetDataPoint()
+ if len(dp.GetTagFamilies()) == 0 || len(dp.GetTagFamilies()[0].GetTags()) == 0 {
+ continue
+ }
+ entityValues := make(pbv1.EntityValues, 0, len(dp.GetTagFamilies()[0].GetTags()))
+ for _, tag := range dp.GetTagFamilies()[0].GetTags() {
+ entityValues = append(entityValues, tag.Value)
+ }
+ entityKey := entityValues.String()
+ fieldVal := dp.GetFields()[t.fieldIdx].GetValue().GetInt().GetValue()
+ edHeap.Put(entityKey, fieldVal, idp)
+ }
+ }
+ return &topNDistinctIterator{
+ elements: edHeap.Elements(),
+ index: -1,
+ }, nil
+}
+
+// topNDistinctIterator iterates over the top N distinct entity results.
+type topNDistinctIterator struct {
+ elements []*measurev1.InternalDataPoint
+ index int
+}
+
+func (t *topNDistinctIterator) Next() bool {
+ t.index++
+ return t.index < len(t.elements)
+}
+
+func (t *topNDistinctIterator) Current() []*measurev1.InternalDataPoint {
+ return []*measurev1.InternalDataPoint{t.elements[t.index]}
+}
+
+func (t *topNDistinctIterator) Close() error {
+ return nil
+}
diff --git a/pkg/query/logical/measure/topn_plan_distinct_test.go b/pkg/query/logical/measure/topn_plan_distinct_test.go
new file mode 100644
index 000000000..b9d1608c3
--- /dev/null
+++ b/pkg/query/logical/measure/topn_plan_distinct_test.go
@@ -0,0 +1,173 @@
+// Licensed to Apache Software Foundation (ASF) under one or more contributor
+// license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright
+// ownership. Apache Software Foundation (ASF) licenses this file to you 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 measure
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+
+ measurev1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/measure/v1"
+ modelv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/model/v1"
+)
+
+func strTagValue(v string) *modelv1.TagValue {
+ return &modelv1.TagValue{Value: &modelv1.TagValue_Str{Str: &modelv1.Str{Value: v}}}
+}
+
+func intFieldValue(v int64) *modelv1.FieldValue {
+ return &modelv1.FieldValue{Value: &modelv1.FieldValue_Int{Int: &modelv1.Int{Value: v}}}
+}
+
+func makeIDP(entityValues []*modelv1.TagValue, fieldVal int64) *measurev1.InternalDataPoint {
+ tags := make([]*modelv1.Tag, len(entityValues))
+ for i, ev := range entityValues {
+ tags[i] = &modelv1.Tag{Key: "entity", Value: ev}
+ }
+ return &measurev1.InternalDataPoint{
+ DataPoint: &measurev1.DataPoint{
+ TagFamilies: []*modelv1.TagFamily{
+ {Name: "_topN", Tags: tags},
+ },
+ Fields: []*measurev1.DataPoint_Field{
+ {
+ Name: "value",
+ Value: intFieldValue(fieldVal),
+ },
+ },
+ },
+ }
+}
+
+func TestEntityDedupTopN_DESC(t *testing.T) {
+ h := newEntityDedupTopN(3, true)
+
+ h.Put("A", 100, makeIDP([]*modelv1.TagValue{strTagValue("A")}, 100))
+ h.Put("B", 90, makeIDP([]*modelv1.TagValue{strTagValue("B")}, 90))
+ h.Put("A", 200, makeIDP([]*modelv1.TagValue{strTagValue("A")}, 200))
+ h.Put("C", 80, makeIDP([]*modelv1.TagValue{strTagValue("C")}, 80))
+ h.Put("D", 150, makeIDP([]*modelv1.TagValue{strTagValue("D")}, 150))
+
+ result := h.Elements()
+ require.Len(t, result, 3)
+ vals := extractFieldValues(result)
+ assert.Equal(t, []int64{200, 150, 90}, vals)
+}
+
+func TestEntityDedupTopN_ASC(t *testing.T) {
+ h := newEntityDedupTopN(3, false)
+
+ h.Put("A", 100, makeIDP([]*modelv1.TagValue{strTagValue("A")}, 100))
+ h.Put("B", 90, makeIDP([]*modelv1.TagValue{strTagValue("B")}, 90))
+ h.Put("A", 50, makeIDP([]*modelv1.TagValue{strTagValue("A")}, 50))
+ h.Put("C", 80, makeIDP([]*modelv1.TagValue{strTagValue("C")}, 80))
+ h.Put("D", 30, makeIDP([]*modelv1.TagValue{strTagValue("D")}, 30))
+
+ result := h.Elements()
+ require.Len(t, result, 3)
+ vals := extractFieldValues(result)
+ assert.Equal(t, []int64{30, 50, 80}, vals)
+}
+
+func TestEntityDedupTopN_EvictAndReAdd(t *testing.T) {
+ h := newEntityDedupTopN(2, true)
+
+ h.Put("A", 100, makeIDP([]*modelv1.TagValue{strTagValue("A")}, 100))
+ h.Put("B", 90, makeIDP([]*modelv1.TagValue{strTagValue("B")}, 90))
+ h.Put("C", 80, makeIDP([]*modelv1.TagValue{strTagValue("C")}, 80))
+ h.Put("C", 95, makeIDP([]*modelv1.TagValue{strTagValue("C")}, 95))
+
+ result := h.Elements()
+ require.Len(t, result, 2)
+ vals := extractFieldValues(result)
+ assert.Equal(t, []int64{100, 95}, vals)
+}
+
+func TestEntityDedupTopN_EmptyInput(t *testing.T) {
+ h := newEntityDedupTopN(3, true)
+ result := h.Elements()
+ assert.Empty(t, result)
+}
+
+func TestEntityDedupTopN_TopN1(t *testing.T) {
+ h := newEntityDedupTopN(1, true)
+ h.Put("A", 100, makeIDP([]*modelv1.TagValue{strTagValue("A")}, 100))
+ h.Put("B", 200, makeIDP([]*modelv1.TagValue{strTagValue("B")}, 200))
+ h.Put("A", 50, makeIDP([]*modelv1.TagValue{strTagValue("A")}, 50))
+ h.Put("B", 300, makeIDP([]*modelv1.TagValue{strTagValue("B")}, 300))
+
+ result := h.Elements()
+ require.Len(t, result, 1)
+ vals := extractFieldValues(result)
+ assert.Equal(t, []int64{300}, vals)
+}
+
+func TestEntityDedupTopN_AllSameEntity(t *testing.T) {
+ h := newEntityDedupTopN(3, true)
+ h.Put("A", 100, makeIDP([]*modelv1.TagValue{strTagValue("A")}, 100))
+ h.Put("A", 200, makeIDP([]*modelv1.TagValue{strTagValue("A")}, 200))
+ h.Put("A", 150, makeIDP([]*modelv1.TagValue{strTagValue("A")}, 150))
+
+ result := h.Elements()
+ require.Len(t, result, 1)
+ vals := extractFieldValues(result)
+ assert.Equal(t, []int64{200}, vals)
+}
+
+func extractFieldValues(idps []*measurev1.InternalDataPoint) []int64 {
+ vals := make([]int64, len(idps))
+ for i, idp := range idps {
+ vals[i] = idp.GetDataPoint().GetFields()[0].GetValue().GetInt().GetValue()
+ }
+ return vals
+}
+
+func TestTopNDistinctIterator(t *testing.T) {
+ items := []*measurev1.InternalDataPoint{
+ makeIDP([]*modelv1.TagValue{strTagValue("svc-A")}, 200),
+ makeIDP([]*modelv1.TagValue{strTagValue("svc-D")}, 150),
+ makeIDP([]*modelv1.TagValue{strTagValue("svc-B")}, 90),
+ }
+ iter := &topNDistinctIterator{elements: items, index: -1}
+
+ var result []*measurev1.InternalDataPoint
+ for iter.Next() {
+ current := iter.Current()
+ result = append(result, current[0])
+ }
+ require.NoError(t, iter.Close())
+
+ require.Len(t, result, 3)
+ vals := extractFieldValues(result)
+ assert.Equal(t, []int64{200, 150, 90}, vals)
+}
+
+func TestTopNDistinctIterator_Empty(t *testing.T) {
+ iter := &topNDistinctIterator{elements: nil, index: -1}
+ require.False(t, iter.Next())
+ require.NoError(t, iter.Close())
+}
+
+func TestTopNDistinctOp_String(t *testing.T) {
+ op := &topNDistinctOp{topN: 10, sortDesc: true}
+ assert.Equal(t, "TopNDistinct(topN=10,sort=DESC)", op.String())
+
+ opAsc := &topNDistinctOp{topN: 5, sortDesc: false}
+ assert.Equal(t, "TopNDistinct(topN=5,sort=ASC)", opAsc.String())
+}
diff --git a/pkg/test/measure/testdata/measures/service_instance_metric_topn_test.json b/pkg/test/measure/testdata/measures/service_instance_metric_topn_test.json
new file mode 100644
index 000000000..82b97bddc
--- /dev/null
+++ b/pkg/test/measure/testdata/measures/service_instance_metric_topn_test.json
@@ -0,0 +1,41 @@
+{
+ "metadata": {
+ "name": "service_instance_metric_topn_test",
+ "group": "sw_metric"
+ },
+ "tag_families": [
+ {
+ "name": "default",
+ "tags": [
+ {
+ "name": "id",
+ "type": "TAG_TYPE_STRING"
+ },
+ {
+ "name": "service_id",
+ "type": "TAG_TYPE_STRING"
+ }
+ ]
+ }
+ ],
+ "fields": [
+ {
+ "name": "value",
+ "field_type": "FIELD_TYPE_INT",
+ "encoding_method": "ENCODING_METHOD_GORILLA",
+ "compression_method": "COMPRESSION_METHOD_ZSTD"
+ }
+ ],
+ "entity": {
+ "tag_names": [
+ "service_id"
+ ]
+ },
+ "sharding_key": {
+ "tag_names": [
+ "service_id"
+ ]
+ },
+ "interval": "1m",
+ "updated_at": "2021-04-15T01:30:15.01Z"
+}
diff --git a/pkg/test/measure/testdata/topn_aggregations/topn_agg_test.json b/pkg/test/measure/testdata/topn_aggregations/topn_agg_test.json
new file mode 100644
index 000000000..9403cfe7d
--- /dev/null
+++ b/pkg/test/measure/testdata/topn_aggregations/topn_agg_test.json
@@ -0,0 +1,17 @@
+{
+ "metadata": {
+ "name": "topn_agg_test",
+ "group": "sw_metric"
+ },
+ "source_measure": {
+ "name": "service_instance_metric_topn_test",
+ "group": "sw_metric"
+ },
+ "field_name": "value",
+ "field_value_sort": 0,
+ "group_by_tag_names": [
+ "service_id"
+ ],
+ "counters_number": 1000,
+ "lru_size": 10
+}
diff --git a/test/cases/init.go b/test/cases/init.go
index f5b167200..401ecc27e 100644
--- a/test/cases/init.go
+++ b/test/cases/init.go
@@ -101,6 +101,7 @@ func Initialize(addr string, now time.Time) {
casesmeasuredata.Write(conn, "service_cpm_minute", "sw_updated", "service_cpm_minute_updated_data.json", now.Add(10*time.Minute), interval)
casesmeasuredata.Write(conn, "endpoint_resp_time_minute", "sw_metric", "endpoint_resp_time_minute_data.json", now, interval)
casesmeasuredata.Write(conn, "endpoint_resp_time_minute", "sw_metric", "endpoint_resp_time_minute_data1.json", now.Add(10*time.Second), interval)
+ casesmeasuredata.Write(conn, "service_instance_metric_topn_test", "sw_metric", "service_instance_metric_topn_test_data.json", now, interval)
casesmeasuredata.WriteMixed(conn, now.Add(30*time.Minute), interval,
casesmeasuredata.WriteSpec{
Metadata: &commonv1.Metadata{Name: "service_cpm_minute", Group: "sw_spec"},
diff --git a/test/cases/measure/data/testdata/service_instance_metric_topn_test_data.json b/test/cases/measure/data/testdata/service_instance_metric_topn_test_data.json
new file mode 100644
index 000000000..58b68eda9
--- /dev/null
+++ b/test/cases/measure/data/testdata/service_instance_metric_topn_test_data.json
@@ -0,0 +1,152 @@
+[
+ {
+ "tag_families": [
+ {
+ "tags": [
+ {
+ "str": {
+ "value": "1"
+ }
+ },
+ {
+ "str": {
+ "value": "svc_1"
+ }
+ }
+ ]
+ }
+ ],
+ "fields": [
+ {
+ "int": {
+ "value": 100
+ }
+ }
+ ]
+ },
+ {
+ "tag_families": [
+ {
+ "tags": [
+ {
+ "str": {
+ "value": "2"
+ }
+ },
+ {
+ "str": {
+ "value": "svc_1"
+ }
+ }
+ ]
+ }
+ ],
+ "fields": [
+ {
+ "int": {
+ "value": 200
+ }
+ }
+ ]
+ },
+ {
+ "tag_families": [
+ {
+ "tags": [
+ {
+ "str": {
+ "value": "3"
+ }
+ },
+ {
+ "str": {
+ "value": "svc_1"
+ }
+ }
+ ]
+ }
+ ],
+ "fields": [
+ {
+ "int": {
+ "value": 151
+ }
+ }
+ ]
+ },
+ {
+ "tag_families": [
+ {
+ "tags": [
+ {
+ "str": {
+ "value": "4"
+ }
+ },
+ {
+ "str": {
+ "value": "svc_2"
+ }
+ }
+ ]
+ }
+ ],
+ "fields": [
+ {
+ "int": {
+ "value": 300
+ }
+ }
+ ]
+ },
+ {
+ "tag_families": [
+ {
+ "tags": [
+ {
+ "str": {
+ "value": "5"
+ }
+ },
+ {
+ "str": {
+ "value": "svc_2"
+ }
+ }
+ ]
+ }
+ ],
+ "fields": [
+ {
+ "int": {
+ "value": 50
+ }
+ }
+ ]
+ },
+ {
+ "tag_families": [
+ {
+ "tags": [
+ {
+ "str": {
+ "value": "6"
+ }
+ },
+ {
+ "str": {
+ "value": "svc_3"
+ }
+ }
+ ]
+ }
+ ],
+ "fields": [
+ {
+ "int": {
+ "value": 180
+ }
+ }
+ ]
+ }
+]
diff --git a/test/cases/topn/data/data.go b/test/cases/topn/data/data.go
index b00e41868..1ffa4fff0 100644
--- a/test/cases/topn/data/data.go
+++ b/test/cases/topn/data/data.go
@@ -22,6 +22,7 @@ import (
"context"
"embed"
"regexp"
+ "slices"
"strings"
"github.com/google/go-cmp/cmp"
@@ -37,6 +38,7 @@ import (
commonv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/common/v1"
databasev1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/database/v1"
measurev1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/measure/v1"
+ modelv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/model/v1"
"github.com/apache/skywalking-banyandb/banyand/metadata"
"github.com/apache/skywalking-banyandb/banyand/metadata/schema"
"github.com/apache/skywalking-banyandb/pkg/bydbql"
@@ -82,6 +84,13 @@ var VerifyFn = func(innerGm gm.Gomega, sharedContext helpers.SharedContext, args
innerGm.Expect(err).NotTo(gm.HaveOccurred())
want := &measurev1.TopNResponse{}
helpers.UnmarshalYAML(ww, want)
+ // Sort items by value, using entity as tie-breaker for deterministic ordering.
+ for _, list := range resp.Lists {
+ slices.SortFunc(list.Items, compareTopNItems)
+ }
+ for _, list := range want.Lists {
+ slices.SortFunc(list.Items, compareTopNItems)
+ }
success := innerGm.Expect(cmp.Equal(resp, want,
protocmp.IgnoreUnknown(),
protocmp.IgnoreFields(&measurev1.TopNList{}, "timestamp"),
@@ -109,6 +118,88 @@ var VerifyFn = func(innerGm gm.Gomega, sharedContext helpers.SharedContext, args
innerGm.Expect(resp.Trace.GetSpans()).NotTo(gm.BeEmpty())
}
+// compareTopNItems compares two TopNList_Item by value first, then by entity tags for deterministic ordering.
+func compareTopNItems(a, b *measurev1.TopNList_Item) int {
+ // Primary: sort by value to preserve ranking verification.
+ aVal := a.GetValue().GetInt().GetValue()
+ bVal := b.GetValue().GetInt().GetValue()
+ if aVal < bVal {
+ return -1
+ }
+ if aVal > bVal {
+ return 1
+ }
+ // Secondary: sort by entity tags as tie-breaker for equal values.
+ for tagIdx, aTag := range a.Entity {
+ if tagIdx >= len(b.Entity) {
+ return 1
+ }
+ if tagCmp := compareTagValue(aTag.GetValue(), b.Entity[tagIdx].GetValue()); tagCmp != 0 {
+ return tagCmp
+ }
+ }
+ if len(a.Entity) < len(b.Entity) {
+ return -1
+ }
+ return 0
+}
+
+// compareTagValue compares two TagValue instances in a type-aware manner.
+func compareTagValue(a, b *modelv1.TagValue) int {
+ aOrd := tagValOrder(a)
+ bOrd := tagValOrder(b)
+ if aOrd != bOrd {
+ return aOrd - bOrd
+ }
+ switch a.GetValue().(type) {
+ case *modelv1.TagValue_Str:
+ aVal := a.GetStr().GetValue()
+ bVal := b.GetStr().GetValue()
+ switch {
+ case aVal < bVal:
+ return -1
+ case aVal > bVal:
+ return 1
+ default:
+ return 0
+ }
+ case *modelv1.TagValue_Int:
+ aVal := a.GetInt().GetValue()
+ bVal := b.GetInt().GetValue()
+ switch {
+ case aVal < bVal:
+ return -1
+ case aVal > bVal:
+ return 1
+ default:
+ return 0
+ }
+ default:
+ return 0
+ }
+}
+
+func tagValOrder(v *modelv1.TagValue) int {
+ switch v.GetValue().(type) {
+ case *modelv1.TagValue_Null:
+ return 0
+ case *modelv1.TagValue_Str:
+ return 1
+ case *modelv1.TagValue_Int:
+ return 2
+ case *modelv1.TagValue_StrArray:
+ return 3
+ case *modelv1.TagValue_IntArray:
+ return 4
+ case *modelv1.TagValue_BinaryData:
+ return 5
+ case *modelv1.TagValue_Timestamp:
+ return 6
+ default:
+ return -1
+ }
+}
+
// verifyQLWithRequest verifies that the QL file produces an equivalent QueryRequest to the YAML.
func verifyQLWithRequest(ctx context.Context, innerGm gm.Gomega, args helpers.Args, yamlQuery *measurev1.TopNRequest, conn *grpclib.ClientConn) {
qlContent, err := qlFS.ReadFile("input/" + args.Input + ".ql")
diff --git a/test/cases/topn/data/input/topn_count.ql b/test/cases/topn/data/input/topn_count.ql
new file mode 100644
index 000000000..59bb42fa4
--- /dev/null
+++ b/test/cases/topn/data/input/topn_count.ql
@@ -0,0 +1,22 @@
+# Licensed to Apache Software Foundation (ASF) under one or more contributor
+# license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright
+# ownership. Apache Software Foundation (ASF) licenses this file to you 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.
+
+SHOW TOP 3
+FROM MEASURE topn_agg_test IN sw_metric
+TIME > '-15m'
+AGGREGATE BY COUNT
+ORDER BY DESC
diff --git a/test/cases/topn/data/input/topn_count.yaml b/test/cases/topn/data/input/topn_count.yaml
new file mode 100644
index 000000000..e4c366780
--- /dev/null
+++ b/test/cases/topn/data/input/topn_count.yaml
@@ -0,0 +1,22 @@
+# Licensed to Apache Software Foundation (ASF) under one or more contributor
+# license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright
+# ownership. Apache Software Foundation (ASF) licenses this file to you 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.
+
+name: "topn_agg_test"
+groups: ["sw_metric"]
+topN: 3
+fieldValueSort: 1
+agg: 4
diff --git a/test/cases/topn/data/input/topn_max.ql b/test/cases/topn/data/input/topn_max.ql
new file mode 100644
index 000000000..18f0c542f
--- /dev/null
+++ b/test/cases/topn/data/input/topn_max.ql
@@ -0,0 +1,22 @@
+# Licensed to Apache Software Foundation (ASF) under one or more contributor
+# license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright
+# ownership. Apache Software Foundation (ASF) licenses this file to you 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.
+
+SHOW TOP 3
+FROM MEASURE topn_agg_test IN sw_metric
+TIME > '-15m'
+AGGREGATE BY MAX
+ORDER BY DESC
diff --git a/test/cases/topn/data/input/topn_max.yaml b/test/cases/topn/data/input/topn_max.yaml
new file mode 100644
index 000000000..1ed40d2f2
--- /dev/null
+++ b/test/cases/topn/data/input/topn_max.yaml
@@ -0,0 +1,22 @@
+# Licensed to Apache Software Foundation (ASF) under one or more contributor
+# license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright
+# ownership. Apache Software Foundation (ASF) licenses this file to you 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.
+
+name: "topn_agg_test"
+groups: ["sw_metric"]
+topN: 3
+fieldValueSort: 1
+agg: 2
diff --git a/test/cases/topn/data/input/topn_mean.ql b/test/cases/topn/data/input/topn_mean.ql
new file mode 100644
index 000000000..aa6d8f691
--- /dev/null
+++ b/test/cases/topn/data/input/topn_mean.ql
@@ -0,0 +1,22 @@
+# Licensed to Apache Software Foundation (ASF) under one or more contributor
+# license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright
+# ownership. Apache Software Foundation (ASF) licenses this file to you 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.
+
+SHOW TOP 2
+FROM MEASURE topn_agg_test IN sw_metric
+TIME > '-15m'
+AGGREGATE BY MEAN
+ORDER BY DESC
diff --git a/test/cases/topn/data/input/topn_mean.yaml b/test/cases/topn/data/input/topn_mean.yaml
new file mode 100644
index 000000000..0c8ab3513
--- /dev/null
+++ b/test/cases/topn/data/input/topn_mean.yaml
@@ -0,0 +1,22 @@
+# Licensed to Apache Software Foundation (ASF) under one or more contributor
+# license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright
+# ownership. Apache Software Foundation (ASF) licenses this file to you 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.
+
+name: "topn_agg_test"
+groups: ["sw_metric"]
+topN: 2
+fieldValueSort: 1
+agg: 1
diff --git a/test/cases/topn/data/input/topn_min.ql b/test/cases/topn/data/input/topn_min.ql
new file mode 100644
index 000000000..d1e756f42
--- /dev/null
+++ b/test/cases/topn/data/input/topn_min.ql
@@ -0,0 +1,22 @@
+# Licensed to Apache Software Foundation (ASF) under one or more contributor
+# license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright
+# ownership. Apache Software Foundation (ASF) licenses this file to you 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.
+
+SHOW TOP 3
+FROM MEASURE topn_agg_test IN sw_metric
+TIME > '-15m'
+AGGREGATE BY MIN
+ORDER BY ASC
diff --git a/test/cases/topn/data/input/topn_min.yaml b/test/cases/topn/data/input/topn_min.yaml
new file mode 100644
index 000000000..1c9263e9b
--- /dev/null
+++ b/test/cases/topn/data/input/topn_min.yaml
@@ -0,0 +1,22 @@
+# Licensed to Apache Software Foundation (ASF) under one or more contributor
+# license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright
+# ownership. Apache Software Foundation (ASF) licenses this file to you 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.
+
+name: "topn_agg_test"
+groups: ["sw_metric"]
+topN: 3
+fieldValueSort: 2
+agg: 3
diff --git a/test/cases/topn/data/input/topn_sum.ql b/test/cases/topn/data/input/topn_sum.ql
new file mode 100644
index 000000000..5d6f80363
--- /dev/null
+++ b/test/cases/topn/data/input/topn_sum.ql
@@ -0,0 +1,22 @@
+# Licensed to Apache Software Foundation (ASF) under one or more contributor
+# license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright
+# ownership. Apache Software Foundation (ASF) licenses this file to you 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.
+
+SHOW TOP 3
+FROM MEASURE topn_agg_test IN sw_metric
+TIME > '-15m'
+AGGREGATE BY SUM
+ORDER BY DESC
diff --git a/test/cases/topn/data/input/topn_sum.yaml b/test/cases/topn/data/input/topn_sum.yaml
new file mode 100644
index 000000000..4bf8dc541
--- /dev/null
+++ b/test/cases/topn/data/input/topn_sum.yaml
@@ -0,0 +1,22 @@
+# Licensed to Apache Software Foundation (ASF) under one or more contributor
+# license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright
+# ownership. Apache Software Foundation (ASF) licenses this file to you 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.
+
+name: "topn_agg_test"
+groups: ["sw_metric"]
+topN: 3
+fieldValueSort: 1
+agg: 5
diff --git a/test/cases/topn/data/want/topn_count.yaml b/test/cases/topn/data/want/topn_count.yaml
new file mode 100644
index 000000000..bca240308
--- /dev/null
+++ b/test/cases/topn/data/want/topn_count.yaml
@@ -0,0 +1,43 @@
+# Licensed to Apache Software Foundation (ASF) under one or more contributor
+# license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright
+# ownership. Apache Software Foundation (ASF) licenses this file to you 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.
+
+lists:
+- items:
+ - entity:
+ - key: service_id
+ value:
+ str:
+ value: svc_2
+ value:
+ int:
+ value: "1"
+ - entity:
+ - key: service_id
+ value:
+ str:
+ value: svc_1
+ value:
+ int:
+ value: "1"
+ - entity:
+ - key: service_id
+ value:
+ str:
+ value: svc_3
+ value:
+ int:
+ value: "1"
diff --git a/test/cases/topn/data/want/topn_max.yaml b/test/cases/topn/data/want/topn_max.yaml
new file mode 100644
index 000000000..6a48d11af
--- /dev/null
+++ b/test/cases/topn/data/want/topn_max.yaml
@@ -0,0 +1,43 @@
+# Licensed to Apache Software Foundation (ASF) under one or more contributor
+# license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright
+# ownership. Apache Software Foundation (ASF) licenses this file to you 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.
+
+lists:
+- items:
+ - entity:
+ - key: service_id
+ value:
+ str:
+ value: svc_2
+ value:
+ int:
+ value: "300"
+ - entity:
+ - key: service_id
+ value:
+ str:
+ value: svc_1
+ value:
+ int:
+ value: "200"
+ - entity:
+ - key: service_id
+ value:
+ str:
+ value: svc_3
+ value:
+ int:
+ value: "180"
diff --git a/test/cases/topn/data/want/topn_mean.yaml b/test/cases/topn/data/want/topn_mean.yaml
new file mode 100644
index 000000000..7016d75dc
--- /dev/null
+++ b/test/cases/topn/data/want/topn_mean.yaml
@@ -0,0 +1,35 @@
+# Licensed to Apache Software Foundation (ASF) under one or more contributor
+# license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright
+# ownership. Apache Software Foundation (ASF) licenses this file to you 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.
+
+lists:
+- items:
+ - entity:
+ - key: service_id
+ value:
+ str:
+ value: svc_2
+ value:
+ int:
+ value: "300"
+ - entity:
+ - key: service_id
+ value:
+ str:
+ value: svc_1
+ value:
+ int:
+ value: "200"
diff --git a/test/cases/topn/data/want/topn_min.yaml b/test/cases/topn/data/want/topn_min.yaml
new file mode 100644
index 000000000..d77ced46d
--- /dev/null
+++ b/test/cases/topn/data/want/topn_min.yaml
@@ -0,0 +1,43 @@
+# Licensed to Apache Software Foundation (ASF) under one or more contributor
+# license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright
+# ownership. Apache Software Foundation (ASF) licenses this file to you 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.
+
+lists:
+- items:
+ - entity:
+ - key: service_id
+ value:
+ str:
+ value: svc_2
+ value:
+ int:
+ value: "50"
+ - entity:
+ - key: service_id
+ value:
+ str:
+ value: svc_1
+ value:
+ int:
+ value: "100"
+ - entity:
+ - key: service_id
+ value:
+ str:
+ value: svc_3
+ value:
+ int:
+ value: "180"
diff --git a/test/cases/topn/data/want/topn_sum.yaml b/test/cases/topn/data/want/topn_sum.yaml
new file mode 100644
index 000000000..6a48d11af
--- /dev/null
+++ b/test/cases/topn/data/want/topn_sum.yaml
@@ -0,0 +1,43 @@
+# Licensed to Apache Software Foundation (ASF) under one or more contributor
+# license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright
+# ownership. Apache Software Foundation (ASF) licenses this file to you 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.
+
+lists:
+- items:
+ - entity:
+ - key: service_id
+ value:
+ str:
+ value: svc_2
+ value:
+ int:
+ value: "300"
+ - entity:
+ - key: service_id
+ value:
+ str:
+ value: svc_1
+ value:
+ int:
+ value: "200"
+ - entity:
+ - key: service_id
+ value:
+ str:
+ value: svc_3
+ value:
+ int:
+ value: "180"
diff --git a/test/cases/topn/topn.go b/test/cases/topn/topn.go
index 2664b2391..d08317f13 100644
--- a/test/cases/topn/topn.go
+++ b/test/cases/topn/topn.go
@@ -42,7 +42,7 @@ var (
}
)
-var _ = g.DescribeTable("TopN Tests", verify,
+var topnEntries = []any{
g.Entry("max top3 order by desc", helpers.Args{Input: "aggr_desc", Duration: 25 * time.Minute, Offset: -20 * time.Minute}),
g.Entry("max top3 with condition order by desc", helpers.Args{Input: "condition_aggr_desc", Duration: 25 * time.Minute, Offset: -20 * time.Minute}),
g.Entry("max top3 for null group order by desc", helpers.Args{Input: "null_group", Duration: 25 * time.Minute, Offset: -20 * time.Minute}),
@@ -52,4 +52,16 @@ var _ = g.DescribeTable("TopN Tests", verify,
g.Entry("using in operation in aggregation", helpers.Args{Input: "in", Duration: 25 * time.Minute, Offset: -20 * time.Minute}),
g.Entry("using not-in operation in aggregation", helpers.Args{Input: "not_in", Duration: 25 * time.Minute, Offset: -20 * time.Minute}),
g.Entry("max top3 with version merged order by desc", helpers.Args{Input: "aggr_version_merged", Duration: 25 * time.Minute, Offset: -20 * time.Minute}),
-)
+ g.Entry("int64 max aggregation test", helpers.Args{Input: "topn_max", Duration: 25 * time.Minute, Offset: -20 * time.Minute}),
+ g.Entry("int64 min aggregation test", helpers.Args{Input: "topn_min", Duration: 25 * time.Minute, Offset: -20 * time.Minute}),
+ g.Entry("int64 sum aggregation test", helpers.Args{Input: "topn_sum", Duration: 25 * time.Minute, Offset: -20 * time.Minute}),
+ g.Entry("int64 mean aggregation test", helpers.Args{Input: "topn_mean", Duration: 25 * time.Minute, Offset: -20 * time.Minute}),
+ g.Entry("int64 count aggregation test", helpers.Args{Input: "topn_count", Duration: 25 * time.Minute, Offset: -20 * time.Minute}),
+}
+
+// RegisterTable registers the topn test table with the given description.
+func RegisterTable(description string) bool {
+ return g.DescribeTable(description, append([]any{verify}, topnEntries...)...)
+}
+
+var _ = RegisterTable("TopN Tests")