Skip to content

Commit

Permalink
PromQL engine update
Browse files Browse the repository at this point in the history
Expected either string or int32 tag value, prefer a string
  • Loading branch information
alpinskiy committed Nov 25, 2024
1 parent 70954a2 commit 83fb499
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 34 deletions.
38 changes: 8 additions & 30 deletions internal/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2847,14 +2847,15 @@ func (c *pointsSelectCols) rowAt(i int) tsSelectRow {
row.val[j] = c.val[j][i]
}
for j := range c.tag {
row.tag[c.tagIx[j]] = c.tag[j][i]
}
for j := range c.stag {
st := c.stag[j]
if st.Buf == nil || len(st.Pos) < i || len(st.Buf) < st.Pos[i].End {
continue
var hasSTag bool
if len(c.stag) != 0 {
hasSTag = c.stag[j].Pos[i].Start < c.stag[j].Pos[i].End && c.stag[j].Pos[i].End <= len(c.stag[j].Buf)
}
if hasSTag {
row.stag[c.tagIx[j]] = string(c.stag[j].Buf[c.stag[j].Pos[i].Start:c.stag[j].Pos[i].End])
} else {
row.tag[c.tagIx[j]] = c.tag[j][i]
}
row.stag[c.tagIx[j]] = string(st.Buf[st.Pos[i].Start:st.Pos[i].End])
}
if c.tagStr.Pos != nil && i < len(c.tagStr.Pos) {
copy(row.tagStr[:], c.tagStr.Buf[c.tagStr.Pos[i].Start:c.tagStr.Pos[i].End])
Expand Down Expand Up @@ -2964,14 +2965,7 @@ func loadPoints(ctx context.Context, h *requestHandler, pq *pointsQuery, lod dat
metric := pq.metricID
table := lod.Table
kind := pq.kind
var metricName string
if m, ok := format.BuiltinMetrics[pq.metricID]; ok {
metricName = m.Name
} else if m := h.metricsStorage.GetMetaMetric(pq.metricID); m != nil {
metricName = m.Name
}
start := time.Now()
mappings := make(map[string]int32)
err = h.doSelect(ctx, util.QueryMetaInto{
IsFast: isFast,
IsLight: isLight,
Expand All @@ -2990,22 +2984,6 @@ func loadPoints(ctx context.Context, h *requestHandler, pq *pointsQuery, lod dat
replaceInfNan(&cols.val[j][i])
}
row := cols.rowAt(i)
if metricName != "" { // should be always true
for k := range row.stag {
// check if tag value is mapped
if v, ok := mappings[row.stag[k]]; ok && v != 0 {
row.tag[k] = v
row.stag[k] = ""
}
// call GetTagMapping only first time for each tag value, since it's expensive
v, _, _, _ := h.metadataLoader.GetTagMapping(ctx, row.stag[k], metricName, false)
mappings[row.stag[k]] = v
if v != 0 {
row.tag[k] = v
row.stag[k] = ""
}
}
}
ix, err := lod.IndexOf(row.time)
if err != nil {
return err
Expand Down
7 changes: 5 additions & 2 deletions internal/api/promql.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,9 @@ func (h *requestHandler) QuerySeries(ctx context.Context, qry *promql.SeriesQuer
x, ok := tagX[data[i][j].tsTags]
if !ok {
x = len(res.Data)
if x > maxSeriesRows {
return promql.Series{}, nil, errTooManyRows
}
tagX[data[i][j].tsTags] = x
for _, fn := range what {
v := h.Alloc(len(qry.Timescale.Time))
Expand Down Expand Up @@ -619,8 +622,8 @@ func (h *requestHandler) QuerySeries(ctx context.Context, qry *promql.SeriesQuer
Name: m.Name,
Value: v.tag[m.Index],
}
if qry.Options.Version == Version3 && m.Index < len(v.stag) {
st.SValue = v.stag[m.Index]
if qry.Options.Version == Version3 && m.Index < len(v.stag) && v.stag[m.Index] != "" {
st.SetSValue(v.stag[m.Index])
}
res.AddTagAt(i+j, st)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/promql/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,10 @@ func (tg *SeriesTag) stringify(ev *evaluator) {
})
}
}
tg.setSValue(v)
tg.SetSValue(v)
}

func (tg *SeriesTag) setSValue(v string) {
func (tg *SeriesTag) SetSValue(v string) {
tg.SValue = v
tg.stringified = true
}
Expand Down

0 comments on commit 83fb499

Please sign in to comment.