-
Notifications
You must be signed in to change notification settings - Fork 3.9k
chore: Aggregation groupings for by() and without() #19928
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
spiridonov
wants to merge
17
commits into
main
Choose a base branch
from
spiridonov-more-funcs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
741be75
aggregation groupings
spiridonov 296a9b8
Merge branch 'main' of github.com:grafana/loki into spiridonov-more-f…
spiridonov 12de19e
hacks
spiridonov bcf4455
Merge branch 'main' of github.com:grafana/loki into spiridonov-more-f…
spiridonov 91586e7
lint
spiridonov a6f32f6
part 1
spiridonov 49bbec7
Merge branch 'main' of github.com:grafana/loki into spiridonov-more-f…
spiridonov ef010e5
rollback
spiridonov 223ad95
Merge branch 'main' of github.com:grafana/loki into spiridonov-more-f…
spiridonov bd180c2
minor
spiridonov 47d3764
Merge branch 'main' of github.com:grafana/loki into spiridonov-more-f…
spiridonov 74db8d2
lint
spiridonov a6616cd
Merge branch 'main' into spiridonov-more-funcs
spiridonov 195e774
Merge branch 'main' of github.com:grafana/loki into spiridonov-more-f…
spiridonov 166ac75
Merge branch 'spiridonov-more-funcs' of github.com:grafana/loki into …
spiridonov c35dd89
Merge branch 'main' into spiridonov-more-funcs
spiridonov de52aa3
Merge branch 'main' into spiridonov-more-funcs
spiridonov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| package executor | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "maps" | ||
| "slices" | ||
| "strings" | ||
|
|
@@ -12,14 +11,14 @@ import ( | |
| "github.com/apache/arrow-go/v18/arrow/memory" | ||
| "github.com/cespare/xxhash/v2" | ||
|
|
||
| "github.com/grafana/loki/v3/pkg/engine/internal/planner/physical" | ||
| "github.com/grafana/loki/v3/pkg/engine/internal/semconv" | ||
| "github.com/grafana/loki/v3/pkg/engine/internal/types" | ||
| ) | ||
|
|
||
| type groupState struct { | ||
| value float64 // aggregated value | ||
| labelValues []string // grouping label values | ||
| value float64 // aggregated value | ||
| count int64 // values counter | ||
| labels []arrow.Field // grouping labels | ||
| labelValues []string // grouping label values | ||
| } | ||
|
|
||
| type aggregationOperation int | ||
|
|
@@ -29,23 +28,23 @@ const ( | |
| aggregationOperationMax | ||
| aggregationOperationMin | ||
| aggregationOperationCount | ||
| aggregationOperationAvg | ||
| aggregationOperationStddev | ||
| aggregationOperationStdvar | ||
| aggregationOperationBytes | ||
| ) | ||
|
|
||
| // aggregator is used to aggregate sample values by a set of grouping keys for each point in time. | ||
| type aggregator struct { | ||
| groupBy []physical.ColumnExpression // columns to group by | ||
| points map[time.Time]map[uint64]*groupState // holds the groupState for each point in time series | ||
| digest *xxhash.Digest // used to compute key for each group | ||
| operation aggregationOperation // aggregation type | ||
| labels []arrow.Field // combined list of all label fields for all sample values | ||
| } | ||
|
|
||
| // newAggregator creates a new aggregator with the specified groupBy columns. | ||
| // empty groupBy indicates no grouping. All values are aggregated into a single group. | ||
| // TODO: add without argument to support `without(...)` grouping. | ||
| // A special case of `without()` that has empty groupBy is used for Noop grouping which retains the input labels as is. | ||
| func newAggregator(groupBy []physical.ColumnExpression, pointsSizeHint int, operation aggregationOperation) *aggregator { | ||
| // newAggregator creates a new aggregator with the specified grouping. | ||
| func newAggregator(pointsSizeHint int, operation aggregationOperation) *aggregator { | ||
| a := aggregator{ | ||
| groupBy: groupBy, | ||
| digest: xxhash.New(), | ||
| operation: operation, | ||
| } | ||
|
|
@@ -61,21 +60,35 @@ func newAggregator(groupBy []physical.ColumnExpression, pointsSizeHint int, oper | |
|
|
||
| // Add adds a new sample value to the aggregation for the given timestamp and grouping label values. | ||
| // It expects labelValues to be in the same order as the groupBy columns. | ||
| func (a *aggregator) Add(ts time.Time, value float64, labelValues []string) { | ||
| func (a *aggregator) Add(ts time.Time, value float64, labels []arrow.Field, labelValues []string) { | ||
| if len(labels) != len(labelValues) { | ||
| panic("len(labels) != len(labelValues)") | ||
| } | ||
|
|
||
| for _, label := range labels { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can be expensive as we run it for each row. can we check the benchmarks for a metric test with this change? |
||
| if !slices.ContainsFunc(a.labels, func(l arrow.Field) bool { | ||
| return label.Equal(l) | ||
| }) { | ||
| a.labels = append(a.labels, label) | ||
| } | ||
| } | ||
|
|
||
| point, ok := a.points[ts] | ||
| if !ok { | ||
| point = make(map[uint64]*groupState) | ||
| a.points[ts] = point | ||
| } | ||
|
|
||
| var key uint64 | ||
| if len(a.groupBy) != 0 { | ||
| if len(labelValues) != 0 { | ||
| a.digest.Reset() | ||
| for i, val := range labelValues { | ||
| if i > 0 { | ||
| _, _ = a.digest.Write([]byte{0}) // separator | ||
| } | ||
|
|
||
| _, _ = a.digest.WriteString(labels[i].Name) | ||
| _, _ = a.digest.Write([]byte("=")) | ||
| _, _ = a.digest.WriteString(val) | ||
| } | ||
| key = a.digest.Sum64() | ||
|
|
@@ -96,26 +109,23 @@ func (a *aggregator) Add(ts time.Time, value float64, labelValues []string) { | |
| if value < state.value { | ||
| state.value = value | ||
| } | ||
| case aggregationOperationCount: | ||
| state.value = state.value + 1 | ||
|
|
||
| case aggregationOperationAvg: | ||
| state.value += value | ||
| } | ||
|
|
||
| state.count++ | ||
| } else { | ||
| v := value | ||
| if a.operation == aggregationOperationCount { | ||
| v = 1 | ||
| } | ||
| count := int64(1) | ||
|
|
||
| if len(a.groupBy) == 0 { | ||
| if len(labels) == 0 { | ||
| // special case: All values aggregated into a single group. | ||
| // This applies to queries like `sum(...)`, `sum by () (...)`, `count_over_time by () (...)`. | ||
| point[key] = &groupState{ | ||
| value: v, | ||
| value: value, | ||
| count: count, | ||
| } | ||
| return | ||
| } | ||
|
|
||
| // create a new slice since labelValues is reused by the calling code | ||
| labelValuesCopy := make([]string, len(labelValues)) | ||
| for i, v := range labelValues { | ||
| // copy the value as this is backed by the arrow array data buffer. | ||
|
|
@@ -126,26 +136,27 @@ func (a *aggregator) Add(ts time.Time, value float64, labelValues []string) { | |
|
|
||
| // TODO: add limits on number of groups | ||
| point[key] = &groupState{ | ||
| labels: labels, | ||
| labelValues: labelValuesCopy, | ||
| value: v, | ||
| value: value, | ||
| count: count, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (a *aggregator) BuildRecord() (arrow.RecordBatch, error) { | ||
| fields := make([]arrow.Field, 0, len(a.groupBy)+2) | ||
| fields := make([]arrow.Field, 0, len(a.labels)+2) | ||
| fields = append(fields, | ||
| semconv.FieldFromIdent(semconv.ColumnIdentTimestamp, false), | ||
| semconv.FieldFromIdent(semconv.ColumnIdentValue, false), | ||
| ) | ||
|
|
||
| for _, column := range a.groupBy { | ||
| colExpr, ok := column.(*physical.ColumnExpr) | ||
| if !ok { | ||
| panic(fmt.Sprintf("invalid column expression type %T", column)) | ||
| } | ||
| ident := semconv.NewIdentifier(colExpr.Ref.Column, colExpr.Ref.Type, types.Loki.String) | ||
| fields = append(fields, semconv.FieldFromIdent(ident, true)) | ||
| for _, label := range a.labels { | ||
| fields = append(fields, arrow.Field{ | ||
| Name: label.Name, | ||
| Type: label.Type, | ||
| Nullable: true, | ||
| Metadata: label.Metadata, | ||
| }) | ||
| } | ||
|
|
||
| schema := arrow.NewSchema(fields, nil) | ||
|
|
@@ -156,16 +167,34 @@ func (a *aggregator) BuildRecord() (arrow.RecordBatch, error) { | |
| tsValue, _ := arrow.TimestampFromTime(ts, arrow.Nanosecond) | ||
|
|
||
| for _, entry := range a.points[ts] { | ||
| var value float64 | ||
| switch a.operation { | ||
| case aggregationOperationAvg: | ||
| value = entry.value / float64(entry.count) | ||
| case aggregationOperationCount: | ||
| value = float64(entry.count) | ||
| default: | ||
| value = entry.value | ||
| } | ||
|
|
||
| rb.Field(0).(*array.TimestampBuilder).Append(tsValue) | ||
| rb.Field(1).(*array.Float64Builder).Append(entry.value) | ||
| rb.Field(1).(*array.Float64Builder).Append(value) | ||
|
|
||
| for i, label := range a.labels { | ||
| builder := rb.Field(2 + i) // offset by 2 as the first 2 fields are timestamp and value | ||
|
|
||
| for col, val := range entry.labelValues { | ||
| builder := rb.Field(col + 2) // offset by 2 as the first 2 fields are timestamp and value | ||
| // TODO: differentiate between null and actual empty string | ||
| if val == "" { | ||
| j := slices.IndexFunc(entry.labels, func(l arrow.Field) bool { | ||
| return l.Name == label.Name | ||
| }) | ||
| if j == -1 { | ||
| builder.(*array.StringBuilder).AppendNull() | ||
| } else { | ||
| builder.(*array.StringBuilder).Append(val) | ||
| // TODO: differentiate between null and actual empty string | ||
| if entry.labelValues[j] == "" { | ||
| builder.(*array.StringBuilder).AppendNull() | ||
| } else { | ||
| builder.(*array.StringBuilder).Append(entry.labelValues[j]) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this added to handle error columns that are of type
Generated?