linkrs-query 0.1.0

Query engine, parser, and executor for Linkrs
Documentation
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
//! Cost-Based Optimization Strategy Tests
//!
//! Test coverage:
//! - Join order optimization
//! - Index selection strategies
//! - Traversal direction optimization
//! - Aggregate strategy selection
//! - Plan enumeration

use crate::common::test_scenario::TestScenario;

// ==================== Join Order Tests ====================

mod join_order {
    use super::*;

    #[test]
    fn test_simple_join_order() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_simple_join")
            .exec_ddl("CREATE TAG person(id INT, name STRING)")
            .exec_ddl("CREATE TAG company(id INT, name STRING)")
            .exec_ddl("CREATE EDGE works_at()")
            .assert_success()
            .query("MATCH (p:person)-[:works_at]->(c:company) RETURN p, c")
            .assert_success();
    }

    #[test]
    fn test_multi_table_join_order() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_multi_join")
            .exec_ddl("CREATE TAG person(id INT, name STRING)")
            .exec_ddl("CREATE TAG company(id INT, name STRING)")
            .exec_ddl("CREATE TAG department(id INT, name STRING)")
            .exec_ddl("CREATE EDGE works_at()")
            .exec_ddl("CREATE EDGE belongs_to()")
            .assert_success()
            .query("MATCH (p:person)-[:works_at]->(c:company)-[:belongs_to]->(d:department) RETURN p, d")
            .assert_success();
    }

    #[test]
    fn test_join_with_filter() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_join_filter")
            .exec_ddl("CREATE TAG person(id INT, name STRING, age INT)")
            .exec_ddl("CREATE TAG company(id INT, name STRING)")
            .exec_ddl("CREATE EDGE works_at()")
            .assert_success()
            .query("MATCH (p:person)-[:works_at]->(c:company) WHERE p.age > 30 RETURN p, c")
            .assert_success();
    }
}

// ==================== Index Selection Tests ====================

mod index_selection {
    use super::*;

    #[test]
    fn test_single_index_selection() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_single_index")
            .exec_ddl("CREATE TAG person(id INT, name STRING, age INT)")
            .exec_ddl("CREATE TAG INDEX idx_person_age ON person(age)")
            .assert_success()
            .query("MATCH (n:person) WHERE n.age = 30 RETURN n")
            .assert_success();
    }

    #[test]
    fn test_composite_index_selection() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_composite_index")
            .exec_ddl("CREATE TAG person(id INT, name STRING, age INT, city STRING)")
            .exec_ddl("CREATE TAG INDEX idx_person_age_city ON person(age, city)")
            .assert_success()
            .query("MATCH (n:person) WHERE n.age = 30 AND n.city = 'Beijing' RETURN n")
            .assert_success();
    }

    #[test]
    fn test_index_with_range_query() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_index_range")
            .exec_ddl("CREATE TAG person(id INT, name STRING, age INT)")
            .exec_ddl("CREATE TAG INDEX idx_person_age ON person(age)")
            .assert_success()
            .query("MATCH (n:person) WHERE n.age > 25 AND n.age < 50 RETURN n")
            .assert_success();
    }

    #[test]
    fn test_multiple_index_candidates() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_multi_index")
            .exec_ddl("CREATE TAG person(id INT, name STRING, age INT, city STRING)")
            .exec_ddl("CREATE TAG INDEX idx_person_age ON person(age)")
            .exec_ddl("CREATE TAG INDEX idx_person_city ON person(city)")
            .assert_success()
            .query("MATCH (n:person) WHERE n.age = 30 AND n.city = 'Beijing' RETURN n")
            .assert_success();
    }
}

// ==================== Traversal Direction Tests ====================

mod traversal_direction {
    use super::*;

    #[test]
    fn test_forward_traversal() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_forward")
            .exec_ddl("CREATE TAG person(id INT, name STRING)")
            .exec_ddl("CREATE EDGE follows()")
            .assert_success()
            .query("MATCH (a:person)-[:follows]->(b:person) RETURN a, b")
            .assert_success();
    }

    #[test]
    fn test_backward_traversal_rejected() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_backward")
            .exec_ddl("CREATE TAG person(id INT, name STRING)")
            .exec_ddl("CREATE EDGE follows()")
            .assert_success()
            .query("GO FROM 1 OVER follows REVERSELY YIELD $$.person.name AS name")
            .assert_error();
    }

    #[test]
    fn test_bidirectional_traversal() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_bidirectional")
            .exec_ddl("CREATE TAG person(id INT, name STRING)")
            .exec_ddl("CREATE EDGE follows()")
            .assert_success()
            .query("MATCH (a:person)-[:follows]-(b:person) RETURN a, b")
            .assert_success();
    }

    #[test]
    fn test_multi_hop_traversal() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_multi_hop")
            .exec_ddl("CREATE TAG person(id INT, name STRING)")
            .exec_ddl("CREATE EDGE follows()")
            .assert_success()
            .query("MATCH (a:person)-[:follows]->(b:person)-[:follows]->(c:person) RETURN a, c")
            .assert_success();
    }
}

