Skip to content

Commit f922986

Browse files
committed
Add RESET to the Postgres dialect #2078
1 parent 67684c8 commit f922986

File tree

6 files changed

+131
-0
lines changed

6 files changed

+131
-0
lines changed

src/ast/mod.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4263,6 +4263,14 @@ pub enum Statement {
42634263
/// ```
42644264
/// [Redshift](https://docs.aws.amazon.com/redshift/latest/dg/r_VACUUM_command.html)
42654265
Vacuum(VacuumStatement),
4266+
/// Restore the value of a run-time parameter to the default value.
4267+
///
4268+
/// ```sql
4269+
/// RESET configuration_parameter;
4270+
/// RESET ALL;
4271+
/// ```
4272+
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-reset.html)
4273+
Reset(ResetStatement),
42664274
}
42674275

42684276
impl From<Analyze> for Statement {
@@ -5757,6 +5765,7 @@ impl fmt::Display for Statement {
57575765
Statement::AlterSchema(s) => write!(f, "{s}"),
57585766
Statement::Vacuum(s) => write!(f, "{s}"),
57595767
Statement::AlterUser(s) => write!(f, "{s}"),
5768+
Statement::Reset(s) => write!(f, "{s}"),
57605769
}
57615770
}
57625771
}
@@ -10519,6 +10528,38 @@ impl fmt::Display for VacuumStatement {
1051910528
}
1052010529
}
1052110530

10531+
/// Variants of the RESET statement
10532+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
10533+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10534+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
10535+
pub enum Reset {
10536+
/// Resets all session parameters to their default values.
10537+
ALL,
10538+
10539+
/// Resets a specific session parameter to its default value.
10540+
ConfigurationParameter(ObjectName),
10541+
}
10542+
10543+
/// Resets a session parameter to its default value.
10544+
/// ```sql
10545+
/// RESET { ALL | <configuration_parameter> }
10546+
/// ```
10547+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
10548+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10549+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
10550+
pub struct ResetStatement {
10551+
pub reset: Reset,
10552+
}
10553+
10554+
impl fmt::Display for ResetStatement {
10555+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
10556+
match &self.reset {
10557+
Reset::ALL => write!(f, "RESET ALL"),
10558+
Reset::ConfigurationParameter(param) => write!(f, "RESET {}", param),
10559+
}
10560+
}
10561+
}
10562+
1052210563
impl From<Set> for Statement {
1052310564
fn from(s: Set) -> Self {
1052410565
Self::Set(s)
@@ -10759,6 +10800,12 @@ impl From<VacuumStatement> for Statement {
1075910800
}
1076010801
}
1076110802

10803+
impl From<ResetStatement> for Statement {
10804+
fn from(r: ResetStatement) -> Self {
10805+
Self::Reset(r)
10806+
}
10807+
}
10808+
1076210809
#[cfg(test)]
1076310810
mod tests {
1076410811
use crate::tokenizer::Location;

src/ast/spans.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ impl Spanned for Statement {
475475
Statement::AlterSchema(s) => s.span(),
476476
Statement::Vacuum(..) => Span::empty(),
477477
Statement::AlterUser(..) => Span::empty(),
478+
Statement::Reset(..) => Span::empty(),
478479
}
479480
}
480481
}

src/dialect/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,17 @@ pub trait Dialect: Debug + Any {
12071207
fn supports_semantic_view_table_factor(&self) -> bool {
12081208
false
12091209
}
1210+
1211+
/// Returns true if the dialect supports the `RESET` statement
1212+
/// for resetting session variables.
1213+
///
1214+
/// ```sql
1215+
/// RESET configuration_parameter;
1216+
/// RESET ALL;
1217+
/// ```
1218+
fn supports_reset(&self) -> bool {
1219+
false
1220+
}
12101221
}
12111222

12121223
/// This represents the operators for which precedence must be defined

src/dialect/postgresql.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,4 +280,11 @@ impl Dialect for PostgreSqlDialect {
280280
fn supports_interval_options(&self) -> bool {
281281
true
282282
}
283+
284+
/// Postgres supports the `RESET` statement for resetting session variables.
285+
///
286+
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-reset.html)
287+
fn supports_reset(&self) -> bool {
288+
true
289+
}
283290
}

