Skip to content

Commit 1821806

Browse files
chaudumpull[bot]
authored andcommitted
Fix parsing of vector expression (grafana#8448)
Signed-off-by: Christian Haudum <[email protected]>
1 parent 0da17c2 commit 1821806

File tree

5 files changed

+31
-15
lines changed

5 files changed

+31
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* [8120](https://github.com/grafana/loki/pull/8120) **ashwanthgoli** fix panic on hitting /scheduler/ring when ring is disabled.
4242
* [8251](https://github.com/grafana/loki/pull/8251) **sandeepsukhani** index-store: fix indexing of chunks overlapping multiple schemas.
4343
* [8151](https://github.com/grafana/loki/pull/8151) **sandeepsukhani** fix log deletion with line filters.
44+
* [8448](https://github.com/grafana/loki/pull/8448) **chaudum**: Fix bug in LogQL parser that caused certain queries that contain a vector expression to fail.
4445

4546
##### Changes
4647

pkg/logql/syntax/ast.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1755,7 +1755,7 @@ func (e *VectorExpr) Value() (float64, error) {
17551755

17561756
func (e *VectorExpr) Selector() (LogSelectorExpr, error) { return e, e.err }
17571757
func (e *VectorExpr) HasFilter() bool { return false }
1758-
func (e *VectorExpr) Shardable() bool { return true }
1758+
func (e *VectorExpr) Shardable() bool { return false }
17591759
func (e *VectorExpr) Walk(f WalkFn) { f(e) }
17601760
func (e *VectorExpr) Pipeline() (log.Pipeline, error) { return log.NewNoopPipeline(), nil }
17611761
func (e *VectorExpr) Matchers() []*labels.Matcher { return nil }

pkg/logql/syntax/parser.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,12 @@ func ParseExprWithoutValidation(input string) (expr Expr, err error) {
103103
func validateExpr(expr Expr) error {
104104
switch e := expr.(type) {
105105
case SampleExpr:
106-
err := validateSampleExpr(e)
107-
if err != nil {
108-
return err
109-
}
106+
return validateSampleExpr(e)
110107
case LogSelectorExpr:
111-
err := validateMatchers(e.Matchers())
112-
if err != nil {
113-
return err
114-
}
108+
return validateLogSelectorExpression(e)
115109
default:
116110
return logqlmodel.NewParseError(fmt.Sprintf("unexpected expression type: %v", e), 0, 0)
117111
}
118-
return nil
119112
}
120113

121114
// validateMatchers checks whether a query would touch all the streams in the query range or uses at least one matcher to select specific streams.
@@ -169,7 +162,6 @@ func validateSampleExpr(expr SampleExpr) error {
169162
return err
170163
}
171164
return validateSampleExpr(e.RHS)
172-
173165
case *LiteralExpr:
174166
if e.err != nil {
175167
return e.err
@@ -195,7 +187,16 @@ func validateSampleExpr(expr SampleExpr) error {
195187
if err != nil {
196188
return err
197189
}
198-
return validateMatchers(selector.Matchers())
190+
return validateLogSelectorExpression(selector)
191+
}
192+
}
193+
194+
func validateLogSelectorExpression(expr LogSelectorExpr) error {
195+
switch e := expr.(type) {
196+
case *VectorExpr:
197+
return nil
198+
default:
199+
return validateMatchers(e.Matchers())
199200
}
200201
}
201202

pkg/logql/syntax/parser_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2867,6 +2867,23 @@ func TestParse(t *testing.T) {
28672867
in: `vector(abc)`,
28682868
err: logqlmodel.NewParseError("syntax error: unexpected IDENTIFIER, expecting NUMBER", 1, 8),
28692869
},
2870+
{
2871+
in: `vector(1)`,
2872+
exp: &VectorExpr{Val: 1, err: nil},
2873+
},
2874+
{
2875+
in: `label_replace(vector(0), "foo", "bar", "", "")`,
2876+
exp: mustNewLabelReplaceExpr(&VectorExpr{Val: 0, err: nil}, "foo", "bar", "", ""),
2877+
},
2878+
{
2879+
in: `sum(vector(0))`,
2880+
exp: &VectorAggregationExpr{
2881+
Left: &VectorExpr{Val: 0, err: nil},
2882+
Grouping: &Grouping{},
2883+
Params: 0,
2884+
Operation: "sum",
2885+
},
2886+
},
28702887
{
28712888
in: `{app="foo"}
28722889
# |= "bar"

pkg/querier/queryrange/querysharding.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,6 @@ func (ast *astMapperware) Do(ctx context.Context, r queryrangebase.Request) (que
129129
}
130130

131131
mapper := logql.NewShardMapper(resolver, ast.metrics)
132-
if err != nil {
133-
return nil, err
134-
}
135132

136133
noop, parsed, err := mapper.Parse(r.GetQuery())
137134
if err != nil {

0 commit comments

Comments
 (0)