Skip to content

Commit 40d87f8

Browse files
committed
Merge remote-tracking branch 'origin/main' into feat/catalog-extended
# Conflicts: # src/ast/ddl.rs # src/ast/mod.rs # src/ast/spans.rs # src/parser/mod.rs # tests/sqlparser_postgres.rs
2 parents 2be0202 + 2b1ec01 commit 40d87f8

6 files changed

Lines changed: 489 additions & 4 deletions

File tree

src/ast/ddl.rs

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6535,6 +6535,51 @@ impl fmt::Display for StatisticsKind {
65356535
StatisticsKind::NDistinct => write!(f, "ndistinct"),
65366536
StatisticsKind::Dependencies => write!(f, "dependencies"),
65376537
StatisticsKind::Mcv => write!(f, "mcv"),
6538+
/// The object kind targeted by a `SECURITY LABEL` statement.
6539+
///
6540+
/// See <https://www.postgresql.org/docs/current/sql-securitylabel.html>
6541+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6542+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6543+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6544+
pub enum SecurityLabelObjectKind {
6545+
/// `TABLE name`
6546+
Table,
6547+
/// `COLUMN name.colname`
6548+
Column,
6549+
/// `DATABASE name`
6550+
Database,
6551+
/// `DOMAIN name`
6552+
Domain,
6553+
/// `FUNCTION name`
6554+
Function,
6555+
/// `ROLE name`
6556+
Role,
6557+
/// `SCHEMA name`
6558+
Schema,
6559+
/// `SEQUENCE name`
6560+
Sequence,
6561+
/// `TYPE name`
6562+
Type,
6563+
/// `VIEW name`
6564+
View,
6565+
/// `MATERIALIZED VIEW name`
6566+
MaterializedView,
6567+
}
6568+
6569+
impl fmt::Display for SecurityLabelObjectKind {
6570+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6571+
match self {
6572+
SecurityLabelObjectKind::Table => write!(f, "TABLE"),
6573+
SecurityLabelObjectKind::Column => write!(f, "COLUMN"),
6574+
SecurityLabelObjectKind::Database => write!(f, "DATABASE"),
6575+
SecurityLabelObjectKind::Domain => write!(f, "DOMAIN"),
6576+
SecurityLabelObjectKind::Function => write!(f, "FUNCTION"),
6577+
SecurityLabelObjectKind::Role => write!(f, "ROLE"),
6578+
SecurityLabelObjectKind::Schema => write!(f, "SCHEMA"),
6579+
SecurityLabelObjectKind::Sequence => write!(f, "SEQUENCE"),
6580+
SecurityLabelObjectKind::Type => write!(f, "TYPE"),
6581+
SecurityLabelObjectKind::View => write!(f, "VIEW"),
6582+
SecurityLabelObjectKind::MaterializedView => write!(f, "MATERIALIZED VIEW"),
65386583
}
65396584
}
65406585
}
@@ -6571,6 +6616,108 @@ impl fmt::Display for CreateStatistics {
65716616
}
65726617
write!(f, " ON {}", display_comma_separated(&self.on))?;
65736618
write!(f, " FROM {}", self.from)?;
6619+
/// A `SECURITY LABEL` statement.
6620+
///
6621+
/// Note: this is a PostgreSQL-specific statement.
6622+
/// <https://www.postgresql.org/docs/current/sql-securitylabel.html>
6623+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6624+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6625+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6626+
pub struct SecurityLabel {
6627+
/// Optional `FOR provider_name` clause.
6628+
pub provider: Option<Ident>,
6629+
/// The kind of object the label is applied to.
6630+
pub object_kind: SecurityLabelObjectKind,
6631+
/// The name of the object the label is applied to.
6632+
pub object_name: ObjectName,
6633+
/// The label string, or `None` for `IS NULL`.
6634+
pub label: Option<Value>,
6635+
}
6636+
6637+
impl fmt::Display for SecurityLabel {
6638+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6639+
write!(f, "SECURITY LABEL")?;
6640+
if let Some(provider) = &self.provider {
6641+
write!(f, " FOR {provider}")?;
6642+
}
6643+
write!(f, " ON {} {}", self.object_kind, self.object_name)?;
6644+
write!(f, " IS ")?;
6645+
match &self.label {
6646+
Some(label) => write!(f, "{label}"),
6647+
None => write!(f, "NULL"),
6648+
}
6649+
}
6650+
}
6651+
6652+
impl From<SecurityLabel> for crate::ast::Statement {
6653+
fn from(v: SecurityLabel) -> Self {
6654+
crate::ast::Statement::SecurityLabel(v)
6655+
}
6656+
}
6657+
6658+
/// The role specification in a `CREATE USER MAPPING` statement.
6659+
///
6660+
/// See <https://www.postgresql.org/docs/current/sql-createusermapping.html>
6661+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6662+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6663+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6664+
pub enum UserMappingUser {
6665+
/// A specific role name.
6666+
Ident(Ident),
6667+
/// `USER` (current user)
6668+
User,
6669+
/// `CURRENT_ROLE`
6670+
CurrentRole,
6671+
/// `CURRENT_USER`
6672+
CurrentUser,
6673+
/// `PUBLIC`
6674+
Public,
6675+
}
6676+
6677+
impl fmt::Display for UserMappingUser {
6678+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6679+
match self {
6680+
UserMappingUser::Ident(ident) => write!(f, "{ident}"),
6681+
UserMappingUser::User => write!(f, "USER"),
6682+
UserMappingUser::CurrentRole => write!(f, "CURRENT_ROLE"),
6683+
UserMappingUser::CurrentUser => write!(f, "CURRENT_USER"),
6684+
UserMappingUser::Public => write!(f, "PUBLIC"),
6685+
}
6686+
}
6687+
}
6688+
6689+
/// A `CREATE USER MAPPING` statement.
6690+
///
6691+
/// Note: this is a PostgreSQL-specific statement.
6692+
/// <https://www.postgresql.org/docs/current/sql-createusermapping.html>
6693+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6694+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6695+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6696+
pub struct CreateUserMapping {
6697+
/// `IF NOT EXISTS`
6698+
pub if_not_exists: bool,
6699+
/// The user/role for the mapping.
6700+
pub user: UserMappingUser,
6701+
/// The foreign server name.
6702+
pub server_name: Ident,
6703+
/// Optional `OPTIONS (key 'value', ...)` clause.
6704+
pub options: Option<Vec<CreateServerOption>>,
6705+
}
6706+
6707+
impl fmt::Display for CreateUserMapping {
6708+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6709+
write!(f, "CREATE USER MAPPING")?;
6710+
if self.if_not_exists {
6711+
write!(f, " IF NOT EXISTS")?;
6712+
}
6713+
write!(f, " FOR {} SERVER {}", self.user, self.server_name)?;
6714+
if let Some(options) = &self.options {
6715+
write!(
6716+
f,
6717+
" OPTIONS ({})",
6718+
display_comma_separated(options)
6719+
)?;
6720+
}
65746721
Ok(())
65756722
}
65766723
}
@@ -6699,6 +6846,40 @@ impl fmt::Display for CreateEventTrigger {
66996846
"FUNCTION"
67006847
};
67016848
write!(f, " EXECUTE {func_kw} {}()", self.execute)?;
6849+
impl From<CreateUserMapping> for crate::ast::Statement {
6850+
fn from(v: CreateUserMapping) -> Self {
6851+
crate::ast::Statement::CreateUserMapping(v)
6852+
}
6853+
}
6854+
6855+
/// A `CREATE TABLESPACE` statement.
6856+
///
6857+
/// Note: this is a PostgreSQL-specific statement.
6858+
/// <https://www.postgresql.org/docs/current/sql-createtablespace.html>
6859+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6860+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6861+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6862+
pub struct CreateTablespace {
6863+
/// The tablespace name.
6864+
pub name: Ident,
6865+
/// Optional `OWNER role` clause.
6866+
pub owner: Option<Ident>,
6867+
/// The `LOCATION 'directory'` string.
6868+
pub location: Value,
6869+
/// Optional `WITH (option = value, ...)` clause.
6870+
pub with_options: Vec<SqlOption>,
6871+
}
6872+
6873+
impl fmt::Display for CreateTablespace {
6874+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6875+
write!(f, "CREATE TABLESPACE {}", self.name)?;
6876+
if let Some(owner) = &self.owner {
6877+
write!(f, " OWNER {owner}")?;
6878+
}
6879+
write!(f, " LOCATION {}", self.location)?;
6880+
if !self.with_options.is_empty() {
6881+
write!(f, " WITH ({})", display_comma_separated(&self.with_options))?;
6882+
}
67026883
Ok(())
67036884
}
67046885
}
@@ -6776,5 +6957,8 @@ impl fmt::Display for CreateTransform {
67766957
impl From<CreateTransform> for crate::ast::Statement {
67776958
fn from(v: CreateTransform) -> Self {
67786959
crate::ast::Statement::CreateTransform(v)
6960+
impl From<CreateTablespace> for crate::ast::Statement {
6961+
fn from(v: CreateTablespace) -> Self {
6962+
crate::ast::Statement::CreateTablespace(v)
67796963
}
67806964
}

src/ast/mod.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,11 @@ pub use self::ddl::{
7575
CreateDomain, CreateExtension, CreateForeignDataWrapper, CreateForeignTable, CreateFunction,
7676
CreateIndex, CreateOperator, CreateOperatorClass, CreateOperatorFamily, CreatePolicy,
7777
CreatePolicyCommand, CreatePolicyType, CreatePublication, CreateSubscription, CreateTable,
78+
AccessMethodType, CreateAccessMethod, CreateEventTrigger, CreateStatistics, CreateTablespace,
7879
CreateTextSearchConfiguration, CreateTextSearchDictionary, CreateTextSearchParser,
79-
CreateTextSearchTemplate, CreateTrigger, PublicationTarget,
80-
AccessMethodType, CreateAccessMethod, CreateEventTrigger, CreateStatistics, CreateTransform,
81-
EventTriggerEvent, StatisticsKind, TransformElement,
80+
CreateTextSearchTemplate, CreateTransform, CreateTrigger, CreateUserMapping,
81+
EventTriggerEvent, PublicationTarget, SecurityLabel, SecurityLabelObjectKind, StatisticsKind,
82+
TransformElement, UserMappingUser,
8283
CreateView, Deduplicate, DeferrableInitial, DistStyle, DropBehavior, DropExtension,
8384
DropFunction, DropOperator, DropOperatorClass, DropOperatorFamily, DropOperatorSignature,
8485
DropPolicy, DropTrigger, FdwRoutineClause, ForValues, FunctionReturnType, GeneratedAs,
@@ -4075,6 +4076,24 @@ pub enum Statement {
40754076
/// <https://www.postgresql.org/docs/current/sql-createtransform.html>
40764077
CreateTransform(CreateTransform),
40774078
/// ```sql
4079+
/// SECURITY LABEL [ FOR provider_name ] ON object_type object_name IS { 'label' | NULL }
4080+
/// ```
4081+
/// Note: this is a PostgreSQL-specific statement.
4082+
/// <https://www.postgresql.org/docs/current/sql-securitylabel.html>
4083+
SecurityLabel(SecurityLabel),
4084+
/// ```sql
4085+
/// CREATE USER MAPPING [ IF NOT EXISTS ] FOR { role | USER | CURRENT_ROLE | CURRENT_USER | PUBLIC } SERVER server_name [ OPTIONS (...) ]
4086+
/// ```
4087+
/// Note: this is a PostgreSQL-specific statement.
4088+
/// <https://www.postgresql.org/docs/current/sql-createusermapping.html>
4089+
CreateUserMapping(CreateUserMapping),
4090+
/// ```sql
4091+
/// CREATE TABLESPACE name [ OWNER role ] LOCATION 'directory' [ WITH (options) ]
4092+
/// ```
4093+
/// Note: this is a PostgreSQL-specific statement.
4094+
/// <https://www.postgresql.org/docs/current/sql-createtablespace.html>
4095+
CreateTablespace(CreateTablespace),
4096+
/// ```sql
40784097
/// DROP EXTENSION [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ]
40794098
/// ```
40804099
/// Note: this is a PostgreSQL-specific statement.
@@ -5567,6 +5586,9 @@ impl fmt::Display for Statement {
55675586
Statement::CreateAccessMethod(v) => write!(f, "{v}"),
55685587
Statement::CreateEventTrigger(v) => write!(f, "{v}"),
55695588
Statement::CreateTransform(v) => write!(f, "{v}"),
5589+
Statement::SecurityLabel(v) => write!(f, "{v}"),
5590+
Statement::CreateUserMapping(v) => write!(f, "{v}"),
5591+
Statement::CreateTablespace(v) => write!(f, "{v}"),
55705592
Statement::DropExtension(drop_extension) => write!(f, "{drop_extension}"),
55715593
Statement::DropOperator(drop_operator) => write!(f, "{drop_operator}"),
55725594
Statement::DropOperatorFamily(drop_operator_family) => {

src/ast/spans.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@ impl Spanned for Statement {
394394
Statement::CreateAccessMethod(_) => Span::empty(),
395395
Statement::CreateEventTrigger(_) => Span::empty(),
396396
Statement::CreateTransform(_) => Span::empty(),
397+
Statement::SecurityLabel(_) => Span::empty(),
398+
Statement::CreateUserMapping(_) => Span::empty(),
399+
Statement::CreateTablespace(_) => Span::empty(),
397400
Statement::DropExtension(drop_extension) => drop_extension.span(),
398401
Statement::DropOperator(drop_operator) => drop_operator.span(),
399402
Statement::DropOperatorFamily(drop_operator_family) => drop_operator_family.span(),

src/keywords.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@ define_keywords!(
567567
KEYS,
568568
KEY_BLOCK_SIZE,
569569
KILL,
570+
LABEL,
570571
LAG,
571572
LAMBDA,
572573
LANGUAGE,
@@ -616,6 +617,7 @@ define_keywords!(
616617
MANAGEDLOCATION,
617618
MANIFEST,
618619
MAP,
620+
MAPPING,
619621
MASKING,
620622
MATCH,
621623
MATCHED,

0 commit comments

Comments
 (0)