Skip to content

Commit

Permalink
style: add more comments & refactor on pg-wire (risingwavelabs#3358)
Browse files Browse the repository at this point in the history
style: add more comments on pg-wire code

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
BowenXiao1999 and mergify[bot] authored Jun 21, 2022
1 parent 1c09432 commit 02405e1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
33 changes: 20 additions & 13 deletions src/utils/pgwire/src/pg_extended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use regex::Regex;
use crate::pg_field_descriptor::{PgFieldDescriptor, TypeOid};
use crate::pg_protocol::cstr_to_str;

/// Parse params accoring the type description.
/// Parse params according the type description.
///
/// # Example
///
Expand All @@ -31,7 +31,7 @@ use crate::pg_protocol::cstr_to_str;
/// assert_eq!(params, vec!["'A'", "'B'", "'C'"])
/// ```
fn parse_params(type_description: &[TypeOid], raw_params: &[Bytes]) -> Vec<String> {
assert!(type_description.len() == raw_params.len());
assert_eq!(type_description.len(), raw_params.len());

raw_params
.iter()
Expand Down Expand Up @@ -118,17 +118,17 @@ impl PgStatement {
self.row_description.clone()
}

pub fn instance(&self, name: String, params: &[Bytes]) -> PgPortal {
pub fn instance(&self, portal_name: String, params: &[Bytes]) -> PgPortal {
let statement = cstr_to_str(&self.query_string).unwrap().to_owned();

if params.is_empty() {
return PgPortal {
name,
name: portal_name,
query_string: self.query_string.clone(),
};
}

// 1. Identify all the $n.
// 1. Identify all the $n. For example, "SELECT $3, $2, $1" -> [3, 2, 1].
let parameter_pattern = Regex::new(r"\$[0-9][0-9]*").unwrap();
let generic_params: Vec<usize> = parameter_pattern
.find_iter(statement.as_str())
Expand All @@ -141,15 +141,16 @@ impl PgStatement {
})
.collect();

// 2. parse params.
// 2. Parse params bytes into string form according to type.
let params = parse_params(&self.type_description, params);

// 3. replace params.
// 3. Replace generic params in statement to real value. For example, "SELECT $3, $2, $1" ->
// "SELECT 'A', 'B', 'C'".
let instance_query_string = replace_params(statement, &generic_params, &params);

// 4. Create a new portal.
PgPortal {
name,
name: portal_name,
query_string: Bytes::from(instance_query_string),
}
}
Expand Down Expand Up @@ -189,14 +190,14 @@ mod tests {
let params = parse_params(&type_description, &raw_params);

let res = replace_params("SELECT $3,$2,$1".to_string(), &[1, 2, 3], &params);
assert!(res == "SELECT 'C','B','A'");
assert_eq!(res, "SELECT 'C','B','A'");

let res = replace_params(
"SELECT $2,$3,$1 ,$3 ,$2 ,$1 ;".to_string(),
&[1, 2, 3],
&params,
);
assert!(res == "SELECT 'B','C','A' ,'C' ,'B' ,'A' ;");
assert_eq!(res, "SELECT 'B','C','A' ,'C' ,'B' ,'A' ;");
}

{
Expand All @@ -221,7 +222,7 @@ mod tests {
&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
&params,
);
assert!(res == "SELECT 'K','B','A';");
assert_eq!(res, "SELECT 'K','B','A';");
}

{
Expand All @@ -247,14 +248,20 @@ mod tests {
&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
&params,
);
assert!(res == "SELECT 'B','A','K','J' ,'K', 'A','L' , 'B', 'He1ll2o',1;");
assert_eq!(
res,
"SELECT 'B','A','K','J' ,'K', 'A','L' , 'B', 'He1ll2o',1;"
);

let res = replace_params(
"SELECT $2,$1,$11,$10 ,$11, $1,$12 , $2, 'He1ll2o',1;".to_string(),
&[2, 1, 11, 10, 12, 9, 7, 8, 5, 6, 4, 3],
&params,
);
assert!(res == "SELECT 'B','A','K','J' ,'K', 'A','L' , 'B', 'He1ll2o',1;");
assert_eq!(
res,
"SELECT 'B','A','K','J' ,'K', 'A','L' , 'B', 'He1ll2o',1;"
);
}

{
Expand Down
3 changes: 0 additions & 3 deletions src/utils/pgwire/src/pg_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,6 @@ where
// NOTE there is no ReadyForQuery message.
}
FeMessage::Describe(m) => {
// FIXME: Introduce parser to analyze statements and bind data type. Here just
// hard-code a VARCHAR.

// 1. Get statement.
let name = cstr_to_str(&m.query_name).unwrap().to_string();
let statement = if name.is_empty() {
Expand Down

0 comments on commit 02405e1

Please sign in to comment.