Skip to content

Commit a6d7a35

Browse files
authored
feat: support DISCARD [ALL | PLANS | SEQUENCES | TEMPORARY | TEMP] (apache#500)
1 parent 7ab30d9 commit a6d7a35

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

src/ast/mod.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,11 @@ pub enum Statement {
897897
/// deleted along with the dropped table
898898
purge: bool,
899899
},
900+
/// DISCARD [ ALL | PLANS | SEQUENCES | TEMPORARY | TEMP ]
901+
///
902+
/// Note: this is a PostgreSQL-specific statement,
903+
/// but may also compatible with other SQL.
904+
Discard { object_type: DiscardObject },
900905
/// SET [ SESSION | LOCAL ] ROLE role_name
901906
///
902907
/// Note: this is a PostgreSQL-specific statement,
@@ -1561,6 +1566,10 @@ impl fmt::Display for Statement {
15611566
if *cascade { " CASCADE" } else { "" },
15621567
if *purge { " PURGE" } else { "" }
15631568
),
1569+
Statement::Discard { object_type } => {
1570+
write!(f, "DISCARD {object_type}", object_type = object_type)?;
1571+
Ok(())
1572+
}
15641573
Statement::SetRole {
15651574
local,
15661575
session,
@@ -2533,6 +2542,26 @@ impl fmt::Display for MergeClause {
25332542
}
25342543
}
25352544

2545+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2546+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2547+
pub enum DiscardObject {
2548+
ALL,
2549+
PLANS,
2550+
SEQUENCES,
2551+
TEMP,
2552+
}
2553+
2554+
impl fmt::Display for DiscardObject {
2555+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2556+
match self {
2557+
DiscardObject::ALL => f.write_str("ALL"),
2558+
DiscardObject::PLANS => f.write_str("PLANS"),
2559+
DiscardObject::SEQUENCES => f.write_str("SEQUENCES"),
2560+
DiscardObject::TEMP => f.write_str("TEMP"),
2561+
}
2562+
}
2563+
}
2564+
25362565
#[cfg(test)]
25372566
mod tests {
25382567
use super::*;

src/keywords.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ define_keywords!(
184184
DESCRIBE,
185185
DETERMINISTIC,
186186
DIRECTORY,
187+
DISCARD,
187188
DISCONNECT,
188189
DISTINCT,
189190
DISTRIBUTE,
@@ -371,6 +372,7 @@ define_keywords!(
371372
PERCENTILE_DISC,
372373
PERCENT_RANK,
373374
PERIOD,
375+
PLANS,
374376
PORTION,
375377
POSITION,
376378
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()?),
@@ -1786,6 +1787,24 @@ impl<'a> Parser<'a> {
17861787
})
17871788
}
17881789

1790+
pub fn parse_discard(&mut self) -> Result<Statement, ParserError> {
1791+
let object_type = if self.parse_keyword(Keyword::ALL) {
1792+
DiscardObject::ALL
1793+
} else if self.parse_keyword(Keyword::PLANS) {
1794+
DiscardObject::PLANS
1795+
} else if self.parse_keyword(Keyword::SEQUENCES) {
1796+
DiscardObject::SEQUENCES
1797+
} else if self.parse_keyword(Keyword::TEMP) || self.parse_keyword(Keyword::TEMPORARY) {
1798+
DiscardObject::TEMP
1799+
} else {
1800+
return self.expected(
1801+
"ALL, PLANS, SEQUENCES, TEMP or TEMPORARY after DISCARD",
1802+
self.peek_token(),
1803+
);
1804+
};
1805+
Ok(Statement::Discard { object_type })
1806+
}
1807+
17891808
pub fn parse_create_index(&mut self, unique: bool) -> Result<Statement, ParserError> {
17901809
let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
17911810
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
@@ -4787,3 +4787,30 @@ fn parse_is_boolean() {
47874787
res.unwrap_err()
47884788
);
47894789
}
4790+
4791+
#[test]
4792+
fn parse_discard() {
4793+
let sql = "DISCARD ALL";
4794+
match verified_stmt(sql) {
4795+
Statement::Discard { object_type, .. } => assert_eq!(object_type, DiscardObject::ALL),
4796+
_ => unreachable!(),
4797+
}
4798+
4799+
let sql = "DISCARD PLANS";
4800+
match verified_stmt(sql) {
4801+
Statement::Discard { object_type, .. } => assert_eq!(object_type, DiscardObject::PLANS),
4802+
_ => unreachable!(),
4803+
}
4804+
4805+
let sql = "DISCARD SEQUENCES";
4806+
match verified_stmt(sql) {
4807+
Statement::Discard { object_type, .. } => assert_eq!(object_type, DiscardObject::SEQUENCES),
4808+
_ => unreachable!(),
4809+
}
4810+
4811+
let sql = "DISCARD TEMP";
4812+
match verified_stmt(sql) {
4813+
Statement::Discard { object_type, .. } => assert_eq!(object_type, DiscardObject::TEMP),
4814+
_ => unreachable!(),
4815+
}
4816+
}

0 commit comments

Comments
 (0)