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

feat: vector selector yacc rule #7

Merged
merged 12 commits into from
Dec 13, 2022
Merged

Conversation

yuanbohan
Copy link
Contributor

@yuanbohan yuanbohan commented Dec 11, 2022

What's Included

Sorry but this is a huge PR, the whole purpose of this PR is to show how to use grmtools and *.y file to generate PromQL parser.

  • This PR just focused on Parser part, and the Lexer is not implemented yet
  • Only NumberLiteral, StringLiteral, VectorSelector yacc rules are implemented
  • the promql.y is only part of promql's generated_parser.y so far, there is much work left to be done

Example

cargo run --example parser

// node_cpu_seconds_total{cpu=0,mode=idle}
AST: VectorSelector { name: Some("node_cpu_seconds_total"), offset: None, start_or_end: None, label_matchers: Matchers { matchers: [Matcher { op: Equal, name: "cpu", value: "0" }, Matcher { op: Equal, name: "mode", value: "idle" }, Matcher { op: Equal, name: "__name__", value: "node_cpu_seconds_total" }] } }

What's to do next

  • Lexer
  • the left parts of yacc generated Parser

@yuanbohan yuanbohan force-pushed the vector-selector-yacc-rule branch from 6c121dc to 56741ff Compare December 11, 2022 14:48
@yuanbohan yuanbohan requested a review from waynexia December 11, 2022 14:48
@yuanbohan yuanbohan force-pushed the vector-selector-yacc-rule branch from 56741ff to e2d9913 Compare December 11, 2022 15:05
Copy link
Member

@waynexia waynexia left a comment

Choose a reason for hiding this comment

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

Splendid! I look roughly at the .y part, which generally looks good to me. And left a few comments in other parts.

src/parser/value.rs Outdated Show resolved Hide resolved
src/parser/production.rs Outdated Show resolved Hide resolved
src/util/duration.rs Outdated Show resolved Hide resolved
src/parser/ast.rs Outdated Show resolved Hide resolved
src/parser/ast.rs Outdated Show resolved Hide resolved
src/parser/value.rs Outdated Show resolved Hide resolved
@yuanbohan yuanbohan force-pushed the vector-selector-yacc-rule branch 3 times, most recently from c51db71 to 5c66a88 Compare December 12, 2022 07:34
Copy link
Member

@waynexia waynexia left a comment

Choose a reason for hiding this comment

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

LGTM 👍

"1", "1y1m1d", "-1w", "1.5d", "d",
"",
// these are invalid in PromQL Go Version
// "294y", "200y10400w", "107675d", "2584200h",
Copy link
Member

Choose a reason for hiding this comment

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

We can un-comment this in another test, and assert they are valid in our version I suspose

Comment on lines 43 to 44
let v = cap.as_str().parse::<u64>().unwrap();
result = result + Duration::from_millis(v * millis);
Copy link
Member

Choose a reason for hiding this comment

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

If want to be corrected with any input, I suggest to use checked_add for result + Duration and checked_mul for v * millis. Because if it will overflow, it overflows here. So I'm afraid the check below (if result > Duration::MAX) is logically unreachable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, It's idiomatic Rust way

MatchType::MatchNotEqual => self.value.ne(s),
MatchType::MatchRegexp => todo!(),
MatchType::MatchNotRegexp => todo!(),
pub fn new_matcher(token: Token, name: String, value: String) -> Result<Matcher, String> {
Copy link
Contributor

Choose a reason for hiding this comment

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

We may need to define an Error type for the parser instead of using String.

Copy link
Member

@waynexia waynexia Dec 12, 2022

Choose a reason for hiding this comment

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

We can file a todo issue for this. I think we can leave them as are for now (i.e. keep using String). Parser error is a bit complex problem because it's part of an important user-facing UI, and we need to consider how to make it accurate and understandable.

T_EQL => Ok(Matcher::new(MatchOp::Equal, name, value)),
T_NEQ => Ok(Matcher::new(MatchOp::NotEqual, name, value)),
T_EQL_REGEX => {
let re = Regex::new(&value).map_err(|_| format!("illegal regex for {}", &value))?;
Copy link
Contributor

Choose a reason for hiding this comment

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

Also display the source error in the error message? It might contains some helpful message.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The error mechanism will be carefully re-designed in the future in Issue #9

]
.into_iter()
// map captured string to Option<Duration> iterator
// FIXME: None is ignored in closure. It is better to tell users which part is wrong.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I leave a FIXME here to resolve the issue in the future.

@yuanbohan yuanbohan force-pushed the vector-selector-yacc-rule branch 2 times, most recently from 8f1ac97 to 388d4b9 Compare December 12, 2022 12:48
@yuanbohan
Copy link
Contributor Author

I almost redo duration.rs. Any review comment is welcome.

@yuanbohan yuanbohan force-pushed the vector-selector-yacc-rule branch from 388d4b9 to 13c1e37 Compare December 12, 2022 14:58
Copy link

@killme2008 killme2008 left a comment

Choose a reason for hiding this comment

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

LGTM

@killme2008 killme2008 merged commit 71d8a90 into develop Dec 13, 2022
@killme2008 killme2008 deleted the vector-selector-yacc-rule branch December 13, 2022 02:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants