Skip to content

Support DISCARD [ALL | PLANS | SEQUENCES | TEMPORARY | TEMP] #500

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 1 commit into from
May 22, 2022
Merged
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
29 changes: 29 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,11 @@ pub enum Statement {
/// deleted along with the dropped table
purge: bool,
},
/// DISCARD [ ALL | PLANS | SEQUENCES | TEMPORARY | TEMP ]
///
/// Note: this is a PostgreSQL-specific statement,
/// but may also compatible with other SQL.
Discard { object_type: DiscardObject },
/// SET [ SESSION | LOCAL ] ROLE role_name
///
/// Note: this is a PostgreSQL-specific statement,
Expand Down Expand Up @@ -1575,6 +1580,10 @@ impl fmt::Display for Statement {
if *cascade { " CASCADE" } else { "" },
if *purge { " PURGE" } else { "" }
),
Statement::Discard { object_type } => {
write!(f, "DISCARD {object_type}", object_type = object_type)?;
Ok(())
}
Statement::SetRole {
local,
session,
Expand Down Expand Up @@ -2547,6 +2556,26 @@ impl fmt::Display for MergeClause {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum DiscardObject {
ALL,
PLANS,
SEQUENCES,
TEMP,
}

impl fmt::Display for DiscardObject {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
DiscardObject::ALL => f.write_str("ALL"),
DiscardObject::PLANS => f.write_str("PLANS"),
DiscardObject::SEQUENCES => f.write_str("SEQUENCES"),
DiscardObject::TEMP => f.write_str("TEMP"),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 2 additions & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ define_keywords!(
DESCRIBE,
DETERMINISTIC,
DIRECTORY,
DISCARD,
DISCONNECT,
DISTINCT,
DISTRIBUTE,
Expand Down Expand Up @@ -371,6 +372,7 @@ define_keywords!(
PERCENTILE_DISC,
PERCENT_RANK,
PERIOD,
PLANS,
PORTION,
POSITION,
POSITION_REGEX,
Expand Down
19 changes: 19 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ impl<'a> Parser<'a> {
Keyword::MSCK => Ok(self.parse_msck()?),
Keyword::CREATE => Ok(self.parse_create()?),
Keyword::DROP => Ok(self.parse_drop()?),
Keyword::DISCARD => Ok(self.parse_discard()?),
Keyword::DELETE => Ok(self.parse_delete()?),
Keyword::INSERT => Ok(self.parse_insert()?),
Keyword::UPDATE => Ok(self.parse_update()?),
Expand Down Expand Up @@ -1786,6 +1787,24 @@ impl<'a> Parser<'a> {
})
}

pub fn parse_discard(&mut self) -> Result<Statement, ParserError> {
let object_type = if self.parse_keyword(Keyword::ALL) {
DiscardObject::ALL
} else if self.parse_keyword(Keyword::PLANS) {
DiscardObject::PLANS
} else if self.parse_keyword(Keyword::SEQUENCES) {
DiscardObject::SEQUENCES
} else if self.parse_keyword(Keyword::TEMP) || self.parse_keyword(Keyword::TEMPORARY) {
DiscardObject::TEMP
} else {
return self.expected(
"ALL, PLANS, SEQUENCES, TEMP or TEMPORARY after DISCARD",
self.peek_token(),
);
};
Ok(Statement::Discard { object_type })
}

pub fn parse_create_index(&mut self, unique: bool) -> Result<Statement, ParserError> {
let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
let index_name = self.parse_object_name()?;
Expand Down
27 changes: 27 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4740,3 +4740,30 @@ fn parse_is_boolean() {
res.unwrap_err()
);
}

#[test]
fn parse_discard() {
let sql = "DISCARD ALL";
match verified_stmt(sql) {
Statement::Discard { object_type, .. } => assert_eq!(object_type, DiscardObject::ALL),
_ => unreachable!(),
}

let sql = "DISCARD PLANS";
match verified_stmt(sql) {
Statement::Discard { object_type, .. } => assert_eq!(object_type, DiscardObject::PLANS),
_ => unreachable!(),
}

let sql = "DISCARD SEQUENCES";
match verified_stmt(sql) {
Statement::Discard { object_type, .. } => assert_eq!(object_type, DiscardObject::SEQUENCES),
_ => unreachable!(),
}

let sql = "DISCARD TEMP";
match verified_stmt(sql) {
Statement::Discard { object_type, .. } => assert_eq!(object_type, DiscardObject::TEMP),
_ => unreachable!(),
}
}