Skip to content

Commit dbc3dab

Browse files
authored
Merge pull request #6 from amaksimo/feat/dsql-supported-syntax
Parse DSQL-supported PostgreSQL statements
2 parents 056bedb + 721b534 commit dbc3dab

9 files changed

Lines changed: 351 additions & 22 deletions

File tree

‎Cargo.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
[package]
1919
name = "sqlparser-dsql"
20-
description = "SQL parser fork with Aurora DSQL extensions (CREATE INDEX ASYNC, ALTER TABLE ASYNC, PostgreSQL constraint syntax, ALTER COLUMN DROP IDENTITY/EXPRESSION, ALTER COLUMN SET STORAGE, order-independent CREATE SEQUENCE, INCLUDE on table constraints). Based on sqlparser 0.62.0."
21-
version = "0.62.8"
20+
description = "SQL parser fork with Aurora DSQL extensions and DSQL-supported PostgreSQL syntax. Based on sqlparser 0.62.0."
21+
version = "0.62.9"
2222
authors = [
2323
"Apache DataFusion <dev@datafusion.apache.org>",
2424
"Amazon Web Services",

‎src/ast/dcl.rs‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,76 @@ impl From<Grant> for crate::ast::Statement {
485485
}
486486
}
487487

488+
/// An `ALTER DEFAULT PRIVILEGES` operation.
489+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
490+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
491+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
492+
pub enum AlterDefaultPrivilegesOperation {
493+
/// Grant default privileges on future tables.
494+
Grant {
495+
/// Privileges being granted.
496+
privileges: Privileges,
497+
/// Grantees receiving the privileges.
498+
grantees: Vec<Grantee>,
499+
/// Whether `WITH GRANT OPTION` is present.
500+
with_grant_option: bool,
501+
},
502+
/// Revoke default privileges on future tables.
503+
Revoke {
504+
/// Whether `GRANT OPTION FOR` is present.
505+
grant_option_for: bool,
506+
/// Privileges being revoked.
507+
privileges: Privileges,
508+
/// Grantees losing the privileges.
509+
grantees: Vec<Grantee>,
510+
/// Optional `CASCADE`/`RESTRICT` behavior.
511+
cascade: Option<CascadeOption>,
512+
},
513+
}
514+
515+
impl fmt::Display for AlterDefaultPrivilegesOperation {
516+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
517+
match self {
518+
Self::Grant {
519+
privileges,
520+
grantees,
521+
with_grant_option,
522+
} => {
523+
write!(
524+
f,
525+
"GRANT {privileges} ON TABLES TO {}",
526+
display_comma_separated(grantees)
527+
)?;
528+
if *with_grant_option {
529+
write!(f, " WITH GRANT OPTION")?;
530+
}
531+
Ok(())
532+
}
533+
Self::Revoke {
534+
grant_option_for,
535+
privileges,
536+
grantees,
537+
cascade,
538+
} => {
539+
write!(
540+
f,
541+
"REVOKE {}{privileges} ON TABLES FROM {}",
542+
if *grant_option_for {
543+
"GRANT OPTION FOR "
544+
} else {
545+
""
546+
},
547+
display_comma_separated(grantees)
548+
)?;
549+
if let Some(cascade) = cascade {
550+
write!(f, " {cascade}")?;
551+
}
552+
Ok(())
553+
}
554+
}
555+
}
556+
}
557+
488558
/// REVOKE privileges ON objects FROM grantees
489559
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
490560
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]

‎src/ast/ddl.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5546,13 +5546,16 @@ pub enum AlterFunctionKind {
55465546
Function,
55475547
/// `AGGREGATE`
55485548
Aggregate,
5549+
/// `ROUTINE`
5550+
Routine,
55495551
}
55505552

