Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tests-fuzz): add CreateTableExprGenerator & AlterTableExprGenerator #3182

Merged
merged 5 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 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/partition/src/partition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub enum PartitionBound {
MaxValue,
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct PartitionDef {
partition_columns: Vec<String>,
partition_bounds: Vec<PartitionBound>,
Expand Down
11 changes: 11 additions & 0 deletions tests-fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,14 @@ license.workspace = true

[dependencies]
async-trait = { workspace = true }
common-error = { workspace = true }
common-macro = { workspace = true }
common-query = { workspace = true }
datatypes = { workspace = true }
derive_builder = { workspace = true }
faker_rand = "0.1"
lazy_static = { workspace = true }
partition = { workspace = true }
rand = { workspace = true }
snafu = { workspace = true }
sql = { workspace = true }
41 changes: 41 additions & 0 deletions tests-fuzz/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2023 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use common_macro::stack_trace_debug;
use snafu::{Location, Snafu};

use crate::ir::create_expr::CreateTableExprBuilderError;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Snafu)]
#[snafu(visibility(pub))]
#[stack_trace_debug]
pub enum Error {
#[snafu(display("Unexpected, violated: {violated}"))]
Unexpected {
violated: String,
location: Location,
},

#[snafu(display("Failed to build create table expr"))]
BuildCreateTableExpr {
#[snafu(source)]
error: CreateTableExprBuilderError,
location: Location,
},

#[snafu(display("No droppable columns"))]
DroppableColumns { location: Location },
}
14 changes: 12 additions & 2 deletions tests-fuzz/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,21 @@
// See the License for the specific language governing permissions and
// limitations under the License.

pub mod alter_expr;
pub mod create_expr;

use std::fmt;

#[async_trait::async_trait]
use crate::error::Error;
use crate::ir::{AlterTableExpr, CreateTableExpr};

pub type CreateTableExprGenerator =
Box<dyn Generator<CreateTableExpr, Error = Error> + Sync + Send>;

pub type AlterTableExprGenerator = Box<dyn Generator<AlterTableExpr, Error = Error> + Sync + Send>;

pub(crate) trait Generator<T> {
type Error: Sync + Send + fmt::Debug;

async fn generate(&self) -> Result<T, Self::Error>;
fn generate(&self) -> Result<T, Self::Error>;
}
142 changes: 142 additions & 0 deletions tests-fuzz/src/generator/alter_expr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright 2023 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::f64::consts::E;
use std::sync::Arc;

use common_query::AddColumnLocation;
use faker_rand::lorem::Word;
use rand::{random, Rng};
use snafu::ensure;

use crate::error::{self, Result};
use crate::ir::alter_expr::{AlterTableExpr, AlterTableOperation};
use crate::ir::{droppable_columns, Column};

pub struct AlterTableExprGenerator {
name: String,
columns: Arc<Vec<Column>>,
ignore_no_droppable_column: bool,
}

impl AlterTableExprGenerator {
pub fn new(name: String, columns: Arc<Vec<Column>>) -> Self {
Self {
name,
columns,
ignore_no_droppable_column: false,
}
}

/// If the `ignore_no_droppable_column` is true, it retries if there is no droppable column.
WenyXu marked this conversation as resolved.
Show resolved Hide resolved
pub fn ignore_no_droppable_column(mut self, v: bool) -> Self {
self.ignore_no_droppable_column = v;
self
}

fn generate_inner(&self) -> Result<AlterTableExpr> {
let mut rng = rand::thread_rng();
let idx = rng.gen_range(0..3);
// 0 -> AddColumn
// 1 -> DropColumn(invariant: We can't non-primary key columns, non-ts columns)
// 2 -> RenameTable
let alter_expr = match idx {
WenyXu marked this conversation as resolved.
Show resolved Hide resolved
0 => {
let with_location = rng.gen::<bool>();
let location = if with_location {
let use_first = rng.gen::<bool>();
let location = if use_first {
AddColumnLocation::First
} else {
AddColumnLocation::After {
column_name: self.columns[rng.gen_range(0..self.columns.len())]
.name
.to_string(),
}
};
Some(location)
} else {
None
};
let column = rng.gen::<Column>();
AlterTableExpr {
name: self.name.to_string(),
alter_options: AlterTableOperation::AddColumn { column, location },
}
}
1 => {
let droppable = droppable_columns(&self.columns);
ensure!(!droppable.is_empty(), error::DroppableColumnsSnafu);
let name = droppable[rng.gen_range(0..droppable.len())]
.name
.to_string();
AlterTableExpr {
name: self.name.to_string(),
alter_options: AlterTableOperation::DropColumn { name },
}
}
2 => {
let mut new_table_name = rng.gen::<Word>().to_string();
if new_table_name == self.name {
new_table_name = format!("{}-{}", self.name, rng.gen::<u64>());
}
AlterTableExpr {
name: self.name.to_string(),
alter_options: AlterTableOperation::RenameTable { new_table_name },
}
}
_ => unreachable!(),
};

Ok(alter_expr)
}

/// Generates the [AlterTableExpr].
pub fn generate(&self) -> Result<AlterTableExpr> {
match self.generate_inner() {
Ok(expr) => Ok(expr),
Err(err) => {
if matches!(err, error::Error::DroppableColumns { .. }) {
return self.generate();
}
Err(err)
}
}
}
}

#[cfg(test)]
mod tests {
use std::sync::Arc;

use super::AlterTableExprGenerator;
use crate::generator::create_expr::CreateTableExprGenerator;
use crate::generator::Generator;

#[test]
fn test_alter_table_expr_generator() {
let create_expr = CreateTableExprGenerator::default()
.columns(10)
.generate()
.unwrap();

let alter_expr = AlterTableExprGenerator::new(
create_expr.name.to_string(),
Arc::new(create_expr.columns),
)
.ignore_no_droppable_column(true)
.generate()
.unwrap();
}
}
Loading
Loading