Skip to content

Commit 47a7b66

Browse files
committed
feat: support DISCARD [ALL | PLANS | SEQUENCES | TEMPORARY | TEMP]
1 parent 90bbbcf commit 47a7b66

4 files changed

Lines changed: 77 additions & 1 deletion

File tree

src/ast/mod.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,11 @@ pub enum Statement {
858858
/// deleted along with the dropped table
859859
purge: bool,
860860
},
861+
/// DISCARD [ ALL | PLANS | SEQUENCES | TEMPORARY | TEMP ]
862+
///
863+
/// Note: this is a PostgreSQL-specific statement,
864+
/// but may also compatible with other SQL.
865+
Discard { object_type: DiscardObject },
861866
/// SET <variable>
862867
///
863868
/// Note: this is not a standard SQL statement, but it is supported by at
@@ -1534,7 +1539,10 @@ impl fmt::Display for Statement {
15341539
if *cascade { " CASCADE" } else { "" },
15351540
if *purge { " PURGE" } else { "" }
15361541
),
1537-
1542+
Statement::Discard { object_type } => {
1543+
write!(f, "DISCARD {object_type}", object_type = object_type)?;
1544+
Ok(())
1545+
}
15381546
Statement::SetVariable { key_values } => {
15391547
f.write_str("SET ")?;
15401548

@@ -2565,6 +2573,26 @@ impl fmt::Display for MergeClause {
25652573
}
25662574
}
25672575

2576+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2577+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2578+
pub enum DiscardObject {
2579+
ALL,
2580+
PLANS,
2581+
SEQUENCES,
2582+
TEMP,
2583+
}
2584+
2585+
impl fmt::Display for DiscardObject {
2586+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2587+
match self {
2588+
DiscardObject::ALL => f.write_str("ALL"),
2589+
DiscardObject::PLANS => f.write_str("PLANS"),
2590+
DiscardObject::SEQUENCES => f.write_str("SEQUENCES"),
2591+
DiscardObject::TEMP => f.write_str("TEMP"),
2592+
}
2593+
}
2594+
}
2595+
25682596
#[cfg(test)]
25692597
mod tests {
25702598
use super::*;

src/keywords.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ define_keywords!(
186186
DESCRIBE,
187187
DETERMINISTIC,
188188
DIRECTORY,
189+
DISCARD,
189190
DISCONNECT,
190191
DISTINCT,
191192
DISTRIBUTE,
@@ -372,6 +373,7 @@ define_keywords!(
372373
PERCENTILE_DISC,
373374
PERCENT_RANK,
374375
PERIOD,
376+
PLANS,
375377
PORTION,
376378
POSITION,
377379
POSITION_REGEX,

src/parser.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ impl<'a> Parser<'a> {
166166
Keyword::MSCK => Ok(self.parse_msck()?),
167167
Keyword::CREATE => Ok(self.parse_create()?),
168168
Keyword::DROP => Ok(self.parse_drop()?),
169+
Keyword::DISCARD => Ok(self.parse_discard()?),
169170
Keyword::DELETE => Ok(self.parse_delete()?),
170171
Keyword::INSERT => Ok(self.parse_insert()?),
171172
Keyword::UPDATE => Ok(self.parse_update()?),
@@ -1746,6 +1747,24 @@ impl<'a> Parser<'a> {
17461747
})
17471748
}
17481749

1750+
pub fn parse_discard(&mut self) -> Result<Statement, ParserError> {
1751+
let object_type = if self.parse_keyword(Keyword::ALL) {
1752+
DiscardObject::ALL
1753+
} else if self.parse_keyword(Keyword::PLANS) {
1754+
DiscardObject::PLANS
1755+
} else if self.parse_keyword(Keyword::SEQUENCES) {
1756+
DiscardObject::SEQUENCES
1757+
} else if self.parse_keyword(Keyword::TEMP) || self.parse_keyword(Keyword::TEMPORARY) {
1758+
DiscardObject::TEMP
1759+
} else {
1760+
return self.expected(
1761+
"ALL, PLANS, SEQUENCES, TEMP or TEMPORARY after DISCARD",
1762+
self.peek_token(),
1763+
);
1764+
};
1765+
Ok(Statement::Discard { object_type })
1766+
}
1767+
17491768
pub fn parse_create_index(&mut self, unique: bool) -> Result<Statement, ParserError> {
17501769
let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
17511770
let index_name = self.parse_object_name()?;

tests/sqlparser_common.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4739,3 +4739,30 @@ fn parse_is_boolean() {
47394739
res.unwrap_err()
47404740
);
47414741
}
4742+
4743+
#[test]
4744+
fn parse_discard() {
4745+
let sql = "DISCARD ALL";
4746+
match verified_stmt(sql) {
4747+
Statement::Discard { object_type, .. } => assert_eq!(object_type, DiscardObject::ALL),
4748+
_ => unreachable!(),
4749+
}
4750+
4751+
let sql = "DISCARD PLANS";
4752+
match verified_stmt(sql) {
4753+
Statement::Discard { object_type, .. } => assert_eq!(object_type, DiscardObject::PLANS),
4754+
_ => unreachable!(),
4755+
}
4756+
4757+
let sql = "DISCARD SEQUENCES";
4758+
match verified_stmt(sql) {
4759+
Statement::Discard { object_type, .. } => assert_eq!(object_type, DiscardObject::SEQUENCES),
4760+
_ => unreachable!(),
4761+
}
4762+
4763+
let sql = "DISCARD TEMP";
4764+
match verified_stmt(sql) {
4765+
Statement::Discard { object_type, .. } => assert_eq!(object_type, DiscardObject::TEMP),
4766+
_ => unreachable!(),
4767+
}
4768+
}

0 commit comments

Comments
 (0)