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: revamp PRQL codegen framework #2669

Merged
merged 8 commits into from
Jun 1, 2023
Merged

feat: revamp PRQL codegen framework #2669

merged 8 commits into from
Jun 1, 2023

Conversation

aljazerzen
Copy link
Member

Closes #1420

Comment on lines +307 to +323
pub fn write_ident_part(s: &str) -> String {
fn forbidden_start(c: char) -> bool {
!(c.is_ascii_lowercase() || matches!(c, '_' | '$'))
}
fn forbidden_subsequent(c: char) -> bool {
!(c.is_ascii_lowercase() || c.is_ascii_digit() || matches!(c, '_'))
}
let needs_escape = s.is_empty()
|| s.starts_with(forbidden_start)
|| (s.len() > 1 && s.chars().skip(1).any(forbidden_subsequent));

if needs_escape {
format!("`{s}`")
} else {
s.to_string()
}
}
Copy link
Member

Choose a reason for hiding this comment

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

GPT4 wrote this as a regex:

Suggested change
pub fn write_ident_part(s: &str) -> String {
fn forbidden_start(c: char) -> bool {
!(c.is_ascii_lowercase() || matches!(c, '_' | '$'))
}
fn forbidden_subsequent(c: char) -> bool {
!(c.is_ascii_lowercase() || c.is_ascii_digit() || matches!(c, '_'))
}
let needs_escape = s.is_empty()
|| s.starts_with(forbidden_start)
|| (s.len() > 1 && s.chars().skip(1).any(forbidden_subsequent));
if needs_escape {
format!("`{s}`")
} else {
s.to_string()
}
}
use regex::Regex;
use once_cell::sync::Lazy;
static IDENT_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[_$a-z][_a-z\d]*$").unwrap());
pub fn write_ident_part(s: &str) -> String {
if IDENT_REGEX.is_match(s) {
s.to_string()
} else {
format!("`{}`", s)
}
}

I actually though I did this before, but probably it was for something different...

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

I remembered where I did this before — is this the same as https://github.com/prql/prql/blob/aa3ed6196596e38b3dfffad63d0b590e9eeed535/prql-compiler/src/sql/gen_expr.rs#L783 ? (fine to have it twice unless it's definitely going to remain the same)

Copy link
Member Author

Choose a reason for hiding this comment

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

This is the problem with GPT: it looks likes it's correct, but it's not. The regex does not quote empty strings, but it should.

In anycase, this snippet was copied from the gen_expr.rs a while back and was living on codegen branch for last few months.

If you feel like it needs to be improved, go ahead. But add tests if you are using GPT.

Copy link
Member

Choose a reason for hiding this comment

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

Very good point!!

Yeah, agree it has that frailty. I have found it helpful for these sorts of "chore" tasks; agree tests become more important...

Copy link
Member

Choose a reason for hiding this comment

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

Though in this case is it actually wrong? An empty string does get quoted by the regex.

Copy link
Member Author

Choose a reason for hiding this comment

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

No, it's actually correct. An empty string does get quoted by the regex, which is correct. The logic has also been inverted.

But that's my point: it's more work validating the output as opposed to rewriting it. And things like this don't even need a rewrite or any more time spent on them.

Copy link
Member

Choose a reason for hiding this comment

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

Yes I agree re focus

Copy link
Member

Choose a reason for hiding this comment

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

(I had already put #2678 in — which I think does use the version that gets the * correct, but not 100% sure)

@max-sixty
Copy link
Member

Extremely impressive @aljazerzen !

@aljazerzen aljazerzen merged commit 7da5731 into main Jun 1, 2023
@aljazerzen aljazerzen deleted the codegen branch June 1, 2023 07:31
max-sixty added a commit to max-sixty/prql that referenced this pull request Jun 1, 2023
From PRQL#2669 (comment)

The behavior change is on handling `*`, which I _think_ is correct — at least it seems to compile OK.
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.

PRQL code generation & formatting
2 participants