mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-08-04 06:18:17 +00:00
Keep the COLUMN keyword only if it exists when dropping the column (#1862)
This commit is contained in:
parent
301726541a
commit
9159d08c5e
5 changed files with 12 additions and 4 deletions
|
@ -139,6 +139,7 @@ pub enum AlterTableOperation {
|
||||||
},
|
},
|
||||||
/// `DROP [ COLUMN ] [ IF EXISTS ] <column_name> [ CASCADE ]`
|
/// `DROP [ COLUMN ] [ IF EXISTS ] <column_name> [ CASCADE ]`
|
||||||
DropColumn {
|
DropColumn {
|
||||||
|
has_column_keyword: bool,
|
||||||
column_name: Ident,
|
column_name: Ident,
|
||||||
if_exists: bool,
|
if_exists: bool,
|
||||||
drop_behavior: Option<DropBehavior>,
|
drop_behavior: Option<DropBehavior>,
|
||||||
|
@ -606,12 +607,14 @@ impl fmt::Display for AlterTableOperation {
|
||||||
AlterTableOperation::DropPrimaryKey => write!(f, "DROP PRIMARY KEY"),
|
AlterTableOperation::DropPrimaryKey => write!(f, "DROP PRIMARY KEY"),
|
||||||
AlterTableOperation::DropForeignKey { name } => write!(f, "DROP FOREIGN KEY {name}"),
|
AlterTableOperation::DropForeignKey { name } => write!(f, "DROP FOREIGN KEY {name}"),
|
||||||
AlterTableOperation::DropColumn {
|
AlterTableOperation::DropColumn {
|
||||||
|
has_column_keyword,
|
||||||
column_name,
|
column_name,
|
||||||
if_exists,
|
if_exists,
|
||||||
drop_behavior,
|
drop_behavior,
|
||||||
} => write!(
|
} => write!(
|
||||||
f,
|
f,
|
||||||
"DROP COLUMN {}{}{}",
|
"DROP {}{}{}{}",
|
||||||
|
if *has_column_keyword { "COLUMN " } else { "" },
|
||||||
if *if_exists { "IF EXISTS " } else { "" },
|
if *if_exists { "IF EXISTS " } else { "" },
|
||||||
column_name,
|
column_name,
|
||||||
match drop_behavior {
|
match drop_behavior {
|
||||||
|
|
|
@ -1090,6 +1090,7 @@ impl Spanned for AlterTableOperation {
|
||||||
drop_behavior: _,
|
drop_behavior: _,
|
||||||
} => name.span,
|
} => name.span,
|
||||||
AlterTableOperation::DropColumn {
|
AlterTableOperation::DropColumn {
|
||||||
|
has_column_keyword: _,
|
||||||
column_name,
|
column_name,
|
||||||
if_exists: _,
|
if_exists: _,
|
||||||
drop_behavior: _,
|
drop_behavior: _,
|
||||||
|
|
|
@ -8608,11 +8608,12 @@ impl<'a> Parser<'a> {
|
||||||
} else if self.parse_keywords(&[Keyword::CLUSTERING, Keyword::KEY]) {
|
} else if self.parse_keywords(&[Keyword::CLUSTERING, Keyword::KEY]) {
|
||||||
AlterTableOperation::DropClusteringKey
|
AlterTableOperation::DropClusteringKey
|
||||||
} else {
|
} else {
|
||||||
let _ = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
|
let has_column_keyword = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
|
||||||
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
|
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
|
||||||
let column_name = self.parse_identifier()?;
|
let column_name = self.parse_identifier()?;
|
||||||
let drop_behavior = self.parse_optional_drop_behavior();
|
let drop_behavior = self.parse_optional_drop_behavior();
|
||||||
AlterTableOperation::DropColumn {
|
AlterTableOperation::DropColumn {
|
||||||
|
has_column_keyword,
|
||||||
column_name,
|
column_name,
|
||||||
if_exists,
|
if_exists,
|
||||||
drop_behavior,
|
drop_behavior,
|
||||||
|
|
|
@ -4926,17 +4926,18 @@ fn parse_alter_table_drop_column() {
|
||||||
check_one("DROP COLUMN IF EXISTS is_active CASCADE");
|
check_one("DROP COLUMN IF EXISTS is_active CASCADE");
|
||||||
check_one("DROP COLUMN IF EXISTS is_active RESTRICT");
|
check_one("DROP COLUMN IF EXISTS is_active RESTRICT");
|
||||||
one_statement_parses_to(
|
one_statement_parses_to(
|
||||||
"ALTER TABLE tab DROP IF EXISTS is_active CASCADE",
|
"ALTER TABLE tab DROP COLUMN IF EXISTS is_active CASCADE",
|
||||||
"ALTER TABLE tab DROP COLUMN IF EXISTS is_active CASCADE",
|
"ALTER TABLE tab DROP COLUMN IF EXISTS is_active CASCADE",
|
||||||
);
|
);
|
||||||
one_statement_parses_to(
|
one_statement_parses_to(
|
||||||
"ALTER TABLE tab DROP is_active CASCADE",
|
"ALTER TABLE tab DROP is_active CASCADE",
|
||||||
"ALTER TABLE tab DROP COLUMN is_active CASCADE",
|
"ALTER TABLE tab DROP is_active CASCADE",
|
||||||
);
|
);
|
||||||
|
|
||||||
fn check_one(constraint_text: &str) {
|
fn check_one(constraint_text: &str) {
|
||||||
match alter_table_op(verified_stmt(&format!("ALTER TABLE tab {constraint_text}"))) {
|
match alter_table_op(verified_stmt(&format!("ALTER TABLE tab {constraint_text}"))) {
|
||||||
AlterTableOperation::DropColumn {
|
AlterTableOperation::DropColumn {
|
||||||
|
has_column_keyword: true,
|
||||||
column_name,
|
column_name,
|
||||||
if_exists,
|
if_exists,
|
||||||
drop_behavior,
|
drop_behavior,
|
||||||
|
|
|
@ -2801,6 +2801,7 @@ fn parse_alter_table_with_algorithm() {
|
||||||
operations,
|
operations,
|
||||||
vec![
|
vec![
|
||||||
AlterTableOperation::DropColumn {
|
AlterTableOperation::DropColumn {
|
||||||
|
has_column_keyword: true,
|
||||||
column_name: Ident::new("password_digest"),
|
column_name: Ident::new("password_digest"),
|
||||||
if_exists: false,
|
if_exists: false,
|
||||||
drop_behavior: None,
|
drop_behavior: None,
|
||||||
|
@ -2848,6 +2849,7 @@ fn parse_alter_table_with_lock() {
|
||||||
operations,
|
operations,
|
||||||
vec![
|
vec![
|
||||||
AlterTableOperation::DropColumn {
|
AlterTableOperation::DropColumn {
|
||||||
|
has_column_keyword: true,
|
||||||
column_name: Ident::new("password_digest"),
|
column_name: Ident::new("password_digest"),
|
||||||
if_exists: false,
|
if_exists: false,
|
||||||
drop_behavior: None,
|
drop_behavior: None,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue