Skip to content

feat: support alter schema for bigquery #1980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2537,3 +2537,46 @@ impl fmt::Display for CreateConnector {
Ok(())
}
}

/// An `ALTER SCHEMA` (`Statement::AlterSchema`) operation.
///
/// See [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#alter_schema_collate_statement)
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum AlterSchemaOperation {
SetDefaultCollate {
collate: Expr,
},
AddReplica {
replica: Ident,
options: Option<Vec<SqlOption>>,
},
DropReplica {
replica: Ident,
},
SetOptionsParens {
options: Vec<SqlOption>,
},
}

impl fmt::Display for AlterSchemaOperation {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
AlterSchemaOperation::SetDefaultCollate { collate } => {
write!(f, "SET DEFAULT COLLATE {collate}")
}
AlterSchemaOperation::AddReplica { replica, options } => {
write!(f, "ADD REPLICA {replica}")?;
if let Some(options) = options {
write!(f, " OPTIONS ({})", display_comma_separated(options))?;
}
Ok(())
}
AlterSchemaOperation::DropReplica { replica } => write!(f, "DROP REPLICA {replica}"),
AlterSchemaOperation::SetOptionsParens { options } => {
write!(f, "SET OPTIONS ({})", display_comma_separated(options))
}
}
}
}
40 changes: 30 additions & 10 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ pub use self::dcl::{
};
pub use self::ddl::{
AlterColumnOperation, AlterConnectorOwner, AlterIndexOperation, AlterPolicyOperation,
AlterTableAlgorithm, AlterTableLock, AlterTableOperation, AlterType, AlterTypeAddValue,
AlterTypeAddValuePosition, AlterTypeOperation, AlterTypeRename, AlterTypeRenameValue,
ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, ColumnOptions, ColumnPolicy,
ColumnPolicyProperty, ConstraintCharacteristics, CreateConnector, CreateDomain, CreateFunction,
Deduplicate, DeferrableInitial, DropBehavior, GeneratedAs, GeneratedExpressionMode,
IdentityParameters, IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind,
IdentityPropertyOrder, IndexOption, IndexType, KeyOrIndexDisplay, NullsDistinctOption, Owner,
Partition, ProcedureParam, ReferentialAction, ReplicaIdentity, TableConstraint,
TagsColumnOption, UserDefinedTypeCompositeAttributeDef, UserDefinedTypeRepresentation,
ViewColumnDef,
AlterSchemaOperation, AlterTableAlgorithm, AlterTableLock, AlterTableOperation, AlterType,
AlterTypeAddValue, AlterTypeAddValuePosition, AlterTypeOperation, AlterTypeRename,
AlterTypeRenameValue, ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, ColumnOptions,
ColumnPolicy, ColumnPolicyProperty, ConstraintCharacteristics, CreateConnector, CreateDomain,
CreateFunction, Deduplicate, DeferrableInitial, DropBehavior, GeneratedAs,
GeneratedExpressionMode, IdentityParameters, IdentityProperty, IdentityPropertyFormatKind,
IdentityPropertyKind, IdentityPropertyOrder, IndexOption, IndexType, KeyOrIndexDisplay,
NullsDistinctOption, Owner, Partition, ProcedureParam, ReferentialAction, ReplicaIdentity,
TableConstraint, TagsColumnOption, UserDefinedTypeCompositeAttributeDef,
UserDefinedTypeRepresentation, ViewColumnDef,
};
pub use self::dml::{CreateIndex, CreateTable, Delete, IndexColumn, Insert};
pub use self::operator::{BinaryOperator, UnaryOperator};
Expand Down Expand Up @@ -3381,6 +3381,17 @@ pub enum Statement {
iceberg: bool,
},
/// ```sql
/// ALTER SCHEMA
/// ```
/// See [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#alter_schema_collate_statement)
AlterSchema {
/// Schema name
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
name: ObjectName,
if_exists: bool,
operations: Vec<AlterSchemaOperation>,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use a named struct syntax here? e.g. AlterSchema(AlterSchemaStatement)

/// ```sql
/// ALTER INDEX
/// ```
AlterIndex {
Expand Down Expand Up @@ -6209,6 +6220,15 @@ impl fmt::Display for Statement {
Statement::Remove(command) => write!(f, "REMOVE {command}"),
Statement::ExportData(e) => write!(f, "{e}"),
Statement::CreateUser(s) => write!(f, "{s}"),
Statement::AlterSchema {
name, operations, ..
} => {
write!(f, "ALTER SCHEMA {name}")?;
for operation in operations {
write!(f, " {operation}")?;
}
Ok(())
}
}
}
}
Expand Down
25 changes: 24 additions & 1 deletion src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
// specific language governing permissions and limitations
// under the License.

use crate::ast::{query::SelectItemQualifiedWildcardKind, ColumnOptions, ExportData};
use crate::ast::{
query::SelectItemQualifiedWildcardKind, AlterSchemaOperation, ColumnOptions, ExportData,
};
use core::iter;

