promql-parser icon indicating copy to clipboard operation
promql-parser copied to clipboard

feat: Support Filtering by multiple or filters

Open groobyming opened this issue 1 year ago • 6 comments

@yuanbohan Hi Yuanbo, I want to support or filter but even if I add something in promql.y I get the error "invalid promql query", I am new to yacc and I have used antlr before (anltr is relatively It's relatively simple, just modify the grammar definition, but yacc doesn't seem to work), so I hope you can give me some suggestions.

【victoriametrics or expression】 image https://docs.victoriametrics.com/keyconcepts/#filtering-by-multiple-or-filters

【promql.y】

label_match_list -> Result<Matchers, String>:
                label_match_list COMMA label_matcher { Ok($1?.append($3?)) }
        |       label_match_list LOR label_matcher { Ok($1?.append($3?)) }  // support or filter
        |       label_matcher { Ok(Matchers::empty().append($1?)) }
;

【error】 expression: a{label1="1" or label2="2"}

 Err("invalid promql query")

groobyming avatar May 13 '24 03:05 groobyming

MetricsQL is an enhancement option, and our team has discussed it. But we have no plan to totally implement the extension, and have no milestone for it.

This issue can be kept here, and we will resolve it

yuanbohan avatar May 13 '24 03:05 yuanbohan

@yuanbohan Thanks! Looking forward to the progress

groobyming avatar May 13 '24 03:05 groobyming

I have not tested yet, but I think it should be placed in label_matchers production

label_matchers -> Result<Matchers, String>:
                LEFT_BRACE label_match_list LOR label_match_list RIGHT_BRACE { $2 }
        |       LEFT_BRACE label_match_list RIGHT_BRACE { $2 }
        |       LEFT_BRACE label_match_list COMMA RIGHT_BRACE { $2 }
        |       LEFT_BRACE RIGHT_BRACE { Ok(Matchers::empty()) }
        |       LEFT_BRACE COMMA RIGHT_BRACE
                { Err("unexpected ',' in label matching, expected identifier or right_brace".into()) }
;

and maybe there SHOULD BE some special logic in inside_braces for the LOR, maybe it is tokenized as identifier instead of the LOR token

yuanbohan avatar May 13 '24 07:05 yuanbohan

I have not tested yet, but I think it should be placed in label_matchers production

label_matchers -> Result<Matchers, String>:
                LEFT_BRACE label_match_list LOR label_match_list RIGHT_BRACE { $2 }
        |       LEFT_BRACE label_match_list RIGHT_BRACE { $2 }
        |       LEFT_BRACE label_match_list COMMA RIGHT_BRACE { $2 }
        |       LEFT_BRACE RIGHT_BRACE { Ok(Matchers::empty()) }
        |       LEFT_BRACE COMMA RIGHT_BRACE
                { Err("unexpected ',' in label matching, expected identifier or right_brace".into()) }
;

and maybe there SHOULD BE some special logic in inside_braces for the LOR, maybe it is tokenized as identifier instead of the LOR token

@yuanbohan Thanks, I'll try to modify it and test it

groobyming avatar May 13 '24 07:05 groobyming

@groobyming If you're trying this way as I mentioned above, you should notice that it's not exactly tested.

LEFT_BRACE label_match_list LOR label_match_list RIGHT_BRACE { $2 }

NOTE the tail part, the { $2 } is wrongly typed, and you MUST change it. And the following part MUST BE considered carefully:

  • lexer: treat the LOR as not identifier
  • ast: the VectorSelector struct may not match this feature. Maybe the Matchers struct has to extend the vector of Matcher to support the OR feature

yuanbohan avatar May 13 '24 09:05 yuanbohan

If you do have time and interest to commit PR to extend MetricsQL, we can discuss it in details, and make a plan before your work.

Thanks for your passion and time for open-source promql-parser.

yuanbohan avatar May 13 '24 09:05 yuanbohan

fixed by #84

yuanbohan avatar May 20 '24 04:05 yuanbohan