Skip to content

Commit 31c63ec

Browse files
committed
init
1 parent 40b187f commit 31c63ec

5 files changed

Lines changed: 151 additions & 11 deletions

File tree

src/ast/ddl.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2537,3 +2537,46 @@ impl fmt::Display for CreateConnector {
25372537
Ok(())
25382538
}
25392539
}
2540+
2541+
/// An `ALTER SCHEMA` (`Statement::AlterSchema`) operation.
2542+
///
2543+
/// See [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#alter_schema_collate_statement)
2544+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2545+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2546+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2547+
pub enum AlterSchemaOperation {
2548+
SetDefaultCollate {
2549+
collate: Expr,
2550+
},
2551+
AddReplica {
2552+
replica: Ident,
2553+
options: Option<Vec<SqlOption>>,
2554+
},
2555+
DropReplica {
2556+
replica: Ident,
2557+
},
2558+
SetOptionsParens {
2559+
options: Vec<SqlOption>,
2560+
},
2561+
}
2562+
2563+
impl fmt::Display for AlterSchemaOperation {
2564+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2565+
match self {
2566+
AlterSchemaOperation::SetDefaultCollate { collate } => {
2567+
write!(f, "SET DEFAULT COLLATE {collate}")
2568+
}
2569+
AlterSchemaOperation::AddReplica { replica, options } => {
2570+
write!(f, "ADD REPLICA {replica}")?;
2571+
if let Some(options) = options {
2572+
write!(f, " OPTIONS ({})", display_comma_separated(options))?;
2573+
}
2574+
Ok(())
2575+
}
2576+
AlterSchemaOperation::DropReplica { replica } => write!(f, "DROP REPLICA {replica}"),
2577+
AlterSchemaOperation::SetOptionsParens { options } => {
2578+
write!(f, "SET OPTIONS ({})", display_comma_separated(options))
2579+
}
2580+
}
2581+
}
2582+
}

src/ast/mod.rs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ pub use self::dcl::{
5959
};
6060
pub use self::ddl::{
6161
AlterColumnOperation, AlterConnectorOwner, AlterIndexOperation, AlterPolicyOperation,
62-
AlterTableAlgorithm, AlterTableLock, AlterTableOperation, AlterType, AlterTypeAddValue,
63-
AlterTypeAddValuePosition, AlterTypeOperation, AlterTypeRename, AlterTypeRenameValue,
64-
ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, ColumnOptions, ColumnPolicy,
65-
ColumnPolicyProperty, ConstraintCharacteristics, CreateConnector, CreateDomain, CreateFunction,
66-
Deduplicate, DeferrableInitial, DropBehavior, GeneratedAs, GeneratedExpressionMode,
67-
IdentityParameters, IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind,
68-
IdentityPropertyOrder, IndexOption, IndexType, KeyOrIndexDisplay, NullsDistinctOption, Owner,
69-
Partition, ProcedureParam, ReferentialAction, ReplicaIdentity, TableConstraint,
70-
TagsColumnOption, UserDefinedTypeCompositeAttributeDef, UserDefinedTypeRepresentation,
71-
ViewColumnDef,
62+
AlterSchemaOperation, AlterTableAlgorithm, AlterTableLock, AlterTableOperation, AlterType,
63+
AlterTypeAddValue, AlterTypeAddValuePosition, AlterTypeOperation, AlterTypeRename,
64+
AlterTypeRenameValue, ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, ColumnOptions,
65+
ColumnPolicy, ColumnPolicyProperty, ConstraintCharacteristics, CreateConnector, CreateDomain,
66+
CreateFunction, Deduplicate, DeferrableInitial, DropBehavior, GeneratedAs,
67+
GeneratedExpressionMode, IdentityParameters, IdentityProperty, IdentityPropertyFormatKind,
68+
IdentityPropertyKind, IdentityPropertyOrder, IndexOption, IndexType, KeyOrIndexDisplay,
69+
NullsDistinctOption, Owner, Partition, ProcedureParam, ReferentialAction, ReplicaIdentity,
70+
TableConstraint, TagsColumnOption, UserDefinedTypeCompositeAttributeDef,
71+
UserDefinedTypeRepresentation, ViewColumnDef,
7272
};
7373
pub use self::dml::{CreateIndex, CreateTable, Delete, IndexColumn, Insert};
7474
pub use self::operator::{BinaryOperator, UnaryOperator};
@@ -3381,6 +3381,17 @@ pub enum Statement {
33813381
iceberg: bool,
33823382
},
33833383
/// ```sql
3384+
/// ALTER SCHEMA
3385+
/// ```
3386+
/// 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+
},
3394+
/// ```sql
33843395
/// ALTER INDEX
33853396
/// ```
33863397
AlterIndex {
@@ -6199,6 +6210,15 @@ impl fmt::Display for Statement {
61996210
Statement::List(command) => write!(f, "LIST {command}"),
62006211
Statement::Remove(command) => write!(f, "REMOVE {command}"),
62016212
Statement::CreateUser(s) => write!(f, "{s}"),
6213+
Statement::AlterSchema {
6214+
name, operations, ..
6215+
} => {
6216+
write!(f, "ALTER SCHEMA {name}")?;
6217+
for operation in operations {
6218+
write!(f, " {operation}")?;
6219+
}
6220+
Ok(())
6221+
}
62026222
}
62036223
}
62046224
}