src/parser/mod.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ impl<'a> Parser<'a> {
656656
self.prev_token();
657657
self.parse_vacuum()
658658
}
659+
Keyword::RESET if self.dialect.supports_reset() => self.parse_reset(),
659660
_ => self.expected("an SQL statement", next_token),
660661
},
661662
Token::LParen => {
@@ -17723,6 +17724,18 @@ impl<'a> Parser<'a> {
1772317724
_ => self.expected("expected option value", self.peek_token()),
1772417725
}
1772517726
}
17727+
17728+
fn parse_reset(&mut self) -> Result<Statement, ParserError> {
17729+
// RESET { ALL | <configuration_parameter> }
17730+
if self.parse_keyword(Keyword::ALL) {
17731+
return Ok(Statement::Reset(ResetStatement { reset: Reset::ALL }));
17732+
}
17733+
17734+
let obj = self.parse_object_name(false)?;
17735+
Ok(Statement::Reset(ResetStatement {
17736+
reset: Reset::ConfigurationParameter(obj),
17737+
}))
17738+
}
1772617739
}
1772717740

1772817741
fn maybe_prefixed_expr(expr: Expr, prefix: Option<Ident>) -> Expr {
@@ -18529,4 +18542,29 @@ mod tests {
1852918542
assert!(Parser::parse_sql(&GenericDialect, &sql).is_err());
1853018543
}
1853118544
}
18545+
18546+
#[test]
18547+
fn test_reset_all() {
18548+
let sql = "RESET ALL";
18549+
let ast = Parser::parse_sql(&PostgreSqlDialect {}, sql).unwrap();
18550+
assert_eq!(
18551+
ast,
18552+
vec![Statement::Reset(ResetStatement { reset: Reset::ALL })]
18553+
);
18554+
}
18555+
18556+
#[test]
18557+
fn test_reset_parameter() {
18558+
for w in ["parameter_name", "extension.parameter_name"] {
18559+
let sql = format!("RESET {w}");
18560+
let parts = w.split(".").map(|s| s.into()).collect::<Vec<Ident>>();
18561+
let ast = Parser::parse_sql(&PostgreSqlDialect {}, &sql).unwrap();
18562+
assert_eq!(
18563+
ast,
18564+
vec![Statement::Reset(ResetStatement {
18565+
reset: Reset::ConfigurationParameter(ObjectName::from(parts))
18566+
})]
18567+
);
18568+
}
18569+
}
1853218570
}

tests/sqlparser_postgres.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6652,3 +6652,30 @@ fn parse_foreign_key_match_with_actions() {
66526652

66536653
pg_and_generic().verified_stmt(sql);
66546654
}
6655+
6656+
#[test]
6657+
fn parse_reset_statement() {
6658+
match pg().verified_stmt("RESET some_parameter") {
6659+
Statement::Reset(ResetStatement { reset }) => match reset {
6660+
Reset::ConfigurationParameter(o) => {
6661+
assert_eq!(o, ObjectName::from(vec!["some_parameter".into()]))
6662+
}
6663+
_ => unreachable!(),
6664+
},
6665+
_ => unreachable!(),
6666+
}
6667+
match pg().verified_stmt("RESET some_extension.some_parameter") {
6668+
Statement::Reset(ResetStatement { reset }) => match reset {
6669+
Reset::ConfigurationParameter(o) => assert_eq!(
6670+
o,
6671+
ObjectName::from(vec!["some_extension".into(), "some_parameter".into()])
6672+
),
6673+
_ => unreachable!(),
6674+
},
6675+
_ => unreachable!(),
6676+
}
6677+
match pg().verified_stmt("RESET ALL") {
6678+
Statement::Reset(ResetStatement { reset }) => assert_eq!(reset, Reset::ALL),
6679+
_ => unreachable!(),
6680+
}
6681+
}

0 commit comments

Comments
 (0)