Skip to content

Commit 4a41319

Browse files
Formatted code
1 parent 4d45739 commit 4a41319

File tree

2 files changed

+76
-32
lines changed

2 files changed

+76
-32
lines changed

src/parser/mod.rs

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6434,12 +6434,14 @@ impl<'a> Parser<'a> {
64346434
/// Parse an operator name, which can contain special characters like +, -, <, >, =
64356435
/// that are tokenized as operator tokens rather than identifiers.
64366436
/// This is used for PostgreSQL CREATE OPERATOR statements.
6437-
///
6437+
///
64386438
/// Examples: `+`, `myschema.+`, `pg_catalog.<=`
64396439
pub fn parse_operator_name(&mut self) -> Result<ObjectName, ParserError> {
64406440
let mut parts = vec![];
64416441
loop {
6442-
parts.push(ObjectNamePart::Identifier(Ident::new(self.next_token().to_string())));
6442+
parts.push(ObjectNamePart::Identifier(Ident::new(
6443+
self.next_token().to_string(),
6444+
)));
64436445
if !self.consume_token(&Token::Period) {
64446446
break;
64456447
}
@@ -6473,38 +6475,57 @@ impl<'a> Parser<'a> {
64736475

64746476
loop {
64756477
let keyword = self.expect_one_of_keywords(&[
6476-
Keyword::FUNCTION, Keyword::PROCEDURE, Keyword::LEFTARG, Keyword::RIGHTARG,
6477-
Keyword::COMMUTATOR, Keyword::NEGATOR, Keyword::RESTRICT, Keyword::JOIN,
6478-
Keyword::HASHES, Keyword::MERGES,
6478+
Keyword::FUNCTION,
6479+
Keyword::PROCEDURE,
6480+
Keyword::LEFTARG,
6481+
Keyword::RIGHTARG,
6482+
Keyword::COMMUTATOR,
6483+
Keyword::NEGATOR,
6484+
Keyword::RESTRICT,
6485+
Keyword::JOIN,
6486+
Keyword::HASHES,
6487+
Keyword::MERGES,
64796488
])?;
64806489

64816490
match keyword {
64826491
Keyword::HASHES => {
6483-
if hashes { return Err(dup_err!("HASHES")); }
6492+
if hashes {
6493+
return Err(dup_err!("HASHES"));
6494+
}
64846495
hashes = true;
64856496
}
64866497
Keyword::MERGES => {
6487-
if merges { return Err(dup_err!("MERGES")); }
6498+
if merges {
6499+
return Err(dup_err!("MERGES"));
6500+
}
64886501
merges = true;
64896502
}
64906503
Keyword::FUNCTION | Keyword::PROCEDURE => {
6491-
if function.is_some() { return Err(dup_err!("FUNCTION/PROCEDURE")); }
6504+
if function.is_some() {
6505+
return Err(dup_err!("FUNCTION/PROCEDURE"));
6506+
}
64926507
self.expect_token(&Token::Eq)?;
64936508
function = Some(self.parse_object_name(false)?);
64946509
is_procedure = keyword == Keyword::PROCEDURE;
64956510
}
64966511
Keyword::LEFTARG => {
6497-
if left_arg.is_some() { return Err(dup_err!("LEFTARG")); }
6512+
if left_arg.is_some() {
6513+
return Err(dup_err!("LEFTARG"));
6514+
}
64986515
self.expect_token(&Token::Eq)?;
64996516
left_arg = Some(self.parse_data_type()?);
65006517
}
65016518
Keyword::RIGHTARG => {
6502-
if right_arg.is_some() { return Err(dup_err!("RIGHTARG")); }
6519+
if right_arg.is_some() {
6520+
return Err(dup_err!("RIGHTARG"));
6521+
}
65036522
self.expect_token(&Token::Eq)?;
65046523
right_arg = Some(self.parse_data_type()?);
65056524
}
65066525
Keyword::COMMUTATOR => {
6507-
if commutator.is_some() { return Err(dup_err!("COMMUTATOR")); }
6526+
if commutator.is_some() {
6527+
return Err(dup_err!("COMMUTATOR"));
6528+
}
65086529
self.expect_token(&Token::Eq)?;
65096530
if self.parse_keyword(Keyword::OPERATOR) {
65106531
self.expect_token(&Token::LParen)?;
@@ -6515,7 +6536,9 @@ impl<'a> Parser<'a> {
65156536
}
65166537
}
65176538
Keyword::NEGATOR => {
6518-
if negator.is_some() { return Err(dup_err!("NEGATOR")); }
6539+
if negator.is_some() {
6540+
return Err(dup_err!("NEGATOR"));
6541+
}
65196542
self.expect_token(&Token::Eq)?;
65206543
if self.parse_keyword(Keyword::OPERATOR) {
65216544
self.expect_token(&Token::LParen)?;
@@ -6526,18 +6549,25 @@ impl<'a> Parser<'a> {
65266549
}
65276550
}
65286551
Keyword::RESTRICT => {
6529-
if restrict.is_some() { return Err(dup_err!("RESTRICT")); }
6552+
if restrict.is_some() {
6553+
return Err(dup_err!("RESTRICT"));
6554+
}
65306555
self.expect_token(&Token::Eq)?;
65316556
restrict = Some(self.parse_object_name(false)?);
65326557
}
65336558
Keyword::JOIN => {
6534-
if join.is_some() { return Err(dup_err!("JOIN")); }
6559+
if join.is_some() {
6560+
return Err(dup_err!("JOIN"));
6561+
}
65356562
self.expect_token(&Token::Eq)?;
65366563
join = Some(self.parse_object_name(false)?);
65376564
}
6538-
_ => return Err(ParserError::ParserError(format!(
6539-
"Unexpected keyword {:?} in CREATE OPERATOR", keyword
6540-
))),
6565+
_ => {
6566+
return Err(ParserError::ParserError(format!(
6567+
"Unexpected keyword {:?} in CREATE OPERATOR",
6568+
keyword
6569+
)))
6570+
}
65416571
}
65426572

65436573
if !self.consume_token(&Token::Comma) {

tests/sqlparser_postgres.rs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6710,7 +6710,12 @@ fn parse_create_operator() {
67106710
),
67116711
] {
67126712
match pg().verified_stmt(&format!("CREATE OPERATOR {name} (FUNCTION = f)")) {
6713-
Statement::CreateOperator(CreateOperator { name, hashes: false, merges: false, .. }) => {
6713+
Statement::CreateOperator(CreateOperator {
6714+
name,
6715+
hashes: false,
6716+
merges: false,
6717+
..
6718+
}) => {
67146719
assert_eq!(name, expected_name);
67156720
}
67166721
_ => unreachable!(),
@@ -6750,14 +6755,18 @@ fn parse_create_operator() {
67506755
fn parse_create_operator_family() {
67516756
for index_method in &["btree", "hash", "gist", "gin", "spgist", "brin"] {
67526757
assert_eq!(
6753-
pg().verified_stmt(&format!("CREATE OPERATOR FAMILY my_family USING {index_method}")),
6758+
pg().verified_stmt(&format!(
6759+
"CREATE OPERATOR FAMILY my_family USING {index_method}"
6760+
)),
67546761
Statement::CreateOperatorFamily(CreateOperatorFamily {
67556762
name: ObjectName::from(vec![Ident::new("my_family")]),
67566763
using: Ident::new(*index_method),
67576764
})
67586765
);
67596766
assert_eq!(
6760-
pg().verified_stmt(&format!("CREATE OPERATOR FAMILY myschema.test_family USING {index_method}")),
6767+
pg().verified_stmt(&format!(
6768+
"CREATE OPERATOR FAMILY myschema.test_family USING {index_method}"
6769+
)),
67616770
Statement::CreateOperatorFamily(CreateOperatorFamily {
67626771
name: ObjectName::from(vec![Ident::new("myschema"), Ident::new("test_family")]),
67636772
using: Ident::new(*index_method),
@@ -6773,7 +6782,10 @@ fn parse_create_operator_class() {
67736782
for (has_family, family_clause) in [(false, ""), (true, " FAMILY int4_family")] {
67746783
for (class_name, expected_name) in [
67756784
("int4_ops", ObjectName::from(vec![Ident::new("int4_ops")])),
6776-
("myschema.test_ops", ObjectName::from(vec![Ident::new("myschema"), Ident::new("test_ops")])),
6785+
(
6786+
"myschema.test_ops",
6787+
ObjectName::from(vec![Ident::new("myschema"), Ident::new("test_ops")]),
6788+
),
67776789
] {
67786790
let sql = format!(
67796791
"CREATE OPERATOR CLASS {class_name} {default_clause}FOR TYPE INT4 USING btree{family_clause} AS OPERATOR 1 <"
@@ -6793,7 +6805,11 @@ fn parse_create_operator_class() {
67936805
assert_eq!(using, &Ident::new("btree"));
67946806
assert_eq!(
67956807
family,
6796-
&if has_family { Some(ObjectName::from(vec![Ident::new("int4_family")])) } else { None }
6808+
&if has_family {
6809+
Some(ObjectName::from(vec![Ident::new("int4_family")]))
6810+
} else {
6811+
None
6812+
}
67976813
);
67986814
assert_eq!(items.len(), 1);
67996815
}
@@ -6823,11 +6839,10 @@ fn parse_create_operator_class() {
68236839
}
68246840

68256841
// Test operator with argument types
6826-
match pg().verified_stmt("CREATE OPERATOR CLASS test_ops FOR TYPE INT4 USING gist AS OPERATOR 1 < (INT4, INT4)") {
6827-
Statement::CreateOperatorClass(CreateOperatorClass {
6828-
ref items,
6829-
..
6830-
}) => {
6842+
match pg().verified_stmt(
6843+
"CREATE OPERATOR CLASS test_ops FOR TYPE INT4 USING gist AS OPERATOR 1 < (INT4, INT4)",
6844+
) {
6845+
Statement::CreateOperatorClass(CreateOperatorClass { ref items, .. }) => {
68316846
assert_eq!(items.len(), 1);
68326847
match &items[0] {
68336848
OperatorClassItem::Operator {
@@ -6845,11 +6860,10 @@ fn parse_create_operator_class() {
68456860
}
68466861

68476862
// Test operator FOR SEARCH
6848-
match pg().verified_stmt("CREATE OPERATOR CLASS test_ops FOR TYPE INT4 USING gist AS OPERATOR 1 < FOR SEARCH") {
6849-
Statement::CreateOperatorClass(CreateOperatorClass {
6850-
ref items,
6851-
..
6852-
}) => {
6863+
match pg().verified_stmt(
6864+
"CREATE OPERATOR CLASS test_ops FOR TYPE INT4 USING gist AS OPERATOR 1 < FOR SEARCH",
6865+
) {
6866+
Statement::CreateOperatorClass(CreateOperatorClass { ref items, .. }) => {
68536867
assert_eq!(items.len(), 1);
68546868
match &items[0] {
68556869
OperatorClassItem::Operator {

0 commit comments

Comments
 (0)