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::{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 }
43}
44
45pub(crate) fn from_clause(t: Target) -> &'static str {
46 match t {
47 Target::Docs => "docs d",
48 Target::Blocks => "blocks b JOIN docs d ON d.doc_id = b.doc_id",
49 Target::Nodes => "nodes n JOIN docs d ON d.doc_id = n.doc_id",
50 Target::Links => "edges e JOIN docs d ON d.doc_id = e.src_doc",
51 }
52}
53
54pub(crate) fn columns(t: Target) -> &'static str {
57 match t {
58 Target::Docs => "d.*",
59 Target::Blocks => "b.*, d.path AS __path",
60 Target::Nodes => "n.*, d.path AS __path",
61 Target::Links => "e.*, d.path AS __path",
62 }
63}
64
65pub(crate) fn order_clause(t: Target) -> &'static str {
67 match t {
68 Target::Docs => "d.path, d.doc_id",
69 Target::Blocks => "d.path, b.block_id",
70 Target::Nodes => "d.path, n.node_id",
71 Target::Links => "d.path, e.edge_id",
72 }
73}
74
75pub(crate) fn guards(t: Target) -> &'static str {
77 match t {
78 Target::Docs => "d.repo_id = ? AND d.deleted_commit IS NULL",
79 Target::Blocks => "b.repo_id = ? AND b.deleted_commit IS NULL AND d.deleted_commit IS NULL",
80 Target::Nodes => "n.repo_id = ? AND d.deleted_commit IS NULL",
81 Target::Links => "e.repo_id = ? AND e.to_commit IS NULL AND d.deleted_commit IS NULL",
82 }
83}
84
85pub(crate) fn from_by_doc(t: Target) -> &'static str {
91 match t {
92 Target::Docs => from_clause(t),
93 Target::Blocks => "docs d CROSS JOIN blocks b ON b.doc_id = d.doc_id",
94 Target::Nodes => "docs d CROSS JOIN nodes n ON n.doc_id = d.doc_id",
95 Target::Links => "docs d CROSS JOIN edges e ON e.src_doc = d.doc_id",
96 }
97}
98
99pub(crate) fn guards_by_doc(t: Target) -> &'static str {
104 match t {
105 Target::Docs => guards(t),
106 Target::Blocks => {
107 "d.repo_id = ? AND +b.repo_id = ? AND b.deleted_commit IS NULL AND d.deleted_commit IS NULL"
108 }
109 Target::Nodes => "d.repo_id = ? AND +n.repo_id = ? AND d.deleted_commit IS NULL",
110 Target::Links => {
111 "d.repo_id = ? AND +e.repo_id = ? AND e.to_commit IS NULL AND d.deleted_commit IS NULL"
112 }
113 }
114}
115
116pub(crate) fn root_target(source: &Expr) -> Option<Target> {
121 match source {
122 Expr::Ident { name, .. } => Target::parse(name),
123 _ => None,
124 }
125}
126
127fn is_target_relation_of(target: Target, name: &str) -> bool {
130 matches!(
131 (target, name),
132 (Target::Docs, "nodes" | "blocks") | (Target::Blocks, "nodes") | (Target::Nodes, "blocks")
133 )
134}
135
136fn reach_through(head: &str) -> Option<Target> {
139 match head {
140 "doc" | "dst" => Some(Target::Docs),
141 "block" => Some(Target::Blocks),
142 "section" => Some(Target::Nodes),
143 _ => None,
144 }
145}
146
147fn residual_may_raise(w: &Where, target: Target) -> bool {
168 where_may_raise(w, target, true)
169}
170
171fn where_may_raise(w: &Where, target: Target, root: bool) -> bool {
172 match w {
173 Where::And { parts, .. } | Where::Or { parts, .. } => {
174 parts.iter().any(|p| where_may_raise(p, target, root))
175 }
176 Where::Not { expr, .. } => where_may_raise(expr, target, root),
177 Where::Scalar { expr, .. } => expr_may_raise(expr, target, root),
178 Where::Op(op) => op_may_raise(op, target, root),
179 }
180}
181
182fn op_may_raise(op: &OpNode, target: Target, root: bool) -> bool {
183 op.op == Consumer::Single
184 || expr_may_raise(&op.receiver, target, root)
185 || subquery_may_raise(&op.sub, target)
186}
187
188fn subquery_may_raise(sub: &Subquery, target: Target) -> bool {
189 let inner = |e: &Expr| expr_may_raise(e, target, false);
190 sub.from.iter().any(inner)
191 || sub
192 .r#where
193 .as_ref()
194 .is_some_and(|w| where_may_raise(w, target, false))
195 || sub.select.iter().any(|item| match item {
196 SelectItem::Field { expr, lift, .. } => *lift > 0 || inner(expr),
197 SelectItem::Collect { op, .. } => op_may_raise(op, target, false),
198 })
199 || sub.order_by.iter().flatten().any(|o| inner(&o.expr))
200 || sub
201 .follow
202 .as_ref()
203 .is_some_and(|f| follow_may_raise(f, target))
204 || sub.frontier.as_ref().is_some_and(inner)
205 || sub.limit.as_ref().is_some_and(inner)
206 || sub.offset.as_ref().is_some_and(inner)
207}
208
209fn follow_may_raise(f: &Follow, target: Target) -> bool {
212 let inner = |e: &Expr| expr_may_raise(e, target, false);
213 f.destinations.iter().any(|d| match d {
214 FollowDestination::Relation(e) => inner(e),
215 FollowDestination::Block(op) => op_may_raise(op, target, false),
216 })
217}
218
219fn expr_may_raise(e: &Expr, target: Target, root: bool) -> bool {
220 let again = |e: &Expr| expr_may_raise(e, target, root);
221 match e {
222 Expr::Lit { .. } | Expr::Binding { .. } => false,
223 Expr::Ident { name, .. } if Target::parse(name).is_some() => {
227 !root || !is_target_relation_of(target, name)
228 }
229 Expr::Ident { .. } => false,
231 Expr::Outer { .. } | Expr::Call { .. } => true,
232 Expr::Member { recv, name, .. } => {
235 (Target::parse(name).is_some()
236 && !matches!(&**recv, Expr::Ident { name: head, .. }
237 if reach_through(head).is_some_and(|t| is_target_relation_of(t, name))))
238 || again(recv)
239 }
240 Expr::Unary { expr, .. } => again(expr),
241 Expr::Required { .. } => true,
244 Expr::Op(op) => op_may_raise(op, target, root),
245 Expr::Binary { left, right, .. }
246 | Expr::Logical { left, right, .. }
247 | Expr::In { left, right, .. } => again(left) || again(right),
248 Expr::Range { lo, hi, .. } => {
249 lo.as_deref().is_some_and(again) || hi.as_deref().is_some_and(again)
250 }
251 Expr::Tuple { items, .. } => items.iter().any(again),
253 }
254}
255
256#[derive(Clone, Debug, PartialEq)]
259pub struct Compiled {
260 pub target: Target,
261 pub sql: String,
262 pub params: Vec<SqlValue>,
263 pub residual: Query,
264}
265
266#[must_use]
270pub fn compile(query: &Query, params: &[Value], repo_id: &str) -> Option<Compiled> {
271 if query.follow.is_some() || !query.from.is_empty() {
272 return None;
273 }
274 let target = root_target(&query.source)?;
275 let (self_alias, doc_alias) = aliases(target);
276 let ctx = TranslateCtx {
277 target,
278 self_alias,
279 doc_alias,
280 params,
281 };
282 let (pushed, residual) = partition_pushable(query.r#where.as_ref(), |e| {
283 translate_predicate(e, &ctx).is_some()
284 });
285 if pushed.is_empty() {
286 return None;
287 }
288 if residual
291 .as_ref()
292 .is_some_and(|w| residual_may_raise(w, target))
293 {
294 return None;
295 }
296 let mut where_sql = guards(target).to_owned();
297 let mut sql_params = vec![SqlValue::Text(repo_id.to_owned())];
298 for e in &pushed {
299 let frag = translate_predicate(e, &ctx).expect("accepted by partition_pushable");
300 where_sql.push_str(" AND (");
301 where_sql.push_str(&frag.sql);
302 where_sql.push(')');
303 sql_params.extend(frag.params);
304 }
305 let sql = format!(
306 "SELECT {} FROM {} WHERE {where_sql} ORDER BY {}",
307 columns(target),
308 from_clause(target),
309 order_clause(target)
310 );
311 Some(Compiled {
312 target,
313 sql,
314 params: sql_params,
315 residual: residual_query(query, residual),
316 })
317}
318
319pub struct SqlitePlanner<'a> {
321 conn: &'a Connection,
322 repo_id: String,
323}
324
325impl<'a> SqlitePlanner<'a> {
326 #[must_use]
327 pub fn new(conn: &'a Connection, repo_id: &str) -> Self {
328 Self {
329 conn,
330 repo_id: repo_id.to_owned(),
331 }
332 }
333
334 pub fn try_plan(&self, query: &Query, params: &[Value]) -> rusqlite::Result<Option<Plan>> {
339 let Some(compiled) = compile(query, params, &self.repo_id) else {
340 return Ok(None);
341 };
342 let rows = fetch_rows(self.conn, &compiled.sql, &compiled.params)?;
343 Ok(Some(Plan::new(
344 tag_rows(rows, compiled.target),
345 compiled.residual,
346 )))
347 }
348}
349
350impl QueryPlanner for SqlitePlanner<'_> {
351 fn plan(&self, query: &Query, params: &[Value]) -> Option<Plan> {
354 self.try_plan(query, params).ok().flatten()
355 }
356}
357
358#[cfg(test)]
359mod tests {
360 use super::*;
361 use oqx::ROWS_ROOT;
362 use oqx::ast::Where;
363
364 fn parse(src: &str) -> Query {
365 oqx::parse_string(src).expect("parses")
366 }
367
368 fn text(s: &str) -> SqlValue {
369 SqlValue::Text(s.to_owned())
370 }
371
372 #[test]
373 fn a_pushable_scan_compiles_to_one_statement_in_root_order() {
374 let c =
375 compile(&parse("from docs where path == \"index.md\""), &[], "r_1").expect("planned");
376 assert_eq!(c.target, Target::Docs);
377 assert_eq!(
378 c.sql,
379 "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"
380 );
381 assert_eq!(c.params, vec![text("r_1"), text("index.md")]);
382 assert_eq!(
383 c.residual.source,
384 Expr::Ident {
385 name: ROWS_ROOT.to_owned(),
386 span: oqx::Span::EMPTY,
387 }
388 );
389 assert_eq!(c.residual.r#where, None);
390 }
391
392 #[test]
393 fn every_target_has_its_join_columns_guards_and_order() {
394 let b = compile(
395 &parse("from blocks where path.startsWith(\"lab/\")"),
396 &[],
397 "r",
398 )
399 .unwrap();
400 assert_eq!(
401 b.sql,
402 "SELECT b.*, d.path AS __path FROM blocks b JOIN docs d ON d.doc_id = b.doc_id \
403 WHERE b.repo_id = ? AND b.deleted_commit IS NULL AND d.deleted_commit IS NULL \
404 AND ((substr(('/' || d.path), 1, length(?)) = ?)) ORDER BY d.path, b.block_id"
405 );
406 assert_eq!(b.params, vec![text("r"), text("lab/"), text("lab/")]);
407 let n = compile(
408 &parse("nodes count { where kind == \"md:task\" }"),
409 &[],
410 "r",
411 )
412 .unwrap();
413 assert_eq!(n.target, Target::Nodes);
414 assert!(n.sql.starts_with(
415 "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 ?))"
416 ));
417 assert!(n.sql.ends_with("ORDER BY d.path, n.node_id"));
418 let e = compile(
419 &parse("from links where predicate == \"references\""),
420 &[],
421 "r",
422 )
423 .unwrap();
424 assert!(e.sql.starts_with(
425 "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 ?))"
426 ));
427 assert!(e.sql.ends_with("ORDER BY d.path, e.edge_id"));
428 }
429
430 #[test]
431 fn mixed_conjunctions_push_the_translatable_parts_and_keep_the_rest() {
432 let q = parse(
433 "from docs where path.startsWith(\"processes/\") && nodes exists { where kind == \"md:task\" } && layer == \"canon\"",
434 );
435 let c = compile(&q, &[], "r").expect("planned");
436 assert!(
437 c.sql
438 .contains("(substr(('/' || d.path), 1, length(?)) = ?)"),
439 "{}",
440 c.sql
441 );
442 assert!(c.sql.contains("p.key = 'layer'"), "{}", c.sql);
443 assert_eq!(
445 c.params,
446 vec![
447 text("r"),
448 text("processes/"),
449 text("processes/"),
450 text("canon")
451 ]
452 );
453 assert!(
455 matches!(c.residual.r#where, Some(Where::Op(_))),
456 "{:?}",
457 c.residual.r#where
458 );
459 assert!(c.residual.from.is_empty());
460 assert_eq!(c.residual.select, q.select);
461 assert_eq!(c.residual.consumer, q.consumer);
462 }
463
464 #[test]
465 fn declined_shapes_return_none() {
466 assert!(compile(&parse("from docs"), &[], "r").is_none());
468 assert!(compile(&parse("from docs where era in 800..1680"), &[], "r").is_none());
469 assert!(compile(&parse("from docs where !verified"), &[], "r").is_none());
470 assert!(
471 compile(
472 &parse("from docs where path == \"a\" || path == \"b\""),
473 &[],
474 "r"
475 )
476 .is_none()
477 );
478 assert!(
479 compile(
480 &parse("from docs where nodes exists { where kind == \"md:task\" }"),
481 &[],
482 "r"
483 )
484 .is_none()
485 );
486 assert!(
488 compile(
489 &parse("from docs where path == \"a.md\" follow distinct out_links collect { from dst }"),
490 &[],
491 "r"
492 )
493 .is_none()
494 );
495 assert!(
498 compile(
499 &parse("from docs where path == \"a.md\" follow out_links collect { from dst }, in_links collect { from doc }"),
500 &[],
501 "r"
502 )
503 .is_none()
504 );
505 assert!(
506 compile(
507 &parse(
508 "from docs where path == \"a.md\" follow ^docs collect { where after.contains(^path) }"
509 ),
510 &[],
511 "r"
512 )
513 .is_none()
514 );
515 assert!(
517 compile(
518 &parse("from docs where path == \"a.md\" frontier type == \"hub\" follow distinct out_links collect { from dst frontier type == \"practitioner\" }"),
519 &[],
520 "r"
521 )
522 .is_none()
523 );
524 assert!(compile(&parse("from things where path == \"a.md\""), &[], "r").is_none());
526 assert!(compile(&parse("from nope where path == \"a.md\""), &[], "r").is_none());
527 assert!(compile(&parse("from docs.nodes where kind == \"x\""), &[], "r").is_none());
528 let mut q = parse("from docs where path == \"a.md\"");
530 q.from.push(Expr::Ident {
531 name: "nodes".to_owned(),
532 span: oqx::Span::EMPTY,
533 });
534 assert!(compile(&q, &[], "r").is_none());
535 }
536
537 #[test]
540 fn a_residual_that_could_raise_declines_the_whole_query() {
541 let declined = |src: &str| {
542 assert!(
543 compile(&parse(src), &[], "r").is_none(),
544 "should decline: {src}"
545 );
546 };
547 let planned = |src: &str| {
548 assert!(
549 compile(&parse(src), &[], "r").is_some(),
550 "should plan: {src}"
551 );
552 };
553 planned("from docs where path == \"x\" && path == \"nope.md\"");
556 planned("from docs where path == \"nope.md\" && !raw");
557 planned("from docs where path == \"nope.md\" && updated_at");
558 planned("from docs where path == \"nope.md\" && tags");
559 planned("from blocks where path == \"x\" && doc.path == \"y\"");
561 planned("from blocks where path == \"x\" && nodes exists { where doc.path == \"y\" }");
562 declined("from docs where path.matches(\"[\") && path == \"nope.md\"");
564 declined("from docs where nope(\"x\") && path == \"nope.md\"");
565 declined("from docs where path == \"x\" && size(tags) > 1");
566 declined("from docs where path == \"x\" && nodes exists { where name.lower() == \"a\" }");
567 declined(
568 "from docs where path == \"x\" && nodes count { where kind == \"a\" order by size(name) } > 1",
569 );
570 declined("from docs where path == \"x\" && ^slug == \"y\"");
572 declined("from docs where path == \"x\" && nodes exists { where name == ^title }");
573 declined("from docs where path == \"x\" && nodes collect { ^first_task: name }");
574 declined(
576 "from docs where path == \"x\" && blocks exists { select t: nodes single { where kind == \"md:task\" } }",
577 );
578 planned("from docs where path.startsWith(\"lab/\") && path == \"x\"");
580 planned("from docs where path == \"x\" && era in 800..1680");
582 planned("from docs where path == \"x\" && !verified");
583 planned("from docs where path == \"x\" && (layer == \"a\" || layer == \"b\")");
584 planned("from docs where path == \"x\" && verified == true");
585 planned("from docs where path == \"x\" && nodes exists { where kind == \"md:task\" }");
586 planned(
587 "from docs where path == \"x\" && nodes count { where kind == \"md:task\" limit 5 } > 1",
588 );
589 planned("from blocks where path == \"x\" && !path");
592 planned("from docs where path == \"x\" && nodes exists { where path == \"y\" }");
593 planned("from docs where path == \"x\" && frontmatter.path == \"y\"");
594 let fm = compile(
598 &parse("from docs where frontmatter.layer == \"canon\""),
599 &[],
600 "r",
601 )
602 .expect("planned");
603 assert!(fm.sql.contains("p.source = 'frontmatter'"), "{}", fm.sql);
604 let data =
605 compile(&parse("from docs where data.layer == \"canon\""), &[], "r").expect("planned");
606 assert!(!data.sql.contains("p.source"), "{}", data.sql);
607 planned("from blocks where doc.inline.element == \"fire\"");
608 planned("from docs where path == \"x\" && frontmatter == null");
609 declined("from docs where path == \"x\" && entries(frontmatter) exists { }");
610 }
611
612 #[test]
613 fn a_top_level_not_or_or_is_the_whole_residual_and_declines() {
614 assert!(compile(&parse("from docs where !(path == \"a\")"), &[], "r").is_none());
617 assert!(
618 compile(
619 &parse("from docs where (path == \"a\" || path == \"b\") && layer == \"canon\""),
620 &[],
621 "r"
622 )
623 .is_some()
624 );
625 }
626}