-
Notifications
You must be signed in to change notification settings - Fork 576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Postgres: support for OWNER TO
clause
#1314
Changes from 9 commits
af560ee
c351eb8
e4d1e95
1d5f01d
c56120e
5c21442
4d06a61
52c162d
6076c2d
a3a4996
25d6a40
0ff17d7
4180911
210c5c3
4340c65
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -157,6 +157,28 @@ pub enum AlterTableOperation { | |||||||||||||||||||||
SwapWith { table_name: ObjectName }, | ||||||||||||||||||||||
/// 'SET TBLPROPERTIES ( { property_key [ = ] property_val } [, ...] )' | ||||||||||||||||||||||
SetTblProperties { table_properties: Vec<SqlOption> }, | ||||||||||||||||||||||
|
||||||||||||||||||||||
/// `OWNER TO { <new_owner> | CURRENT_ROLE | CURRENT_USER | SESSION_USER }` | ||||||||||||||||||||||
/// | ||||||||||||||||||||||
/// Note: this is a PostgreSQL-specific operation. | ||||||||||||||||||||||
OwnerTo { new_owner: Owner }, | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] | ||||||||||||||||||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||||||||||||||||||||||
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))] | ||||||||||||||||||||||
pub enum Owner { | ||||||||||||||||||||||
Ident(Ident), | ||||||||||||||||||||||
Expr(Expr), | ||||||||||||||||||||||
} | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I'm thinking the Expr might be somewhat opaque to represent the others, from the docs the other variants seem enumerable so figured we can represent them verbatim in the enum as above? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @iffyio Thanks for the review! Currently
I think it would be better to maintain this compatibility. Additionally, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I see, I think it makes a lot of sense to represent them as functions when parsing arbitrary expressions - when parsing an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree it is better to be explicit and match the allowed postgres syntax more exactly. I double checked https://www.postgresql.org/docs/current/sql-altertable.html and it says:
So I think @iffyio 's suggestion is a good one. I took the liberty of making this change in 0ff17d7 as I am preparing for a sqlparser release |
||||||||||||||||||||||
|
||||||||||||||||||||||
impl fmt::Display for Owner { | ||||||||||||||||||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||||||||||||||||||||||
match self { | ||||||||||||||||||||||
Owner::Ident(ident) => write!(f, "{}", ident), | ||||||||||||||||||||||
Owner::Expr(expr) => write!(f, "{}", expr), | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] | ||||||||||||||||||||||
|
@@ -322,6 +344,9 @@ impl fmt::Display for AlterTableOperation { | |||||||||||||||||||||
AlterTableOperation::SwapWith { table_name } => { | ||||||||||||||||||||||
write!(f, "SWAP WITH {table_name}") | ||||||||||||||||||||||
} | ||||||||||||||||||||||
AlterTableOperation::OwnerTo { new_owner } => { | ||||||||||||||||||||||
write!(f, "OWNER TO {new_owner}") | ||||||||||||||||||||||
} | ||||||||||||||||||||||
AlterTableOperation::SetTblProperties { table_properties } => { | ||||||||||||||||||||||
write!( | ||||||||||||||||||||||
f, | ||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -508,6 +508,7 @@ define_keywords!( | |
OVERLAY, | ||
OVERWRITE, | ||
OWNED, | ||
OWNER, | ||
PARALLEL, | ||
PARAMETER, | ||
PARQUET, | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -6274,6 +6274,30 @@ impl<'a> Parser<'a> { | |||||
self.expect_keyword(Keyword::WITH)?; | ||||||
let table_name = self.parse_object_name(false)?; | ||||||
AlterTableOperation::SwapWith { table_name } | ||||||
} else if dialect_of!(self is PostgreSqlDialect) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
if no conflicts we can include support for the generic dialect There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 25d6a40 |
||||||
&& self.parse_keywords(&[Keyword::OWNER, Keyword::TO]) | ||||||
{ | ||||||
let next_token = self.next_token(); | ||||||
let new_owner = match next_token.token { | ||||||
Token::DoubleQuotedString(ref s) => Owner::Ident(Ident::new(s.to_string())), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of having two separate cases (this DoubleQuotedString and NoKeyword below) to handle the ident variant, would it make sense to call match self.parse_one_of_keywords(&[CURRENT_USER, CURRENT_ROLE, SESSION_USER]) {
Some(CURRENT_USER) => {}
...
_ =|> self.parse_identifier()
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in 0ff17d7 |
||||||
Token::Word(ref w) => match w.keyword { | ||||||
Keyword::CURRENT_USER | Keyword::CURRENT_ROLE | Keyword::SESSION_USER => { | ||||||
Owner::Expr(Expr::Function(Function { | ||||||
name: ObjectName(vec![w.to_ident()]), | ||||||
args: FunctionArguments::None, | ||||||
null_treatment: None, | ||||||
filter: None, | ||||||
over: None, | ||||||
within_group: vec![], | ||||||
})) | ||||||
}, | ||||||
Keyword::NoKeyword => Owner::Ident(w.to_ident()), | ||||||
_ => self.expected("CURRENT_USER, CURRENT_ROLE, SESSION_USER or identifier after OWNER TO clause", next_token)?, | ||||||
}, | ||||||
_ => self.expected("Token::Word", next_token)? | ||||||
}; | ||||||
|
||||||
AlterTableOperation::OwnerTo { new_owner } | ||||||
} else { | ||||||
let options: Vec<SqlOption> = | ||||||
self.parse_options_with_keywords(&[Keyword::SET, Keyword::TBLPROPERTIES])?; | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we include a link to the docs for the feature? I think it'd be this one https://www.postgresql.org/docs/current/sql-altertable.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done in a3a4996