Skip to content

Commit 4a960b9

Browse files
committed
Implement timestamp() for native histograms
Signed-off-by: 🌲 Harry 🌊 John 🏔 <[email protected]>
1 parent ad0ee3d commit 4a960b9

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

engine/engine_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,6 +2302,16 @@ or
23022302
end: time.UnixMilli(124000),
23032303
step: 15 * time.Second,
23042304
},
2305+
{
2306+
name: "native histogram timestamp",
2307+
load: `load 2m
2308+
http_request_duration_seconds{pod="nginx-1"} {{schema:0 count:3 sum:14.00 buckets:[1 2]}}+{{schema:0 count:4 buckets:[1 2 1]}}x20
2309+
http_request_duration_seconds{pod="nginx-2"} 1x10 {{schema:0 count:2 sum:14.00 buckets:[2]}}+{{schema:0 count:6 buckets:[2 2 2]}}x10`,
2310+
query: `timestamp(http_request_duration_seconds)`,
2311+
start: time.UnixMilli(0),
2312+
end: time.UnixMilli(300000),
2313+
step: 15 * time.Second,
2314+
},
23052315
}
23062316

23072317
disableOptimizerOpts := []bool{true, false}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
go test fuzz v1
2+
int64(111)
3+
uint32(0)
4+
uint32(151)
5+
uint32(3)
6+
int8(0)
7+
int8(0)
8+
uint64(15)
9+
uint64(0)
10+
uint64(106)

execution/function/operator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func NewFunctionOperator(funcExpr *logicalplan.FunctionCall, nextOps []model.Vec
2727
case "scalar":
2828
return newScalarOperator(model.NewVectorPoolWithSize(stepsBatch, 1), nextOps[0], opts), nil
2929
case "timestamp":
30-
return newTimestampOperator(nextOps[0], opts), nil
30+
return newTimestampOperator(model.NewVectorPool(stepsBatch), nextOps[0], opts), nil
3131
case "label_join", "label_replace":
3232
return newRelabelOperator(nextOps[0], funcExpr, opts), nil
3333
case "absent":

execution/function/timestamp.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ import (
1616
)
1717

1818
type timestampOperator struct {
19+
pool *model.VectorPool
1920
next model.VectorOperator
2021

2122
series []labels.Labels
2223
once sync.Once
2324
}
2425

25-
func newTimestampOperator(next model.VectorOperator, opts *query.Options) model.VectorOperator {
26+
func newTimestampOperator(pool *model.VectorPool, next model.VectorOperator, opts *query.Options) model.VectorOperator {
2627
oper := &timestampOperator{
28+
pool: pool,
2729
next: next,
2830
}
2931
return telemetry.NewOperator(telemetry.NewTelemetry(oper, opts), oper)
@@ -65,7 +67,7 @@ func (o *timestampOperator) loadSeries(ctx context.Context) error {
6567
}
6668

6769
func (o *timestampOperator) GetPool() *model.VectorPool {
68-
return o.next.GetPool()
70+
return o.pool
6971
}
7072

7173
func (o *timestampOperator) Next(ctx context.Context) ([]model.StepVector, error) {
@@ -76,13 +78,34 @@ func (o *timestampOperator) Next(ctx context.Context) ([]model.StepVector, error
7678
}
7779

7880
in, err := o.next.Next(ctx)
81+
7982
if err != nil {
8083
return nil, err
8184
}
85+
if len(in) == 0 {
86+
return nil, nil
87+
}
88+
result := o.pool.GetVectorBatch()
8289
for _, vector := range in {
83-
for i := range vector.Samples {
84-
vector.Samples[i] = float64(vector.T / 1000)
90+
out := o.pool.GetStepVector(vector.T)
91+
value := float64(vector.T / 1000)
92+
93+
sampleTimestamps := make([]float64, len(vector.Samples))
94+
for i := range sampleTimestamps {
95+
sampleTimestamps[i] = value
8596
}
97+
out.AppendSamples(o.pool, vector.SampleIDs, sampleTimestamps)
98+
99+
histogramTimestamps := make([]float64, len(vector.Histograms))
100+
for i := range histogramTimestamps {
101+
histogramTimestamps[i] = value
102+
}
103+
out.AppendSamples(o.pool, vector.HistogramIDs, histogramTimestamps)
104+
105+
result = append(result, out)
106+
o.next.GetPool().PutStepVector(vector)
86107
}
87-
return in, nil
108+
109+
o.next.GetPool().PutVectors(in)
110+
return result, nil
88111
}

0 commit comments

Comments
 (0)