Skip to content

Commit a00d5cd

Browse files
authored
feat: Add support for SET SESSION AUTHORIZATION #2086 (#2087)
1 parent f69407b commit a00d5cd

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

src/ast/mod.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2919,6 +2919,15 @@ pub enum Set {
29192919
/// MySQL-style
29202920
/// SET a = 1, b = 2, ..;
29212921
MultipleAssignments { assignments: Vec<SetAssignment> },
2922+
/// Session authorization for Postgres/Redshift
2923+
///
2924+
/// ```sql
2925+
/// SET SESSION AUTHORIZATION { user_name | DEFAULT }
2926+
/// ```
2927+
///
2928+
/// See <https://www.postgresql.org/docs/current/sql-set-session-authorization.html>
2929+
/// See <https://docs.aws.amazon.com/redshift/latest/dg/r_SET_SESSION_AUTHORIZATION.html>
2930+
SetSessionAuthorization(SetSessionAuthorizationParam),
29222931
/// MS-SQL session
29232932
///
29242933
/// See <https://learn.microsoft.com/en-us/sql/t-sql/statements/set-statements-transact-sql>
@@ -2993,6 +3002,7 @@ impl Display for Set {
29933002
modifier = context_modifier.map(|m| format!("{m}")).unwrap_or_default()
29943003
)
29953004
}
3005+
Self::SetSessionAuthorization(kind) => write!(f, "SET SESSION AUTHORIZATION {kind}"),
29963006
Self::SetSessionParam(kind) => write!(f, "SET {kind}"),
29973007
Self::SetTransaction {
29983008
modes,
@@ -9822,6 +9832,42 @@ impl fmt::Display for TableObject {
98229832
}
98239833
}
98249834

9835+
/// Represents a SET SESSION AUTHORIZATION statement
9836+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
9837+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9838+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
9839+
pub struct SetSessionAuthorizationParam {
9840+
pub scope: ContextModifier,
9841+
pub kind: SetSessionAuthorizationParamKind,
9842+
}
9843+
9844+
impl fmt::Display for SetSessionAuthorizationParam {
9845+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9846+
write!(f, "{}", self.kind)
9847+
}
9848+
}
9849+
9850+
/// Represents the parameter kind for SET SESSION AUTHORIZATION
9851+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
9852+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9853+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
9854+
pub enum SetSessionAuthorizationParamKind {
9855+
/// Default authorization
9856+
Default,
9857+
9858+
/// User name
9859+
User(Ident),
9860+
}
9861+
9862+
impl fmt::Display for SetSessionAuthorizationParamKind {
9863+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9864+
match self {
9865+
SetSessionAuthorizationParamKind::Default => write!(f, "DEFAULT"),
9866+
SetSessionAuthorizationParamKind::User(name) => write!(f, "{}", name),
9867+
}
9868+
}
9869+
}
9870+
98259871
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
98269872
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
98279873
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]

src/parser/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13112,6 +13112,18 @@ impl<'a> Parser<'a> {
1311213112
session: false,
1311313113
}
1311413114
.into());
13115+
} else if self.parse_keyword(Keyword::AUTHORIZATION) {
13116+
let auth_value = if self.parse_keyword(Keyword::DEFAULT) {
13117+
SetSessionAuthorizationParamKind::Default
13118+
} else {
13119+
let value = self.parse_identifier()?;
13120+
SetSessionAuthorizationParamKind::User(value)
13121+
};
13122+
return Ok(Set::SetSessionAuthorization(SetSessionAuthorizationParam {
13123+
scope: scope.expect("SET ... AUTHORIZATION must have a scope"),
13124+
kind: auth_value,
13125+
})
13126+
.into());
1311513127
}
1311613128

1311713129
if self.dialect.supports_comma_separated_set_assignments() {

tests/sqlparser_common.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17672,3 +17672,28 @@ fn parse_reset_statement() {
1767217672
_ => unreachable!(),
1767317673
}
1767417674
}
17675+
17676+
#[test]
17677+
fn test_parse_set_session_authorization() {
17678+
let stmt = verified_stmt("SET SESSION AUTHORIZATION DEFAULT");
17679+
assert_eq!(
17680+
stmt,
17681+
Statement::Set(Set::SetSessionAuthorization(SetSessionAuthorizationParam {
17682+
scope: ContextModifier::Session,
17683+
kind: SetSessionAuthorizationParamKind::Default,
17684+
}))
17685+
);
17686+
17687+
let stmt = verified_stmt("SET SESSION AUTHORIZATION 'username'");
17688+
assert_eq!(
17689+
stmt,
17690+
Statement::Set(Set::SetSessionAuthorization(SetSessionAuthorizationParam {
17691+
scope: ContextModifier::Session,
17692+
kind: SetSessionAuthorizationParamKind::User(Ident {
17693+
value: "username".to_string(),
17694+
quote_style: Some('\''),
17695+
span: Span::empty(),
17696+
}),
17697+
}))
17698+
);
17699+
}

0 commit comments

Comments
 (0)