use crate::tokenizer::Span;
Expand Down Expand Up @@ -543,6 +545,11 @@ impl Spanned for Statement {
.chain(connection.iter().map(|i| i.span())),
),
Statement::CreateUser(..) => Span::empty(),
Statement::AlterSchema {
name, operations, ..
} => union_spans(
core::iter::once(name.span()).chain(operations.iter().map(|i| i.span())),
),
}
}
}
Expand Down Expand Up @@ -2372,6 +2379,22 @@ impl Spanned for OpenStatement {
}
}

impl Spanned for AlterSchemaOperation {
fn span(&self) -> Span {
match self {
AlterSchemaOperation::SetDefaultCollate { collate } => collate.span(),
AlterSchemaOperation::AddReplica { replica, options } => union_spans(
core::iter::once(replica.span)
.chain(options.iter().flat_map(|i| i.iter().map(|i| i.span()))),
),
AlterSchemaOperation::DropReplica { replica } => replica.span,
AlterSchemaOperation::SetOptionsParens { options } => {
union_spans(options.iter().map(|i| i.span()))
}
}
}
}

#[cfg(test)]
pub mod tests {
use crate::dialect::{Dialect, GenericDialect, SnowflakeDialect};
Expand Down
36 changes: 36 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9108,8 +9108,10 @@ impl<'a> Parser<'a> {
Keyword::POLICY,
Keyword::CONNECTOR,
Keyword::ICEBERG,
Keyword::SCHEMA,
])?;
match object_type {
Keyword::SCHEMA => self.parse_alter_schema(),
Keyword::VIEW => self.parse_alter_view(),
Keyword::TYPE => self.parse_alter_type(),
Keyword::TABLE => self.parse_alter_table(false),
Expand Down Expand Up @@ -9241,6 +9243,40 @@ impl<'a> Parser<'a> {
}
}

pub fn parse_alter_schema(&mut self) -> Result<Statement, ParserError> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can call prev_token before invoking this method, so that it is self contained and able to parse a full ALTER SCHEMA statement. Also can we add some documentation for this function, ideally mentioning that it returns a Statement::AlterSchema variant

let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
let name = self.parse_object_name(false)?;
let operation = if self.parse_keywords(&[Keyword::SET, Keyword::OPTIONS]) {
self.prev_token();
let options = self.parse_options(Keyword::OPTIONS)?;
AlterSchemaOperation::SetOptionsParens { options }
} else if self.parse_keywords(&[Keyword::SET, Keyword::DEFAULT, Keyword::COLLATE]) {
let collate = self.parse_expr()?;
AlterSchemaOperation::SetDefaultCollate { collate }
} else if self.parse_keywords(&[Keyword::ADD, Keyword::REPLICA]) {
let replica = self.parse_identifier()?;
let options = if self.peek_keyword(Keyword::OPTIONS) {
Some(self.parse_options(Keyword::OPTIONS)?)
} else {
None
};
AlterSchemaOperation::AddReplica { replica, options }
} else if self.parse_keywords(&[Keyword::DROP, Keyword::REPLICA]) {
let replica = self.parse_identifier()?;
AlterSchemaOperation::DropReplica { replica }
} else {
return self.expected_ref(
"{SET OPTIONS | SET DEFAULT COLLATE | ADD REPLICA | DROP REPLICA}",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"{SET OPTIONS | SET DEFAULT COLLATE | ADD REPLICA | DROP REPLICA}",
"ALTER SCHEMA operation",

Thinking something generic, in order to avoid an maintaining a growing list that may potentially go out of sync

self.peek_token_ref(),
);
};
Ok(Statement::AlterSchema {
name,
if_exists,
operations: vec![operation],
})
}

/// Parse a `CALL procedure_name(arg1, arg2, ...)`
/// or `CALL procedure_name` statement
pub fn parse_call(&mut self) -> Result<Statement, ParserError> {
Expand Down
20 changes: 20 additions & 0 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2806,3 +2806,23 @@ fn test_begin_transaction() {
fn test_begin_statement() {
bigquery().verified_stmt("BEGIN");
}

#[test]
fn test_alter_schema_default_collate() {
bigquery_and_generic().verified_stmt("ALTER SCHEMA mydataset SET DEFAULT COLLATE 'und:ci'");
}

#[test]
fn test_alter_schema_add_replica() {
bigquery_and_generic().verified_stmt("ALTER SCHEMA mydataset ADD REPLICA 'us'");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from the implementation, ADD REPLICA takes in options, can we add test coverage for that behavior?

}

#[test]
fn test_alter_schema_drop_replica() {
bigquery_and_generic().verified_stmt("ALTER SCHEMA mydataset DROP REPLICA 'us'");
}

#[test]
fn test_alter_schema_set_options() {
bigquery_and_generic().verified_stmt("ALTER SCHEMA mydataset SET OPTIONS (location = 'us')");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we merge these into a single test_alter_schema() function?

Also we can move this to the sqlparser_common.rs file since the parser covers the statement it for all dialects (I imagine other dialects have some variant of this statement as well).

Also can we add scenarios for e.g.

ALTER SCHEMA DROP REPLICA;
ALTER SCHEMA SET OPTIONS ();
ALTER SCHEMA  SET OPTIONS;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

postgres also has alter schema, but it's quite different. https://www.postgresql.org/docs/17/sql-alterschema.html

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, yeah we can leave the tests in bigquery file as is