This is an automated email from the ASF dual-hosted git repository. hanahmily pushed a commit to branch trace/sidx in repository https://gitbox.apache.org/repos/asf/skywalking-banyandb.git
commit 1e1c66ff212401d7514929ebc87ff26410811d74 Author: Gao Hongtao <[email protected]> AuthorDate: Sun Aug 31 22:38:58 2025 +0800 Refactor trace query and filter logic to improve min/max value handling. Updated filter functions to return correct min/max values and optimized trace filtering by adjusting tag processing. Enhanced span handling in blockCursor to ensure proper data cloning. --- banyand/trace/block.go | 5 ++++- banyand/trace/query.go | 17 ----------------- banyand/trace/write_standalone.go | 5 ++++- pkg/query/logical/trace/index_filter.go | 8 ++++---- pkg/query/logical/trace/trace_plan_tag_filter.go | 2 +- test/cases/trace/trace.go | 2 +- 6 files changed, 14 insertions(+), 25 deletions(-) diff --git a/banyand/trace/block.go b/banyand/trace/block.go index 7a87a8a3..212e0196 100644 --- a/banyand/trace/block.go +++ b/banyand/trace/block.go @@ -18,6 +18,7 @@ package trace import ( + "bytes" "fmt" "slices" "sort" @@ -464,7 +465,9 @@ func (bc *blockCursor) loadData(tmpBlock *block) bool { return false } - bc.spans = append(bc.spans, tmpBlock.spans...) + for i := range tmpBlock.spans { + bc.spans = append(bc.spans, bytes.Clone(tmpBlock.spans[i])) + } for _, t := range tmpBlock.tags { if len(t.values) == 0 { diff --git a/banyand/trace/query.go b/banyand/trace/query.go index 14434932..ffdcf699 100644 --- a/banyand/trace/query.go +++ b/banyand/trace/query.go @@ -48,23 +48,6 @@ type queryOptions struct { maxTimestamp int64 } -func (qo *queryOptions) reset() { - qo.TraceQueryOptions.Reset() - qo.seriesToEntity = nil - qo.traceIDs = nil - qo.minTimestamp = 0 - qo.maxTimestamp = 0 -} - -func (qo *queryOptions) copyFrom(other *queryOptions) { - qo.TraceQueryOptions.CopyFrom(&other.TraceQueryOptions) - qo.seriesToEntity = other.seriesToEntity - qo.traceIDs = make([]string, len(other.traceIDs)) - copy(qo.traceIDs, other.traceIDs) - qo.minTimestamp = other.minTimestamp - qo.maxTimestamp = other.maxTimestamp -} - func (t *trace) Query(ctx context.Context, tqo model.TraceQueryOptions) (model.TraceQueryResult, error) { if tqo.TimeRange == nil { return nil, errors.New("invalid query options: timeRange are required") diff --git a/banyand/trace/write_standalone.go b/banyand/trace/write_standalone.go index 5baf6ba2..429ee482 100644 --- a/banyand/trace/write_standalone.go +++ b/banyand/trace/write_standalone.go @@ -255,11 +255,14 @@ func processTraces(schemaRepo *schemaRepo, tracesInTable *tracesInTable, writeEv } entityValues := make([]*modelv1.TagValue, 0, len(indexRule.Tags)) - for _, tagName := range indexRule.Tags { + for i, tagName := range indexRule.Tags { tagIdx, err := getTagIndex(stm, tagName) if err != nil || tagIdx >= len(req.Tags) { continue } + if i == len(indexRule.Tags)-1 { + break + } entityValues = append(entityValues, req.Tags[tagIdx]) } diff --git a/pkg/query/logical/trace/index_filter.go b/pkg/query/logical/trace/index_filter.go index a42161b7..44fad4d3 100644 --- a/pkg/query/logical/trace/index_filter.go +++ b/pkg/query/logical/trace/index_filter.go @@ -38,7 +38,7 @@ func buildFilter(criteria *modelv1.Criteria, tagNames map[string]bool, entityDict map[string]int, entity []*modelv1.TagValue, traceIDTagName string, orderByTag string, ) (index.Filter, [][]*modelv1.TagValue, []string, []string, int64, int64, error) { if criteria == nil { - return nil, [][]*modelv1.TagValue{entity}, nil, nil, math.MaxInt64, math.MinInt64, nil + return nil, [][]*modelv1.TagValue{entity}, nil, nil, math.MinInt64, math.MaxInt64, nil } switch criteria.GetExp().(type) { @@ -48,7 +48,7 @@ func buildFilter(criteria *modelv1.Criteria, tagNames map[string]bool, return buildFilterFromLogicalExpression(criteria.GetLe(), tagNames, entityDict, entity, traceIDTagName, orderByTag) } - return nil, nil, nil, nil, math.MaxInt64, math.MinInt64, logical.ErrInvalidCriteriaType + return nil, nil, nil, nil, math.MinInt64, math.MaxInt64, logical.ErrInvalidCriteriaType } func parseConditionToFilter(cond *modelv1.Condition, entity []*modelv1.TagValue, expr logical.LiteralExpr) (index.Filter, [][]*modelv1.TagValue, error) { @@ -281,8 +281,8 @@ func buildFilterFromCondition(cond *modelv1.Condition, tagNames map[string]bool, ) (index.Filter, [][]*modelv1.TagValue, []string, []string, int64, int64, error) { var collectedTagNames []string var traceIDs []string - minVal := int64(math.MaxInt64) - maxVal := int64(math.MinInt64) + minVal := int64(math.MinInt64) + maxVal := int64(math.MaxInt64) if !tagNames[cond.Name] { return nil, nil, collectedTagNames, traceIDs, minVal, maxVal, errors.Errorf("tag name '%s' not found in trace schema", cond.Name) diff --git a/pkg/query/logical/trace/trace_plan_tag_filter.go b/pkg/query/logical/trace/trace_plan_tag_filter.go index dadb1cd4..e68cb2c5 100644 --- a/pkg/query/logical/trace/trace_plan_tag_filter.go +++ b/pkg/query/logical/trace/trace_plan_tag_filter.go @@ -176,7 +176,7 @@ func buildTraceFilter(criteria *modelv1.Criteria, s logical.Schema, entityDict m entity []*modelv1.TagValue, traceIDTagName string, orderByTag string, ) (index.Filter, [][]*modelv1.TagValue, []string, []string, int64, int64, error) { if criteria == nil { - return nil, [][]*modelv1.TagValue{entity}, nil, nil, math.MaxInt64, math.MinInt64, nil + return nil, [][]*modelv1.TagValue{entity}, nil, nil, math.MinInt64, math.MaxInt64, nil } // Create a map of valid tag names from the schema tagNames := make(map[string]bool) diff --git a/test/cases/trace/trace.go b/test/cases/trace/trace.go index 9e1e285c..a4cc3e84 100644 --- a/test/cases/trace/trace.go +++ b/test/cases/trace/trace.go @@ -44,5 +44,5 @@ var _ = g.DescribeTable("Scanning Traces", func(args helpers.Args) { }, g.Entry("query by trace id", helpers.Args{Input: "eq_trace_id", Duration: 1 * time.Hour}), g.Entry("order by timestamp", helpers.Args{Input: "order_timestamp_desc", Duration: 1 * time.Hour}), - g.FEntry("order by duration", helpers.Args{Input: "order_duration_desc", Duration: 1 * time.Hour}), + g.Entry("order by duration", helpers.Args{Input: "order_duration_desc", Duration: 1 * time.Hour}), )
