Skip to content

Commit bd1a7b6

Browse files
authored
Merge branch 'apache:main' into snowflake_table_function
2 parents 8edeb97 + 698154d commit bd1a7b6

6 files changed

Lines changed: 73 additions & 11 deletions

File tree

src/ast/ddl.rs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ use crate::ast::{
3333
display_comma_separated, display_separated, ArgMode, CommentDef, CreateFunctionBody,
3434
CreateFunctionUsing, DataType, Expr, FunctionBehavior, FunctionCalledOnNull,
3535
FunctionDeterminismSpecifier, FunctionParallel, Ident, IndexColumn, MySQLColumnPosition,
36-
ObjectName, OperateFunctionArg, OrderByExpr, ProjectionSelect, SequenceOptions, SqlOption, Tag,
37-
Value, ValueWithSpan,
36+
ObjectName, OperateFunctionArg, OrderByExpr, ProjectionSelect, SequenceOptions, Spanned,
37+
SqlOption, Tag, Value, ValueWithSpan,
3838
};
3939
use crate::keywords::Keyword;
40-
use crate::tokenizer::Token;
40+
use crate::tokenizer::{Span, Token};
4141

4242
/// ALTER TABLE operation REPLICA IDENTITY values
4343
/// See [Postgres ALTER TABLE docs](https://www.postgresql.org/docs/current/sql-altertable.html)
@@ -264,7 +264,7 @@ pub enum AlterTableOperation {
264264
},
265265
/// `RENAME TO <table_name>`
266266
RenameTable {
267-
table_name: ObjectName,
267+
table_name: RenameTableNameKind,
268268
},
269269
// CHANGE [ COLUMN ] <old_name> <new_name> <data_type> [ <options> ]
270270
ChangeColumn {
@@ -697,7 +697,7 @@ impl fmt::Display for AlterTableOperation {
697697
new_column_name,
698698
} => write!(f, "RENAME COLUMN {old_column_name} TO {new_column_name}"),
699699
AlterTableOperation::RenameTable { table_name } => {
700-
write!(f, "RENAME TO {table_name}")
700+
write!(f, "RENAME {table_name}")
701701
}
702702
AlterTableOperation::ChangeColumn {
703703
old_name,
@@ -2537,3 +2537,34 @@ impl fmt::Display for CreateConnector {
25372537
Ok(())
25382538
}
25392539
}
2540+
2541+
/// `RenameTableNameKind` is the kind used in an `ALTER TABLE _ RENAME` statement.
2542+
///
2543+
/// Note: [MySQL] is the only database that supports the AS keyword for this operation.
2544+
///
2545+
/// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/alter-table.html
2546+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2547+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2548+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2549+
pub enum RenameTableNameKind {
2550+
As(ObjectName),
2551+
To(ObjectName),
2552+
}
2553+
2554+
impl fmt::Display for RenameTableNameKind {
2555+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2556+
match self {
2557+
RenameTableNameKind::As(name) => write!(f, "AS {name}"),
2558+
RenameTableNameKind::To(name) => write!(f, "TO {name}"),
2559+
}
2560+
}
2561+
}
2562+
2563+
impl Spanned for RenameTableNameKind {
2564+
fn span(&self) -> Span {
2565+
match self {
2566+
RenameTableNameKind::As(name) => name.span(),
2567+
RenameTableNameKind::To(name) => name.span(),
2568+
}
2569+
}
2570+
}

src/ast/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ pub use self::ddl::{
6666
Deduplicate, DeferrableInitial, DropBehavior, GeneratedAs, GeneratedExpressionMode,
6767
IdentityParameters, IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind,
6868
IdentityPropertyOrder, IndexOption, IndexType, KeyOrIndexDisplay, NullsDistinctOption, Owner,
69-
Partition, ProcedureParam, ReferentialAction, ReplicaIdentity, TableConstraint,
70-
TagsColumnOption, UserDefinedTypeCompositeAttributeDef, UserDefinedTypeRepresentation,
71-
ViewColumnDef,
69+
Partition, ProcedureParam, ReferentialAction, RenameTableNameKind, 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};

