1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright 2025 Stoolap Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! CTE Filtering Tests
//!
//! Tests CTE operations with various WHERE clause filters and aggregates
use stoolap::Database;
fn setup_products_db(test_name: &str) -> Database {
let db = Database::open(&format!("memory://cte_filter_{}", test_name))
.expect("Failed to create database");
// Create test table
db.execute(
"CREATE TABLE products (
id INTEGER PRIMARY KEY,
name TEXT,
price FLOAT,
category TEXT,
in_stock BOOLEAN
)",
(),
)
.expect("Failed to create table");
// Insert 1000 test rows
for i in 1..=1000 {
let category = match i % 5 {
1 => "CategoryA",
2 => "CategoryB",
3 => "CategoryC",
4 => "CategoryD",
_ => "CategoryE",
};
let in_stock = i % 2 == 0;
let price = (i as f64) * 10.5;
db.execute(&format!(
"INSERT INTO products (id, name, price, category, in_stock) VALUES ({}, 'Product{}', {:.1}, '{}', {})",
i, i, price, category, in_stock
), ())
.expect("Failed to insert data");
}
db
}
/// Test CTE with range filtering (price > 5000)
#[test]
fn test_cte_with_range_filtering() {
let db = setup_products_db("filtering");
let count: i64 = db
.query_one(
"WITH expensive_products AS (
SELECT id, name, price, category
FROM products
WHERE price > 5000
)
SELECT COUNT(*) FROM expensive_products",
(),
)
.expect("Failed to execute query");
// Products with price > 5000 are those with id > 476 (since price = id * 10.5)
// 5000 / 10.5 = 476.19, so id > 476 means ids 477-1000 = 524 products
assert_eq!(count, 524, "Expected 524 products with price > 5000");
}
/// Test CTE with equality filter
#[test]
fn test_cte_with_equality_filter() {
let db = setup_products_db("equality");
let count: i64 = db
.query_one(
"WITH category_products AS (
SELECT id, name, price, category
FROM products
WHERE category = 'CategoryA'
)
SELECT COUNT(*) FROM category_products",
(),
)
.expect("Failed to execute query");
// CategoryA is for ids where id % 5 == 1 (1, 6, 11, 16, ..., 996)
// That's 200 products
assert_eq!(count, 200, "Expected 200 products in CategoryA");
}
/// Test CTE with MIN/MAX aggregates
#[test]
fn test_cte_min_max() {
let db = setup_products_db("min_max");
let result = db
.query(
"WITH all_products AS (
SELECT id, name, price
FROM products
)
SELECT MIN(price), MAX(price) FROM all_products",
(),
)
.expect("Failed to execute query");
for row in result {
let row = row.expect("Failed to get row");
let min_price: f64 = row.get(0).unwrap();
let max_price: f64 = row.get(1).unwrap();
// id=1, price=1*10.5=10.5
assert!(
(min_price - 10.5).abs() < 0.01,
"Expected min price 10.5, got {}",
min_price
);
// id=1000, price=1000*10.5=10500.0
assert!(
(max_price - 10500.0).abs() < 0.01,
"Expected max price 10500.0, got {}",
max_price
);
}
}
/// Test CTE reused multiple times
#[test]
fn test_cte_reused_multiple_times() {
let db = setup_products_db("reused");
// Test simpler query - just get the count of cheap products
let total: i64 = db
.query_one(
"WITH cheap_products AS (
SELECT id, name, price
FROM products
WHERE price < 1000
)
SELECT COUNT(*) FROM cheap_products",
(),
)
.expect("Failed to execute query");
// price < 1000 means id < 96 (95 products: 1-95, since 95*10.5=997.5 < 1000)
assert_eq!(total, 95, "Expected 95 total cheap products");
}
/// Test CTE with very cheap products count
#[test]
fn test_cte_very_cheap_products() {
let db = setup_products_db("very_cheap");
let very_cheap: i64 = db
.query_one(
"WITH cheap_products AS (
SELECT id, name, price
FROM products
WHERE price < 500
)
SELECT COUNT(*) FROM cheap_products",
(),
)
.expect("Failed to execute query");
// price < 500 means id < 48 (47 products: 1-47, since 47*10.5=493.5 < 500)
assert_eq!(very_cheap, 47, "Expected 47 very cheap products");
}
/// Test CTE with boolean filter
#[test]
fn test_cte_boolean_filter() {
let db = setup_products_db("boolean");
let count: i64 = db
.query_one(
"WITH in_stock_products AS (
SELECT id, name, price
FROM products
WHERE in_stock = true
)
SELECT COUNT(*) FROM in_stock_products",
(),
)
.expect("Failed to execute query");
// in_stock is true for even ids (2, 4, 6, ..., 1000) = 500 products
assert_eq!(count, 500, "Expected 500 in-stock products");
}
/// Test CTE with combined filters
#[test]
fn test_cte_combined_filters() {
let db = setup_products_db("combined");
let count: i64 = db
.query_one(
"WITH filtered_products AS (
SELECT id, name, price, category
FROM products
WHERE price > 500 AND category = 'CategoryA'
)
SELECT COUNT(*) FROM filtered_products",
(),
)
.expect("Failed to execute query");
// price > 500 means id > 47
// CategoryA means id % 5 == 1
// So ids: 51, 56, 61, ..., 996 where id % 5 == 1 and id > 47
// Starting from 51, stepping by 5: 51, 56, 61, ... 996
// Count: (996 - 51) / 5 + 1 = 190
assert_eq!(
count, 190,
"Expected 190 products matching combined filters"
);
}
/// Test CTE with SUM aggregate
#[test]
fn test_cte_sum_aggregate() {
let db = setup_products_db("sum");
let sum: f64 = db
.query_one(
"WITH all_products AS (
SELECT price FROM products WHERE id <= 10
)
SELECT SUM(price) FROM all_products",
(),
)
.expect("Failed to execute query");
// Sum of prices for ids 1-10: (1+2+3+4+5+6+7+8+9+10) * 10.5 = 55 * 10.5 = 577.5
assert!(
(sum - 577.5).abs() < 0.01,
"Expected sum 577.5, got {}",
sum
);
}
/// Test CTE with AVG aggregate
#[test]
fn test_cte_avg_aggregate() {
let db = setup_products_db("avg");
let avg: f64 = db
.query_one(
"WITH subset AS (
SELECT price FROM products WHERE id <= 10
)
SELECT AVG(price) FROM subset",
(),
)
.expect("Failed to execute query");
// Average of prices for ids 1-10: 577.5 / 10 = 57.75
assert!(
(avg - 57.75).abs() < 0.01,
"Expected avg 57.75, got {}",
avg
);
}
/// Test nested CTE
#[test]
fn test_nested_cte() {
let db = setup_products_db("nested");
let count: i64 = db
.query_one(
"WITH cheap AS (
SELECT id, price FROM products WHERE price < 1000
),
very_cheap AS (
SELECT id, price FROM cheap WHERE price < 500
)
SELECT COUNT(*) FROM very_cheap",
(),
)
.expect("Failed to execute query");
// price < 500 means id < 48 (47 products)
assert_eq!(count, 47, "Expected 47 very cheap products from nested CTE");
}