qualia_cli/query/
compiler.rs1use spargebra::algebra::GraphPattern;
2use spargebra::term::{NamedNodePattern, TermPattern, TriplePattern};
3use std::collections::HashMap;
4
5const OP_MATCH_SUBJ: u8 = 0x01;
6const OP_MATCH_PRED: u8 = 0x02;
7const OP_MATCH_OBJ: u8 = 0x03;
8
9const OP_BIND_VAR: u8 = 0x10;
11const SLOT_SUBJ: u8 = 0x00;
12const SLOT_PRED: u8 = 0x01;
13const SLOT_OBJ: u8 = 0x02;
14
15const OP_HALT: u8 = 0xFF;
16
17struct VariableRegistry {
18 name_to_id: std::collections::HashMap<String, u8>,
19 next_id: u8,
20}
21
22impl VariableRegistry {
23 fn new() -> Self {
24 Self {
25 name_to_id: std::collections::HashMap::new(),
26 next_id: 0,
27 }
28 }
29 fn get_or_assign(&mut self, name: &str) -> u8 {
30 *self.name_to_id.entry(name.to_string()).or_insert_with(|| {
31 let id = self.next_id;
32 self.next_id += 1;
33 id
34 })
35 }
36}
37
38pub struct SparqlCompiler {
39 lexicon: HashMap<String, u64>,
41}
42
43impl SparqlCompiler {
44 pub fn new(lexicon: HashMap<String, u64>) -> Self {
46 Self { lexicon }
47 }
48
49 pub fn compile(&self, sparql_string: &str) -> Result<Vec<u8>, String> {
51 let query = spargebra::SparqlParser::new()
52 .parse_query(sparql_string)
53 .map_err(|e| e.to_string())?;
54 let mut bytecode = Vec::new();
55 let mut var_registry = VariableRegistry::new();
56
57 let spargebra::Query::Select { pattern, .. } = query else {
59 return Err("Only SELECT queries are supported in the MVP".to_string());
60 };
61
62 if let GraphPattern::Bgp { patterns } = pattern {
63 for triple in patterns {
64 self.compile_triple_pattern(&triple, &mut bytecode, &mut var_registry)?;
65 }
66 } else {
67 return Err("Only Basic Graph Patterns (BGPs) are currently supported".to_string());
68 }
69
70 bytecode.push(OP_HALT);
71 Ok(bytecode)
72 }
73
74 fn compile_triple_pattern(
75 &self,
76 triple: &TriplePattern,
77 bytecode: &mut Vec<u8>,
78 vars: &mut VariableRegistry,
79 ) -> Result<(), String> {
80 match &triple.subject {
82 TermPattern::NamedNode(node) => {
83 let hash = self.lexicon.get(node.as_str()).unwrap_or(&0); bytecode.push(OP_MATCH_SUBJ);
85 bytecode.extend_from_slice(&hash.to_le_bytes());
86 }
87 TermPattern::Variable(var) => {
88 let var_id = vars.get_or_assign(var.as_str());
89 bytecode.extend_from_slice(&[OP_BIND_VAR, SLOT_SUBJ, var_id]);
90 }
91 _ => return Err("Unsupported Subject Type".into()),
92 }
93
94 match &triple.predicate {
96 NamedNodePattern::NamedNode(node) => {
97 let hash = self.lexicon.get(node.as_str()).unwrap_or(&0);
98 bytecode.push(OP_MATCH_PRED);
99 bytecode.extend_from_slice(&hash.to_le_bytes());
100 }
101 NamedNodePattern::Variable(var) => {
102 let var_id = vars.get_or_assign(var.as_str());
103 bytecode.extend_from_slice(&[OP_BIND_VAR, SLOT_PRED, var_id]);
104 }
105 }
106
107 match &triple.object {
109 TermPattern::NamedNode(node) => {
110 let hash = self.lexicon.get(node.as_str()).unwrap_or(&0);
111 bytecode.push(OP_MATCH_OBJ);
112 bytecode.extend_from_slice(&hash.to_le_bytes());
113 }
114 TermPattern::Literal(literal) => {
115 let hash = self.lexicon.get(literal.value()).unwrap_or(&0);
118 bytecode.push(OP_MATCH_OBJ);
119 bytecode.extend_from_slice(&hash.to_le_bytes());
120 }
121 TermPattern::Variable(var) => {
122 let var_id = vars.get_or_assign(var.as_str());
123 bytecode.extend_from_slice(&[OP_BIND_VAR, SLOT_OBJ, var_id]);
124 }
125 _ => return Err("Unsupported Object Type".into()),
126 }
127
128 Ok(())
129 }
130}