Skip to content

Commit 5fb3fb9

Browse files
committed
Add another tpc_c transaction implementation
1 parent 4a39e6f commit 5fb3fb9

File tree

4 files changed

+113
-38
lines changed

4 files changed

+113
-38
lines changed

tests/tpc_c/main.rs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rust_query::{
66
};
77

88
mod new_order;
9+
mod order_status;
910
mod payment;
1011

1112
#[schema(Schema)]
@@ -71,6 +72,7 @@ pub mod vN {
7172
}
7273
pub struct Order {
7374
pub customer: Customer,
75+
pub number: i64,
7476
pub entry_d: i64,
7577
pub carrier_id: Option<i64>,
7678
pub order_line_cnt: i64,
@@ -81,7 +83,7 @@ pub mod vN {
8183
pub order: Order,
8284
pub number: i64,
8385
pub stock: Stock,
84-
pub derlivery_d: Option<i64>,
86+
pub delivery_d: Option<i64>,
8587
pub quantity: i64,
8688
pub amount: i64, // total cost of this line
8789
pub dist_info: String,
@@ -142,3 +144,50 @@ pub fn random_to_last_name(num: i64) -> String {
142144
}
143145
out
144146
}
147+
148+
enum CustomerIdent<'a> {
149+
Number(TableRow<'a, Customer>),
150+
Name(TableRow<'a, District>, String),
151+
}
152+
153+
impl<'a> CustomerIdent<'a> {
154+
pub fn lookup_customer(self, txn: &Transaction<'a, Schema>) -> TableRow<'a, Customer> {
155+
match self {
156+
CustomerIdent::Number(customer) => customer,
157+
CustomerIdent::Name(district, last_name) => {
158+
let mut customers = txn.query(|rows| {
159+
let customer = Customer::join(rows);
160+
rows.filter(customer.district().eq(district));
161+
rows.filter(customer.last().eq(last_name));
162+
rows.into_vec((customer.first(), customer))
163+
});
164+
customers.sort_by(|a, b| a.0.cmp(&b.0));
165+
166+
let count = customers.len();
167+
let id = count.div_ceil(2) - 1;
168+
customers.swap_remove(id).1
169+
}
170+
}
171+
}
172+
}
173+
174+
fn customer_ident<'a>(
175+
txn: &Transaction<'a, Schema>,
176+
rng: &mut ThreadRng,
177+
customer_district: TableRow<'a, District>,
178+
) -> CustomerIdent<'a> {
179+
if rng.random_ratio(60, 100) {
180+
CustomerIdent::Name(
181+
customer_district,
182+
random_to_last_name(rng.nurand(255, 0, 999)),
183+
)
184+
} else {
185+
let customer = txn
186+
.query_one(Customer::unique(
187+
customer_district,
188+
rng.nurand(1023, 1, 3000),
189+
))
190+
.unwrap();
191+
CustomerIdent::Number(customer)
192+
}
193+
}

tests/tpc_c/new_order.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub fn new_order<'a>(
5757
) -> OutputData<'a> {
5858
let district = txn.query_one(input.customer.district());
5959

