Skip to content

Commit fbdedba

Browse files
committed
update
1 parent 0aaa561 commit fbdedba

5 files changed

Lines changed: 55 additions & 44 deletions

File tree

src/ast/ddl.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2580,3 +2580,27 @@ impl fmt::Display for AlterSchemaOperation {
25802580
}
25812581
}
25822582
}
2583+
2584+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2585+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2586+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2587+
pub struct AlterSchema {
2588+
pub name: ObjectName,
2589+
pub if_exists: bool,
2590+
pub operations: Vec<AlterSchemaOperation>,
2591+
}
2592+
2593+
impl fmt::Display for AlterSchema {
2594+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2595+
write!(f, "ALTER SCHEMA ")?;
2596+
if self.if_exists {
2597+
write!(f, "IF EXISTS ")?;
2598+
}
2599+
write!(f, "{}", self.name)?;
2600+
for operation in &self.operations {
2601+
write!(f, " {operation}")?;
2602+
}
2603+
2604+
Ok(())
2605+
}
2606+
}

src/ast/mod.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ pub use self::dcl::{
5959
};
6060
pub use self::ddl::{
6161
AlterColumnOperation, AlterConnectorOwner, AlterIndexOperation, AlterPolicyOperation,
62-
AlterSchemaOperation, AlterTableAlgorithm, AlterTableLock, AlterTableOperation, AlterType,
63-
AlterTypeAddValue, AlterTypeAddValuePosition, AlterTypeOperation, AlterTypeRename,
62+
AlterSchema, AlterSchemaOperation, AlterTableAlgorithm, AlterTableLock, AlterTableOperation,
63+
AlterType, AlterTypeAddValue, AlterTypeAddValuePosition, AlterTypeOperation, AlterTypeRename,
6464
AlterTypeRenameValue, ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, ColumnOptions,
6565
ColumnPolicy, ColumnPolicyProperty, ConstraintCharacteristics, CreateConnector, CreateDomain,
6666
CreateFunction, Deduplicate, DeferrableInitial, DropBehavior, GeneratedAs,
@@ -3384,13 +3384,7 @@ pub enum Statement {
33843384
/// ALTER SCHEMA
33853385
/// ```
33863386
/// See [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#alter_schema_collate_statement)
3387-
AlterSchema {
3388-
/// Schema name
3389-
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
3390-
name: ObjectName,
3391-
if_exists: bool,
3392-
operations: Vec<AlterSchemaOperation>,
3393-
},
3387+
AlterSchema(AlterSchema),
33943388
/// ```sql
33953389
/// ALTER INDEX
33963390
/// ```
@@ -6220,15 +6214,7 @@ impl fmt::Display for Statement {
62206214
Statement::Remove(command) => write!(f, "REMOVE {command}"),
62216215
Statement::ExportData(e) => write!(f, "{e}"),
62226216
Statement::CreateUser(s) => write!(f, "{s}"),
6223-
Statement::AlterSchema {
6224-
name, operations, ..
6225-
} => {
6226-
write!(f, "ALTER SCHEMA {name}")?;
6227-
for operation in operations {
6228-
write!(f, " {operation}")?;
6229-
}
6230-
Ok(())
6231-
}
6217+
Statement::AlterSchema(s) => write!(f, "{s}"),
62326218
}
62336219
}
62346220
}

src/ast/spans.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
// under the License.
1717

1818
use crate::ast::{
19-
query::SelectItemQualifiedWildcardKind, AlterSchemaOperation, ColumnOptions, ExportData,
19+
ddl::AlterSchema, query::SelectItemQualifiedWildcardKind, AlterSchemaOperation, ColumnOptions,
20+
ExportData,
2021
};
2122
use core::iter;
2223

@@ -545,11 +546,7 @@ impl Spanned for Statement {
545546
.chain(connection.iter().map(|i| i.span())),
546547
),
547548
Statement::CreateUser(..) => Span::empty(),
548-
Statement::AlterSchema {
549-
name, operations, ..
550-
} => union_spans(
551-
core::iter::once(name.span()).chain(operations.iter().map(|i| i.span())),
552-
),
549+
Statement::AlterSchema(s) => s.span(),
553550
}
554551
}
555552
}
@@ -2395,6 +2392,14 @@ impl Spanned for AlterSchemaOperation {
23952392
}
23962393
}
23972394

