Skip to content

Commit e06911a

Browse files
authored
Merge pull request #25123 from petrosagg/derive-arbitrary
storage: derive `Arbitrary` where possible
2 parents 83cbedd + 04f1efc commit e06911a

File tree

14 files changed

+76
-520
lines changed

14 files changed

+76
-520
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/mysql-util/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ mysql_async = { version = "0.33.0", default-features = false, features = ["minim
2222
once_cell = "1.16.0"
2323
prost = { version = "0.11.3", features = ["no-recursion-limit"] }
2424
proptest = { version = "1.0.0", default-features = false, features = ["std"] }
25+
proptest-derive = { version = "0.3.0", features = ["boxed_union"]}
2526
serde = { version = "1.0.152", features = ["derive"] }
2627
thiserror = "1.0.37"
2728
tracing = "0.1.37"

src/mysql-util/src/desc.rs

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ use std::collections::BTreeSet;
1111

1212
use mz_proto::{IntoRustIfSome, RustType, TryFromProtoError};
1313
use mz_repr::ColumnType;
14-
use proptest::prelude::{any, Arbitrary};
15-
use proptest::strategy::{BoxedStrategy, Strategy};
14+
use proptest::prelude::any;
15+
use proptest_derive::Arbitrary;
1616
use serde::{Deserialize, Serialize};
1717

1818
include!(concat!(env!("OUT_DIR"), "/mz_mysql_util.rs"));
1919

20-
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
20+
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, Arbitrary)]
2121
pub struct MySqlTableDesc {
2222
/// In MySQL the schema and database of a table are synonymous.
2323
pub schema_name: String,
@@ -28,9 +28,11 @@ pub struct MySqlTableDesc {
2828
/// The index of each column is based on its `ordinal_position`
2929
/// reported by the information_schema.columns table, which defines
3030
/// the order of column values when received in a row.
31+
#[proptest(strategy = "proptest::collection::vec(any::<MySqlColumnDesc>(), 0..4)")]
3132
pub columns: Vec<MySqlColumnDesc>,
3233
/// Applicable keys for this table (i.e. primary key and unique
3334
/// constraints).
35+
#[proptest(strategy = "proptest::collection::btree_set(any::<MySqlKeyDesc>(), 0..4)")]
3436
pub keys: BTreeSet<MySqlKeyDesc>,
3537
}
3638

@@ -62,28 +64,7 @@ impl RustType<ProtoMySqlTableDesc> for MySqlTableDesc {
6264
}
6365
}
6466

65-
impl Arbitrary for MySqlTableDesc {
66-
type Parameters = ();
67-
type Strategy = BoxedStrategy<Self>;
68-
69-
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
70-
(
71-
any::<String>(),
72-
any::<String>(),
73-
proptest::collection::vec(any::<MySqlColumnDesc>(), 1..4),
74-
proptest::collection::btree_set(any::<MySqlKeyDesc>(), 1..4),
75-
)
76-
.prop_map(|(schema_name, name, columns, keys)| Self {
77-
schema_name,
78-
name,
79-
columns,
80-
keys,
81-
})
82-
.boxed()
83-
}
84-
}
85-
86-
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
67+
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, Arbitrary)]
8768
pub struct MySqlColumnDesc {
8869
/// The name of the column.
8970
pub name: String,
@@ -109,24 +90,14 @@ impl RustType<ProtoMySqlColumnDesc> for MySqlColumnDesc {
10990
}
11091
}
11192

112-
impl Arbitrary for MySqlColumnDesc {
113-
type Parameters = ();
114-
type Strategy = BoxedStrategy<Self>;
115-
116-
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
117-
(any::<String>(), any::<ColumnType>())
118-
.prop_map(|(name, column_type)| Self { name, column_type })
119-
.boxed()
120-
}
121-
}
122-
123-
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, Ord, PartialOrd)]
93+
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, Ord, PartialOrd, Arbitrary)]
12494
pub struct MySqlKeyDesc {
12595
/// The name of the index.
12696
pub name: String,
12797
/// Whether or not this key is the primary key.
12898
pub is_primary: bool,
12999
/// The columns that make up the key.
100+
#[proptest(strategy = "proptest::collection::vec(any::<String>(), 0..4)")]
130101
pub columns: Vec<String>,
131102
}
132103

@@ -147,18 +118,3 @@ impl RustType<ProtoMySqlKeyDesc> for MySqlKeyDesc {
147118
})
148119
}
149120
}
150-
151-
impl Arbitrary for MySqlKeyDesc {
152-
type Parameters = ();
153-
type Strategy = BoxedStrategy<Self>;
154-
155-
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
156-
(any::<String>(), any::<bool>(), any::<Vec<String>>())
157-
.prop_map(|(name, is_primary, columns)| Self {
158-
name,
159-
is_primary,
160-
columns,
161-
})
162-
.boxed()
163-
}
164-
}

src/postgres-util/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ postgres-openssl = { version = "0.5.0" }
2626
proptest = { version = "1.0.0", default-features = false, features = [
2727
"std",
2828
], optional = true }
29+
proptest-derive = { version = "0.3.0", features = ["boxed_union"]}
2930
prost = { version = "0.11.3", features = [
3031
"no-recursion-limit",
3132
], optional = true }

src/postgres-util/src/desc.rs

Lines changed: 8 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use std::collections::BTreeSet;
1313

