From 48d091e112757abaf091376fd84f06aca7a2dc37 Mon Sep 17 00:00:00 2001 From: alpaylan Date: Sat, 1 Feb 2025 10:40:07 -0500 Subject: [PATCH] add insert into
> for Select { - fn arbitrary_from(rng: &mut R, tables: &Vec
) -> Self { - let table = pick(tables, rng); +impl ArbitraryFrom<&SimulatorEnv> for Select { + fn arbitrary_from(rng: &mut R, env: &SimulatorEnv) -> Self { + let table = pick(&env.tables, rng); Self { table: table.name.clone(), predicate: Predicate::arbitrary_from(rng, table), limit: Some(rng.gen_range(0..=1000)), + distinct: Distinctness::All, } } } -impl ArbitraryFrom<&Vec<&Table>> for Select { - fn arbitrary_from(rng: &mut R, tables: &Vec<&Table>) -> Self { - let table = pick(tables, rng); - Self { - table: table.name.clone(), - predicate: Predicate::arbitrary_from(rng, *table), - limit: Some(rng.gen_range(0..=1000)), - } - } -} - -impl ArbitraryFrom<&Table> for Insert { - fn arbitrary_from(rng: &mut R, table: &Table) -> Self { - let num_rows = rng.gen_range(1..10); - let values: Vec> = (0..num_rows) - .map(|_| { - table - .columns - .iter() - .map(|c| Value::arbitrary_from(rng, &c.column_type)) - .collect() +impl ArbitraryFrom<&SimulatorEnv> for Insert { + fn arbitrary_from(rng: &mut R, env: &SimulatorEnv) -> Self { + let gen_values = |rng: &mut R| { + let table = pick(&env.tables, rng); + let num_rows = rng.gen_range(1..10); + let values: Vec> = (0..num_rows) + .map(|_| { + table + .columns + .iter() + .map(|c| Value::arbitrary_from(rng, &c.column_type)) + .collect() + }) + .collect(); + Some(Insert::Values { + table: table.name.clone(), + values, }) - .collect(); - Self { - table: table.name.clone(), - values, - } - } -} + }; -impl ArbitraryFrom<&Table> for Delete { - fn arbitrary_from(rng: &mut R, table: &Table) -> Self { - Self { - table: table.name.clone(), - predicate: Predicate::arbitrary_from(rng, table), - } - } -} + let _gen_select = |rng: &mut R| { + // Find a non-empty table + let table = env.tables.iter().find(|t| !t.rows.is_empty()); + if table.is_none() { + return None; + } -impl ArbitraryFrom<&Table> for Query { - fn arbitrary_from(rng: &mut R, table: &Table) -> Self { - frequency( + let select_table = table.unwrap(); + let row = pick(&select_table.rows, rng); + let predicate = Predicate::arbitrary_from(rng, (select_table, row)); + // Pick another table to insert into + let select = Select { + table: select_table.name.clone(), + predicate, + limit: None, + distinct: Distinctness::All, + }; + let table = pick(&env.tables, rng); + Some(Insert::Select { + table: table.name.clone(), + select: Box::new(select), + }) + }; + + backtrack( vec![ - (1, Box::new(|rng| Self::Create(Create::arbitrary(rng)))), - ( - 100, - Box::new(|rng| Self::Select(Select::arbitrary_from(rng, &vec![table]))), - ), - ( - 100, - Box::new(|rng| Self::Insert(Insert::arbitrary_from(rng, table))), - ), - ( - 0, - Box::new(|rng| Self::Delete(Delete::arbitrary_from(rng, table))), - ), + (1, Box::new(|rng| gen_values(rng))), + // todo: test and enable this once `INSERT INTO
SELECT * FROM
` is supported + // (1, Box::new(|rng| gen_select(rng))), ], rng, ) } } -impl ArbitraryFrom<(&Table, &Remaining)> for Query { - fn arbitrary_from(rng: &mut R, (table, remaining): (&Table, &Remaining)) -> Self { +impl ArbitraryFrom<&SimulatorEnv> for Delete { + fn arbitrary_from(rng: &mut R, env: &SimulatorEnv) -> Self { + let table = pick(&env.tables, rng); + Self { + table: table.name.clone(), + predicate: Predicate::arbitrary_from(rng, table), + } + } +} + +impl ArbitraryFrom<(&SimulatorEnv, &Remaining)> for Query { + fn arbitrary_from(rng: &mut R, (env, remaining): (&SimulatorEnv, &Remaining)) -> Self { frequency( vec![ ( @@ -100,15 +105,15 @@ impl ArbitraryFrom<(&Table, &Remaining)> for Query { ), ( remaining.read, - Box::new(|rng| Self::Select(Select::arbitrary_from(rng, &vec![table]))), + Box::new(|rng| Self::Select(Select::arbitrary_from(rng, env))), ), ( remaining.write, - Box::new(|rng| Self::Insert(Insert::arbitrary_from(rng, table))), + Box::new(|rng| Self::Insert(Insert::arbitrary_from(rng, env))), ), ( 0.0, - Box::new(|rng| Self::Delete(Delete::arbitrary_from(rng, table))), + Box::new(|rng| Self::Delete(Delete::arbitrary_from(rng, env))), ), ], rng, diff --git a/simulator/model/query.rs b/simulator/model/query.rs index 8c861edc4..bc97be0b2 100644 --- a/simulator/model/query.rs +++ b/simulator/model/query.rs @@ -101,7 +101,8 @@ impl Query { match self { Query::Create(_) => vec![], Query::Select(Select { table, .. }) - | Query::Insert(Insert { table, .. }) + | Query::Insert(Insert::Select { table, .. }) + | Query::Insert(Insert::Values { table, .. }) | Query::Delete(Delete { table, .. }) => vec![table.clone()], } } @@ -109,12 +110,13 @@ impl Query { match self { Query::Create(Create { table }) => vec![table.name.clone()], Query::Select(Select { table, .. }) - | Query::Insert(Insert { table, .. }) + | Query::Insert(Insert::Select { table, .. }) + | Query::Insert(Insert::Values { table, .. }) | Query::Delete(Delete { table, .. }) => vec![table.clone()], } } - pub(crate) fn shadow(&self, env: &mut SimulatorEnv) { + pub(crate) fn shadow(&self, env: &mut SimulatorEnv) -> Vec> { match self { Query::Create(create) => create.shadow(env), Query::Insert(insert) => insert.shadow(env), @@ -129,34 +131,110 @@ pub(crate) struct Create { } impl Create { - pub(crate) fn shadow(&self, env: &mut SimulatorEnv) { + pub(crate) fn shadow(&self, env: &mut SimulatorEnv) -> Vec> { if !env.tables.iter().any(|t| t.name == self.table.name) { env.tables.push(self.table.clone()); } + + vec![] } } +impl Display for Create { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "CREATE TABLE {} (", self.table.name)?; + + for (i, column) in self.table.columns.iter().enumerate() { + if i != 0 { + write!(f, ",")?; + } + write!(f, "{} {}", column.name, column.column_type)?; + } + + write!(f, ")") + } +} + +/// `SELECT` distinctness +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub enum Distinctness { + /// `DISTINCT` + Distinct, + /// `ALL` + All, +} + #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub(crate) struct Select { pub(crate) table: String, pub(crate) predicate: Predicate, + pub(crate) distinct: Distinctness, pub(crate) limit: Option, } impl Select { - pub(crate) fn shadow(&self, _env: &mut SimulatorEnv) {} + pub(crate) fn shadow(&self, env: &mut SimulatorEnv) -> Vec> { + let table = env.tables.iter().find(|t| t.name == self.table.as_str()); + if let Some(table) = table { + table + .rows + .iter() + .filter(|row| self.predicate.test(row, table)) + .cloned() + .collect() + } else { + vec![] + } + } +} + +impl Display for Select { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "SELECT * FROM {} WHERE {}{}", + self.table, + self.predicate, + self.limit + .map_or("".to_string(), |l| format!(" LIMIT {}", l)) + ) + } } #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] -pub(crate) struct Insert { - pub(crate) table: String, - pub(crate) values: Vec>, +pub(crate) enum Insert { + Values { + table: String, + values: Vec>, + }, + Select { + table: String, + select: Box