// ==================== Aggregate Strategy Tests ====================

mod aggregate_strategy {
    use super::*;

    #[test]
    fn test_simple_aggregate() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_simple_agg")
            .exec_ddl("CREATE TAG person(id INT, age INT)")
            .assert_success()
            .query("MATCH (n:person) RETURN count(n)")
            .assert_success();
    }

    #[test]
    fn test_grouped_aggregate() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_grouped_agg")
            .exec_ddl("CREATE TAG person(id INT, city STRING, age INT)")
            .assert_success()
            .query("MATCH (n:person) RETURN n.city, count(n), avg(n.age)")
            .assert_success();
    }

    #[test]
    fn test_aggregate_with_filter() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_agg_filter")
            .exec_ddl("CREATE TAG person(id INT, age INT)")
            .assert_success()
            .query("MATCH (n:person) WHERE n.age > 18 RETURN count(n)")
            .assert_success();
    }

    #[test]
    fn test_multiple_aggregates() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_multi_agg")
            .exec_ddl("CREATE TAG person(id INT, age INT, salary INT)")
            .assert_success()
            .query("MATCH (n:person) RETURN count(n), avg(n.age), sum(n.salary), max(n.age), min(n.age)")
            .assert_success();
    }
}

// ==================== Plan Enumeration Tests ====================

mod plan_enumeration {
    use super::*;

    #[test]
    fn test_simple_plan_enumeration() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_simple_enum")
            .exec_ddl("CREATE TAG person(id INT, name STRING)")
            .assert_success()
            .query("MATCH (n:person) RETURN n")
            .assert_success();
    }

    #[test]
    fn test_join_plan_enumeration() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_join_enum")
            .exec_ddl("CREATE TAG person(id INT, name STRING)")
            .exec_ddl("CREATE TAG company(id INT, name STRING)")
            .exec_ddl("CREATE EDGE works_at()")
            .assert_success()
            .query("MATCH (p:person)-[:works_at]->(c:company) RETURN p, c")
            .assert_success();
    }

    #[test]
    fn test_complex_query_enumeration() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_complex_enum")
            .exec_ddl("CREATE TAG person(id INT, name STRING, age INT)")
            .exec_ddl("CREATE TAG company(id INT, name STRING)")
            .exec_ddl("CREATE EDGE works_at()")
            .assert_success()
            .query("MATCH (p:person)-[:works_at]->(c:company) WHERE p.age > 25 RETURN p.name, c.name ORDER BY p.age LIMIT 10")
            .assert_success();
    }
}

// ==================== Subquery Unnesting Tests ====================

mod subquery_unnesting {
    use super::*;

    #[test]
    fn test_simple_subquery_unnesting() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_subquery_simple")
            .exec_ddl("CREATE TAG person(id INT, name STRING, age INT)")
            .assert_success()
            .query("MATCH (n:person) WHERE n.age IN (SELECT age FROM person WHERE name = 'Alice') RETURN n")
            .assert_success();
    }

    /// Scalar subqueries (`expr > (MATCH ... RETURN agg(...))`) are not yet
    /// implemented end-to-end: the parser lacks the `(MATCH ...)` primary
    /// form and the binder rejects EXISTS/IN subquery expressions
    /// ("subquery binding not yet implemented"). Ignored until that feature
    /// lands; the unnesting optimizer itself is exercised by programmatically
    /// constructed apply plans in `cost_based/subquery_unnesting.rs`.
    #[test]
    #[ignore = "scalar MATCH subqueries not implemented (parser/binder)"]
    fn test_subquery_with_aggregation() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_subquery_agg")
            .exec_ddl("CREATE TAG employee(id INT, salary INT, department STRING)")
            .assert_success()
            .query("MATCH (e:employee) WHERE e.salary > (MATCH (m:employee) RETURN avg(m.salary)) RETURN e")
            .assert_success();
    }
}

// ==================== Materialization Decision Tests ====================

mod materialization_decisions {
    use super::*;