1414
use anyhow::bail;
1515
use mz_proto::{IntoRustIfSome, RustType, TryFromProtoError};
16-
use proptest::prelude::{any, Arbitrary};
17-
use proptest::strategy::{BoxedStrategy, Strategy};
16+
use proptest::prelude::any;
17+
use proptest_derive::Arbitrary;
1818
use serde::{Deserialize, Serialize};
1919
use tokio_postgres::types::Oid;
2020
use tracing::warn;
@@ -35,7 +35,7 @@ pub struct PostgresSchemaDesc {
3535
}
3636

3737
/// Describes a table in a PostgreSQL database.
38-
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
38+
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, Arbitrary)]
3939
pub struct PostgresTableDesc {
4040
/// The OID of the table.
4141
pub oid: Oid,
@@ -44,9 +44,11 @@ pub struct PostgresTableDesc {
4444
/// The name of the table.
4545
pub name: String,
4646
/// The description of each column, in order of their position in the table.
47+
#[proptest(strategy = "proptest::collection::vec(any::<PostgresColumnDesc>(), 1..4)")]
4748
pub columns: Vec<PostgresColumnDesc>,
4849
/// Applicable keys for this table (i.e. primary key and unique
4950
/// constraints).
51+
#[proptest(strategy = "proptest::collection::btree_set(any::<PostgresKeyDesc>(), 1..4)")]
5052
pub keys: BTreeSet<PostgresKeyDesc>,
5153
}
5254

@@ -128,31 +130,8 @@ impl RustType<ProtoPostgresTableDesc> for PostgresTableDesc {
128130
}
129131
}
130132

131-
impl Arbitrary for PostgresTableDesc {
132-
type Strategy = BoxedStrategy<Self>;
133-
type Parameters = ();
134-
135-
fn arbitrary_with(_: Self::Parameters) -> Self::Strategy {
136-
(
137-
any::<String>(),
138-
any::<String>(),
139-
any::<u32>(),
140-
proptest::collection::vec(any::<PostgresColumnDesc>(), 1..4),
141-
proptest::collection::btree_set(any::<PostgresKeyDesc>(), 1..4),
142-
)
143-
.prop_map(|(name, namespace, oid, columns, keys)| PostgresTableDesc {
144-
name,
145-
namespace,
146-
oid,
147-
columns,
148-
keys,
149-
})
150-
.boxed()
151-
}
152-
}
153-
154133
/// Describes a column in a [`PostgresTableDesc`].
155-
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
134+
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, Arbitrary)]
156135
pub struct PostgresColumnDesc {
157136
/// The name of the column.
158137
pub name: String,
@@ -214,40 +193,16 @@ impl RustType<ProtoPostgresColumnDesc> for PostgresColumnDesc {
214193
}
215194
}
216195

217-
impl Arbitrary for PostgresColumnDesc {
218-
type Strategy = BoxedStrategy<Self>;
219-
type Parameters = ();
220-
221-
fn arbitrary_with(_: Self::Parameters) -> Self::Strategy {
222-
(
223-
any::<String>(),
224-
any::<u16>(),
225-
any::<u32>(),
226-
any::<i32>(),
227-
any::<bool>(),
228-
)
229-
.prop_map(
230-
|(name, col_num, type_oid, type_mod, nullable)| PostgresColumnDesc {
231-
name,
232-
col_num,
233-
type_oid,
234-
type_mod,
235-
nullable,
236-
},
237-
)
238-
.boxed()
239-
}
240-
}
241-
242196
/// Describes a key in a [`PostgresTableDesc`].
243-
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, PartialOrd, Ord)]
197+
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, PartialOrd, Ord, Arbitrary)]
244198
pub struct PostgresKeyDesc {
245199
/// This key is derived from the `pg_constraint` with this OID.
246200
pub oid: Oid,
247201
/// The name of the constraints.
248202
pub name: String,
249203
/// The `attnum` of the columns comprising the key. `attnum` is a unique identifier for a column
250204
/// in a PG table; see <https://www.postgresql.org/docs/current/catalog-pg-attribute.html>
205+
#[proptest(strategy = "proptest::collection::vec(any::<u16>(), 0..4)")]
251206
pub cols: Vec<u16>,
252207
/// Whether or not this key is the primary key.
253208
pub is_primary: bool,
@@ -281,28 +236,3 @@ impl RustType<ProtoPostgresKeyDesc> for PostgresKeyDesc {
281236
})
282237
}
283238
}
284-
285-
impl Arbitrary for PostgresKeyDesc {
286-
type Strategy = BoxedStrategy<Self>;
287-
type Parameters = ();
288-
289-
fn arbitrary_with(_: Self::Parameters) -> Self::Strategy {
290-
(
291-
any::<u32>(),
292-
any::<String>(),
293-
any::<Vec<u16>>(),
294-
any::<bool>(),
295-
any::<bool>(),
296-
)
297-
.prop_map(
298-
|(oid, name, cols, is_primary, nulls_not_distinct)| PostgresKeyDesc {
299-
oid,
300-
name,
301-
cols,
302-
is_primary,
303-
nulls_not_distinct,
304-
},
305-
)
306-
.boxed()
307-
}
308-
}

src/storage-client/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ mz-service = { path = "../service" }
3030
mz-storage-types = { path = "../storage-types" }
3131
mz-timely-util = { path = "../timely-util" }
3232
proptest = { version = "1.0.0", default-features = false, features = ["std"] }
33+
proptest-derive = { version = "0.3.0", features = ["boxed_union"]}
3334
prometheus = { version = "0.13.3", default-features = false }
3435
prost = { version = "0.11.3", features = ["no-recursion-limit"] }
3536
rdkafka = { version = "0.29.0", features = [

0 commit comments

Comments
 (0)