Skip to content

Commit 62f6fd7

Browse files
watford-epayman-sigma
authored andcommitted
feat: Add support for SET SESSION AUTHORIZATION apache#2086 (apache#2087)
1 parent 475a88d commit 62f6fd7

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
@@ -2953,6 +2953,15 @@ pub enum Set {
29532953
/// MySQL-style
29542954
/// SET a = 1, b = 2, ..;
29552955
MultipleAssignments { assignments: Vec<SetAssignment> },
2956+
/// Session authorization for Postgres/Redshift
2957+
///
2958+
/// ```sql
2959+
/// SET SESSION AUTHORIZATION { user_name | DEFAULT }
2960+
/// ```
2961+
///
2962+
/// See <https://www.postgresql.org/docs/current/sql-set-session-authorization.html>
2963+
/// See <https://docs.aws.amazon.com/redshift/latest/dg/r_SET_SESSION_AUTHORIZATION.html>
2964+
SetSessionAuthorization(SetSessionAuthorizationParam),
29562965
/// MS-SQL session
29572966
///
29582967
/// See <https://learn.microsoft.com/en-us/sql/t-sql/statements/set-statements-transact-sql>
@@ -3027,6 +3036,7 @@ impl Display for Set {
30273036
modifier = context_modifier.map(|m| format!("{m}")).unwrap_or_default()
30283037
)
30293038
}
3039+
Self::SetSessionAuthorization(kind) => write!(f, "SET SESSION AUTHORIZATION {kind}"),
30303040
Self::SetSessionParam(kind) => write!(f, "SET {kind}"),
30313041
Self::SetTransaction {
30323042
modes,
@@ -9856,6 +9866,42 @@ impl fmt::Display for TableObject {
98569866
}
98579867
}
98589868

9869+
/// Represents a SET SESSION AUTHORIZATION statement
9870+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
9871+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9872+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
9873+
pub struct SetSessionAuthorizationParam {
9874+
pub scope: ContextModifier,
9875+
pub kind: SetSessionAuthorizationParamKind,
9876+
}
9877+
9878+
impl fmt::Display for SetSessionAuthorizationParam {
9879+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9880+
write!(f, "{}", self.kind)
9881+
}
9882+
}
9883+
9884+
/// Represents the parameter kind for SET SESSION AUTHORIZATION
9885+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
9886+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9887+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
9888+
pub enum SetSessionAuthorizationParamKind {
9889+
/// Default authorization
9890+
Default,
9891+
9892+
/// User name
9893+
User(Ident),
9894+
}
9895+
9896+
impl fmt::Display for SetSessionAuthorizationParamKind {
9897+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9898+
match self {
9899+
SetSessionAuthorizationParamKind::Default => write!(f, "DEFAULT"),
9900+
SetSessionAuthorizationParamKind::User(name) => write!(f, "{}", name),
9901+
}
9902+
}
9903+
}
9904+
98599905
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
98609906
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
98619907
#[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
@@ -13143,6 +13143,18 @@ impl<'a> Parser<'a> {
1314313143
session: false,
1314413144
}
1314513145
.into());
13146+
} else if self.parse_keyword(Keyword::AUTHORIZATION) {
13147+
let auth_value = if self.parse_keyword(Keyword::DEFAULT) {
13148+
SetSessionAuthorizationParamKind::Default
13149+
} else {
13150+
let value = self.parse_identifier()?;
13151+
SetSessionAuthorizationParamKind::User(value)
13152+
};
13153+
return Ok(Set::SetSessionAuthorization(SetSessionAuthorizationParam {
13154+
scope: scope.expect("SET ... AUTHORIZATION must have a scope"),
13155+
kind: auth_value,
13156+
})
13157+
.into());
1314613158
}
1314713159

1314813160
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
@@ -17698,3 +17698,28 @@ fn parse_reset_statement() {
1769817698
_ => unreachable!(),
1769917699
}
1770017700
}
17701+
17702+
#[test]
17703+
fn test_parse_set_session_authorization() {
17704+
let stmt = verified_stmt("SET SESSION AUTHORIZATION DEFAULT");
17705+
assert_eq!(
17706+
stmt,
17707+
Statement::Set(Set::SetSessionAuthorization(SetSessionAuthorizationParam {
17708+
scope: ContextModifier::Session,
17709+
kind: SetSessionAuthorizationParamKind::Default,
17710+
}))
17711+
);
17712+
17713+
let stmt = verified_stmt("SET SESSION AUTHORIZATION 'username'");
17714+
assert_eq!(
17715+
stmt,
17716+
Statement::Set(Set::SetSessionAuthorization(SetSessionAuthorizationParam {
17717+
scope: ContextModifier::Session,
17718+
kind: SetSessionAuthorizationParamKind::User(Ident {
17719+
value: "username".to_string(),
17720+
quote_style: Some('\''),
17721+
span: Span::empty(),
17722+
}),
17723+
}))
17724+
);
17725+
}

0 commit comments

Comments
 (0)