2395+
impl Spanned for AlterSchema {
2396+
fn span(&self) -> Span {
2397+
union_spans(
2398+
core::iter::once(self.name.span()).chain(self.operations.iter().map(|i| i.span())),
2399+
)
2400+
}
2401+
}
2402+
23982403
#[cfg(test)]
23992404
pub mod tests {
24002405
use crate::dialect::{Dialect, GenericDialect, SnowflakeDialect};

src/parser/mod.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9111,7 +9111,11 @@ impl<'a> Parser<'a> {
91119111
Keyword::SCHEMA,
91129112
])?;
91139113
match object_type {
9114-
Keyword::SCHEMA => self.parse_alter_schema(),
9114+
Keyword::SCHEMA => {
9115+
self.prev_token();
9116+
self.prev_token();
9117+
self.parse_alter_schema()
9118+
}
91159119
Keyword::VIEW => self.parse_alter_view(),
91169120
Keyword::TYPE => self.parse_alter_type(),
91179121
Keyword::TABLE => self.parse_alter_table(false),
@@ -9243,7 +9247,10 @@ impl<'a> Parser<'a> {
92439247
}
92449248
}
92459249

9250+
// Parse a [Statement::AlterSchema]
9251+
// ALTER SCHEMA [ IF EXISTS ] schema_name
92469252
pub fn parse_alter_schema(&mut self) -> Result<Statement, ParserError> {
9253+
self.expect_keywords(&[Keyword::ALTER, Keyword::SCHEMA])?;
92479254
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
92489255
let name = self.parse_object_name(false)?;
92499256
let operation = if self.parse_keywords(&[Keyword::SET, Keyword::OPTIONS]) {
@@ -9265,16 +9272,13 @@ impl<'a> Parser<'a> {
92659272
let replica = self.parse_identifier()?;
92669273
AlterSchemaOperation::DropReplica { replica }
92679274
} else {
9268-
return self.expected_ref(
9269-
"{SET OPTIONS | SET DEFAULT COLLATE | ADD REPLICA | DROP REPLICA}",
9270-
self.peek_token_ref(),
9271-
);
9275+
return self.expected_ref("ALTER SCHEMA operation", self.peek_token_ref());
92729276
};
9273-
Ok(Statement::AlterSchema {
9277+
Ok(Statement::AlterSchema(AlterSchema {
92749278
name,
92759279
if_exists,
92769280
operations: vec![operation],
9277-
})
9281+
}))
92789282
}
92799283

92809284
/// Parse a `CALL procedure_name(arg1, arg2, ...)`

tests/sqlparser_bigquery.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2808,21 +2808,13 @@ fn test_begin_statement() {
28082808
}
28092809

28102810
#[test]
2811-
fn test_alter_schema_default_collate() {
2811+
fn test_alter_schema() {
28122812
bigquery_and_generic().verified_stmt("ALTER SCHEMA mydataset SET DEFAULT COLLATE 'und:ci'");
2813-
}
2814-
2815-
#[test]
2816-
fn test_alter_schema_add_replica() {
28172813
bigquery_and_generic().verified_stmt("ALTER SCHEMA mydataset ADD REPLICA 'us'");
2818-
}
2819-
2820-
#[test]
2821-
fn test_alter_schema_drop_replica() {
2814+
bigquery_and_generic()
2815+
.verified_stmt("ALTER SCHEMA mydataset ADD REPLICA 'us' OPTIONS (location = 'us')");
28222816
bigquery_and_generic().verified_stmt("ALTER SCHEMA mydataset DROP REPLICA 'us'");
2823-
}
2824-
2825-
#[test]
2826-
fn test_alter_schema_set_options() {
28272817
bigquery_and_generic().verified_stmt("ALTER SCHEMA mydataset SET OPTIONS (location = 'us')");
2818+
bigquery_and_generic()
2819+
.verified_stmt("ALTER SCHEMA IF EXISTS mydataset SET OPTIONS (location = 'us')");
28282820
}

0 commit comments

Comments
 (0)