mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2025-12-27 08:29:59 +00:00
feat(fuzz): support to create metric table (#3617)
Co-authored-by: tison <wander4096@gmail.com>
This commit is contained in:
@@ -12,6 +12,9 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use datatypes::value::Value;
|
||||
use derive_builder::Builder;
|
||||
use partition::partition::{PartitionBound, PartitionDef};
|
||||
use rand::seq::SliceRandom;
|
||||
@@ -39,6 +42,8 @@ pub struct CreateTableExprGenerator<R: Rng + 'static> {
|
||||
if_not_exists: bool,
|
||||
#[builder(setter(into))]
|
||||
name: String,
|
||||
#[builder(setter(into))]
|
||||
with_clause: HashMap<String, String>,
|
||||
name_generator: Box<dyn Random<Ident, R>>,
|
||||
ts_column_type_generator: ConcreteDataTypeGenerator<R>,
|
||||
column_type_generator: ConcreteDataTypeGenerator<R>,
|
||||
@@ -58,6 +63,7 @@ impl<R: Rng + 'static> Default for CreateTableExprGenerator<R> {
|
||||
if_not_exists: false,
|
||||
partition: 0,
|
||||
name: String::new(),
|
||||
with_clause: HashMap::default(),
|
||||
name_generator: Box::new(MappedGenerator::new(WordGenerator, random_capitalize_map)),
|
||||
ts_column_type_generator: Box::new(TsColumnTypeGenerator),
|
||||
column_type_generator: Box::new(ColumnTypeGenerator),
|
||||
@@ -183,6 +189,13 @@ impl<R: Rng + 'static> Generator<CreateTableExpr, R> for CreateTableExprGenerato
|
||||
} else {
|
||||
builder.table_name(self.name.to_string());
|
||||
}
|
||||
if !self.with_clause.is_empty() {
|
||||
let mut options = HashMap::new();
|
||||
for (key, value) in &self.with_clause {
|
||||
options.insert(key.to_string(), Value::from(value.to_string()));
|
||||
}
|
||||
builder.options(options);
|
||||
}
|
||||
builder.build().context(error::BuildCreateTableExprSnafu)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,11 +29,12 @@ impl DslTranslator<CreateTableExpr, String> for CreateTableExprTranslator {
|
||||
|
||||
fn translate(&self, input: &CreateTableExpr) -> Result<String> {
|
||||
Ok(format!(
|
||||
"CREATE TABLE{}{}(\n{}\n)\n{};",
|
||||
"CREATE TABLE{}{}(\n{}\n)\n{}{};",
|
||||
Self::create_if_not_exists(input),
|
||||
input.table_name,
|
||||
Self::format_columns(input),
|
||||
Self::format_table_options(input)
|
||||
Self::format_table_options(input),
|
||||
Self::format_with_clause(input),
|
||||
))
|
||||
}
|
||||
}
|
||||
@@ -146,6 +147,18 @@ impl CreateTableExprTranslator {
|
||||
|
||||
output.join("\n")
|
||||
}
|
||||
|
||||
fn format_with_clause(input: &CreateTableExpr) -> String {
|
||||
if input.options.is_empty() {
|
||||
String::new()
|
||||
} else {
|
||||
let mut output = vec![];
|
||||
for (key, value) in &input.options {
|
||||
output.push(format!("\"{key}\" = \"{value}\""));
|
||||
}
|
||||
format!(" with ({})", output.join("\n"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CreateDatabaseExprTranslator;
|
||||
|
||||
@@ -61,16 +61,31 @@ impl Arbitrary<'_> for FuzzInput {
|
||||
|
||||
fn generate_expr(input: FuzzInput) -> Result<CreateTableExpr> {
|
||||
let mut rng = ChaChaRng::seed_from_u64(input.seed);
|
||||
let create_table_generator = CreateTableExprGeneratorBuilder::default()
|
||||
.name_generator(Box::new(MappedGenerator::new(
|
||||
WordGenerator,
|
||||
merge_two_word_map_fn(random_capitalize_map, uppercase_and_keyword_backtick_map),
|
||||
)))
|
||||
.columns(input.columns)
|
||||
.engine("mito")
|
||||
.build()
|
||||
.unwrap();
|
||||
create_table_generator.generate(&mut rng)
|
||||
let metric_engine = rng.gen_bool(0.5);
|
||||
if metric_engine {
|
||||
let create_table_generator = CreateTableExprGeneratorBuilder::default()
|
||||
.name_generator(Box::new(MappedGenerator::new(
|
||||
WordGenerator,
|
||||
merge_two_word_map_fn(random_capitalize_map, uppercase_and_keyword_backtick_map),
|
||||
)))
|
||||
.columns(input.columns)
|
||||
.engine("metric")
|
||||
.with_clause([("physical_metric_table".to_string(), "".to_string())])
|
||||
.build()
|
||||
.unwrap();
|
||||
create_table_generator.generate(&mut rng)
|
||||
} else {
|
||||
let create_table_generator = CreateTableExprGeneratorBuilder::default()
|
||||
.name_generator(Box::new(MappedGenerator::new(
|
||||
WordGenerator,
|
||||
merge_two_word_map_fn(random_capitalize_map, uppercase_and_keyword_backtick_map),
|
||||
)))
|
||||
.columns(input.columns)
|
||||
.engine("mito")
|
||||
.build()
|
||||
.unwrap();
|
||||
create_table_generator.generate(&mut rng)
|
||||
}
|
||||
}
|
||||
|
||||
async fn execute_create_table(ctx: FuzzContext, input: FuzzInput) -> Result<()> {
|
||||
|
||||
Reference in New Issue
Block a user