src/dialect/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ pub trait Dialect: Debug + Any {
590590
false
591591
}
592592

593-
/// Returne true if the dialect supports specifying multiple options
593+
/// Return true if the dialect supports specifying multiple options
594594
/// in a `CREATE TABLE` statement for the structure of the new table. For example:
595595
/// `CREATE TABLE t (a INT, b INT) AS SELECT 1 AS b, 2 AS a`
596596
fn supports_create_table_multi_schema_info_sources(&self) -> bool {

src/parser/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7708,6 +7708,9 @@ impl<'a> Parser<'a> {
77087708

77097709
while let Some(option) = self.parse_plain_option()? {
77107710
options.push(option);
7711+
// Some dialects support comma-separated options; it shouldn't introduce ambiguity to
7712+
// consume it for all dialects.
7713+
let _ = self.consume_token(&Token::Comma);
77117714
}
77127715

77137716
Ok(options)
@@ -8749,7 +8752,14 @@ impl<'a> Parser<'a> {
87498752
AlterTableOperation::RenameConstraint { old_name, new_name }
87508753
} else if self.parse_keyword(Keyword::TO) {
87518754
let table_name = self.parse_object_name(false)?;
8752-
AlterTableOperation::RenameTable { table_name }
8755+
AlterTableOperation::RenameTable {
8756+
table_name: RenameTableNameKind::To(table_name),
8757+
}
8758+
} else if self.parse_keyword(Keyword::AS) {
8759+
let table_name = self.parse_object_name(false)?;
8760+
AlterTableOperation::RenameTable {
8761+
table_name: RenameTableNameKind::As(table_name),
8762+
}
87538763
} else {
87548764
let _ = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
87558765
let old_column_name = self.parse_identifier()?;

tests/sqlparser_common.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4692,7 +4692,21 @@ fn parse_alter_table() {
46924692
let rename_table = "ALTER TABLE tab RENAME TO new_tab";
46934693
match alter_table_op(verified_stmt(rename_table)) {
46944694
AlterTableOperation::RenameTable { table_name } => {
4695-
assert_eq!("new_tab", table_name.to_string());
4695+
assert_eq!(
4696+
RenameTableNameKind::To(ObjectName::from(vec![Ident::new("new_tab")])),
4697+
table_name
4698+
);
4699+
}
4700+
_ => unreachable!(),
4701+
};
4702+
4703+
let rename_table_as = "ALTER TABLE tab RENAME AS new_tab";
4704+
match alter_table_op(verified_stmt(rename_table_as)) {
4705+
AlterTableOperation::RenameTable { table_name } => {
4706+
assert_eq!(
4707+
RenameTableNameKind::As(ObjectName::from(vec![Ident::new("new_tab")])),
4708+
table_name
4709+
);
46964710
}
46974711
_ => unreachable!(),
46984712
};

tests/sqlparser_mysql.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,13 @@ fn parse_create_table_gencol() {
13611361
mysql_and_generic().verified_stmt("CREATE TABLE t1 (a INT, b INT AS (a * 2) STORED)");
13621362
}
13631363

1364+
#[test]
1365+
fn parse_create_table_options_comma_separated() {
1366+
let sql = "CREATE TABLE t (x INT) DEFAULT CHARSET = utf8mb4, ENGINE = InnoDB , AUTO_INCREMENT 1 DATA DIRECTORY '/var/lib/mysql/data'";
1367+
let canonical = "CREATE TABLE t (x INT) DEFAULT CHARSET = utf8mb4 ENGINE = InnoDB AUTO_INCREMENT = 1 DATA DIRECTORY = '/var/lib/mysql/data'";
1368+
mysql_and_generic().one_statement_parses_to(sql, canonical);
1369+
}
1370+
13641371
#[test]
13651372
fn parse_quote_identifiers() {
13661373
let sql = "CREATE TABLE `PRIMARY` (`BEGIN` INT PRIMARY KEY)";

0 commit comments

Comments
 (0)