Skip to content
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

fix: Return empty vector instead of nil for empty evaluator. #13485

Merged
merged 5 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pkg/logql/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,10 @@ func (q *query) evalSample(ctx context.Context, expr syntax.SampleExpr) (promql_
return nil, fmt.Errorf("unsupported result type: %T", r)
}
}
return nil, nil
return promql.Vector{}, nil
jeschkies marked this conversation as resolved.
Show resolved Hide resolved
}

func (q *query) JoinSampleVector(next bool, r StepResult, stepEvaluator StepEvaluator, maxSeries int) (promql_parser.Value, error) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's my formatted ^^

seriesIndex := map[uint64]*promql.Series{}

vec := promql.Vector{}
Expand Down
49 changes: 39 additions & 10 deletions pkg/logql/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,17 @@ func TestEngine_LogsRateUnwrap(t *testing.T) {
{newSeries(testSize, offset(46, constantValue(1)), `{app="foo"}`)},
},
[]SelectSampleParams{
{&logproto.SampleQueryRequest{
Start: time.Unix(30, 0),
End: time.Unix(60, 0),
Selector: `rate({app="foo"} | unwrap foo[30s])`,
Plan: &plan.QueryPlan{
AST: syntax.MustParseExpr(`rate({app="foo"} | unwrap foo[30s])`),
{
&logproto.SampleQueryRequest{
Start: time.Unix(30, 0),
End: time.Unix(60, 0),
Selector: `rate({app="foo"} | unwrap foo[30s])`,
Plan: &plan.QueryPlan{
AST: syntax.MustParseExpr(`rate({app="foo"} | unwrap foo[30s])`),
},
},
},
}},
},
// there are 15 samples (from 47 to 61) matched from the generated series
// SUM(n=47, 61, 1) = 15
// 15 / 30 = 0.5
Expand Down Expand Up @@ -955,7 +957,6 @@ func TestEngine_InstantQuery(t *testing.T) {
} {
test := test
t.Run(fmt.Sprintf("%s %s", test.qs, test.direction), func(t *testing.T) {

eng := NewEngine(EngineOpts{}, newQuerierRecorder(t, test.data, test.params), NoLimits, log.NewNopLogger())

params, err := NewLiteralParams(test.qs, test.ts, test.ts, 0, 0, test.direction, test.limit, nil, nil)
Expand Down Expand Up @@ -1590,10 +1591,12 @@ func TestEngine_RangeQuery(t *testing.T) {
promql.Series{
// vector result
Metric: labels.Labels(nil),
Floats: []promql.FPoint{{T: 60000, F: 0}, {T: 80000, F: 0}, {T: 100000, F: 0}, {T: 120000, F: 0}, {T: 140000, F: 0}, {T: 160000, F: 0}, {T: 180000, F: 0}}},
Floats: []promql.FPoint{{T: 60000, F: 0}, {T: 80000, F: 0}, {T: 100000, F: 0}, {T: 120000, F: 0}, {T: 140000, F: 0}, {T: 160000, F: 0}, {T: 180000, F: 0}},
},
promql.Series{
Metric: labels.FromStrings("app", "foo"),
Floats: []promql.FPoint{{T: 60000, F: 0.03333333333333333}, {T: 80000, F: 0.06666666666666667}, {T: 100000, F: 0.06666666666666667}, {T: 120000, F: 0.03333333333333333}, {T: 180000, F: 0.03333333333333333}}},
Floats: []promql.FPoint{{T: 60000, F: 0.03333333333333333}, {T: 80000, F: 0.06666666666666667}, {T: 100000, F: 0.06666666666666667}, {T: 120000, F: 0.03333333333333333}, {T: 180000, F: 0.03333333333333333}},
},
},
},
{
Expand Down Expand Up @@ -2635,6 +2638,32 @@ func TestHashingStability(t *testing.T) {
}
}

func TestEmptyResults(t *testing.T) {
ctx := user.InjectOrgID(context.Background(), "fake")

mock := &mockEvaluatorFactory{SampleEvaluatorFunc(func(context.Context, SampleEvaluatorFactory, syntax.SampleExpr, Params) (StepEvaluator, error) {
return EmptyEvaluator[SampleVector]{value: nil}, nil
})}

eng := NewEngine(EngineOpts{}, nil, NoLimits, log.NewNopLogger())
params, err := NewLiteralParams(`first_over_time({a=~".+"} | logfmt | unwrap value [1s])`, time.Now(), time.Now(), 0, 0, logproto.BACKWARD, 0, nil, nil)
require.NoError(t, err)
q := eng.Query(params).(*query)
q.evaluator = mock

r, err := q.Exec(ctx)
require.NoError(t, err)
require.NotNil(t, r.Data)
}

type mockEvaluatorFactory struct {
SampleEvaluatorFactory
}

func (*mockEvaluatorFactory) NewIterator(context.Context, syntax.LogSelectorExpr, Params) (iter.EntryIterator, error) {
return nil, errors.New("unimplemented mock EntryEvaluatorFactory")
}

func getLocalQuerier(size int64) Querier {
return &querierRecorder{
series: map[string][]logproto.Series{
Expand Down
Loading