Skip to content

Commit 1423a0c

Browse files
committed
feat: Support SET TIME ZONE syntax
Signed-off-by: Alex Qyoun-ae <4062971+MazterQyou@users.noreply.github.com>
1 parent 16f0514 commit 1423a0c

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

src/ast/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,16 @@ pub enum Statement {
10451045
charset_name: String,
10461046
collation_name: Option<String>,
10471047
},
1048+
/// SET [ SESSION | LOCAL ] TIME ZONE timezone
1049+
///
1050+
/// Note: this is a PostgreSQL-specific statement,
1051+
/// but may also compatible with other SQL.
1052+
SetTimeZone {
1053+
local: bool,
1054+
// SESSION is the default if neither SESSION nor LOCAL appears.
1055+
session: bool,
1056+
timezone: Expr,
1057+
},
10481058
/// SHOW <variable>
10491059
///
10501060
/// Note: this is a PostgreSQL-specific statement.
@@ -1849,6 +1859,20 @@ impl fmt::Display for Statement {
18491859

18501860
Ok(())
18511861
}
1862+
Statement::SetTimeZone {
1863+
local,
1864+
session,
1865+
timezone,
1866+
} => {
1867+
let modifier = if *local {
1868+
"LOCAL "
1869+
} else if *session {
1870+
"SESSION "
1871+
} else {
1872+
""
1873+
};
1874+
write!(f, "SET {}TIME ZONE {}", modifier, timezone)
1875+
}
18521876
Statement::ShowVariable { variable } => {
18531877
write!(f, "SHOW")?;
18541878
if !variable.is_empty() {

src/parser.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3769,6 +3769,19 @@ impl<'a> Parser<'a> {
37693769
});
37703770
}
37713771

3772+
if dialect_of!(self is PostgreSqlDialect | RedshiftSqlDialect)
3773+
&& self.parse_keywords(&[Keyword::TIME, Keyword::ZONE])
3774+
{
3775+
let local = modifier == Some(Keyword::LOCAL);
3776+
let session = modifier == Some(Keyword::SESSION);
3777+
let timezone = self.parse_expr()?;
3778+
return Ok(Statement::SetTimeZone {
3779+
local,
3780+
session,
3781+
timezone,
3782+
});
3783+
}
3784+
37723785
let mut key_values: Vec<SetVariableKeyValue> = vec![];
37733786

37743787
if dialect_of!(self is PostgreSqlDialect | RedshiftSqlDialect) {

tests/sqlparser_postgres.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,45 @@ fn parse_set_role() {
949949
);
950950
}
951951

952+
#[test]
953+
fn parse_set_time_zone() {
954+
let stmt = pg().verified_stmt("SET SESSION TIME ZONE DEFAULT");
955+
assert_eq!(
956+
stmt,
957+
Statement::SetTimeZone {
958+
local: false,
959+
session: true,
960+
timezone: Expr::Identifier(Ident {
961+
value: "DEFAULT".to_string(),
962+
quote_style: None,
963+
}),
964+
}
965+
);
966+
967+
let stmt = pg().verified_stmt("SET LOCAL TIME ZONE 'Europe/Rome'");
968+
assert_eq!(
969+
stmt,
970+
Statement::SetTimeZone {
971+
local: true,
972+
session: false,
973+
timezone: Expr::Value(Value::SingleQuotedString("Europe/Rome".to_string())),
974+
}
975+
);
976+
977+
let stmt = pg().verified_stmt("SET TIME ZONE LOCAL");
978+
assert_eq!(
979+
stmt,
980+
Statement::SetTimeZone {
981+
local: false,
982+
session: false,
983+
timezone: Expr::Identifier(Ident {
984+
value: "LOCAL".to_string(),
985+
quote_style: None,
986+
}),
987+
}
988+
);
989+
}
990+
952991
#[test]
953992
fn parse_show() {
954993
let stmt = pg_and_generic().verified_stmt("SHOW a a");

0 commit comments

Comments
 (0)