From f5139f086ed2facf1fc6fc9f1b5a2410d19bc6e5 Mon Sep 17 00:00:00 2001 From: alpaylan Date: Mon, 27 Jan 2025 01:24:20 +0300 Subject: [PATCH] add select-limit property --- simulator/generation/plan.rs | 3 ++ simulator/generation/property.rs | 64 ++++++++++++++++++++++++++++++++ simulator/generation/query.rs | 2 + simulator/model/query.rs | 10 ++++- simulator/shrink/plan.rs | 12 +++--- 5 files changed, 85 insertions(+), 6 deletions(-) diff --git a/simulator/generation/plan.rs b/simulator/generation/plan.rs index 2a794fbbc..2c8c940cf 100644 --- a/simulator/generation/plan.rs +++ b/simulator/generation/plan.rs @@ -282,6 +282,9 @@ impl Interactions { query.shadow(env); } } + Property::SelectLimit { select } => { + select.shadow(env); + }, } for interaction in property.interactions() { match interaction { diff --git a/simulator/generation/property.rs b/simulator/generation/property.rs index bfa1e1ed5..517baf372 100644 --- a/simulator/generation/property.rs +++ b/simulator/generation/property.rs @@ -62,6 +62,17 @@ pub(crate) enum Property { /// Additional interactions in the middle of the property queries: Vec, }, + /// Select Limit is a property in which the select query + /// has a limit clause that is respected by the query. + /// The execution of the property is as follows + /// SELECT * FROM WHERE LIMIT + /// This property is a single-interaction property. + /// The interaction has the following constraints; + /// - The select query will respect the limit clause. + SelectLimit { + /// The select query + select: Select, + }, } impl Property { @@ -69,6 +80,7 @@ impl Property { match self { Property::InsertSelect { .. } => "Insert-Select".to_string(), Property::DoubleCreateFailure { .. } => "Double-Create-Failure".to_string(), + Property::SelectLimit { .. } => "Select-Limit".to_string(), } } /// interactions construct a list of interactions, which is an executable representation of the property. @@ -164,6 +176,38 @@ impl Property { interactions } + Property::SelectLimit { select } => { + let table_name = select.table.clone(); + + let assumption = Interaction::Assumption(Assertion { + message: format!("table {} exists", table_name), + func: Box::new({ + let table_name = table_name.clone(); + move |_: &Vec, env: &SimulatorEnv| { + Ok(env.tables.iter().any(|t| t.name == table_name)) + } + }), + }); + + let limit = select.limit.clone().unwrap_or(0); + + let assertion = Interaction::Assertion(Assertion { + message: "select query should respect the limit clause".to_string(), + func: Box::new(move |stack: &Vec, _: &SimulatorEnv| { + let last = stack.last().unwrap(); + match last { + Ok(rows) => Ok(limit >= rows.len()), + Err(_) => Ok(true), + } + }), + }); + + vec![ + assumption, + Interaction::Query(Query::Select(select.clone())), + assertion, + ] + } } } } @@ -248,6 +292,7 @@ fn property_insert_select( let select_query = Select { table: table.name.clone(), predicate: Predicate::arbitrary_from(rng, (table, &row)), + limit: None, }; Property::InsertSelect { @@ -258,6 +303,21 @@ fn property_insert_select( } } +fn property_select_limit( + rng: &mut R, + env: &SimulatorEnv, +) -> Property { + // Get a random table + let table = pick(&env.tables, rng); + // Select the table + let select = Select { + table: table.name.clone(), + predicate: Predicate::arbitrary_from(rng, table), + limit: Some(rng.gen_range(1..=5)), + }; + Property::SelectLimit { select } +} + fn property_double_create_failure( rng: &mut R, env: &SimulatorEnv, @@ -312,6 +372,10 @@ impl ArbitraryFrom<(&SimulatorEnv, &InteractionStats)> for Property { remaining_.create / 2.0, Box::new(|rng: &mut R| property_double_create_failure(rng, env, &remaining_)), ), + ( + remaining_.read, + Box::new(|rng: &mut R| property_select_limit(rng, env)), + ), ], rng, ) diff --git a/simulator/generation/query.rs b/simulator/generation/query.rs index 8b93fa993..1a0d167fc 100644 --- a/simulator/generation/query.rs +++ b/simulator/generation/query.rs @@ -23,6 +23,7 @@ impl ArbitraryFrom<&Vec> for Select { Self { table: table.name.clone(), predicate: Predicate::arbitrary_from(rng, table), + limit: Some(rng.gen_range(0..=1000)), } } } @@ -33,6 +34,7 @@ impl ArbitraryFrom<&Vec<&Table>> for Select { Self { table: table.name.clone(), predicate: Predicate::arbitrary_from(rng, *table), + limit: Some(rng.gen_range(0..=1000)), } } } diff --git a/simulator/model/query.rs b/simulator/model/query.rs index f03bbde6f..8c861edc4 100644 --- a/simulator/model/query.rs +++ b/simulator/model/query.rs @@ -140,6 +140,7 @@ impl Create { pub(crate) struct Select { pub(crate) table: String, pub(crate) predicate: Predicate, + pub(crate) limit: Option, } impl Select { @@ -190,7 +191,14 @@ impl Display for Query { Self::Select(Select { table, predicate: guard, - }) => write!(f, "SELECT * FROM {} WHERE {}", table, guard), + limit, + }) => write!( + f, + "SELECT * FROM {} WHERE {}{}", + table, + guard, + limit.map_or("".to_string(), |l| format!(" LIMIT {}", l)) + ), Self::Insert(Insert { table, values }) => { write!(f, "INSERT INTO {} VALUES ", table)?; for (i, row) in values.iter().enumerate() { diff --git a/simulator/shrink/plan.rs b/simulator/shrink/plan.rs index 92867d82e..bc5eaa4d0 100644 --- a/simulator/shrink/plan.rs +++ b/simulator/shrink/plan.rs @@ -1,5 +1,8 @@ use crate::{ - generation::plan::{InteractionPlan, Interactions}, + generation::{ + plan::{InteractionPlan, Interactions}, + property::Property, + }, model::query::Query, runner::execution::Execution, }; @@ -27,12 +30,11 @@ impl InteractionPlan { for interaction in plan.plan.iter_mut() { if let Interactions::Property(p) = interaction { match p { - crate::generation::property::Property::InsertSelect { queries, .. } - | crate::generation::property::Property::DoubleCreateFailure { - queries, .. - } => { + Property::InsertSelect { queries, .. } + | Property::DoubleCreateFailure { queries, .. } => { queries.clear(); } + Property::SelectLimit { .. } => {} } } }