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(pg-wire): fix the simple extended query mode #3104

Merged
merged 3 commits into from
Jun 10, 2022

Conversation

ZENOTME
Copy link
Contributor

@ZENOTME ZENOTME commented Jun 9, 2022

What's changed and what's your intention?

fix the simple extended query mode

  • add parse_params() to parse different param according to the type
    For the simple extended query mode, it just simply replaces the params. This causes problems when I test in local e2e mode.
    For example:
    SELECT $1 => SELECT Hello,World ❌ (Expect: SELECT 'Hello,World' ✔)
    Then I realized that we couldn't simply replace params, different params need different parse. Such as VARCHAR params need to add (') on both sides of it. So I add the parse_params() to parse the params and now it only supports the VARCHAR type. We can expand another kind in future.

Checklist

  • I have written necessary docs and comments
  • I have added necessary unit tests and integration tests
  • All checks passed in ./risedev check (or alias, ./risedev c)

Refer to a related PR or issue link (optional)

#2924

@ZENOTME ZENOTME changed the title Zj/simple extended query fix fix(pg-wire): fix the simple extended query mode Jun 9, 2022
@github-actions github-actions bot added type/fix Bug fix and removed Invalid PR Title labels Jun 9, 2022
@codecov
Copy link

codecov bot commented Jun 9, 2022

Codecov Report

Merging #3104 (4b900ec) into main (b23ce00) will decrease coverage by 0.00%.
The diff coverage is 82.22%.

@@            Coverage Diff             @@
##             main    #3104      +/-   ##
==========================================
- Coverage   73.64%   73.64%   -0.01%     
==========================================
  Files         736      736              
  Lines      101570   101606      +36     
==========================================
+ Hits        74805    74825      +20     
- Misses      26765    26781      +16     
Flag Coverage Δ
rust 73.64% <82.22%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/utils/pgwire/src/pg_protocol.rs 76.15% <ø> (-0.08%) ⬇️
src/utils/pgwire/src/pg_extended.rs 87.13% <80.72%> (-8.58%) ⬇️
src/utils/pgwire/src/pg_server.rs 88.60% <100.00%> (+0.36%) ⬆️

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

Copy link
Contributor

@BowenXiao1999 BowenXiao1999 left a comment

Choose a reason for hiding this comment

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

Describe more clearly about what issues solved?

@ZENOTME
Copy link
Contributor Author

ZENOTME commented Jun 9, 2022

Describe more clearly about what issues solved?

Sorry. I have already updated.

Copy link
Contributor

@BowenXiao1999 BowenXiao1999 left a comment

Choose a reason for hiding this comment

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

Rest LGTM

Comment on lines +166 to +175
.map(|x| {
Some(
x.trim_start_matches('\'')
.trim_end_matches('\'')
.to_string(),
)
})
Copy link
Contributor

Choose a reason for hiding this comment

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

Add some comments here?

let mut tmp = query_string;
tmp.push(' ');
for &i in generic_params.iter() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Use meaningful name like for param in generic_params. And I think .iter() is redudant.

@@ -20,19 +20,47 @@ use regex::Regex;
use crate::pg_field_descriptor::{PgFieldDescriptor, TypeOid};
use crate::pg_protocol::cstr_to_str;

fn replace_params(query_string: String, generic_params: &[usize], params: &[Bytes]) -> String {
fn replace_params(query_string: String, generic_params: &[usize], params: &[String]) -> String {
Copy link
Contributor

Choose a reason for hiding this comment

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

You can add PR description as function doc.

tmp = pattern
.replace_all(&tmp, format!("{}$y", param))
.to_string();
}

tmp.pop();
Copy link
Contributor

Choose a reason for hiding this comment

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

Why here extra pop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the line 25, we have tmp.push(' ').

This ' ' helps us to handle the specific case, such as:
'SELECT $2,$1'
Due to '$1' being the last string without any punctuation behind it, it can't be matched by the regex.
To simplify this edge example, I push ' ' to make it be 'SELECT $2,$1 ', now '$1 ' can be matched.

When I finish processing, I pop the final ' '.

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 find another way that is easier to understand.

/// let raw_params = vec!["A".into(), "B".into(), "C".into()];
/// let type_description = vec![TypeOid::Varchar; 3];
/// let params = parse_params(&type_description, &raw_params);
/// assert_eq!(params, vec!["\'A\'", "\'B\'", "\'C\'"])
Copy link
Contributor

Choose a reason for hiding this comment

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

what's the usage of \ ?

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 want to represent escape character, but I find I can directly use ' , I will fix it later.

tmp = pattern
.replace_all(&tmp, format!("{}$y", param))
.to_string();
}
tmp.pop();

Copy link
Contributor

Choose a reason for hiding this comment

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

looks more clean now, btw delete the empty line

@ZENOTME ZENOTME force-pushed the zj/simple_extended_query_fix branch 2 times, most recently from f9f73cf to 80a4f16 Compare June 10, 2022 05:55
@ZENOTME ZENOTME force-pushed the zj/simple_extended_query_fix branch from 80a4f16 to 4b900ec Compare June 10, 2022 06:26
@ZENOTME ZENOTME merged commit b71f469 into main Jun 10, 2022
@ZENOTME ZENOTME deleted the zj/simple_extended_query_fix branch June 10, 2022 06:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/fix Bug fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants