Skip to content

Commit 07faba7

Browse files
committed
frostdb: Simplify queries to be more compact
1 parent 2608fce commit 07faba7

File tree

3 files changed

+27
-56
lines changed

3 files changed

+27
-56
lines changed

frostdb/frostdb.go

Lines changed: 24 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -70,33 +70,15 @@ func (f *FrostDB) Query() error {
7070
}
7171

7272
func (f *FrostQuerier) LabelValues(name string, matchers ...*labels.Matcher) ([]string, storage.Warnings, error) {
73-
exprs := []logicalplan.Expr{}
74-
// Build a filter from matchers
75-
for _, matcher := range matchers {
76-
switch matcher.Type {
77-
case labels.MatchEqual:
78-
exprs = append(exprs, logicalplan.Col("labels."+matcher.Name).Eq(logicalplan.Literal(matcher.Value)))
79-
case labels.MatchNotEqual:
80-
exprs = append(exprs, logicalplan.Col("labels."+matcher.Name).NotEq(logicalplan.Literal(matcher.Value)))
81-
case labels.MatchRegexp:
82-
exprs = append(exprs, logicalplan.Col("labels."+matcher.Name).RegexMatch(matcher.Value))
83-
case labels.MatchNotRegexp:
84-
exprs = append(exprs, logicalplan.Col("labels."+matcher.Name).RegexNotMatch(matcher.Value))
85-
}
86-
}
87-
8873
engine := query.NewEngine(
8974
memory.NewGoAllocator(),
9075
f.db.TableProvider(),
9176
)
9277

93-
bld := engine.ScanTable("metrics")
94-
if len(exprs) != 0 {
95-
bld.Filter(logicalplan.And(exprs...))
96-
}
97-
9878
sets := map[uint64]*series{}
99-
err := bld.Distinct(logicalplan.Col("labels."+name)).
79+
err := engine.ScanTable("metrics").
80+
Filter(promMatchersToFrostDBExprs(matchers)).
81+
Distinct(logicalplan.Col("labels."+name)).
10082
Execute(context.Background(), func(ar arrow.Record) error {
10183
defer ar.Release()
10284
parseRecordIntoSeriesSet(ar, sets)
@@ -117,9 +99,8 @@ func (f *FrostQuerier) LabelValues(name string, matchers ...*labels.Matcher) ([]
11799
return names, nil, nil
118100
}
119101

120-
func (f *FrostQuerier) LabelNames(matchers ...*labels.Matcher) ([]string, storage.Warnings, error) {
102+
func promMatchersToFrostDBExprs(matchers []*labels.Matcher) logicalplan.Expr {
121103
exprs := []logicalplan.Expr{}
122-
// Build a filter from matchers
123104
for _, matcher := range matchers {
124105
switch matcher.Type {
125106
case labels.MatchEqual:
@@ -132,25 +113,26 @@ func (f *FrostQuerier) LabelNames(matchers ...*labels.Matcher) ([]string, storag
132113
exprs = append(exprs, logicalplan.Col("labels."+matcher.Name).RegexNotMatch(matcher.Value))
133114
}
134115
}
116+
return logicalplan.And(exprs...)
117+
}
135118

119+
func (f *FrostQuerier) LabelNames(matchers ...*labels.Matcher) ([]string, storage.Warnings, error) {
136120
engine := query.NewEngine(
137121
memory.NewGoAllocator(),
138122
f.db.TableProvider(),
139123
)
140124

141-
bld := engine.ScanSchema("metrics")
142-
if len(exprs) != 0 {
143-
bld.Filter(logicalplan.And(exprs...))
144-
}
145-
146125
sets := map[string]struct{}{}
147-
err := bld.Project(logicalplan.DynCol("labels")).Execute(context.Background(), func(ar arrow.Record) error {
148-
defer ar.Release()
149-
for i := 0; i < int(ar.NumCols()); i++ {
150-
sets[ar.ColumnName(i)] = struct{}{}
151-
}
152-
return nil
153-
})
126+
err := engine.ScanSchema("metrics").
127+
Distinct(logicalplan.Col("labels")).
128+
Filter(promMatchersToFrostDBExprs(matchers)).
129+
Execute(context.Background(), func(ar arrow.Record) error {
130+
defer ar.Release()
131+
for i := 0; i < int(ar.NumCols()); i++ {
132+
sets[ar.ColumnName(i)] = struct{}{}
133+
}
134+
return nil
135+
})
154136
if err != nil {
155137
return nil, nil, fmt.Errorf(" failed to perform labels query: %v", err)
156138
}
@@ -166,33 +148,20 @@ func (f *FrostQuerier) LabelNames(matchers ...*labels.Matcher) ([]string, storag
166148
func (f *FrostQuerier) Close() error { return nil }
167149

168150
func (f *FrostQuerier) Select(sortSeries bool, hints *storage.SelectHints, matchers ...*labels.Matcher) storage.SeriesSet {
169-
170-
exprs := []logicalplan.Expr{
171-
logicalplan.Col("timestamp").GT(logicalplan.Literal(hints.Start)),
172-
logicalplan.Col("timestamp").LT(logicalplan.Literal(hints.End)),
173-
}
174-
// Build a filter from matchers
175-
for _, matcher := range matchers {
176-
switch matcher.Type {
177-
case labels.MatchEqual:
178-
exprs = append(exprs, logicalplan.Col("labels."+matcher.Name).Eq(logicalplan.Literal(matcher.Value)))
179-
case labels.MatchNotEqual:
180-
exprs = append(exprs, logicalplan.Col("labels."+matcher.Name).NotEq(logicalplan.Literal(matcher.Value)))
181-
case labels.MatchRegexp:
182-
exprs = append(exprs, logicalplan.Col("labels."+matcher.Name).RegexMatch(matcher.Value))
183-
case labels.MatchNotRegexp:
184-
exprs = append(exprs, logicalplan.Col("labels."+matcher.Name).RegexNotMatch(matcher.Value))
185-
}
186-
}
187-
188151
engine := query.NewEngine(
189152
memory.NewGoAllocator(),
190153
f.db.TableProvider(),
191154
)
192155

193156
sets := map[uint64]*series{}
194157
err := engine.ScanTable("metrics").
195-
Filter(logicalplan.And(exprs...)).
158+
Filter(logicalplan.And(
159+
logicalplan.And(
160+
logicalplan.Col("timestamp").GT(logicalplan.Literal(hints.Start)),
161+
logicalplan.Col("timestamp").LT(logicalplan.Literal(hints.End)),
162+
),
163+
promMatchersToFrostDBExprs(matchers),
164+
)).
196165
Project(
197166
logicalplan.DynCol("labels"),
198167
logicalplan.Col("timestamp"),

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ require (
3939
github.com/oklog/run v1.1.0
4040
github.com/oklog/ulid v1.3.1
4141
github.com/pkg/errors v0.9.1
42-
github.com/polarsignals/frostdb v0.0.0-20220715180255-c1e327da7c95
42+
github.com/polarsignals/frostdb v0.0.0-20220719135628-f77c95f65cf0
4343
github.com/prometheus/alertmanager v0.24.0
4444
github.com/prometheus/client_golang v1.12.2
4545
github.com/prometheus/client_model v0.2.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
826826
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
827827
github.com/polarsignals/frostdb v0.0.0-20220715180255-c1e327da7c95 h1:3k4P+NJO+aQ2ts5xZndcxILfWKNOIDxwURFkQnFCajY=
828828
github.com/polarsignals/frostdb v0.0.0-20220715180255-c1e327da7c95/go.mod h1:kpDBAMwctIMm6K3mq5SIwPG8F06cIKpaREKC5coFHmo=
829+
github.com/polarsignals/frostdb v0.0.0-20220719135628-f77c95f65cf0 h1:QojL/1GYJ8ZMiISYkkszAUr+LHqvA1C4yDme4ZxauCo=
830+
github.com/polarsignals/frostdb v0.0.0-20220719135628-f77c95f65cf0/go.mod h1:owFfTjGuDhCbOBn/0qpGOvPSMTc3NtrbMN557Rw9DP8=
829831
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
830832
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
831833
github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=

0 commit comments

Comments
 (0)