60-
let district_info: District!(warehouse<'_>, number, tax) =
60+
let district_info: District!(warehouse<'_>, number, tax, next_order) =
6161
txn.query_one(FromExpr::from_expr(district));
6262

6363
let warehouse_tax = txn.query_one(district.warehouse().tax());
@@ -79,6 +79,7 @@ pub fn new_order<'a>(
7979
.all(|item| item.supplying_warehouse == district_info.warehouse);
8080

8181
let order = txn.insert_ok(Order {
82+
number: district_info.next_order,
8283
customer: input.customer,
8384
entry_d: input.entry_date,
8485
carrier_id: None::<i64>,
@@ -167,7 +168,7 @@ pub fn new_order<'a>(
167168
order,
168169
number: number as i64,
169170
stock,
170-
derlivery_d: None::<i64>,
171+
delivery_d: None::<i64>,
171172
quantity,
172173
amount,
173174
dist_info: stock_info.dist_xx,

tests/tpc_c/order_status.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use super::*;
2+
use rust_query::{FromExpr, TableRow, Transaction, aggregate};
3+
4+
pub fn generate_order_status<'a>(
5+
txn: &Transaction<'a, Schema>,
6+
warehouse: TableRow<'a, Warehouse>,
7+
) -> CustomerIdent<'a> {
8+
let mut rng = rand::rng();
9+
let district = txn
10+
.query_one(District::unique(warehouse, rng.random_range(1..=10)))
11+
.unwrap();
12+
13+
customer_ident(txn, &mut rng, district)
14+
}
15+
16+
pub fn order_status<'a>(
17+
txn: &Transaction<'a, Schema>,
18+
input: CustomerIdent<'a>,
19+
) -> OrderStatus<'a> {
20+
let customer = input.lookup_customer(txn);
21+
let last_order = txn.query(|rows| {
22+
let order = Order::join(rows);
23+
rows.filter(order.customer().eq(customer));
24+
let max_number = rows.filter_some(aggregate(|rows| {
25+
let order = Order::join(rows);
26+
rows.filter_on(order.customer(), customer);
27+
rows.max(order.number())
28+
}));
29+
rows.filter(order.number().eq(max_number));
30+
rows.into_vec(order).swap_remove(0)
31+
});
32+
33+
let order_lines_info = txn.query(|rows| {
34+
let order_line = OrderLine::join(rows);
35+
rows.filter(order_line.order().eq(last_order));
36+
rows.into_vec(FromExpr::from_expr(order_line))
37+
});
38+
39+
OrderStatus {
40+
customer_info: txn.query_one(FromExpr::from_expr(customer)),
41+
order_info: txn.query_one(FromExpr::from_expr(last_order)),
42+
order_lines_info,
43+
}
44+
}
45+
46+
struct OrderStatus<'a> {
47+
customer_info: Customer!(balance, first, middle, last),
48+
order_info: Order!(number, entry_d, carrier_id),
49+
order_lines_info: Vec<OrderLineInfo<'a>>,
50+
}
51+
52+
type OrderLineInfo<'a> = OrderLine!(
53+
stock as Stock!(item<'a>, warehouse<'a>),
54+
quantity,
55+
amount,
56+
delivery_d
57+
);

tests/tpc_c/payment.rs

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,7 @@ pub fn generate_payment<'a>(
1919
.unwrap()
2020
};
2121

22-
let customer = if rng.random_ratio(60, 100) {
23-
CustomerIdent::Name(
24-
customer_district,
25-
random_to_last_name(rng.nurand(255, 0, 999)),
26-
)
27-
} else {
28-
let customer = txn
29-
.query_one(Customer::unique(
30-
customer_district,
31-
rng.nurand(1023, 1, 3000),
32-
))
33-
.unwrap();
34-
CustomerIdent::Number(customer)
35-
};
22+
let customer = customer_ident(txn, &mut rng, customer_district);
3623

3724
PaymentInput {
3825
district,
@@ -42,11 +29,6 @@ pub fn generate_payment<'a>(
4229
}
4330
}
4431

45-
enum CustomerIdent<'a> {
46-
Number(TableRow<'a, Customer>),
47-
Name(TableRow<'a, District>, String),
48-
}
49-
5032
struct PaymentInput<'a> {
5133
district: TableRow<'a, District>,
5234
customer: CustomerIdent<'a>,
@@ -108,22 +90,8 @@ fn payment<'a>(mut txn: TransactionMut<'a, Schema>, input: PaymentInput<'a>) ->
10890
},
10991
);
11092

111-
let (customer, customer_info) = match input.customer {
112-
CustomerIdent::Number(row) => (row, txn.query_one(CustomerInfo::from_expr(row))),
113-
CustomerIdent::Name(customer_district, name) => {
114-
let mut customers = txn.query(|rows| {
115-
let customer = Customer::join(rows);
116-
rows.filter(customer.district().eq(customer_district));
117-
rows.filter(customer.last().eq(name));
118-
rows.into_vec((&customer, CustomerInfo::from_expr(&customer)))
119-
});
120-
customers.sort_by(|a, b| a.1.first.cmp(&b.1.first));
121-
122-
let count = customers.len();
123-
let id = count.div_ceil(2) - 1;
124-
customers.swap_remove(id)
125-
}
126-
};
93+
let customer = input.customer.lookup_customer(&txn);
94+
let customer_info: CustomerInfo = txn.query_one(FromExpr::from_expr(customer));
12795

12896
txn.update_ok(
12997
customer,

0 commit comments

Comments
 (0)