Skip to content

Commit

Permalink
fix: changing column data type can't process type alias
Browse files Browse the repository at this point in the history
  • Loading branch information
killme2008 committed May 17, 2024
1 parent e372e25 commit 4fd770e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/sql/src/parsers/alter_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,71 @@ mod tests {
}
}

#[test]
fn test_parse_alter_change_column_alias_type() {
let sql_1 = "ALTER TABLE my_metric_1 MODIFY COLUMN a MediumText";
let mut result_1 = ParserContext::create_with_dialect(
sql_1,
&GreptimeDbDialect {},
ParseOptions::default(),
)
.unwrap();

match result_1.remove(0) {
Statement::Alter(alter_table) => {
assert_eq!("my_metric_1", alter_table.table_name().0[0].value);

let alter_operation = alter_table.alter_operation();
assert_matches!(
alter_operation,
AlterTableOperation::ChangeColumnType { .. }
);
match alter_operation {
AlterTableOperation::ChangeColumnType {
column_name,
target_type,
} => {
assert_eq!("a", column_name.value);
assert_eq!(DataType::Text, *target_type);
}
_ => unreachable!(),
}
}
_ => unreachable!(),
}

let sql_2 = "ALTER TABLE my_metric_1 MODIFY COLUMN a TIMESTAMP_US";
let mut result_2 = ParserContext::create_with_dialect(
sql_2,
&GreptimeDbDialect {},
ParseOptions::default(),
)
.unwrap();

match result_2.remove(0) {
Statement::Alter(alter_table) => {
assert_eq!("my_metric_1", alter_table.table_name().0[0].value);

let alter_operation = alter_table.alter_operation();
assert_matches!(
alter_operation,
AlterTableOperation::ChangeColumnType { .. }
);
match alter_operation {
AlterTableOperation::ChangeColumnType {
column_name,
target_type,
} => {
assert_eq!("a", column_name.value);
assert!(matches!(target_type, DataType::Timestamp(Some(6), _)));
}
_ => unreachable!(),
}
}
_ => unreachable!(),
}
}

#[test]
fn test_parse_alter_rename_table() {
let sql = "ALTER TABLE test_table table_t";
Expand Down
4 changes: 4 additions & 0 deletions src/sql/src/statements/alter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ impl AlterTable {
pub fn alter_operation(&self) -> &AlterTableOperation {
&self.alter_operation
}

pub fn alter_operation_mut(&mut self) -> &mut AlterTableOperation {
&mut self.alter_operation
}
}

impl Display for AlterTable {
Expand Down
8 changes: 8 additions & 0 deletions src/sql/src/statements/transform/type_alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use sqlparser::ast::{
};

use crate::error::Result;
use crate::statements::alter::AlterTableOperation;
use crate::statements::create::{CreateExternalTable, CreateTable};
use crate::statements::statement::Statement;
use crate::statements::transform::TransformRule;
Expand Down Expand Up @@ -51,6 +52,13 @@ impl TransformRule for TypeAliasTransformRule {
.iter_mut()
.for_each(|ColumnDef { data_type, .. }| replace_type_alias(data_type));
}
Statement::Alter(alter_table) => {
if let AlterTableOperation::ChangeColumnType { target_type, .. } =
alter_table.alter_operation_mut()
{
replace_type_alias(target_type)
}
}
_ => {}
}

Expand Down

0 comments on commit 4fd770e

Please sign in to comment.