1use oqx::Consumer;
27use oqx::ast::{Expr, Follow, FollowDestination, OpNode, Query, SelectItem, Subquery, Where};
28use oqx::{Plan, QueryPlanner, Value, partition_pushable, residual_query};
29use rusqlite::Connection;
30use rusqlite::types::Value as SqlValue;
31
32use crate::context::{REVISION_COLS, REVISION_FROM, Target, fetch_rows, tag_rows};
33use crate::translate::{TranslateCtx, translate_predicate};
34
35fn aliases(t: Target) -> (&'static str, &'static str) {
37 match t {
38 Target::Docs => ("d", "d"),
39 Target::Blocks => ("b", "d"),
40 Target::Nodes => ("n", "d"),
41 Target::Links => ("e", "d"),
42 Target::Revisions => ("r", "d"),
43 }
44}
45
46pub(crate) fn from_clause(t: Target) -> &'static str {
51 match t {
52 Target::Docs => "docs d",
53 Target::Blocks => "blocks b JOIN docs d ON d.doc_id = b.doc_id",
54 Target::Nodes => "nodes n JOIN docs d ON d.doc_id = n.doc_id",
55 Target::Links => "edges e JOIN docs d ON d.doc_id = e.src_doc",
56 Target::Revisions => REVISION_FROM,
57 }
58}
59
60pub(crate) fn columns(t: Target) -> &'static str {
63 match t {
64 Target::Docs => "d.*",
65 Target::Blocks => "b.*, d.path AS __path",
66 Target::Nodes => "n.*, d.path AS __path",
67 Target::Links => "e.*, d.path AS __path",
68 Target::Revisions => REVISION_COLS,
69 }
70}
71
72pub(crate) fn order_clause(t: Target) -> &'static str {
74 match t {
75 Target::Docs => "d.path, d.doc_id",
76 Target::Blocks => "d.path, b.block_id",
77 Target::Nodes => "d.path, n.node_id",
78 Target::Links => "d.path, e.edge_id",
79 Target::Revisions => "r.path, r.rev_id",
80 }
81}
82
83pub(crate) fn guards(t: Target) -> &'static str {
88 match t {
89 Target::Docs => "d.repo_id = ? AND d.deleted_commit IS NULL",
90 Target::Blocks => "b.repo_id = ? AND b.deleted_commit IS NULL AND d.deleted_commit IS NULL",
91 Target::Nodes => "n.repo_id = ? AND d.deleted_commit IS NULL",
92 Target::Links => "e.repo_id = ? AND e.to_commit IS NULL AND d.deleted_commit IS NULL",
93 Target::Revisions => "d.repo_id = ?",
94 }
95}
96
97pub(crate) fn from_by_doc(t: Target) -> &'static str {
103 match t {
104 Target::Docs | Target::Revisions => from_clause(t),
105 Target::Blocks => "docs d CROSS JOIN blocks b ON b.doc_id = d.doc_id",
106 Target::Nodes => "docs d CROSS JOIN nodes n ON n.doc_id = d.doc_id",
107 Target::Links => "docs d CROSS JOIN edges e ON e.src_doc = d.doc_id",
108 }
109}
110
111pub(crate) fn guards_by_doc(t: Target) -> &'static str {
116 match t {
117 Target::Docs | Target::Revisions => guards(t),
118 Target::Blocks => {
119 "d.repo_id = ? AND +b.repo_id = ? AND b.deleted_commit IS NULL AND d.deleted_commit IS NULL"
120 }
121 Target::Nodes => "d.repo_id = ? AND +n.repo_id = ? AND d.deleted_commit IS NULL",
122 Target::Links => {
123 "d.repo_id = ? AND +e.repo_id = ? AND e.to_commit IS NULL AND d.deleted_commit IS NULL"
124 }
125 }
126}
127
128pub(crate) fn root_target(source: &Expr) -> Option<Target> {
135 match source {
136 Expr::Ident { name, .. } => Target::parse(name),
137 _ => None,
138 }
139}
140
141fn is_target_relation_of(target: Target, name: &str) -> bool {
145 matches!(
146 (target, name),
147 (Target::Docs, "nodes" | "blocks" | "revisions")
148 | (Target::Blocks, "nodes")
149 | (Target::Nodes, "blocks")
150 )
151}
152
153fn reach_through(head: &str) -> Option<Target> {
156 match head {
157 "doc" | "dst" => Some(Target::Docs),
158 "block" => Some(Target::Blocks),
159 "section" => Some(Target::Nodes),
160 _ => None,
161 }
162}
163
164fn residual_may_raise(w: &Where, target: Target) -> bool {
185 where_may_raise(w, target, true)
186}
187
188fn where_may_raise(w: &Where, target: Target, root: bool) -> bool {
189 match w {
190 Where::And { parts, .. } | Where::Or { parts, .. } => {
191 parts.iter().any(|p| where_may_raise(p, target, root))
192 }
193 Where::Not { expr, .. } => where_may_raise(expr, target, root),
194 Where::Scalar { expr, .. } => expr_may_raise(expr, target, root),
195 Where::Op(op) => op_may_raise(op, target, root),
196 }
197}
198
199fn op_may_raise(op: &OpNode, target: Target, root: bool) -> bool {
200 op.op == Consumer::Single
201 || expr_may_raise(&op.receiver, target, root)
202 || subquery_may_raise(&op.sub, target)
203}
204
205fn subquery_may_raise(sub: &Subquery, target: Target) -> bool {
206 let inner = |e: &Expr| expr_may_raise(e, target, false);
207 sub.from.iter().any(inner)
208 || sub
209 .r#where
210 .as_ref()
211 .is_some_and(|w| where_may_raise(w, target, false))
212 || sub.select.iter().any(|item| match item {
213 SelectItem::Field { expr, lift, .. } => *lift > 0 || inner(expr),
214 SelectItem::Collect { op, .. } => op_may_raise(op, target, false),
215 })
216 || sub.order_by.iter().flatten().any(|o| inner(&o.expr))
217 || sub
218 .follow
219 .as_ref()
220 .is_some_and(|f| follow_may_raise(f, target))
221 || sub.frontier.as_ref().is_some_and(inner)
222 || sub.limit.as_ref().is_some_and(inner)
223 || sub.offset.as_ref().is_some_and(inner)
224}
225
226fn follow_may_raise(f: &Follow, target: Target) -> bool {
229 let inner = |e: &Expr| expr_may_raise(e, target, false);
230 f.destinations.iter().any(|d| match d {
231 FollowDestination::Relation(e) => inner(e),
232 FollowDestination::Block(op) => op_may_raise(op, target, false),
233 })
234}
235
236fn expr_may_raise(e: &Expr, target: Target, root: bool) -> bool {
237 let again = |e: &Expr| expr_may_raise(e, target, root);
238 match e {
239 Expr::Lit { .. } | Expr::Binding { .. } => false,
240 Expr::Ident { name, .. } if Target::parse(name).is_some() => {
244 !root || !is_target_relation_of(target, name)
245 }
246 Expr::Ident { .. } => false,
248 Expr::Outer { .. } | Expr::Call { .. } => true,
249 Expr::Member { recv, name, .. } => {
252 (Target::parse(name).is_some()
253 && !matches!(&**recv, Expr::Ident { name: head, .. }
254 if reach_through(head).is_some_and(|t| is_target_relation_of(t, name))))
255 || again(recv)
256 }
257 Expr::Unary { expr, .. } => again(expr),
258 Expr::Required { .. } => true,
261 Expr::Op(op) => op_may_raise(op, target, root),
262 Expr::Binary {
265 op, left, right, ..
266 } => op.is_regex() || again(left) || again(right),
267 Expr::Logical { left, right, .. } | Expr::In { left, right, .. } => {
268 again(left) || again(right)
269 }
270 Expr::Range { lo, hi, .. } => {
271 lo.as_deref().is_some_and(again) || hi.as_deref().is_some_and(again)
272 }
273 Expr::Tuple { items, .. } => items.iter().any(again),
275 Expr::Template { parts, .. } => oqx::walk::template_exprs(parts).next().is_some(),
279 }
280}
281
282#[derive(Clone, Debug, PartialEq)]
285pub struct Compiled {
286 pub target: Target,
287 pub sql: String,
288 pub params: Vec<SqlValue>,
289 pub residual: Query,
290}
291
292#[must_use]
296pub fn compile(query: &Query, params: &[Value], repo_id: &str) -> Option<Compiled> {
297 if query.follow.is_some() || !query.from.is_empty() {
298 return None;
299 }
300 let target = root_target(&query.source)?;
301 if target == Target::Revisions {
304 return None;
305 }
306 let (self_alias, doc_alias) = aliases(target);
307 let ctx = TranslateCtx {
308 target,
309 self_alias,
310 doc_alias,
311 params,
312 };
313 let (pushed, residual) = partition_pushable(query.r#where.as_ref(), |e| {
314 translate_predicate(e, &ctx).is_some()
315 });
316 if pushed.is_empty() {
317 return None;
318 }
319 if residual
322 .as_ref()
323 .is_some_and(|w| residual_may_raise(w, target))
324 {
325 return None;
326 }
327 let mut where_sql = guards(target).to_owned();
328 let mut sql_params = vec![SqlValue::Text(repo_id.to_owned())];
329 for e in &pushed {
330 let frag = translate_predicate(e, &ctx).expect("accepted by partition_pushable");
331 where_sql.push_str(" AND (");
332 where_sql.push_str(&frag.sql);
333 where_sql.push(')');
334 sql_params.extend(frag.params);
335 }
336 let sql = format!(
337 "SELECT {} FROM {} WHERE {where_sql} ORDER BY {}",
338 columns(target),
339 from_clause(target),
340 order_clause(target)
341 );
342 Some(Compiled {
343 target,
344 sql,
345 params: sql_params,
346 residual: residual_query(query, residual),
347 })
348}
349
350pub struct SqlitePlanner<'a> {
352 conn: &'a Connection,
353 repo_id: String,
354}
355
356impl<'a> SqlitePlanner<'a> {
357 #[must_use]
358 pub fn new(conn: &'a Connection, repo_id: &str) -> Self {
359 Self {
360 conn,
361 repo_id: repo_id.to_owned(),
362 }
363 }
364
365 pub fn try_plan(&self, query: &Query, params: &[Value]) -> rusqlite::Result<Option<Plan>> {
370 let Some(compiled) = compile(query, params, &self.repo_id) else {
371 return Ok(None);
372 };
373 let rows = fetch_rows(self.conn, &compiled.sql, &compiled.params)?;
374 Ok(Some(Plan::new(
375 tag_rows(rows, compiled.target),
376 compiled.residual,
377 )))
378 }
379}
380
381impl QueryPlanner for SqlitePlanner<'_> {
382 fn plan(&self, query: &Query, params: &[Value]) -> Option<Plan> {
385 self.try_plan(query, params).ok().flatten()
386 }
387}
388
389#[cfg(test)]
390mod tests {
391 use super::*;
392 use oqx::ROWS_ROOT;
393 use oqx::ast::Where;
394
395 fn parse(src: &str) -> Query {
396 oqx::parse_string(src).expect("parses")
397 }
398
399 fn text(s: &str) -> SqlValue {
400 SqlValue::Text(s.to_owned())
401 }
402
403 #[test]
404 fn a_pushable_scan_compiles_to_one_statement_in_root_order() {
405 let c =
406 compile(&parse("from docs where path == \"index.md\""), &[], "r_1").expect("planned");
407 assert_eq!(c.target, Target::Docs);
408 assert_eq!(
409 c.sql,
410 "SELECT d.* FROM docs d WHERE d.repo_id = ? AND d.deleted_commit IS NULL AND ((('/' || d.path) IS ?)) ORDER BY d.path, d.doc_id"
411 );
412 assert_eq!(c.params, vec![text("r_1"), text("index.md")]);
413 assert_eq!(
414 c.residual.source,
415 Expr::Ident {
416 name: ROWS_ROOT.to_owned(),
417 span: oqx::Span::EMPTY,
418 }
419 );
420 assert_eq!(c.residual.r#where, None);
421 }
422
423 #[test]
424 fn every_target_has_its_join_columns_guards_and_order() {
425 let b = compile(
426 &parse("from blocks where path.startsWith(\"lab/\")"),
427 &[],
428 "r",
429 )
430 .unwrap();
431 assert_eq!(
432 b.sql,
433 "SELECT b.*, d.path AS __path FROM blocks b JOIN docs d ON d.doc_id = b.doc_id \
434 WHERE b.repo_id = ? AND b.deleted_commit IS NULL AND d.deleted_commit IS NULL \
435 AND ((substr(('/' || d.path), 1, length(?)) = ?)) ORDER BY d.path, b.block_id"
436 );
437 assert_eq!(b.params, vec![text("r"), text("lab/"), text("lab/")]);
438 let n = compile(
439 &parse("nodes count { where kind == \"md:task\" }"),
440 &[],
441 "r",
442 )
443 .unwrap();
444 assert_eq!(n.target, Target::Nodes);
445 assert!(n.sql.starts_with(
446 "SELECT n.*, d.path AS __path FROM nodes n JOIN docs d ON d.doc_id = n.doc_id WHERE n.repo_id = ? AND d.deleted_commit IS NULL AND ((n.kind IS ?))"
447 ));
448 assert!(n.sql.ends_with("ORDER BY d.path, n.node_id"));
449 let e = compile(
450 &parse("from links where predicate == \"references\""),
451 &[],
452 "r",
453 )
454 .unwrap();
455 assert!(e.sql.starts_with(
456 "SELECT e.*, d.path AS __path FROM edges e JOIN docs d ON d.doc_id = e.src_doc WHERE e.repo_id = ? AND e.to_commit IS NULL AND d.deleted_commit IS NULL AND ((e.predicate IS ?))"
457 ));
458 assert!(e.sql.ends_with("ORDER BY d.path, e.edge_id"));
459 }
460
461 #[test]
462 fn mixed_conjunctions_push_the_translatable_parts_and_keep_the_rest() {
463 let q = parse(
464 "from docs where path.startsWith(\"processes/\") && nodes exists { where kind == \"md:task\" } && layer == \"canon\"",
465 );
466 let c = compile(&q, &[], "r").expect("planned");
467 assert!(
468 c.sql
469 .contains("(substr(('/' || d.path), 1, length(?)) = ?)"),
470 "{}",
471 c.sql
472 );
473 assert!(c.sql.contains("p.key = 'layer'"), "{}", c.sql);
474 assert_eq!(
476 c.params,
477 vec![
478 text("r"),
479 text("processes/"),
480 text("processes/"),
481 text("canon")
482 ]
483 );
484 assert!(
486 matches!(c.residual.r#where, Some(Where::Op(_))),
487 "{:?}",
488 c.residual.r#where
489 );
490 assert!(c.residual.from.is_empty());
491 assert_eq!(c.residual.select, q.select);
492 assert_eq!(c.residual.consumer, q.consumer);
493 }
494
495 #[test]
496 fn declined_shapes_return_none() {
497 assert!(compile(&parse("from docs"), &[], "r").is_none());
499 assert!(compile(&parse("from docs where era in 800..1680"), &[], "r").is_none());
500 assert!(compile(&parse("from docs where !verified"), &[], "r").is_none());
501 assert!(
502 compile(
503 &parse("from docs where path == \"a\" || path == \"b\""),
504 &[],
505 "r"
506 )
507 .is_none()
508 );
509 assert!(
510 compile(
511 &parse("from docs where nodes exists { where kind == \"md:task\" }"),
512 &[],
513 "r"
514 )
515 .is_none()
516 );
517 assert!(
519 compile(
520 &parse("from docs where path == \"a.md\" follow distinct out_links collect { from dst }"),
521 &[],
522 "r"
523 )
524 .is_none()
525 );
526 assert!(
529 compile(
530 &parse("from docs where path == \"a.md\" follow out_links collect { from dst }, in_links collect { from doc }"),
531 &[],
532 "r"
533 )
534 .is_none()
535 );
536 assert!(
537 compile(
538 &parse(
539 "from docs where path == \"a.md\" follow ^docs collect { where after.contains(^path) }"
540 ),
541 &[],
542 "r"
543 )
544 .is_none()
545 );
546 assert!(
548 compile(
549 &parse("from docs where path == \"a.md\" frontier type == \"hub\" follow distinct out_links collect { from dst frontier type == \"practitioner\" }"),
550 &[],
551 "r"
552 )
553 .is_none()
554 );
555 assert!(compile(&parse("from things where path == \"a.md\""), &[], "r").is_none());
557 assert!(compile(&parse("from nope where path == \"a.md\""), &[], "r").is_none());
558 assert!(compile(&parse("from docs.nodes where kind == \"x\""), &[], "r").is_none());
559 let mut q = parse("from docs where path == \"a.md\"");
561 q.from.push(Expr::Ident {
562 name: "nodes".to_owned(),
563 span: oqx::Span::EMPTY,
564 });
565 assert!(compile(&q, &[], "r").is_none());
566 }
567
568 #[test]
571 fn a_residual_that_could_raise_declines_the_whole_query() {
572 let declined = |src: &str| {
573 assert!(
574 compile(&parse(src), &[], "r").is_none(),
575 "should decline: {src}"
576 );
577 };
578 let planned = |src: &str| {
579 assert!(
580 compile(&parse(src), &[], "r").is_some(),
581 "should plan: {src}"
582 );
583 };
584 planned("from docs where path == \"x\" && path == \"nope.md\"");
587 planned("from docs where path == \"nope.md\" && !raw");
588 planned("from docs where path == \"nope.md\" && updated_at");
589 planned("from docs where path == \"nope.md\" && tags");
590 planned("from blocks where path == \"x\" && doc.path == \"y\"");
592 planned("from blocks where path == \"x\" && nodes exists { where doc.path == \"y\" }");
593 declined("from docs where path.matches(\"[\") && path == \"nope.md\"");
595 declined("from docs where nope(\"x\") && path == \"nope.md\"");
596 declined("from docs where path == \"x\" && size(tags) > 1");
597 declined("from docs where path == \"x\" && nodes exists { where name.lower() == \"a\" }");
598 declined(
599 "from docs where path == \"x\" && nodes count { where kind == \"a\" order by size(name) } > 1",
600 );
601 declined("from docs where path == \"x\" && ^slug == \"y\"");
603 declined("from docs where path == \"x\" && nodes exists { where name == ^title }");
604 declined("from docs where path == \"x\" && nodes collect { ^first_task: name }");
605 declined(
607 "from docs where path == \"x\" && blocks exists { select t: nodes single { where kind == \"md:task\" } }",
608 );
609 planned("from docs where path.startsWith(\"lab/\") && path == \"x\"");
611 planned("from docs where path == \"x\" && era in 800..1680");
613 planned("from docs where path == \"x\" && !verified");
614 planned("from docs where path == \"x\" && (layer == \"a\" || layer == \"b\")");
615 planned("from docs where path == \"x\" && verified == true");
616 planned("from docs where path == \"x\" && nodes exists { where kind == \"md:task\" }");
617 planned(
618 "from docs where path == \"x\" && nodes count { where kind == \"md:task\" limit 5 } > 1",
619 );
620 planned("from blocks where path == \"x\" && !path");
623 planned("from docs where path == \"x\" && nodes exists { where path == \"y\" }");
624 planned("from docs where path == \"x\" && frontmatter.path == \"y\"");
625 let fm = compile(
629 &parse("from docs where frontmatter.layer == \"canon\""),
630 &[],
631 "r",
632 )
633 .expect("planned");
634 assert!(fm.sql.contains("p.source = 'frontmatter'"), "{}", fm.sql);
635 let data =
636 compile(&parse("from docs where data.layer == \"canon\""), &[], "r").expect("planned");
637 assert!(!data.sql.contains("p.source"), "{}", data.sql);
638 planned("from blocks where doc.inline.element == \"fire\"");
639 planned("from docs where path == \"x\" && frontmatter == null");
640 declined("from docs where path == \"x\" && entries(frontmatter) exists { }");
641 }
642
643 #[test]
644 fn a_top_level_not_or_or_is_the_whole_residual_and_declines() {
645 assert!(compile(&parse("from docs where !(path == \"a\")"), &[], "r").is_none());
648 assert!(
649 compile(
650 &parse("from docs where (path == \"a\" || path == \"b\") && layer == \"canon\""),
651 &[],
652 "r"
653 )
654 .is_some()
655 );
656 }
657}