tests_fuzz/generator/
select_expr.rsuse std::marker::PhantomData;
use derive_builder::Builder;
use rand::seq::{IndexedRandom, SliceRandom};
use rand::Rng;
use crate::context::TableContextRef;
use crate::error::{Error, Result};
use crate::generator::Generator;
use crate::ir::select_expr::{Direction, SelectExpr};
#[derive(Builder)]
#[builder(pattern = "owned")]
pub struct SelectExprGenerator<R: Rng + 'static> {
table_ctx: TableContextRef,
#[builder(default = "8192")]
max_limit: usize,
#[builder(default)]
_phantom: PhantomData<R>,
}
impl<R: Rng + 'static> Generator<SelectExpr, R> for SelectExprGenerator<R> {
type Error = Error;
fn generate(&self, rng: &mut R) -> Result<SelectExpr> {
let selection = rng.random_range(1..self.table_ctx.columns.len());
let mut selected_columns = self
.table_ctx
.columns
.choose_multiple(rng, selection)
.cloned()
.collect::<Vec<_>>();
selected_columns.shuffle(rng);
let order_by_selection = rng.random_range(1..selection);
let order_by = selected_columns
.choose_multiple(rng, order_by_selection)
.map(|c| c.name.to_string())
.collect::<Vec<_>>();
let limit = rng.random_range(1..self.max_limit);
let direction = if rng.random_bool(1.0 / 2.0) {
Direction::Asc
} else {
Direction::Desc
};
Ok(SelectExpr {
table_name: self.table_ctx.name.to_string(),
columns: selected_columns,
order_by,
direction,
limit,
})
}
}