Skip to content

Commit

Permalink
Parse byte/bit string literals in MySQL and Postgres (apache#1532)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvzink authored Nov 20, 2024
1 parent 73947a5 commit fad2ddd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,8 +704,9 @@ impl<'a> Tokenizer<'a> {
}
Ok(Some(Token::Whitespace(Whitespace::Newline)))
}
// BigQuery uses b or B for byte string literal
b @ 'B' | b @ 'b' if dialect_of!(self is BigQueryDialect | GenericDialect) => {
// BigQuery and MySQL use b or B for byte string literal, Postgres for bit strings
b @ 'B' | b @ 'b' if dialect_of!(self is BigQueryDialect | PostgreSqlDialect | MySqlDialect | GenericDialect) =>
{
chars.next(); // consume
match chars.peek() {
Some('\'') => {
Expand Down
11 changes: 11 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2960,3 +2960,14 @@ fn parse_logical_xor() {
select.projection[3]
);
}

#[test]
fn parse_bitstring_literal() {
let select = mysql_and_generic().verified_only_select("SELECT B'111'");
assert_eq!(
select.projection,
vec![SelectItem::UnnamedExpr(Expr::Value(
Value::SingleQuotedByteStringLiteral("111".to_string())
))]
);
}
11 changes: 11 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5098,3 +5098,14 @@ fn parse_create_type_as_enum() {
_ => unreachable!(),
}
}

#[test]
fn parse_bitstring_literal() {
let select = pg_and_generic().verified_only_select("SELECT B'111'");
assert_eq!(
select.projection,
vec![SelectItem::UnnamedExpr(Expr::Value(
Value::SingleQuotedByteStringLiteral("111".to_string())
))]
);
}

0 comments on commit fad2ddd

Please sign in to comment.