Skip to content

Commit b6bc5c9

Browse files
committed
feat: add parser of simplified sparql
1 parent 1561f87 commit b6bc5c9

4 files changed

Lines changed: 391 additions & 1 deletion

File tree

AGENTS.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pathrex/
1414
├── Cargo.toml # Crate manifest (edition 2024)
1515
├── build.rs # Links LAGraph + LAGraphX; optionally regenerates FFI bindings
1616
├── src/
17-
│ ├── lib.rs # Public modules: graph, formats, lagraph_sys; utils is pub(crate)
17+
│ ├── lib.rs # Public modules: graph, formats, rpq, sparql, lagraph_sys, utils
1818
│ ├── main.rs # Binary entry point (placeholder)
1919
│ ├── lagraph_sys.rs # FFI module — includes generated bindings
2020
│ ├── lagraph_sys_generated.rs# Bindgen output (checked in, regenerated in CI)
@@ -24,6 +24,8 @@ pathrex/
2424
│ │ ├── mod.rs # Core traits (GraphBuilder, GraphDecomposition, GraphSource,
2525
│ │ │ # Backend, Graph<B>), error types, RAII wrappers, GrB init
2626
│ │ └── inmemory.rs # InMemory marker, InMemoryBuilder, InMemoryGraph
27+
│ ├── sparql/
28+
│ │ └── mod.rs # SPARQL parsing (spargebra), PathTriple extraction, parse_rpq
2729
│ └── formats/
2830
│ ├── mod.rs # FormatError enum, re-exports
2931
│ └── csv.rs # Csv<R> — CSV → Edge iterator (CsvConfig, ColumnSpec)
@@ -204,6 +206,42 @@ Configuration is via [`CsvConfig`](src/formats/csv.rs:17):
204206
[`ColumnSpec`](src/formats/csv.rs:11) is either `Index(usize)` or `Name(String)`.
205207
Name-based lookup requires `has_header: true`.
206208

209+
### SPARQL parsing (`src/sparql/mod.rs`)
210+
211+
The [`sparql`](src/sparql/mod.rs) module uses the [`spargebra`](https://crates.io/crates/spargebra)
212+
crate to parse SPARQL 1.1 query strings and extract the single property-path
213+
triple pattern that pathrex's RPQ evaluators operate on.
214+
215+
**Supported query form:** `SELECT` queries with exactly one triple or property
216+
path pattern in the `WHERE` clause, e.g.:
217+
218+
```sparql
219+
SELECT ?x ?y WHERE { ?x <knows>/<likes>* ?y . }
220+
```
221+
222+
Key public items:
223+
224+
- [`parse_query(sparql)`](src/sparql/mod.rs:51) — parses a SPARQL string into a
225+
[`spargebra::Query`].
226+
- [`extract_path(query)`](src/sparql/mod.rs:73) — validates a parsed `Query` is a
227+
`SELECT` with a single path pattern and returns a [`PathTriple`](src/sparql/mod.rs:62).
228+
- [`parse_rpq(sparql)`](src/sparql/mod.rs:196) — convenience function combining
229+
`parse_query` + `extract_path` in one call.
230+
- [`PathTriple`](src/sparql/mod.rs:62) — holds the extracted `subject`
231+
([`TermPattern`]), `path` ([`PropertyPathExpression`]), and `object`
232+
([`TermPattern`]).
233+
- [`ExtractError`](src/sparql/mod.rs:31) — error enum for extraction failures
234+
(`NotSelect`, `NotSinglePath`, `UnsupportedSubject`, `UnsupportedObject`,
235+
`VariablePredicate`).
236+
- [`RpqParseError`](src/sparql/mod.rs:204) — combined error for [`parse_rpq`]
237+
wrapping both [`SparqlSyntaxError`] and [`ExtractError`].
238+
- [`DEFAULT_BASE_IRI`](src/sparql/mod.rs:44)`"http://example.org/"`, the
239+
default base IRI constant.
240+
241+
The module also handles spargebra's desugaring of sequence paths
242+
(`?x <a>/<b>/<c> ?y`) from a chain of BGP triples back into a single
243+
[`PropertyPathExpression::Sequence`].
244+
207245
### FFI layer
208246

209247
[`lagraph_sys`](src/lagraph_sys.rs) exposes raw C bindings for GraphBLAS and
@@ -256,6 +294,8 @@ native libraries.
256294

257295
Tests in `src/formats/csv.rs` are pure Rust and need no native dependencies.
258296

297+
Tests in `src/sparql/mod.rs` are pure Rust and need no native dependencies.
298+
259299
Tests in `src/graph/inmemory.rs` and [`tests/inmemory_tests.rs`](tests/inmemory_tests.rs)
260300
call real GraphBLAS/LAGraph and require the native libraries to be present.
261301

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ csv = "1.4.0"
88
libc = "0.2"
99
oxrdf = "0.3.3"
1010
oxttl = "0.2.3"
11+
spargebra = "0.4.6"
1112
thiserror = "1.0"
1213

1314
[features]

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod formats;
22
pub mod graph;
3+
pub mod sparql;
34
#[allow(unused_unsafe, dead_code)]
45
pub(crate) mod utils;
56

0 commit comments

Comments
 (0)