Skip to content

Commit 4d56b06

Browse files
committed
Fix #2086: add support for set session authorization
1 parent 308a723 commit 4d56b06

3 files changed

Lines changed: 83 additions & 0 deletions

File tree

src/ast/mod.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2924,6 +2924,15 @@ pub enum Set {
29242924
/// MySQL-style
29252925
/// SET a = 1, b = 2, ..;
29262926
MultipleAssignments { assignments: Vec<SetAssignment> },
2927+
/// Session authorization for Postgres/Redshift
2928+
///
2929+
/// ```sql
2930+
/// SET SESSION AUTHORIZATION { user_name | DEFAULT }
2931+
/// ```
2932+
///
2933+
/// See <https://www.postgresql.org/docs/current/sql-set-session-authorization.html>
2934+
/// See <https://docs.aws.amazon.com/redshift/latest/dg/r_SET_SESSION_AUTHORIZATION.html>
2935+
SetSessionAuthorization(SetSessionAuthorizationParam),
29272936
/// MS-SQL session
29282937
///
29292938
/// See <https://learn.microsoft.com/en-us/sql/t-sql/statements/set-statements-transact-sql>
@@ -2998,6 +3007,7 @@ impl Display for Set {
29983007
modifier = context_modifier.map(|m| format!("{m}")).unwrap_or_default()
29993008
)
30003009
}
3010+
Self::SetSessionAuthorization(kind) => write!(f, "SET SESSION AUTHORIZATION {kind}"),
30013011
Self::SetSessionParam(kind) => write!(f, "SET {kind}"),
30023012
Self::SetTransaction {
30033013
modes,
@@ -9818,6 +9828,42 @@ impl fmt::Display for TableObject {
98189828
}
98199829
}
98209830

9831+
/// Represents a SET SESSION AUTHORIZATION statement
9832+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
9833+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9834+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
9835+
pub struct SetSessionAuthorizationParam {
9836+
pub scope: ContextModifier,
9837+
pub kind: SetSessionAuthorizationParamKind,
9838+
}
9839+
9840+
impl fmt::Display for SetSessionAuthorizationParam {
9841+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9842+
write!(f, "{}", self.kind)
9843+
}
9844+
}
9845+
9846+
/// Represents the parameter kind for SET SESSION AUTHORIZATION
9847+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
9848+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9849+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
9850+
pub enum SetSessionAuthorizationParamKind {
9851+
/// Default authorization
9852+
Default,
9853+
9854+
/// User name
9855+
User(Ident),
9856+
}
9857+
9858+
impl fmt::Display for SetSessionAuthorizationParamKind {
9859+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9860+
match self {
9861+
SetSessionAuthorizationParamKind::Default => write!(f, "DEFAULT"),
9862+
SetSessionAuthorizationParamKind::User(name) => write!(f, "{}", name),
9863+
}
9864+
}
9865+
}
9866+
98219867
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
98229868
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
98239869
#[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
@@ -13104,6 +13104,18 @@ impl<'a> Parser<'a> {
1310413104
session: false,
1310513105
}
1310613106
.into());
13107+
} else if self.parse_keyword(Keyword::AUTHORIZATION) {
13108+
let auth_value = if self.parse_keyword(Keyword::DEFAULT) {
13109+
SetSessionAuthorizationParamKind::Default
13110+
} else {
13111+
let value = self.parse_identifier()?;
13112+
SetSessionAuthorizationParamKind::User(value)
13113+
};
13114+
return Ok(Set::SetSessionAuthorization(SetSessionAuthorizationParam {
13115+
scope: scope.expect("SET ... AUTHORIZATION must have a scope"),
13116+
kind: auth_value,
13117+
})
13118+
.into());
1310713119
}
1310813120

1310913121
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
@@ -17632,3 +17632,28 @@ fn parse_generic_unary_ops() {
1763217632
);
1763317633
}
1763417634
}
17635+
17636+
#[test]
17637+
fn test_parse_set_session_authorization() {
17638+
let stmt = verified_stmt("SET SESSION AUTHORIZATION DEFAULT");
17639+
assert_eq!(
17640+
stmt,
17641+
Statement::Set(Set::SetSessionAuthorization(SetSessionAuthorizationParam {
17642+
scope: ContextModifier::Session,
17643+
kind: SetSessionAuthorizationParamKind::Default,
17644+
}))
17645+
);
17646+
17647+
let stmt = verified_stmt("SET SESSION AUTHORIZATION 'username'");
17648+
assert_eq!(
17649+
stmt,
17650+
Statement::Set(Set::SetSessionAuthorization(SetSessionAuthorizationParam {
17651+
scope: ContextModifier::Session,
17652+
kind: SetSessionAuthorizationParamKind::User(Ident {
17653+
value: "username".to_string(),
17654+
quote_style: Some('\''),
17655+
span: Span::empty(),
17656+
}),
17657+
}))
17658+
);
17659+
}

0 commit comments

Comments
 (0)