Skip to content

Add support for PostgreSQL UNLISTEN syntax and Add support for Postgres LOAD extension expr #1531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 11 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3340,6 +3340,13 @@ pub enum Statement {
/// See Postgres <https://www.postgresql.org/docs/current/sql-listen.html>
LISTEN { channel: Ident },
/// ```sql
/// UNLISTEN
/// ```
/// stop listening for a notification
///
/// See Postgres <https://www.postgresql.org/docs/current/sql-unlisten.html>
UNLISTEN { channel: Ident },
/// ```sql
/// NOTIFY channel [ , payload ]
/// ```
/// send a notification event together with an optional “payload” string to channel
Expand Down Expand Up @@ -4948,6 +4955,10 @@ impl fmt::Display for Statement {
write!(f, "LISTEN {channel}")?;
Ok(())
}
Statement::UNLISTEN { channel } => {
write!(f, "UNLISTEN {channel}")?;
Ok(())
}
Statement::NOTIFY { channel, payload } => {
write!(f, "NOTIFY {channel}")?;
if let Some(payload) = payload {
Expand Down
5 changes: 5 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,11 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if the dialect supports the `UNLISTEN` statement
fn supports_unlisten(&self) -> bool {
false
}

/// Returns true if the dialect supports the `NOTIFY` statement
fn supports_notify(&self) -> bool {
false
Expand Down
10 changes: 10 additions & 0 deletions src/dialect/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ impl Dialect for PostgreSqlDialect {
true
}

/// see <https://www.postgresql.org/docs/current/sql-unlisten.html>
fn supports_unlisten(&self) -> bool {
true
}

/// see <https://www.postgresql.org/docs/current/sql-notify.html>
fn supports_notify(&self) -> bool {
true
Expand All @@ -209,6 +214,11 @@ impl Dialect for PostgreSqlDialect {
fn supports_comment_on(&self) -> bool {
true
}

/// See <https://www.postgresql.org/docs/current/sql-load.html>
fn supports_load_extension(&self) -> bool {
true
}
}

pub fn parse_create(parser: &mut Parser) -> Option<Result<Statement, ParserError>> {
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ define_keywords!(
UNION,
UNIQUE,
UNKNOWN,
UNLISTEN,
UNLOAD,
UNLOCK,
UNLOGGED,
Expand Down
18 changes: 17 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,10 @@ impl<'a> Parser<'a> {
Keyword::EXECUTE | Keyword::EXEC => self.parse_execute(),
Keyword::PREPARE => self.parse_prepare(),
Keyword::MERGE => self.parse_merge(),
// `LISTEN` and `NOTIFY` are Postgres-specific
// `LISTEN`, `UNLISTEN` and `NOTIFY` are Postgres-specific
// syntaxes. They are used for Postgres statement.
Keyword::LISTEN if self.dialect.supports_listen() => self.parse_listen(),
Keyword::UNLISTEN if self.dialect.supports_unlisten() => self.parse_unlisten(),
Keyword::NOTIFY if self.dialect.supports_notify() => self.parse_notify(),
// `PRAGMA` is sqlite specific https://www.sqlite.org/pragma.html
Keyword::PRAGMA => self.parse_pragma(),
Expand Down Expand Up @@ -999,6 +1000,21 @@ impl<'a> Parser<'a> {
Ok(Statement::LISTEN { channel })
}

pub fn parse_unlisten(&mut self) -> Result<Statement, ParserError> {
let channel = if self.consume_token(&Token::Mul) {
Ident::new(Expr::Wildcard.to_string())
} else {
match self.parse_identifier(false) {
Ok(expr) => expr,
_ => {
self.prev_token();
return self.expected("wildcard or identent", self.peek_token());
}
}
};
Ok(Statement::UNLISTEN { channel })
}

pub fn parse_notify(&mut self) -> Result<Statement, ParserError> {
let channel = self.parse_identifier(false)?;
let payload = if self.consume_token(&Token::Comma) {
Expand Down
81 changes: 81 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11617,6 +11617,37 @@ fn parse_listen_channel() {
);
}

#[test]
fn parse_unlisten_channel() {
let dialects = all_dialects_where(|d| d.supports_unlisten());

match dialects.verified_stmt("UNLISTEN test1") {
Statement::UNLISTEN { channel } => {
assert_eq!(Ident::new("test1"), channel);
}
_ => unreachable!(),
};

match dialects.verified_stmt("UNLISTEN *") {
Statement::UNLISTEN { channel } => {
assert_eq!(Ident::new("*"), channel);
}
_ => unreachable!(),
};

assert_eq!(
dialects.parse_sql_statements("UNLISTEN +").unwrap_err(),
ParserError::ParserError("Expected: wildcard or identent, found: +".to_string())
);

let dialects = all_dialects_where(|d| !d.supports_listen());

assert_eq!(
dialects.parse_sql_statements("UNLISTEN test1").unwrap_err(),
ParserError::ParserError("Expected: an SQL statement, found: UNLISTEN".to_string())
);
}

#[test]
fn parse_notify_channel() {
let dialects = all_dialects_where(|d| d.supports_notify());
Expand Down Expand Up @@ -11864,6 +11895,56 @@ fn parse_load_data() {
);
}

#[test]
fn test_load_extension() {
let dialects = all_dialects_where(|d| d.supports_load_extension());
let only_supports_load_data_dialects =
all_dialects_where(|d| !d.supports_load_extension() && d.supports_load_data());
let not_supports_load_dialects =
all_dialects_where(|d| !d.supports_load_data() && !d.supports_load_extension());
let sql = "LOAD my_extension";

match dialects.verified_stmt(sql) {
Statement::Load { extension_name } => {
assert_eq!(Ident::new("my_extension"), extension_name);
}
_ => unreachable!(),
};

assert_eq!(
only_supports_load_data_dialects
.parse_sql_statements(sql)
.unwrap_err(),
ParserError::ParserError(
"Expected: `DATA` or an extension name after `LOAD`, found: my_extension".to_string()
)
);

assert_eq!(
not_supports_load_dialects
.parse_sql_statements(sql)
.unwrap_err(),
ParserError::ParserError(
"Expected: `DATA` or an extension name after `LOAD`, found: my_extension".to_string()
)
);

let sql = "LOAD 'filename'";

match dialects.verified_stmt(sql) {
Statement::Load { extension_name } => {
assert_eq!(
Ident {
value: "filename".to_string(),
quote_style: Some('\'')
},
extension_name
);
}
_ => unreachable!(),
};
}

#[test]
fn test_select_top() {
let dialects = all_dialects_where(|d| d.supports_top_before_distinct());
Expand Down
14 changes: 0 additions & 14 deletions tests/sqlparser_duckdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,20 +359,6 @@ fn test_duckdb_install() {
);
}

#[test]
fn test_duckdb_load_extension() {
let stmt = duckdb().verified_stmt("LOAD my_extension");
assert_eq!(
Statement::Load {
extension_name: Ident {
value: "my_extension".to_string(),
quote_style: None
}
},
stmt
);
}

#[test]
fn test_duckdb_struct_literal() {
//struct literal syntax https://duckdb.org/docs/sql/data_types/struct#creating-structs
Expand Down