src/ast/spans.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use crate::ast::{query::SelectItemQualifiedWildcardKind, ColumnOptions};
18+
use crate::ast::{query::SelectItemQualifiedWildcardKind, AlterSchemaOperation, ColumnOptions};
1919
use core::iter;
2020

2121
use crate::tokenizer::Span;
@@ -532,6 +532,11 @@ impl Spanned for Statement {
532532
Statement::Return { .. } => Span::empty(),
533533
Statement::List(..) | Statement::Remove(..) => Span::empty(),
534534
Statement::CreateUser(..) => Span::empty(),
535+
Statement::AlterSchema {
536+
name, operations, ..
537+
} => union_spans(
538+
core::iter::once(name.span()).chain(operations.iter().map(|i| i.span())),
539+
),
535540
}
536541
}
537542
}
@@ -2361,6 +2366,22 @@ impl Spanned for OpenStatement {
23612366
}
23622367
}
23632368

2369+
impl Spanned for AlterSchemaOperation {
2370+
fn span(&self) -> Span {
2371+
match self {
2372+
AlterSchemaOperation::SetDefaultCollate { collate } => collate.span(),
2373+
AlterSchemaOperation::AddReplica { replica, options } => union_spans(
2374+
core::iter::once(replica.span)
2375+
.chain(options.iter().flat_map(|i| i.iter().map(|i| i.span()))),
2376+
),
2377+
AlterSchemaOperation::DropReplica { replica } => replica.span,
2378+
AlterSchemaOperation::SetOptionsParens { options } => {
2379+
union_spans(options.iter().map(|i| i.span()))
2380+
}
2381+
}
2382+
}
2383+
}
2384+
23642385
#[cfg(test)]
23652386
pub mod tests {
23662387
use crate::dialect::{Dialect, GenericDialect, SnowflakeDialect};

src/parser/mod.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9102,8 +9102,10 @@ impl<'a> Parser<'a> {
91029102
Keyword::POLICY,
91039103
Keyword::CONNECTOR,
91049104
Keyword::ICEBERG,
9105+
Keyword::SCHEMA,
91059106
])?;
91069107
match object_type {
9108+
Keyword::SCHEMA => self.parse_alter_schema(),
91079109
Keyword::VIEW => self.parse_alter_view(),
91089110
Keyword::TYPE => self.parse_alter_type(),
91099111
Keyword::TABLE => self.parse_alter_table(false),
@@ -9235,6 +9237,40 @@ impl<'a> Parser<'a> {
92359237
}
92369238
}
92379239

9240+
pub fn parse_alter_schema(&mut self) -> Result<Statement, ParserError> {
9241+
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
9242+
let name = self.parse_object_name(false)?;
9243+
let operation = if self.parse_keywords(&[Keyword::SET, Keyword::OPTIONS]) {
9244+
self.prev_token();
9245+
let options = self.parse_options(Keyword::OPTIONS)?;
9246+
AlterSchemaOperation::SetOptionsParens { options }
9247+
} else if self.parse_keywords(&[Keyword::SET, Keyword::DEFAULT, Keyword::COLLATE]) {
9248+
let collate = self.parse_expr()?;
9249+
AlterSchemaOperation::SetDefaultCollate { collate }
9250+
} else if self.parse_keywords(&[Keyword::ADD, Keyword::REPLICA]) {
9251+
let replica = self.parse_identifier()?;
9252+
let options = if self.peek_keyword(Keyword::OPTIONS) {
9253+
Some(self.parse_options(Keyword::OPTIONS)?)
9254+
} else {
9255+
None
9256+
};
9257+
AlterSchemaOperation::AddReplica { replica, options }
9258+
} else if self.parse_keywords(&[Keyword::DROP, Keyword::REPLICA]) {
9259+
let replica = self.parse_identifier()?;
9260+
AlterSchemaOperation::DropReplica { replica }
9261+
} else {
9262+
return self.expected_ref(
9263+
"{SET OPTIONS | SET DEFAULT COLLATE | ADD REPLICA | DROP REPLICA}",
9264+
self.peek_token_ref(),
9265+
);
9266+
};
9267+
Ok(Statement::AlterSchema {
9268+
name,
9269+
if_exists,
9270+
operations: vec![operation],
9271+
})
9272+
}
9273+
92389274
/// Parse a `CALL procedure_name(arg1, arg2, ...)`
92399275
/// or `CALL procedure_name` statement
92409276
pub fn parse_call(&mut self) -> Result<Statement, ParserError> {

tests/sqlparser_bigquery.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2566,3 +2566,23 @@ fn test_struct_trailing_and_nested_bracket() {
25662566
)
25672567
);
25682568
}
2569+
2570+
#[test]
2571+
fn test_alter_schema_default_collate() {
2572+
bigquery_and_generic().verified_stmt("ALTER SCHEMA mydataset SET DEFAULT COLLATE 'und:ci'");
2573+
}
2574+
2575+
#[test]
2576+
fn test_alter_schema_add_replica() {
2577+
bigquery_and_generic().verified_stmt("ALTER SCHEMA mydataset ADD REPLICA 'us'");
2578+
}
2579+
2580+
#[test]
2581+
fn test_alter_schema_drop_replica() {
2582+
bigquery_and_generic().verified_stmt("ALTER SCHEMA mydataset DROP REPLICA 'us'");
2583+
}
2584+
2585+
#[test]
2586+
fn test_alter_schema_set_options() {
2587+
bigquery_and_generic().verified_stmt("ALTER SCHEMA mydataset SET OPTIONS (location = 'us')");
2588+
}

0 commit comments

Comments
 (0)