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/redshift_2958_comment_cr.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT 1 AS a --, 2
Expand Down
5 changes: 5 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,11 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if a lone `\r` ends a `--` comment, as in PostgreSQL.
fn single_line_comment_ends_at_carriage_return(&self) -> bool {
false
}

/// Returns true if the dialect supports `#` as the start of a single line comment,
/// e.g. `SELECT 1 # comment`
fn supports_hash_comment(&self) -> bool {
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,10 @@ impl Dialect for PostgreSqlDialect {
true
}

fn single_line_comment_ends_at_carriage_return(&self) -> bool {
true
}

fn supports_copy_from_stdin_data(&self) -> bool {
true
}
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/redshift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ impl Dialect for RedshiftSqlDialect {
true
}

fn single_line_comment_ends_at_carriage_return(&self) -> bool {
true
}

/// `INTERVAL` is listed as reserved in Redshift docs but the actual parser allows it as an
/// identifier, consistent with pgsql.
fn is_reserved_for_identifier(&self, kw: Keyword) -> bool {
Expand Down
4 changes: 2 additions & 2 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2356,8 +2356,8 @@ impl<'a> Tokenizer<'a> {
// Consume characters until newline
fn tokenize_single_line_comment(&self, chars: &mut State) -> String {
peeking_take_while(chars, |ch| match ch {
'\n' => false, // Always stop at \n
'\r' if dialect_of!(self is PostgreSqlDialect) => false, // Stop at \r for Postgres
'\n' => false, // Always stop at \n
'\r' if self.dialect.single_line_comment_ends_at_carriage_return() => false,
_ => true, // Keep consuming for other characters
})
}
Expand Down
13 changes: 13 additions & 0 deletions tests/sqlparser_redshift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,16 @@ fn parse_approximate_percentile_disc() {
"SELECT APPROXIMATE AS PERCENTILE_DISC FROM t",
);
}

#[test]
fn parse_single_line_comment_ends_at_carriage_return() {
let sql = "SELECT 1 AS a --\r, 2";

let select = all_dialects_where(|d| d.single_line_comment_ends_at_carriage_return())
.verified_only_select_with_canonical(sql, "SELECT 1 AS a, 2");
assert_eq!(select.projection.len(), 2);

let select = all_dialects_where(|d| !d.single_line_comment_ends_at_carriage_return())
.verified_only_select_with_canonical(sql, "SELECT 1 AS a");
assert_eq!(select.projection.len(), 1);
}
Loading