55515553
impl fmt::Display for AlterFunctionKind {
55525554
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55535555
match self {
55545556
Self::Function => write!(f, "FUNCTION"),
55555557
Self::Aggregate => write!(f, "AGGREGATE"),
5558+
Self::Routine => write!(f, "ROUTINE"),
55565559
}
55575560
}
55585561
}
@@ -5627,7 +5630,7 @@ impl fmt::Display for AlterFunction {
56275630
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56285631
write!(f, "ALTER {} ", self.kind)?;
56295632
match self.kind {
5630-
AlterFunctionKind::Function => {
5633+
AlterFunctionKind::Function | AlterFunctionKind::Routine => {
56315634
write!(f, "{} ", self.function)?;
56325635
}
56335636
AlterFunctionKind::Aggregate => {

‎src/ast/mod.rs‎

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ pub use self::data_type::{
5656
ExactNumberInfo, IntervalFields, StructBracketKind, TimezoneInfo,
5757
};
5858
pub use self::dcl::{
59-
AlterRoleOperation, CreateRole, Grant, ResetConfig, Revoke, RoleOption, SecondaryRoles,
60-
SetConfigValue, Use,
59+
AlterDefaultPrivilegesOperation, AlterRoleOperation, CreateRole, Grant, ResetConfig, Revoke,
60+
RoleOption, SecondaryRoles, SetConfigValue, Use,
6161
};
6262
pub use self::ddl::{
6363
Alignment, AlterCollation, AlterCollationOperation, AlterColumnOperation, AlterColumnStorage,
@@ -3956,6 +3956,17 @@ pub enum Statement {
39563956
/// ```
39573957
DropFunction(DropFunction),
39583958
/// ```sql
3959+
/// DROP ROUTINE
3960+
/// ```
3961+
DropRoutine {
3962+
/// `true` when `IF EXISTS` was present.
3963+
if_exists: bool,
3964+
/// One or more routines to drop.
3965+
routine_desc: Vec<FunctionDesc>,
3966+
/// Optional drop behavior (`CASCADE` or `RESTRICT`).
3967+
drop_behavior: Option<DropBehavior>,
3968+
},
3969+
/// ```sql
39593970
/// DROP DOMAIN
39603971
/// ```
39613972
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-dropdomain.html)
@@ -4319,6 +4330,24 @@ pub enum Statement {
43194330
/// See <https://docs.snowflake.com/en/sql-reference/sql/comment>
43204331
if_exists: bool,
43214332
},
4333+
/// PostgreSQL `COMMENT ON ROUTINE`.
4334+
CommentRoutine {
4335+
/// Routine signature.
4336+
routine: FunctionDesc,
4337+
/// Comment text, or `NULL` to remove it.
4338+
comment: Option<String>,
4339+
/// `true` when `IF EXISTS` was present.
4340+
if_exists: bool,
4341+
},
4342+
/// PostgreSQL `ALTER DEFAULT PRIVILEGES`.
4343+
AlterDefaultPrivileges {
4344+
/// Optional roles whose default privileges are changed.
4345+
target_roles: Vec<ObjectName>,
4346+
/// Optional schemas limiting the change.
4347+
schemas: Vec<ObjectName>,
4348+
/// Grant or revoke operation.
4349+
operation: AlterDefaultPrivilegesOperation,
4350+
},
43224351
/// ```sql
43234352
/// COMMIT [ TRANSACTION | WORK ] [ AND [ NO ] CHAIN ]
43244353
/// ```
@@ -4385,6 +4414,8 @@ pub enum Statement {
43854414
///
43864415
/// [Snowflake](https://docs.snowflake.com/en/sql-reference/sql/create-clone#databases-schemas)
43874416
clone: Option<ObjectName>,
4417+
/// Statements contained in the schema definition.
4418+
statements: Vec<Statement>,
43884419
},
43894420
/// ```sql
43904421
/// CREATE DATABASE
@@ -5676,6 +5707,22 @@ impl fmt::Display for Statement {
56765707
Ok(())
56775708
}
56785709
Statement::DropFunction(drop_function) => write!(f, "{drop_function}"),
5710+
Statement::DropRoutine {
5711+
if_exists,
5712+
routine_desc,
5713+
drop_behavior,
5714+
} => {
5715+
write!(
5716+
f,
5717+
"DROP ROUTINE{} {}",
5718+
if *if_exists { " IF EXISTS" } else { "" },
5719+
display_comma_separated(routine_desc),
5720+
)?;
5721+
if let Some(op) = drop_behavior {
5722+
write!(f, " {op}")?;
5723+
}
5724+
Ok(())
5725+
}
56795726
Statement::DropDomain(DropDomain {
56805727
if_exists,
56815728
name,
@@ -5984,6 +6031,7 @@ impl fmt::Display for Statement {
59846031
options,
59856032
default_collate_spec,
59866033
clone,
6034+
statements,
59876035
} => {
59886036
write!(
59896037
f,
@@ -6007,6 +6055,11 @@ impl fmt::Display for Statement {
60076055
if let Some(clone) = clone {
60086056
write!(f, " CLONE {clone}")?;
60096057
}
6058+
6059+
for statement in statements {
6060+
write!(f, " {statement}")?;
6061+
}
6062+
60106063
Ok(())
60116064
}
60126065
Statement::Assert { condition, message } => {
@@ -6197,6 +6250,35 @@ impl fmt::Display for Statement {
61976250
}
61986251
Ok(())
61996252
}
6253+
Statement::CommentRoutine {
6254+
routine,
6255+
comment,
6256+
if_exists,
6257+
} => {
6258+
write!(
6259+
f,
6260+
"COMMENT{} ON ROUTINE {routine} IS {}",
6261+
if *if_exists { " IF EXISTS" } else { "" },
6262+
comment
6263+
.as_ref()
6264+
.map(|c| format!("'{}'", value::escape_single_quote_string(c)))
6265+
.unwrap_or_else(|| "NULL".to_string())
6266+
)
6267+
}
6268+
Statement::AlterDefaultPrivileges {
6269+
target_roles,
6270+
schemas,
6271+
operation,
6272+
} => {
6273+
write!(f, "ALTER DEFAULT PRIVILEGES")?;
6274+
if !target_roles.is_empty() {
6275+
write!(f, " FOR ROLE {}", display_comma_separated(target_roles))?;
6276+
}
6277+
if !schemas.is_empty() {
6278+
write!(f, " IN SCHEMA {}", display_comma_separated(schemas))?;
6279+
}
6280+
write!(f, " {operation}")
6281+
}
62006282
Statement::CopyIntoSnowflake {
62016283
kind,
62026284
into,
@@ -7450,7 +7532,7 @@ impl fmt::Display for Grantee {
74507532
write!(f, "GROUP ")?;
74517533
}
74527534
GranteesType::Public => {
7453-
write!(f, "PUBLIC ")?;
7535+
write!(f, "PUBLIC{}", if self.name.is_some() { " " } else { "" })?;
74547536
}
74557537
GranteesType::DatabaseRole => {
74567538
write!(f, "DATABASE ROLE ")?;
@@ -7638,6 +7720,8 @@ pub enum GrantObjects {
76387720
/// Optional argument types for overloaded functions.
76397721
arg_types: Vec<DataType>,
76407722
},
7723+
/// Grant privileges on a routine.
7724+
Routine(FunctionDesc),
76417725
}
76427726

76437727
impl fmt::Display for GrantObjects {
@@ -7783,6 +7867,7 @@ impl fmt::Display for GrantObjects {
77837867
}
77847868
Ok(())
77857869
}
7870+
GrantObjects::Routine(routine) => write!(f, "ROUTINE {routine}"),
77867871
}
77877872
}
77887873
}

‎src/ast/query.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3561,6 +3561,8 @@ impl fmt::Display for LockClause {
35613561
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
35623562
/// The lock type used in `FOR <lock>` clauses (e.g. `FOR SHARE`, `FOR UPDATE`).
35633563
pub enum LockType {
3564+
/// `KEY SHARE` lock.
3565+
KeyShare,
35643566
/// `SHARE` lock (shared lock).
35653567
Share,
35663568
/// `UPDATE` lock (exclusive/update lock).
@@ -3570,6 +3572,7 @@ pub enum LockType {
35703572
impl fmt::Display for LockType {
35713573
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35723574
let select_lock = match self {
3575+
LockType::KeyShare => "KEY SHARE",
35733576
LockType::Share => "SHARE",
35743577
LockType::Update => "UPDATE",
35753578
};

‎src/ast/spans.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ impl Spanned for Statement {
426426
Statement::DetachDuckDBDatabase { .. } => Span::empty(),
427427
Statement::Drop { .. } => Span::empty(),
428428
Statement::DropFunction(drop_function) => drop_function.span(),
429+
Statement::DropRoutine { .. } => Span::empty(),
429430
Statement::DropDomain { .. } => Span::empty(),
430431
Statement::DropProcedure { .. } => Span::empty(),
431432
Statement::DropSecret { .. } => Span::empty(),
@@ -446,6 +447,8 @@ impl Spanned for Statement {
446447
Statement::Use(u) => u.span(),
447448
Statement::StartTransaction { .. } => Span::empty(),
448449
Statement::Comment { .. } => Span::empty(),
450+
Statement::CommentRoutine { .. } => Span::empty(),
451+
Statement::AlterDefaultPrivileges { .. } => Span::empty(),
449452
Statement::Commit { .. } => Span::empty(),
450453
Statement::Rollback { .. } => Span::empty(),
451454
Statement::CreateSchema { .. } => Span::empty(),

‎src/keywords.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,7 @@ define_keywords!(
904904
ROLLBACK,
905905
ROLLUP,
906906
ROOT,
907+
ROUTINE,
907908
ROW,
908909
ROWGROUPSIZE,
909910
ROWID,

0 commit comments

Comments
 (0)