    #[test]
    fn test_materialization_for_nested_loop() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_materialize_loop")
            .exec_ddl("CREATE TAG person(id INT, name STRING)")
            .exec_ddl("CREATE TAG company(id INT, name STRING)")
            .exec_ddl("CREATE EDGE works_at()")
            .assert_success()
            .query("MATCH (p:person)-[:works_at]->(c:company), (p2:person)-[:works_at]->(c2:company) RETURN p, c, c2")
            .assert_success();
    }

    #[test]
    fn test_materialization_memory_constraint() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_materialize_memory")
            .exec_ddl("CREATE TAG person(id INT, name STRING, data STRING)")
            .exec_ddl("CREATE TAG company(id INT, name STRING)")
            .exec_ddl("CREATE EDGE works_at()")
            .assert_success()
            .query("MATCH (p:person)-[:works_at]->(c:company) RETURN p, c LIMIT 1000")
            .assert_success();
    }
}

// ==================== Expression Precomputation Tests ====================

mod expression_precomputation {
    use super::*;

    #[test]
    fn test_deterministic_expression_precomputation() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_precompute_det")
            .exec_ddl("CREATE TAG product(id INT, name STRING, price INT)")
            .assert_success()
            .query("MATCH (p:product) RETURN p.name, p.price * 100 + 50 AS computed_value")
            .assert_success();
    }

    #[test]
    fn test_complex_expression_precomputation() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_precompute_complex")
            .exec_ddl("CREATE TAG employee(id INT, salary INT, bonus_rate DOUBLE, years INT)")
            .assert_success()
            .query("MATCH (e:employee) RETURN e.salary, (e.salary * e.bonus_rate) + (e.years * 1000) AS total_comp")
            .assert_success();
    }
}

// ==================== TopN Optimization Tests ====================

mod topn_optimization {
    use super::*;

    #[test]
    fn test_topn_with_index() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_topn_index")
            .exec_ddl("CREATE TAG employee(id INT, name STRING, salary INT)")
            .exec_ddl("CREATE TAG INDEX idx_salary ON employee(salary)")
            .assert_success()
            .query("MATCH (e:employee) RETURN e.name ORDER BY e.salary DESC LIMIT 5")
            .assert_success();
    }

    #[test]
    fn test_topn_without_index() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_topn_no_index")
            .exec_ddl("CREATE TAG employee(id INT, name STRING, salary INT)")
            .assert_success()
            .query("MATCH (e:employee) RETURN e.name ORDER BY e.salary DESC LIMIT 10")
            .assert_success();
    }

    #[test]
    fn test_topn_with_multiple_columns() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_topn_multi")
            .exec_ddl("CREATE TAG employee(id INT, department STRING, salary INT, hire_date STRING)")
            .assert_success()
            .query("MATCH (e:employee) RETURN e.department, e.salary ORDER BY e.department, e.salary DESC LIMIT 20")
            .assert_success();
    }
}

// ==================== Bidirectional Traversal Tests ====================

mod bidirectional_traversal {
    use super::*;

    #[test]
    fn test_bidirectional_simple() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_bidi_simple")
            .exec_ddl("CREATE TAG person(id INT, name STRING)")
            .exec_ddl("CREATE EDGE connected()")
            .assert_success()
            .query("MATCH (a:person)-[:connected]-(b:person) RETURN a, b")
            .assert_success();
    }

    #[test]
    fn test_bidirectional_with_selective_filter() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_bidi_filter")
            .exec_ddl("CREATE TAG person(id INT, age INT)")
            .exec_ddl("CREATE EDGE connected()")
            .assert_success()
            .query("MATCH (a:person)-[:connected]-(b:person) WHERE a.age > 30 RETURN a, b")
            .assert_success();
    }
}

// ==================== Aggregate Strategy Selection ====================

mod aggregate_strategy_selection {
    use super::*;

    #[test]
    fn test_hash_aggregate() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_hash_agg")
            .exec_ddl("CREATE TAG transaction(id INT, category STRING, amount INT)")
            .assert_success()
            .query("MATCH (t:transaction) RETURN t.category, sum(t.amount) GROUP BY t.category")
            .assert_success();
    }

    #[test]
    fn test_sort_aggregate() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_sort_agg")
            .exec_ddl("CREATE TAG sales(id INT, region STRING, amount INT)")
            .assert_success()
            .query("MATCH (s:sales) RETURN s.region, max(s.amount) GROUP BY s.region ORDER BY s.region")
            .assert_success();
    }

    #[test]
    fn test_multiple_aggregates() {
        TestScenario::new()
            .expect("Failed to create test scenario")
            .setup_space("test_multi_agg")
            .exec_ddl("CREATE TAG payment(id INT, category STRING, amount INT)")
            .assert_success()
            .query("MATCH (p:payment) RETURN p.category, count(p), sum(p.amount), avg(p.amount), min(p.amount), max(p.amount) GROUP BY p.category")
            .assert_success();
    }
}