1use crate::ast::{Assignment, Expr, Literal};
29use crate::plan::PlanNode;
30use rustc_hash::FxHashMap;
31
32pub struct PlanCache {
38 cache: FxHashMap<u64, PlanNode>,
39 capacity: usize,
40 pub hits: u64,
41 pub misses: u64,
42}
43
44impl PlanCache {
45 pub fn new(capacity: usize) -> Self {
46 PlanCache {
47 cache: FxHashMap::default(),
48 capacity,
49 hits: 0,
50 misses: 0,
51 }
52 }
53
54 pub fn insert(&mut self, hash: u64, plan: PlanNode) {
57 if self.cache.len() >= self.capacity && !self.cache.contains_key(&hash) {
58 self.cache.clear();
63 }
64 self.cache.insert(hash, plan);
65 }
66
67 pub fn get_with_substitution(&mut self, hash: u64, literals: &[Literal]) -> Option<PlanNode> {
79 match self.cache.get(&hash) {
80 Some(template) => {
81 self.hits += 1;
82 let mut plan = template.clone();
83 let mut idx = 0usize;
84 substitute_plan(&mut plan, literals, &mut idx);
85 debug_assert_eq!(
86 idx,
87 literals.len(),
88 "plan substitution consumed {idx} literals but query had {}",
89 literals.len(),
90 );
91 Some(plan)
92 }
93 None => {
94 self.misses += 1;
95 None
96 }
97 }
98 }
99
100 pub fn len(&self) -> usize {
101 self.cache.len()
102 }
103
104 pub fn is_empty(&self) -> bool {
105 self.cache.is_empty()
106 }
107
108 pub fn clear(&mut self) {
109 self.cache.clear();
110 }
111}
112
113pub(crate) fn substitute_plan(plan: &mut PlanNode, literals: &[Literal], idx: &mut usize) {
132 match plan {
133 PlanNode::SeqScan { .. } => {}
134 PlanNode::AliasScan { .. } => {}
135 PlanNode::IndexScan { key, .. } => {
136 substitute_expr(key, literals, idx);
137 }
138 PlanNode::RangeScan { start, end, .. } => {
139 if let Some((expr, _)) = start {
140 substitute_expr(expr, literals, idx);
141 }
142 if let Some((expr, _)) = end {
143 substitute_expr(expr, literals, idx);
144 }
145 }
146 PlanNode::Filter { input, predicate } => {
147 substitute_plan(input, literals, idx);
148 substitute_expr(predicate, literals, idx);
149 }
150 PlanNode::Project { input, fields } => {
151 substitute_plan(input, literals, idx);
152 for f in fields {
153 substitute_expr(&mut f.expr, literals, idx);
154 }
155 }
156 PlanNode::Sort { input, .. } => substitute_plan(input, literals, idx),
157 PlanNode::AlterTable { .. } => {}
158 PlanNode::DropTable { .. } => {}
159 PlanNode::Limit { input, count } => {
160 if let PlanNode::Offset {
169 input: inner,
170 count: off_count,
171 } = input.as_mut()
172 {
173 substitute_plan(inner, literals, idx);
174 substitute_expr(count, literals, idx);
175 substitute_expr(off_count, literals, idx);
176 } else {
177 substitute_plan(input, literals, idx);
178 substitute_expr(count, literals, idx);
179 }
180 }
181 PlanNode::Offset { input, count } => {
182 substitute_plan(input, literals, idx);
185 substitute_expr(count, literals, idx);
186 }
187 PlanNode::Aggregate { input, .. } => {
188 substitute_plan(input, literals, idx);
189 }
190 PlanNode::NestedLoopJoin {
191 left, right, on, ..
192 } => {
193 substitute_plan(left, literals, idx);
198 substitute_plan(right, literals, idx);
199 if let Some(pred) = on {
200 substitute_expr(pred, literals, idx);
201 }
202 }
203 PlanNode::Distinct { input } => {
204 substitute_plan(input, literals, idx);
205 }
206 PlanNode::GroupBy { input, having, .. } => {
207 substitute_plan(input, literals, idx);
208 if let Some(pred) = having {
209 substitute_expr(pred, literals, idx);
210 }
211 }
212 PlanNode::Insert { rows, .. } => {
213 for assignments in rows {
214 substitute_assignments(assignments, literals, idx);
215 }
216 }
217 PlanNode::Upsert {
218 assignments,
219 on_conflict,
220 ..
221 } => {
222 substitute_assignments(assignments, literals, idx);
223 substitute_assignments(on_conflict, literals, idx);
224 }
225 PlanNode::Update {
226 input, assignments, ..
227 } => {
228 substitute_plan(input, literals, idx);
229 substitute_assignments(assignments, literals, idx);
230 }
231 PlanNode::Delete { input, .. } => {
232 substitute_plan(input, literals, idx);
233 }
234 PlanNode::CreateTable { .. } => {}
235 PlanNode::CreateView { .. } => {}
236 PlanNode::RefreshView { .. } => {}
237 PlanNode::DropView { .. } => {}
238 PlanNode::Window { input, windows } => {
239 substitute_plan(input, literals, idx);
240 for w in windows {
241 for arg in &mut w.args {
242 substitute_expr(arg, literals, idx);
243 }
244 }
245 }
246 PlanNode::Union { left, right, .. } => {
247 substitute_plan(left, literals, idx);
248 substitute_plan(right, literals, idx);
249 }
250 PlanNode::Explain { input } => {
251 substitute_plan(input, literals, idx);
252 }
253 PlanNode::Begin | PlanNode::Commit | PlanNode::Rollback => {}
254 }
255}
256
257fn substitute_assignments(assignments: &mut [Assignment], literals: &[Literal], idx: &mut usize) {
258 for a in assignments {
259 substitute_expr(&mut a.value, literals, idx);
260 }
261}
262
263pub(crate) fn count_literal_slots(plan: &PlanNode) -> usize {
269 let mut n = 0usize;
270 count_plan(plan, &mut n);
271 n
272}
273
274fn count_plan(plan: &PlanNode, n: &mut usize) {
275 match plan {
276 PlanNode::SeqScan { .. } => {}
277 PlanNode::AliasScan { .. } => {}
278 PlanNode::IndexScan { key, .. } => count_expr(key, n),
279 PlanNode::RangeScan { start, end, .. } => {
280 if let Some((expr, _)) = start {
281 count_expr(expr, n);
282 }
283 if let Some((expr, _)) = end {
284 count_expr(expr, n);
285 }
286 }
287 PlanNode::Filter { input, predicate } => {
288 count_plan(input, n);
289 count_expr(predicate, n);
290 }
291 PlanNode::Project { input, fields } => {
292 count_plan(input, n);
293 for f in fields {
294 count_expr(&f.expr, n);
295 }
296 }
297 PlanNode::Sort { input, .. } => count_plan(input, n),
298 PlanNode::Limit { input, count } => {
299 if let PlanNode::Offset {
304 input: inner,
305 count: off_count,
306 } = input.as_ref()
307 {
308 count_plan(inner, n);
309 count_expr(count, n);
310 count_expr(off_count, n);
311 } else {
312 count_plan(input, n);
313 count_expr(count, n);
314 }
315 }
316 PlanNode::Offset { input, count } => {
317 count_plan(input, n);
318 count_expr(count, n);
319 }
320 PlanNode::Aggregate { input, .. } => count_plan(input, n),
321 PlanNode::NestedLoopJoin {
322 left, right, on, ..
323 } => {
324 count_plan(left, n);
325 count_plan(right, n);
326 if let Some(pred) = on {
327 count_expr(pred, n);
328 }
329 }
330 PlanNode::Distinct { input } => count_plan(input, n),
331 PlanNode::GroupBy { input, having, .. } => {
332 count_plan(input, n);
333 if let Some(pred) = having {
334 count_expr(pred, n);
335 }
336 }
337 PlanNode::Insert { rows, .. } => {
338 for assignments in rows {
339 for a in assignments {
340 count_expr(&a.value, n);
341 }
342 }
343 }
344 PlanNode::Upsert {
345 assignments,
346 on_conflict,
347 ..
348 } => {
349 for a in assignments {
350 count_expr(&a.value, n);
351 }
352 for a in on_conflict {
353 count_expr(&a.value, n);
354 }
355 }
356 PlanNode::Update {
357 input, assignments, ..
358 } => {
359 count_plan(input, n);
360 for a in assignments {
361 count_expr(&a.value, n);
362 }
363 }
364 PlanNode::Delete { input, .. } => count_plan(input, n),
365 PlanNode::CreateTable { .. } => {}
366 PlanNode::AlterTable { .. } => {}
367 PlanNode::DropTable { .. } => {}
368 PlanNode::CreateView { .. } => {}
369 PlanNode::RefreshView { .. } => {}
370 PlanNode::DropView { .. } => {}
371 PlanNode::Window { input, windows } => {
372 count_plan(input, n);
373 for w in windows {
374 for arg in &w.args {
375 count_expr(arg, n);
376 }
377 }
378 }
379 PlanNode::Union { left, right, .. } => {
380 count_plan(left, n);
381 count_plan(right, n);
382 }
383 PlanNode::Explain { input } => {
384 count_plan(input, n);
385 }
386 PlanNode::Begin | PlanNode::Commit | PlanNode::Rollback => {}
387 }
388}
389
390fn count_expr(expr: &Expr, n: &mut usize) {
391 match expr {
392 Expr::Literal(_) => *n += 1,
393 Expr::Field(_) | Expr::QualifiedField { .. } | Expr::Param(_) => {}
394 Expr::BinaryOp(l, _, r) => {
395 count_expr(l, n);
396 count_expr(r, n);
397 }
398 Expr::UnaryOp(_, inner) => count_expr(inner, n),
399 Expr::FunctionCall(_, inner) => count_expr(inner, n),
400 Expr::Coalesce(l, r) => {
401 count_expr(l, n);
402 count_expr(r, n);
403 }
404 Expr::InList { expr, list, .. } => {
405 count_expr(expr, n);
406 for item in list {
407 count_expr(item, n);
408 }
409 }
410 Expr::ScalarFunc(_, args) => {
411 for a in args {
412 count_expr(a, n);
413 }
414 }
415 Expr::Cast(inner, _) => count_expr(inner, n),
416 Expr::Case { whens, else_expr } => {
417 for (cond, result) in whens {
418 count_expr(cond, n);
419 count_expr(result, n);
420 }
421 if let Some(e) = else_expr {
422 count_expr(e, n);
423 }
424 }
425 Expr::InSubquery { expr, .. } => {
426 count_expr(expr, n);
427 }
430 Expr::ExistsSubquery { .. } => {
431 }
434 Expr::Window { args, .. } => {
435 for a in args {
436 count_expr(a, n);
437 }
438 }
439 Expr::Null => {}
440 }
441}
442
443fn substitute_expr(expr: &mut Expr, literals: &[Literal], idx: &mut usize) {
444 match expr {
445 Expr::Literal(_) => {
446 *expr = Expr::Literal(literals[*idx].clone());
450 *idx += 1;
451 }
452 Expr::Field(_) | Expr::QualifiedField { .. } | Expr::Param(_) => {}
453 Expr::BinaryOp(l, _, r) => {
454 substitute_expr(l, literals, idx);
455 substitute_expr(r, literals, idx);
456 }
457 Expr::UnaryOp(_, inner) => {
458 substitute_expr(inner, literals, idx);
459 }
460 Expr::FunctionCall(_, inner) => {
461 substitute_expr(inner, literals, idx);
462 }
463 Expr::Coalesce(l, r) => {
464 substitute_expr(l, literals, idx);
465 substitute_expr(r, literals, idx);
466 }
467 Expr::InList { expr, list, .. } => {
468 substitute_expr(expr, literals, idx);
469 for item in list {
470 substitute_expr(item, literals, idx);
471 }
472 }
473 Expr::ScalarFunc(_, args) => {
474 for a in args {
475 substitute_expr(a, literals, idx);
476 }
477 }
478 Expr::Cast(inner, _) => substitute_expr(inner, literals, idx),
479 Expr::Case { whens, else_expr } => {
480 for (cond, result) in whens {
481 substitute_expr(cond, literals, idx);
482 substitute_expr(result, literals, idx);
483 }
484 if let Some(e) = else_expr {
485 substitute_expr(e, literals, idx);
486 }
487 }
488 Expr::InSubquery { expr, .. } => {
489 substitute_expr(expr, literals, idx);
490 }
491 Expr::ExistsSubquery { .. } => {
492 }
495 Expr::Window { args, .. } => {
496 for a in args {
497 substitute_expr(a, literals, idx);
498 }
499 }
500 Expr::Null => {}
501 }
502}
503
504#[cfg(test)]
505mod tests {
506 use super::*;
507 use crate::canonicalize::canonicalize;
508 use crate::planner;
509
510 #[test]
511 fn test_cache_hit_substitutes_literal() {
512 let mut cache = PlanCache::new(100);
513
514 let q1 = "User filter .id = 42";
516 let (h1, lits1) = canonicalize(q1).unwrap();
517 let p1 = planner::plan(q1).unwrap();
518 cache.insert(h1, p1);
519
520 let q2 = "User filter .id = 99";
523 let (h2, lits2) = canonicalize(q2).unwrap();
524 assert_eq!(h1, h2, "different literals must hash the same");
525
526 let plan = cache.get_with_substitution(h2, &lits2).expect("hit");
527
528 match plan {
530 PlanNode::IndexScan { key, .. } => {
531 assert_eq!(key, Expr::Literal(Literal::Int(99)));
532 }
533 other => panic!("expected IndexScan, got {other:?}"),
534 }
535
536 assert_eq!(lits1, vec![Literal::Int(42)]);
539 assert_eq!(cache.hits, 1);
540 assert_eq!(cache.misses, 0);
541 }
542
543 #[test]
544 fn test_cache_miss_returns_none_and_bumps_counter() {
545 let mut cache = PlanCache::new(100);
546 assert!(cache.get_with_substitution(99999, &[]).is_none());
547 assert_eq!(cache.misses, 1);
548 assert_eq!(cache.hits, 0);
549 }
550
551 #[test]
552 fn test_multi_literal_filter_substitution() {
553 let mut cache = PlanCache::new(100);
554 let q1 = r#"User filter .age > 30 and .status = "active" { .name }"#;
555 let (h1, _) = canonicalize(q1).unwrap();
556 cache.insert(h1, planner::plan(q1).unwrap());
557
558 let q2 = r#"User filter .age > 50 and .status = "pending" { .name }"#;
559 let (h2, lits2) = canonicalize(q2).unwrap();
560 let plan = cache.get_with_substitution(h2, &lits2).expect("hit");
561
562 let mut found = Vec::new();
564 collect_literals_for_test(&plan, &mut found);
565 assert_eq!(
566 found,
567 vec![Literal::Int(50), Literal::String("pending".into()),]
568 );
569 }
570
571 #[test]
572 fn test_update_by_pk_substitution() {
573 let mut cache = PlanCache::new(100);
574 let q1 = "User filter .id = 1 update { age := 100 }";
575 let (h1, _) = canonicalize(q1).unwrap();
576 cache.insert(h1, planner::plan(q1).unwrap());
577
578 let q2 = "User filter .id = 7 update { age := 200 }";
579 let (h2, lits2) = canonicalize(q2).unwrap();
580 let plan = cache.get_with_substitution(h2, &lits2).expect("hit");
581
582 let mut found = Vec::new();
583 collect_literals_for_test(&plan, &mut found);
584 assert_eq!(found, vec![Literal::Int(7), Literal::Int(200)]);
585 }
586
587 #[test]
588 fn test_insert_substitution() {
589 let mut cache = PlanCache::new(100);
590 let q1 = r#"insert User { id := 1, name := "Alice", age := 20 }"#;
591 let (h1, _) = canonicalize(q1).unwrap();
592 cache.insert(h1, planner::plan(q1).unwrap());
593
594 let q2 = r#"insert User { id := 2, name := "Bob", age := 30 }"#;
595 let (h2, lits2) = canonicalize(q2).unwrap();
596 let plan = cache.get_with_substitution(h2, &lits2).expect("hit");
597
598 let mut found = Vec::new();
599 collect_literals_for_test(&plan, &mut found);
600 assert_eq!(
601 found,
602 vec![
603 Literal::Int(2),
604 Literal::String("Bob".into()),
605 Literal::Int(30),
606 ]
607 );
608 }
609
610 #[test]
611 fn test_eviction_on_capacity() {
612 let mut cache = PlanCache::new(2);
613 let q1 = "User";
614 let q2 = "User filter .age > 1";
615 let _q3 = "User filter .age > 2";
616 let q3_distinct = "User filter .id = 5";
619
620 let (h1, _) = canonicalize(q1).unwrap();
621 let (h2, _) = canonicalize(q2).unwrap();
622 let (h3, _) = canonicalize(q3_distinct).unwrap();
623 cache.insert(h1, planner::plan(q1).unwrap());
624 cache.insert(h2, planner::plan(q2).unwrap());
625 cache.insert(h3, planner::plan(q3_distinct).unwrap());
627 assert!(cache.cache.contains_key(&h3));
628 assert_eq!(cache.cache.len(), 1);
629 }
630
631 fn collect_literals_for_test(plan: &PlanNode, out: &mut Vec<Literal>) {
635 match plan {
636 PlanNode::SeqScan { .. } => {}
637 PlanNode::AliasScan { .. } => {}
638 PlanNode::IndexScan { key, .. } => collect_expr_literals(key, out),
639 PlanNode::RangeScan { start, end, .. } => {
640 if let Some((expr, _)) = start {
641 collect_expr_literals(expr, out);
642 }
643 if let Some((expr, _)) = end {
644 collect_expr_literals(expr, out);
645 }
646 }
647 PlanNode::Filter { input, predicate } => {
648 collect_literals_for_test(input, out);
649 collect_expr_literals(predicate, out);
650 }
651 PlanNode::Project { input, fields } => {
652 collect_literals_for_test(input, out);
653 for f in fields {
654 collect_expr_literals(&f.expr, out);
655 }
656 }
657 PlanNode::Sort { input, .. } => collect_literals_for_test(input, out),
658 PlanNode::Limit { input, count } => {
659 collect_literals_for_test(input, out);
660 collect_expr_literals(count, out);
661 }
662 PlanNode::Offset { input, count } => {
663 collect_literals_for_test(input, out);
664 collect_expr_literals(count, out);
665 }
666 PlanNode::Aggregate { input, .. } => collect_literals_for_test(input, out),
667 PlanNode::NestedLoopJoin {
668 left, right, on, ..
669 } => {
670 collect_literals_for_test(left, out);
671 collect_literals_for_test(right, out);
672 if let Some(pred) = on {
673 collect_expr_literals(pred, out);
674 }
675 }
676 PlanNode::Insert { rows, .. } => {
677 for assignments in rows {
678 for a in assignments {
679 collect_expr_literals(&a.value, out);
680 }
681 }
682 }
683 PlanNode::Upsert {
684 assignments,
685 on_conflict,
686 ..
687 } => {
688 for a in assignments {
689 collect_expr_literals(&a.value, out);
690 }
691 for a in on_conflict {
692 collect_expr_literals(&a.value, out);
693 }
694 }
695 PlanNode::Update {
696 input, assignments, ..
697 } => {
698 collect_literals_for_test(input, out);
699 for a in assignments {
700 collect_expr_literals(&a.value, out);
701 }
702 }
703 PlanNode::Distinct { input } => collect_literals_for_test(input, out),
704 PlanNode::GroupBy { input, having, .. } => {
705 collect_literals_for_test(input, out);
706 if let Some(pred) = having {
707 collect_expr_literals(pred, out);
708 }
709 }
710 PlanNode::Delete { input, .. } => collect_literals_for_test(input, out),
711 PlanNode::CreateTable { .. } => {}
712 PlanNode::AlterTable { .. } => {}
713 PlanNode::DropTable { .. } => {}
714 PlanNode::CreateView { .. } => {}
715 PlanNode::RefreshView { .. } => {}
716 PlanNode::DropView { .. } => {}
717 PlanNode::Window { input, windows } => {
718 collect_literals_for_test(input, out);
719 for w in windows {
720 for arg in &w.args {
721 collect_expr_literals(arg, out);
722 }
723 }
724 }
725 PlanNode::Union { left, right, .. } => {
726 collect_literals_for_test(left, out);
727 collect_literals_for_test(right, out);
728 }
729 PlanNode::Explain { input } => {
730 collect_literals_for_test(input, out);
731 }
732 PlanNode::Begin | PlanNode::Commit | PlanNode::Rollback => {}
733 }
734 }
735
736 fn collect_expr_literals(expr: &Expr, out: &mut Vec<Literal>) {
737 match expr {
738 Expr::Literal(l) => out.push(l.clone()),
739 Expr::Field(_) | Expr::QualifiedField { .. } | Expr::Param(_) => {}
740 Expr::BinaryOp(l, _, r) => {
741 collect_expr_literals(l, out);
742 collect_expr_literals(r, out);
743 }
744 Expr::UnaryOp(_, inner) => collect_expr_literals(inner, out),
745 Expr::FunctionCall(_, inner) => collect_expr_literals(inner, out),
746 Expr::Coalesce(l, r) => {
747 collect_expr_literals(l, out);
748 collect_expr_literals(r, out);
749 }
750 Expr::InList { expr, list, .. } => {
751 collect_expr_literals(expr, out);
752 for item in list {
753 collect_expr_literals(item, out);
754 }
755 }
756 Expr::ScalarFunc(_, args) => {
757 for a in args {
758 collect_expr_literals(a, out);
759 }
760 }
761 Expr::Cast(inner, _) => collect_expr_literals(inner, out),
762 Expr::Case { whens, else_expr } => {
763 for (cond, result) in whens {
764 collect_expr_literals(cond, out);
765 collect_expr_literals(result, out);
766 }
767 if let Some(e) = else_expr {
768 collect_expr_literals(e, out);
769 }
770 }
771 Expr::InSubquery { expr, .. } => {
772 collect_expr_literals(expr, out);
773 }
774 Expr::ExistsSubquery { .. } => {}
775 Expr::Window { args, .. } => {
776 for a in args {
777 collect_expr_literals(a, out);
778 }
779 }
780 Expr::Null => {}
781 }
782 }
783}