1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//! Auto-generated module
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
use crate::algebra::{
Aggregate, Algebra, Expression, GroupCondition, Iri, OrderCondition, TriplePattern, Variable,
};
use crate::update::UpdateOperation;
use std::collections::HashMap;
/// SPARQL query types
#[derive(Debug, Clone, PartialEq)]
pub enum QueryType {
Select,
Construct,
Ask,
Describe,
}
/// A single resource named by a `DESCRIBE` query.
///
/// `DESCRIBE <iri> ?var …` may mix explicit IRIs (already expanded against the
/// query prologue) and variables that resolve to bound resources in the WHERE
/// clause. `DESCRIBE *` is represented out-of-band by [`Query::describe_all`]
/// and leaves [`Query::describe_targets`] empty.
#[derive(Debug, Clone, PartialEq)]
pub enum DescribeTarget {
/// A concrete IRI target (prefixed names are expanded before storage).
Iri(Iri),
/// A variable target whose bindings supply the resources to describe.
Variable(Variable),
}
/// A single item in a SELECT projection list.
///
/// A projection is an ordered list of these. Plain `?x` produces
/// [`ProjectionItem::Variable`]; a parenthesized `(Expression AS ?v)` produces
/// [`ProjectionItem::Expression`] for a non-aggregate expression or
/// [`ProjectionItem::Aggregate`] when the expression is a SPARQL aggregate
/// (`COUNT`, `SUM`, `MIN`, `MAX`, `AVG`, `SAMPLE`, `GROUP_CONCAT`). The
/// aggregate variant carries the crate's [`Aggregate`] value directly so a
/// consumer can assemble an `Algebra::Group { aggregates, .. }` without
/// re-parsing.
#[derive(Debug, Clone, PartialEq)]
pub enum ProjectionItem {
/// Plain variable projection: `SELECT ?x`.
Variable(Variable),
/// Non-aggregate expression projection: `(?a + 1 AS ?b)`.
Expression {
/// The projected expression.
expr: Expression,
/// The variable the expression result is bound to.
alias: Variable,
},
/// Aggregate projection: `(COUNT(*) AS ?n)`, `(SUM(?a * ?b) AS ?t)`, ….
Aggregate {
/// The parsed aggregate (carries DISTINCT / separator / `*` state).
aggregate: Aggregate,
/// The variable the aggregate result is bound to.
alias: Variable,
},
}
/// Dataset clause for FROM and FROM NAMED
#[derive(Debug, Clone, Default)]
pub struct DatasetClause {
pub default_graphs: Vec<Iri>,
pub named_graphs: Vec<Iri>,
}
/// SPARQL UPDATE request representation
#[derive(Debug, Clone)]
pub struct UpdateRequest {
pub operations: Vec<UpdateOperation>,
pub prefixes: HashMap<String, String>,
pub base_iri: Option<String>,
}
/// Token types for SPARQL parsing
#[derive(Debug, Clone, PartialEq)]
pub enum Token {
Select,
Construct,
Ask,
Describe,
Where,
Optional,
Union,
Minus,
Filter,
Bind,
Service,
Graph,
From,
Named,
Prefix,
Base,
Distinct,
Reduced,
OrderBy,
GroupBy,
Having,
Limit,
Offset,
Asc,
Desc,
As,
Values,
Exists,
NotExists,
Insert,
Delete,
Update,
Create,
Drop,
Clear,
Load,
Copy,
Move,
Add,
Data,
With,
Using,
Silent,
All,
Default,
To,
Equal,
NotEqual,
Less,
LessEqual,
Greater,
GreaterEqual,
And,
Or,
Not,
/// The `IN` operator keyword (`?x IN (…)`, `?x NOT IN (…)`).
In,
Plus,
Minus_,
Multiply,
Divide,
Pipe,
Caret,
Slash,
Question,
Star,
Bang,
LeftParen,
RightParen,
LeftBrace,
RightBrace,
LeftBracket,
RightBracket,
Dot,
Semicolon,
Comma,
Colon,
Iri(String),
PrefixedName(String, String),
/// A SPARQL 1.1 built-in call name (e.g. `LANG`, `isIRI`, `REGEX`),
/// carrying its canonical lower-case spelling. Only a *bare* (colon-free)
/// keyword is classified as a built-in; a leading-colon default-prefix name
/// such as `:lang` stays a [`Token::PrefixedName`] so user functions are
/// never mistaken for built-ins.
BuiltIn(String),
/// The bare `a` keyword: the `rdf:type` predicate shorthand. Only a bare
/// (colon-free) lowercase `a` is classified here; `?a`, `:a`, `"a"` and
/// `a:` (a prefix name) are never affected.
A,
Variable(String),
StringLiteral(String),
/// A string literal carrying a language tag (`"foo"@ja`) or an explicit
/// datatype (`"1"^^xsd:integer`). `datatype` holds the raw form as written
/// (an absolute IRI, or a `prefix:local` name resolved at parse time).
RdfLiteral {
value: String,
language: Option<String>,
datatype: Option<String>,
},
NumericLiteral(String),
BooleanLiteral(bool),
BlankNode(String),
Eof,
Newline,
}
/// SPARQL query representation
#[derive(Debug, Clone)]
pub struct Query {
pub query_type: QueryType,
pub select_variables: Vec<Variable>,
pub where_clause: Algebra,
pub order_by: Vec<OrderCondition>,
pub group_by: Vec<GroupCondition>,
pub having: Option<Expression>,
pub limit: Option<usize>,
pub offset: Option<usize>,
pub distinct: bool,
pub reduced: bool,
pub construct_template: Vec<TriplePattern>,
pub prefixes: HashMap<String, String>,
pub base_iri: Option<String>,
pub dataset: DatasetClause,
/// Ordered SELECT projection items (parallel to `select_variables`, but
/// also carrying `(Expression AS ?v)` / aggregate projections). Empty when
/// the query is `SELECT *` or is not a SELECT query.
pub projection_items: Vec<ProjectionItem>,
/// Explicit `DESCRIBE` targets (IRIs and variables). Empty for non-DESCRIBE
/// queries and for `DESCRIBE *` (see `describe_all`).
pub describe_targets: Vec<DescribeTarget>,
/// `true` for `DESCRIBE *`, which describes every in-scope variable binding
/// rather than an explicit target list.
pub describe_all: bool,
}