Skip to content

Commit f8060f7

Browse files
authored
Merge branch 'main' into feat/export_data_for_bigquery
2 parents 1678c36 + 40b187f commit f8060f7

8 files changed

Lines changed: 343 additions & 94 deletions

File tree

src/ast/ddl.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,16 @@ pub enum AlterTableOperation {
351351
ValidateConstraint {
352352
name: Ident,
353353
},
354+
/// Arbitrary parenthesized `SET` options.
355+
///
356+
/// Example:
357+
/// ```sql
358+
/// SET (scale_factor = 0.01, threshold = 500)`
359+
/// ```
360+
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-altertable.html)
361+
SetOptionsParens {
362+
options: Vec<SqlOption>,
363+
},
354364
}
355365

356366
/// An `ALTER Policy` (`Statement::AlterPolicy`) operation
@@ -791,6 +801,9 @@ impl fmt::Display for AlterTableOperation {
791801
AlterTableOperation::ValidateConstraint { name } => {
792802
write!(f, "VALIDATE CONSTRAINT {name}")
793803
}
804+
AlterTableOperation::SetOptionsParens { options } => {
805+
write!(f, "SET ({})", display_comma_separated(options))
806+
}
794807
}
795808
}
796809
}

src/ast/helpers/key_value_options.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,22 @@ use serde::{Deserialize, Serialize};
3131
#[cfg(feature = "visitor")]
3232
use sqlparser_derive::{Visit, VisitMut};
3333

34+
use crate::ast::display_separated;
35+
3436
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
3537
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3638
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3739
pub struct KeyValueOptions {
3840
pub options: Vec<KeyValueOption>,
41+
pub delimiter: KeyValueOptionsDelimiter,
42+
}
43+
44+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
45+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
46+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
47+
pub enum KeyValueOptionsDelimiter {
48+
Space,
49+
Comma,
3950
}
4051

4152
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -59,18 +70,11 @@ pub struct KeyValueOption {
5970

6071
impl fmt::Display for KeyValueOptions {
6172
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
62-
if !self.options.is_empty() {
63-
let mut first = false;
64-
for option in &self.options {
65-
if !first {
66-
first = true;
67-
} else {
68-
f.write_str(" ")?;
69-
}
70-
write!(f, "{option}")?;
71-
}
72-
}
73-
Ok(())
73+
let sep = match self.delimiter {
74+
KeyValueOptionsDelimiter::Space => " ",
75+
KeyValueOptionsDelimiter::Comma => ", ",
76+
};
77+
write!(f, "{}", display_separated(&self.options, sep))
7478
}
7579
}
7680

src/ast/mod.rs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4355,7 +4355,6 @@ pub enum Statement {
43554355
///
43564356
/// See [ReturnStatement]
43574357
Return(ReturnStatement),
4358-
43594358
/// Export data statement
43604359
///
43614360
/// Example:
@@ -4364,6 +4363,11 @@ pub enum Statement {
43644363
/// SELECT field1, field2 FROM mydataset.table1 ORDER BY field1 LIMIT 10
43654364
/// ```
43664365
ExportData(ExportData),
4366+
/// ```sql
4367+
/// CREATE [OR REPLACE] USER <user> [IF NOT EXISTS]
4368+
/// ```
4369+
/// [Snowflake](https://docs.snowflake.com/en/sql-reference/sql/create-user)
4370+
CreateUser(CreateUser),
43674371
}
43684372

43694373
/// ```sql
@@ -6203,6 +6207,7 @@ impl fmt::Display for Statement {
62036207
Statement::List(command) => write!(f, "LIST {command}"),
62046208
Statement::Remove(command) => write!(f, "REMOVE {command}"),
62056209
Statement::ExportData(e) => write!(f, "{e}"),
6210+
Statement::CreateUser(s) => write!(f, "{s}"),
62066211
}
62076212
}
62086213
}
@@ -6658,7 +6663,7 @@ pub enum Action {
66586663
Replicate,
66596664
ResolveAll,
66606665
Role {
6661-
role: Ident,
6666+
role: ObjectName,
66626667
},
66636668
Select {
66646669
columns: Option<Vec<Ident>>,
@@ -10148,6 +10153,49 @@ impl fmt::Display for ExportData {
1014810153
write!(f, "EXPORT DATA OPTIONS({}) AS {}", display_comma_separated(&self.options), self.query)
1014910154
}
1015010155
}
10156+
/// Creates a user
10157+
///
10158+
/// Syntax:
10159+
/// ```sql
10160+
/// CREATE [OR REPLACE] USER [IF NOT EXISTS] <name> [OPTIONS]
10161+
/// ```
10162+
///
10163+
/// [Snowflake](https://docs.snowflake.com/en/sql-reference/sql/create-user)
10164+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
10165+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10166+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
10167+
pub struct CreateUser {
10168+
pub or_replace: bool,
10169+
pub if_not_exists: bool,
10170+
pub name: Ident,
10171+
pub options: KeyValueOptions,
10172+
pub with_tags: bool,
10173+
pub tags: KeyValueOptions,
10174+
}
10175+
10176+
impl fmt::Display for CreateUser {
10177+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
10178+
write!(f, "CREATE")?;
10179+
if self.or_replace {
10180+
write!(f, " OR REPLACE")?;
10181+
}
10182+
write!(f, " USER")?;
10183+
if self.if_not_exists {
10184+
write!(f, " IF NOT EXISTS")?;
10185+
}
10186+
write!(f, " {}", self.name)?;
10187+
if !self.options.options.is_empty() {
10188+
write!(f, " {}", self.options)?;
10189+
}
10190+
if !self.tags.options.is_empty() {
10191+
if self.with_tags {
10192+
write!(f, " WITH")?;
10193+
}
10194+
write!(f, " TAG ({})", self.tags)?;
10195+
}
10196+
Ok(())
10197+
}
10198+
}
1015110199

1015210200
#[cfg(test)]
1015310201
mod tests {

src/ast/spans.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ impl Spanned for Statement {
534534
Statement::ExportData(ExportData { options, query }) => union_spans(
535535
options.iter().map(|i| i.span()).chain(core::iter::once(query.span()))
536536
),
537+
Statement::CreateUser(..) => Span::empty(),
537538
}
538539
}
539540
}
@@ -1204,6 +1205,9 @@ impl Spanned for AlterTableOperation {
12041205
AlterTableOperation::Lock { .. } => Span::empty(),
12051206
AlterTableOperation::ReplicaIdentity { .. } => Span::empty(),
12061207
AlterTableOperation::ValidateConstraint { name } => name.span,
1208+
AlterTableOperation::SetOptionsParens { options } => {
1209+
union_spans(options.iter().map(|i| i.span()))
1210+
}
12071211
}
12081212
}
12091213
}

0 commit comments

Comments
 (0)