Skip to content

Commit

Permalink
feat(fuzz): add alter table options for alter fuzzer (#5074)
Browse files Browse the repository at this point in the history
* feat(fuzz): add set table options to alter fuzzer

* chore: clippy is happy, I'm sad

* chore: happy ci happy

* fix: unit test

* feat(fuzz): add unset table options to alter fuzzer

* fix: unit test

* feat(fuzz): add table option validator

* fix: make clippy happy

* chore: add comments

* chore: apply review comments

* fix: unit test

* feat(fuzz): add more ttl options

* fix: #5108

* chore: add comments

* chore: add comments
  • Loading branch information
CookiePieWw authored Dec 12, 2024
1 parent d53fbcb commit 03ad6e2
Show file tree
Hide file tree
Showing 20 changed files with 742 additions and 68 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/common/base/src/readable_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub const GIB: u64 = MIB * BINARY_DATA_MAGNITUDE;
pub const TIB: u64 = GIB * BINARY_DATA_MAGNITUDE;
pub const PIB: u64 = TIB * BINARY_DATA_MAGNITUDE;

#[derive(Clone, Copy, PartialEq, Eq, Ord, PartialOrd)]
#[derive(Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Default)]
pub struct ReadableSize(pub u64);

impl ReadableSize {
Expand Down
21 changes: 6 additions & 15 deletions src/sql/src/statements/alter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,20 @@ pub enum AlterTableOperation {
target_type: DataType,
},
/// `SET <table attrs key> = <table attr value>`
SetTableOptions {
options: Vec<KeyValueOption>,
},
UnsetTableOptions {
keys: Vec<String>,
},
SetTableOptions { options: Vec<KeyValueOption> },
/// `UNSET <table attrs key>`
UnsetTableOptions { keys: Vec<String> },
/// `DROP COLUMN <name>`
DropColumn {
name: Ident,
},
DropColumn { name: Ident },
/// `RENAME <new_table_name>`
RenameTable {
new_table_name: String,
},
RenameTable { new_table_name: String },
/// `MODIFY COLUMN <column_name> SET FULLTEXT [WITH <options>]`
SetColumnFulltext {
column_name: Ident,
options: FulltextOptions,
},
/// `MODIFY COLUMN <column_name> UNSET FULLTEXT`
UnsetColumnFulltext {
column_name: Ident,
},
UnsetColumnFulltext { column_name: Ident },
}

impl Display for AlterTableOperation {
Expand Down
11 changes: 6 additions & 5 deletions tests-fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ unstable = ["nix"]
arbitrary = { version = "1.3.0", features = ["derive"] }
async-trait = { workspace = true }
chrono = { workspace = true }
common-base = { workspace = true }
common-error = { workspace = true }
common-macro = { workspace = true }
common-query = { workspace = true }
Expand Down Expand Up @@ -67,14 +68,14 @@ dotenv.workspace = true

[[bin]]
name = "fuzz_create_table"
path = "targets/fuzz_create_table.rs"
path = "targets/ddl/fuzz_create_table.rs"
test = false
bench = false
doc = false

[[bin]]
name = "fuzz_create_logical_table"
path = "targets/fuzz_create_logical_table.rs"
path = "targets/ddl/fuzz_create_logical_table.rs"
test = false
bench = false
doc = false
Expand All @@ -95,21 +96,21 @@ doc = false

[[bin]]
name = "fuzz_alter_table"
path = "targets/fuzz_alter_table.rs"
path = "targets/ddl/fuzz_alter_table.rs"
test = false
bench = false
doc = false

[[bin]]
name = "fuzz_alter_logical_table"
path = "targets/fuzz_alter_logical_table.rs"
path = "targets/ddl/fuzz_alter_logical_table.rs"
test = false
bench = false
doc = false

[[bin]]
name = "fuzz_create_database"
path = "targets/fuzz_create_database.rs"
path = "targets/ddl/fuzz_create_database.rs"
test = false
bench = false
doc = false
Expand Down
59 changes: 52 additions & 7 deletions tests-fuzz/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use snafu::{ensure, OptionExt};

use crate::error::{self, Result};
use crate::generator::Random;
use crate::ir::alter_expr::AlterTableOperation;
use crate::ir::alter_expr::{AlterTableOperation, AlterTableOption};
use crate::ir::{AlterTableExpr, Column, CreateTableExpr, Ident};

pub type TableContextRef = Arc<TableContext>;
Expand All @@ -35,6 +35,7 @@ pub struct TableContext {
// GreptimeDB specific options
pub partition: Option<PartitionDef>,
pub primary_keys: Vec<usize>,
pub table_options: Vec<AlterTableOption>,
}

impl From<&CreateTableExpr> for TableContext {
Expand All @@ -52,6 +53,7 @@ impl From<&CreateTableExpr> for TableContext {
columns: columns.clone(),
partition: partition.clone(),
primary_keys: primary_keys.clone(),
table_options: vec![],
}
}
}
Expand All @@ -64,7 +66,7 @@ impl TableContext {

/// Applies the [AlterTableExpr].
pub fn alter(mut self, expr: AlterTableExpr) -> Result<TableContext> {
match expr.alter_options {
match expr.alter_kinds {
AlterTableOperation::AddColumn { column, location } => {
ensure!(
!self.columns.iter().any(|col| col.name == column.name),
Expand Down Expand Up @@ -140,6 +142,25 @@ impl TableContext {
}
Ok(self)
}
AlterTableOperation::SetTableOptions { options } => {
for option in options {
if let Some(idx) = self
.table_options
.iter()
.position(|opt| opt.key() == option.key())
{
self.table_options[idx] = option;
} else {
self.table_options.push(option);
}
}
Ok(self)
}
AlterTableOperation::UnsetTableOptions { keys } => {
self.table_options
.retain(|opt| !keys.contains(&opt.key().to_string()));
Ok(self)
}
}
}

Expand Down Expand Up @@ -171,10 +192,11 @@ impl TableContext {
#[cfg(test)]
mod tests {
use common_query::AddColumnLocation;
use common_time::Duration;
use datatypes::data_type::ConcreteDataType;

use super::TableContext;
use crate::ir::alter_expr::AlterTableOperation;
use crate::ir::alter_expr::{AlterTableOperation, AlterTableOption, Ttl};
use crate::ir::create_expr::ColumnOption;
use crate::ir::{AlterTableExpr, Column, Ident};

Expand All @@ -185,11 +207,12 @@ mod tests {
columns: vec![],
partition: None,
primary_keys: vec![],
table_options: vec![],
};
// Add a column
let expr = AlterTableExpr {
table_name: "foo".into(),
alter_options: AlterTableOperation::AddColumn {
alter_kinds: AlterTableOperation::AddColumn {
column: Column {
name: "a".into(),
column_type: ConcreteDataType::timestamp_microsecond_datatype(),
Expand All @@ -205,7 +228,7 @@ mod tests {
// Add a column at first
let expr = AlterTableExpr {
table_name: "foo".into(),
alter_options: AlterTableOperation::AddColumn {
alter_kinds: AlterTableOperation::AddColumn {
column: Column {
name: "b".into(),
column_type: ConcreteDataType::timestamp_microsecond_datatype(),
Expand All @@ -221,7 +244,7 @@ mod tests {
// Add a column after "b"
let expr = AlterTableExpr {
table_name: "foo".into(),
alter_options: AlterTableOperation::AddColumn {
alter_kinds: AlterTableOperation::AddColumn {
column: Column {
name: "c".into(),
column_type: ConcreteDataType::timestamp_microsecond_datatype(),
Expand All @@ -239,10 +262,32 @@ mod tests {
// Drop the column "b"
let expr = AlterTableExpr {
table_name: "foo".into(),
alter_options: AlterTableOperation::DropColumn { name: "b".into() },
alter_kinds: AlterTableOperation::DropColumn { name: "b".into() },
};
let table_ctx = table_ctx.alter(expr).unwrap();
assert_eq!(table_ctx.columns[1].name, Ident::new("a"));
assert_eq!(table_ctx.primary_keys, vec![0, 1]);

// Set table options
let ttl_option = AlterTableOption::Ttl(Ttl::Duration(Duration::new_second(60)));
let expr = AlterTableExpr {
table_name: "foo".into(),
alter_kinds: AlterTableOperation::SetTableOptions {
options: vec![ttl_option.clone()],
},
};
let table_ctx = table_ctx.alter(expr).unwrap();
assert_eq!(table_ctx.table_options.len(), 1);
assert_eq!(table_ctx.table_options[0], ttl_option);

// Unset table options
let expr = AlterTableExpr {
table_name: "foo".into(),
alter_kinds: AlterTableOperation::UnsetTableOptions {
keys: vec![ttl_option.key().to_string()],
},
};
let table_ctx = table_ctx.alter(expr).unwrap();
assert_eq!(table_ctx.table_options.len(), 0);
}
}
Loading

0 comments on commit 03ad6e2

Please sign in to comment.