Skip to content

Commit 70a73bd

Browse files
gandronchikmcheshkov
authored andcommitted
feat: support DISCARD [ALL | PLANS | SEQUENCES | TEMPORARY | TEMP]
Can drop this after rebase on commit a6d7a35 "feat: support DISCARD [ALL | PLANS | SEQUENCES | TEMPORARY | TEMP] (apache#500)", first released in 0.18.0
1 parent 484a7b6 commit 70a73bd

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
@@ -911,6 +911,11 @@ pub enum Statement {
911911
/// deleted along with the dropped table
912912
purge: bool,
913913
},
914+
/// DISCARD [ ALL | PLANS | SEQUENCES | TEMPORARY | TEMP ]
915+
///
916+
/// Note: this is a PostgreSQL-specific statement,
917+
/// but may also compatible with other SQL.
918+
Discard { object_type: DiscardObject },
914919
/// SET [ SESSION | LOCAL ] ROLE role_name
915920
///
916921
/// Note: this is a PostgreSQL-specific statement,
@@ -1575,6 +1580,10 @@ impl fmt::Display for Statement {
15751580
if *cascade { " CASCADE" } else { "" },
15761581
if *purge { " PURGE" } else { "" }
15771582
),
1583+
Statement::Discard { object_type } => {
1584+
write!(f, "DISCARD {object_type}", object_type = object_type)?;
1585+
Ok(())
1586+
}
15781587
Statement::SetRole {
15791588
local,
15801589
session,
@@ -2547,6 +2556,26 @@ impl fmt::Display for MergeClause {
25472556
}
25482557
}
25492558

2559+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2560+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2561+
pub enum DiscardObject {
2562+
ALL,
2563+
PLANS,
2564+
SEQUENCES,
2565+
TEMP,
2566+
}
2567+
2568+
impl fmt::Display for DiscardObject {
2569+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2570+
match self {
2571+
DiscardObject::ALL => f.write_str("ALL"),
2572+
DiscardObject::PLANS => f.write_str("PLANS"),
2573+
DiscardObject::SEQUENCES => f.write_str("SEQUENCES"),
2574+
DiscardObject::TEMP => f.write_str("TEMP"),
2575+
}
2576+
}
2577+
}
2578+
25502579
#[cfg(test)]
25512580
mod tests {
25522581
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
@@ -4738,3 +4738,30 @@ fn parse_is_boolean() {
47384738
res.unwrap_err()
47394739
);
47404740
}
4741+
4742+
#[test]
4743+
fn parse_discard() {
4744+
let sql = "DISCARD ALL";
4745+
match verified_stmt(sql) {
4746+
Statement::Discard { object_type, .. } => assert_eq!(object_type, DiscardObject::ALL),
4747+
_ => unreachable!(),
4748+
}
4749+
4750+
let sql = "DISCARD PLANS";
4751+
match verified_stmt(sql) {
4752+
Statement::Discard { object_type, .. } => assert_eq!(object_type, DiscardObject::PLANS),
4753+
_ => unreachable!(),
4754+
}
4755+
4756+
let sql = "DISCARD SEQUENCES";
4757+
match verified_stmt(sql) {
4758+
Statement::Discard { object_type, .. } => assert_eq!(object_type, DiscardObject::SEQUENCES),
4759+
_ => unreachable!(),
4760+
}
4761+
4762+
let sql = "DISCARD TEMP";
4763+
match verified_stmt(sql) {
4764+
Statement::Discard { object_type, .. } => assert_eq!(object_type, DiscardObject::TEMP),
4765+
_ => unreachable!(),
4766+
}
4767+
}

0 commit comments

Comments
 (0)