Skip to content

Commit

Permalink
test: alter table
Browse files Browse the repository at this point in the history
  • Loading branch information
killme2008 committed Nov 17, 2022
1 parent 33a8e61 commit 520911e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
60 changes: 57 additions & 3 deletions src/datanode/src/tests/instance_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ async fn assert_query_result(instance: &Instance, sql: &str, ts: i64, host: &str
}
}

#[tokio::test(flavor = "multi_thread")]
async fn test_execute_insert() {
async fn setup_test_instance() -> Instance {
common_telemetry::init_default_ut_logging();

let (opts, _guard) = test_util::create_tmp_dir_and_datanode_opts("execute_insert");
Expand All @@ -195,6 +194,12 @@ async fn test_execute_insert() {
.await
.unwrap();

instance
}

#[tokio::test(flavor = "multi_thread")]
async fn test_execute_insert() {
let instance = setup_test_instance().await;
let output = instance
.execute_sql(
r#"insert into demo(host, cpu, memory, ts) values
Expand Down Expand Up @@ -479,6 +484,7 @@ async fn test_alter_table() {
.await
.unwrap();

// Add column
let output = instance
.execute_sql("alter table demo add my_tag string null")
.await
Expand All @@ -498,7 +504,10 @@ async fn test_alter_table() {
.unwrap();
assert!(matches!(output, Output::AffectedRows(1)));

let output = instance.execute_sql("select * from demo").await.unwrap();
let output = instance
.execute_sql("select * from demo order by ts")
.await
.unwrap();
let expected = vec![
"+-------+-----+--------+---------------------+--------+",
"| host | cpu | memory | ts | my_tag |",
Expand All @@ -509,6 +518,51 @@ async fn test_alter_table() {
"+-------+-----+--------+---------------------+--------+",
];
check_output_stream(output, expected).await;

// Drop a column
let output = instance
.execute_sql("alter table demo drop column memory")
.await
.unwrap();
assert!(matches!(output, Output::AffectedRows(0)));

let output = instance
.execute_sql("select * from demo order by ts")
.await
.unwrap();
let expected = vec![
"+-------+-----+---------------------+--------+",
"| host | cpu | ts | my_tag |",
"+-------+-----+---------------------+--------+",
"| host1 | 1.1 | 1970-01-01 00:00:01 | |",
"| host2 | 2.2 | 1970-01-01 00:00:02 | hello |",
"| host3 | 3.3 | 1970-01-01 00:00:03 | |",
"+-------+-----+---------------------+--------+",
];
check_output_stream(output, expected).await;

// insert a new row
let output = instance
.execute_sql("insert into demo(host, cpu, ts, my_tag) values ('host4', 400, 4000, 'world')")
.await
.unwrap();
assert!(matches!(output, Output::AffectedRows(1)));

let output = instance
.execute_sql("select * from demo order by ts")
.await
.unwrap();
let expected = vec![
"+-------+-----+---------------------+--------+",
"| host | cpu | ts | my_tag |",
"+-------+-----+---------------------+--------+",
"| host1 | 1.1 | 1970-01-01 00:00:01 | |",
"| host2 | 2.2 | 1970-01-01 00:00:02 | hello |",
"| host3 | 3.3 | 1970-01-01 00:00:03 | |",
"| host4 | 400 | 1970-01-01 00:00:04 | world |",
"+-------+-----+---------------------+--------+",
];
check_output_stream(output, expected).await;
}

async fn test_insert_with_default_value_for_type(type_name: &str) {
Expand Down
4 changes: 1 addition & 3 deletions src/sql/src/statements/alter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ impl TryFrom<AlterTable> for AlterExpr {
}
AlterTableOperation::DropColumn { name } => {
alter_expr::Kind::DropColumns(api::v1::DropColumns {
drop_columns: vec![DropColumn {
name: name.value.clone(),
}],
drop_columns: vec![DropColumn { name: name.value }],
})
}
};
Expand Down

0 comments on commit 520911e

Please sign in to comment.