Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fuzz/fuzz_seeds/postgres_2809_operators.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT a *= b, a @@@ b
1 change: 1 addition & 0 deletions fuzz/fuzz_seeds/postgres_2809_trailing_sign.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT a*=-1
101 changes: 99 additions & 2 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1784,7 +1784,35 @@ impl<'a> Tokenizer<'a> {
}
}
'+' => self.consume_and_return(chars, Token::Plus),
'*' => self.consume_and_return(chars, Token::Mul),
'*' => {
chars.next(); // consume the '*'
match chars.peek() {
// PostgreSQL composite comparison operators (`*=`, `*<`,
// `*<=`, `*<>`, `*>`, `*>=`) are single operators. They
// end after these characters: a following `+`/`-` is a
// separate token, not part of the operator.
Some(&ch)
if matches!(ch, '=' | '<' | '>')
&& self.dialect.is_custom_operator_part(ch) =>
{
chars.next(); // consume the second character
let mut op = String::from("*");
op.push(ch);
let extends = match ch {
'<' => matches!(chars.peek(), Some('=' | '>')),
'>' => matches!(chars.peek(), Some('=')),
_ => false,
};
if extends {
if let Some(third) = chars.next() {
op.push(third);
}
}
Ok(Some(Token::CustomBinaryOperator(op)))
}
_ => Ok(Some(Token::Mul)),
}
}
'%' => {
chars.next(); // advance past '%'
match chars.peek() {
Expand Down Expand Up @@ -2038,7 +2066,7 @@ impl<'a> Tokenizer<'a> {
chars.next();
match chars.peek() {
Some('@') if self.dialect.supports_geometric_types() => {
self.consume_and_return(chars, Token::AtAt)
self.consume_for_binop(chars, "@@", Token::AtAt)
}
Some('-') if self.dialect.supports_geometric_types() => {
chars.next();
Expand Down Expand Up @@ -4010,6 +4038,75 @@ mod tests {
compare(expected, tokens);
}

#[test]
fn tokenize_pg_star_and_at_operators() {
let sql = "SELECT a *= b, a *< b, a @@@ b, a * b, a @@ b";
let dialect = PostgreSqlDialect {};
let tokens = Tokenizer::new(&dialect, sql).tokenize().unwrap();
let expected = vec![
Token::make_keyword("SELECT"),
Token::Whitespace(Whitespace::Space),
Token::make_word("a", None),
Token::Whitespace(Whitespace::Space),
Token::CustomBinaryOperator("*=".to_string()),
Token::Whitespace(Whitespace::Space),
Token::make_word("b", None),
Token::Comma,
Token::Whitespace(Whitespace::Space),
Token::make_word("a", None),
Token::Whitespace(Whitespace::Space),
Token::CustomBinaryOperator("*<".to_string()),
Token::Whitespace(Whitespace::Space),
Token::make_word("b", None),
Token::Comma,
Token::Whitespace(Whitespace::Space),
Token::make_word("a", None),
Token::Whitespace(Whitespace::Space),
Token::CustomBinaryOperator("@@@".to_string()),
Token::Whitespace(Whitespace::Space),
Token::make_word("b", None),
Token::Comma,
Token::Whitespace(Whitespace::Space),
Token::make_word("a", None),
Token::Whitespace(Whitespace::Space),
Token::Mul,
Token::Whitespace(Whitespace::Space),
Token::make_word("b", None),
Token::Comma,
Token::Whitespace(Whitespace::Space),
Token::make_word("a", None),
Token::Whitespace(Whitespace::Space),
Token::AtAt,
Token::Whitespace(Whitespace::Space),
Token::make_word("b", None),
];
compare(expected, tokens);
}

#[test]
fn tokenize_pg_star_operators_with_trailing_sign() {
// A trailing `+`/`-` is not part of a `*`-prefixed operator: an
// operator composed only of `+ - * / < > =` cannot end in `+`/`-`.
let sql = "SELECT a*=-1, a*>=+1";
let dialect = PostgreSqlDialect {};
let tokens = Tokenizer::new(&dialect, sql).tokenize().unwrap();
let expected = vec![
Token::make_keyword("SELECT"),
Token::Whitespace(Whitespace::Space),
Token::make_word("a", None),
Token::CustomBinaryOperator("*=".to_string()),
Token::Minus,
Token::Number(String::from("1"), false),
Token::Comma,
Token::Whitespace(Whitespace::Space),
Token::make_word("a", None),
Token::CustomBinaryOperator("*>=".to_string()),
Token::Plus,
Token::Number(String::from("1"), false),
];
compare(expected, tokens);
}

#[test]
fn tokenize_tilde_without_like_match_operators() {
all_dialects_where(|d| !d.supports_tilde_like_match_operator()).tokenizes_to(
Expand Down
16 changes: 16 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14677,3 +14677,19 @@ fn parse_create_function_transform() {
);
}
}

#[test]
fn parse_pg_star_prefixed_and_triple_at_operators() {
// Composite type comparison operators and the `@@@` text search
// operator are single operators in PostgreSQL, not a `*` or `@@`
// followed by another token.
pg().verified_stmt("SELECT a *= b");
pg().verified_stmt("SELECT a *< b");
pg().verified_stmt("SELECT a *<> b");
pg().verified_stmt("SELECT a *<= b");
pg().verified_stmt("SELECT a *> b");
pg().verified_stmt("SELECT a *>= b");
pg().verified_stmt("SELECT a @@@ b");
pg().verified_stmt("SELECT a OPERATOR(pg_catalog.*=) b");
pg().verified_stmt("SELECT a OPERATOR(pg_catalog.@@@) b");
}
Loading