Skip to content

Commit

Permalink
Single line hash comments support on BigQuery.
Browse files Browse the repository at this point in the history
  • Loading branch information
zdenal committed Feb 22, 2024
1 parent 5bc97b6 commit 4fc55dc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ impl<'a> Tokenizer<'a> {
'^' => self.consume_and_return(chars, Token::Caret),
'{' => self.consume_and_return(chars, Token::LBrace),
'}' => self.consume_and_return(chars, Token::RBrace),
'#' if dialect_of!(self is SnowflakeDialect) => {
'#' if dialect_of!(self is BigQueryDialect | SnowflakeDialect) => {
chars.next(); // consume the '#', starting a snowflake single-line comment
let comment = self.tokenize_single_line_comment(chars);
Ok(Some(Token::Whitespace(Whitespace::SingleLineComment {
Expand Down
29 changes: 29 additions & 0 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::ops::Deref;
use sqlparser::ast::*;
use sqlparser::dialect::{BigQueryDialect, GenericDialect};
use sqlparser::parser::ParserError;
use sqlparser::tokenizer::*;
use test_utils::*;

#[test]
Expand Down Expand Up @@ -1211,3 +1212,31 @@ fn test_select_json_field() {
_select.projection[0]
);
}

#[test]
fn test_bigquery_single_line_comment_tokenize() {
let sql = "CREATE TABLE# this is a comment \ntable_1";
let dialect = BigQueryDialect {};
let tokens = Tokenizer::new(&dialect, sql).tokenize().unwrap();

let expected = vec![
Token::make_keyword("CREATE"),
Token::Whitespace(Whitespace::Space),
Token::make_keyword("TABLE"),
Token::Whitespace(Whitespace::SingleLineComment {
prefix: "#".to_string(),
comment: " this is a comment \n".to_string(),
}),
Token::make_word("table_1", None),
];

assert_eq!(expected, tokens);
}

#[test]
fn test_bigquery_single_line_comment_parsing() {
bigquery().verified_only_select_with_canonical(
"SELECT book# this is a comment \n FROM library",
"SELECT book FROM library",
);
}

0 comments on commit 4fc55dc

Please sign in to comment.